Files
newt/newt/Zoo1.newt

72 lines
1.6 KiB
Plaintext

module Zoo1
-- I'm starting to translate ezoo 01-eval-closures-debruijn as a test cases.
ptype Int
ptype String
------- Prelude stuff
data Nat : U where
Z : Nat
S : Nat -> Nat
data Unit : U where
MkUnit : Unit
data List : U -> U where
Nil : {a : U} -> List a
Cons : {a : U} -> a -> List a -> List a
data Maybe : U -> U where
Just : {a : U} -> a -> Maybe a
Nothing : {a : U} -> Maybe a
Val : U
data Tm : U where
Var : Nat -> Tm
Lam : Tm -> Tm -- lam (x.t)
App : Tm -> Tm -> Tm
Let : Tm -> Tm -> Tm -- let t (x.u)
data Env : U where
ENil : Env
Define : Env -> Val -> Env
data Closure : U where
MkClosure : Env -> Tm -> Closure
data Val : U where
VVar : Nat -> Val
VApp : Val -> Val -> Val
VLam : Closure -> Val
length : Env -> Nat
length ENil = Z
length (Define env _) = S (length env)
lookup : Env -> Nat -> Maybe Val
lookup (Define env v) Z = Just v
-- If I write "Just (lookup env k)" on RHS, it's wrong, but the error message is unusable (mainly due to FC)
-- The FC is fine if I write lookup {Val} env k
lookup (Define env _) (S k) = lookup env k
lookup (ENil) x = Nothing
eval : Env -> Tm -> Val
cApp : Closure -> Val -> Val
-- If I put Closure instead of MkClosure, it reports missing case, fix that (should be bad constructor or something)
cApp (MkClosure env t) u = eval (Define env u) t
hole : Val
eval env (Var x) =
case lookup env x of
-- case doesn't use the new code. We've got a wildcard here that
-- is forced to {Val}, but we don't have forcing/dotting
-- I guess we see what Jesper says about dotting
Just x => x
Nothing => VVar x
eval env _ = hole