remove Prim constructor from Expr

This commit is contained in:
crumbtoo
2023-11-14 12:33:06 -07:00
parent 650e0a3cfe
commit a259d5c42b
2 changed files with 16 additions and 23 deletions

View File

@@ -9,15 +9,16 @@ import Data.String
---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------
data Expr = Var Name data Expr = Var Name
| Con Int Int | Con Int Int -- Con Tag Arity
| Let Rec [Binding] Expr | Let Rec [Binding] Expr
| Case Expr [Alter] | Case Expr [Alter]
| Lam [Name] Expr | Lam [Name] Expr
| App Expr Expr | App Expr Expr
| Prim Prim | IntE Int
deriving Show deriving Show
data Prim = IntP Int data Prim = PrimConstr Int Int -- PrimConstr Tag Arity
| IntP Int
| IntAddP | IntAddP
| IntSubP | IntSubP
| IntMulP | IntMulP
@@ -56,7 +57,7 @@ instance IsString Expr where
instance Pretty Expr where instance Pretty Expr where
prettyPrec (Var k) = withPrec maxBound $ IStr k prettyPrec (Var k) = withPrec maxBound $ IStr k
prettyPrec (Prim n) = prettyPrec n prettyPrec (IntE n) = withPrec maxBound $ IStr (show n)
prettyPrec (Con _ _) = undefined prettyPrec (Con _ _) = undefined
prettyPrec (Let r bs e) = withPrec 0 $ prettyPrec (Let r bs e) = withPrec 0 $
IStr (if r == Rec then "letrec " else "let ") IStr (if r == Rec then "letrec " else "let ")

View File

@@ -115,15 +115,7 @@ instantiate (Let Rec bs e) h g = instantiate e h' env
let (h',a) = instantiate v h env let (h',a) = instantiate v h env
in (h',(k,a)) in (h',(k,a))
instantiate (Prim (IntP n)) h _ = alloc h (NNum n) instantiate (IntE 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 _ _ _ = error "unimplemented" instantiate _ _ _ = error "unimplemented"
@@ -150,7 +142,7 @@ instantiateU (Let NonRec bs e) root h g = h''
let (h',a) = instantiate v h g let (h',a) = instantiate v h g
in (h',(k,a)) 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" , "b" := "pair" :$ "y" :$ "a"
] ]
("fst" :$ ("snd" :$ ("snd" :$ ("snd" :$ "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
idExample = Program idExample = Program
[ ScDef "main" [] $ "id" :$ Prim (IntP 3) [ ScDef "main" [] $ "id" :$ IntE 3
] ]
indExample1 :: Program indExample1 :: Program
indExample1 = Program indExample1 = Program
[ ScDef "main" [] $ "twice" :$ "twice" :$ "id" :$ Prim (IntP 3) [ ScDef "main" [] $ "twice" :$ "twice" :$ "id" :$ IntE 3
] ]
indExample2 :: Program indExample2 :: Program
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
indExample3 = Program indExample3 = Program
[ ScDef "main" [] $ [ ScDef "main" [] $
Let Rec Let Rec
[ "x" := Prim (IntP 2) [ "x" := IntE 2
, "y" := "f" :$ "x" :$ "x" , "y" := "f" :$ "x" :$ "x"
] ]
("g" :$ "y" :$ "y") ("g" :$ "y" :$ "y")
@@ -353,25 +345,25 @@ indExample3 = Program
negExample1 :: Program negExample1 :: Program
negExample1 = Program negExample1 = Program
[ ScDef "main" [] $ [ ScDef "main" [] $
Prim IntNegP :$ ("id" :$ Prim (IntP 3)) "negate#" :$ ("id" :$ IntE 3)
] ]
negExample2 :: Program negExample2 :: Program
negExample2 = Program negExample2 = Program
[ ScDef "main" [] $ [ ScDef "main" [] $
Prim IntNegP :$ Prim (IntP 3) "negate#" :$ IntE 3
] ]
negExample3 :: Program negExample3 :: Program
negExample3 = Program negExample3 = Program
[ ScDef "main" [] $ [ ScDef "main" [] $
"twice" :$ Prim IntNegP :$ Prim (IntP 3) "twice" :$ "negate#" :$ IntE 3
] ]
arithExample1 :: Program arithExample1 :: Program
arithExample1 = Program arithExample1 = Program
[ ScDef "main" [] $ [ ScDef "main" [] $
"+#" :$ (Prim $ IntP 3) :$ ("negate#" :$ (Prim $ IntP 2)) "+#" :$ (IntE 3) :$ ("negate#" :$ (IntE 2))
] ]
---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------