From a259d5c42ba832627a2654db0e289b39532ea101 Mon Sep 17 00:00:00 2001 From: crumbtoo Date: Tue, 14 Nov 2023 12:33:06 -0700 Subject: [PATCH] remove Prim constructor from Expr --- src/Core.hs | 9 +++++---- src/TIM.hs | 30 +++++++++++------------------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/Core.hs b/src/Core.hs index 067d7d9..2d8f152 100644 --- a/src/Core.hs +++ b/src/Core.hs @@ -9,15 +9,16 @@ import Data.String ---------------------------------------------------------------------------------- data Expr = Var Name - | Con Int Int + | Con Int Int -- Con Tag Arity | Let Rec [Binding] Expr | Case Expr [Alter] | Lam [Name] Expr | App Expr Expr - | Prim Prim + | IntE Int deriving Show -data Prim = IntP Int +data Prim = PrimConstr Int Int -- PrimConstr Tag Arity + | IntP Int | IntAddP | IntSubP | IntMulP @@ -56,7 +57,7 @@ instance IsString Expr where instance Pretty Expr where prettyPrec (Var k) = withPrec maxBound $ IStr k - prettyPrec (Prim n) = prettyPrec n + prettyPrec (IntE n) = withPrec maxBound $ IStr (show n) prettyPrec (Con _ _) = undefined prettyPrec (Let r bs e) = withPrec 0 $ IStr (if r == Rec then "letrec " else "let ") diff --git a/src/TIM.hs b/src/TIM.hs index 29d1671..600f711 100644 --- a/src/TIM.hs +++ b/src/TIM.hs @@ -115,15 +115,7 @@ instantiate (Let Rec bs e) h g = instantiate e h' env let (h',a) = instantiate v h env in (h',(k,a)) -instantiate (Prim (IntP n)) h _ = alloc h (NNum n) -instantiate (Prim p) h _ = alloc h (NPrim n p) - where - n = fromMaybe - (error $ "primitive `" <> show p <> "' has no associated name") - $ lookupV p primitives - - lookupV v d = fmap (\ (a,b) -> (b,a)) d - & lookup v +instantiate (IntE n) h _ = alloc h (NNum n) instantiate _ _ _ = error "unimplemented" @@ -150,7 +142,7 @@ instantiateU (Let NonRec bs e) root h g = h'' let (h',a) = instantiate v h g in (h',(k,a)) -instantiateU (Prim (IntP n)) root h _ = update h root (NNum n) +instantiateU (IntE n) root h _ = update h root (NNum n) ---------------------------------------------------------------------------------- @@ -320,29 +312,29 @@ letrecExample = Program , "b" := "pair" :$ "y" :$ "a" ] ("fst" :$ ("snd" :$ ("snd" :$ ("snd" :$ "a")))) - , ScDef "main" [] $ "f" :$ Prim (IntP 3) :$ Prim (IntP 4) + , ScDef "main" [] $ "f" :$ IntE 3 :$ IntE 4 ] idExample :: Program idExample = Program - [ ScDef "main" [] $ "id" :$ Prim (IntP 3) + [ ScDef "main" [] $ "id" :$ IntE 3 ] indExample1 :: Program indExample1 = Program - [ ScDef "main" [] $ "twice" :$ "twice" :$ "id" :$ Prim (IntP 3) + [ ScDef "main" [] $ "twice" :$ "twice" :$ "id" :$ IntE 3 ] indExample2 :: Program indExample2 = Program - [ ScDef "main" [] $ "twice" :$ "twice" :$ "twice" :$ "id" :$ Prim (IntP 3) + [ ScDef "main" [] $ "twice" :$ "twice" :$ "twice" :$ "id" :$ IntE 3 ] indExample3 :: Program indExample3 = Program [ ScDef "main" [] $ Let Rec - [ "x" := Prim (IntP 2) + [ "x" := IntE 2 , "y" := "f" :$ "x" :$ "x" ] ("g" :$ "y" :$ "y") @@ -353,25 +345,25 @@ indExample3 = Program negExample1 :: Program negExample1 = Program [ ScDef "main" [] $ - Prim IntNegP :$ ("id" :$ Prim (IntP 3)) + "negate#" :$ ("id" :$ IntE 3) ] negExample2 :: Program negExample2 = Program [ ScDef "main" [] $ - Prim IntNegP :$ Prim (IntP 3) + "negate#" :$ IntE 3 ] negExample3 :: Program negExample3 = Program [ ScDef "main" [] $ - "twice" :$ Prim IntNegP :$ Prim (IntP 3) + "twice" :$ "negate#" :$ IntE 3 ] arithExample1 :: Program arithExample1 = Program [ ScDef "main" [] $ - "+#" :$ (Prim $ IntP 3) :$ ("negate#" :$ (Prim $ IntP 2)) + "+#" :$ (IntE 3) :$ ("negate#" :$ (IntE 2)) ] ----------------------------------------------------------------------------------