@@ -1,12 +1,14 @@
1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
3−module Evaluate(normalise) where
3+module Evaluate(normalise,
4+ Fun(..), Def(..), Context,
5+ emptyContext, addToCtxt, addConstant,
6+ lookupTy, lookupP, lookupVal, lookupTyEnv) where
5import Debug.Trace
8import Debug.Trace
8−normalise :: Context -> Env -> TT Name -> TT Name
11+-- VALUES (as HOAS) ---------------------------------------------------------
9−normalise ctxt env t = quote 0 (eval ctxt (weakenEnv env) t)
11data Value = VP NameType Name Value
13data Value = VP NameType Name Value
@@ -15,10 +17,18 @@ data Value = VP NameType Name Value
15 | VSet Int
17 | VSet Int
16 | VTmp Int
18 | VTmp Int
20+instance Show Value where
21+ show = show . quote 0
23+-- THE EVALUATOR ------------------------------------------------------------
25+normalise :: Context -> Env -> TT Name -> TT Name
26+normalise ctxt env t = quote 0 (eval ctxt (weakenEnv env) t)
18eval :: Context -> Env -> TT Name -> Value
28eval :: Context -> Env -> TT Name -> Value
19eval ctxt genv tm = ev [] tm where
29eval ctxt genv tm = ev [] tm where
20−-- ev env (P Ref n ty)
30+ ev env (P Ref n ty)
21−-- | Just v <- lookupP n ctxt = v -- FIXME! Needs evalling
31+ | Just v <- lookupVal n ctxt = v -- FIXME! Needs evalling
22 ev env (P nt n ty) = VP nt n (ev env ty)
32 ev env (P nt n ty) = VP nt n (ev env ty)
23 ev env (V i) | i < length env = env !! i
33 ev env (V i) | i < length env = env !! i
24 | i < length env + length genv
34 | i < length env + length genv
@@ -29,16 +39,12 @@ eval ctxt genv tm = ev [] tm where
29 ev env (Bind n (Let t v) sc)
39 ev env (Bind n (Let t v) sc)
30 = ev (ev env v : env) sc
40 = ev (ev env v : env) sc
31 ev env (Bind n b sc) = VBind n (vbind env b) (\x -> ev (x:env) sc)
41 ev env (Bind n b sc) = VBind n (vbind env b) (\x -> ev (x:env) sc)
32− where vbind env (Lam t) = Lam (ev env t)
42+ where vbind env t = fmap (ev env) t
33− vbind env (Pi t) = Pi (ev env t)
34− vbind env (Hole t) = Hole (ev env t)
35− vbind env (PVar t) = PVar (ev env t)
36− vbind env (Guess v t) = Guess (ev env v) (ev env t)
37 ev env (App f a) = evApply env [a] f
43 ev env (App f a) = evApply env [a] f
38 ev env (Set i) = VSet i
44 ev env (Set i) = VSet i
40 evApply env args (App f a) = evApply env (a:args) f
46 evApply env args (App f a) = evApply env (a:args) f
41− evApply env args f = apply env (ev env f) (reverse args)
47+ evApply env args f = apply env (ev env f) args
43 apply env (VBind n (Lam t) sc) (a:as) = apply env (sc (ev env a)) as
49 apply env (VBind n (Lam t) sc) (a:as) = apply env (sc (ev env a)) as
44 apply env f (a:as) = unload env f (a:as)
50 apply env f (a:as) = unload env f (a:as)
@@ -56,79 +62,51 @@ quote i (VApp f a) = App (quote i f) (quote i a)
56quote i (VSet u) = Set u
62quote i (VSet u) = Set u
57quote i (VTmp x) = V (i - x)
63quote i (VTmp x) = V (i - x)
59−-- normalise ctxt env t = quote 0 (eval ctxt (evalEnv env) (hoas [] t))
65+
60−--
66+-- CONTEXTS -----------------------------------------------------------------
68+data Fun = Fun Type Value Term Value
71+data Def = Function Fun
72+ | Constant NameType Type Value
61−-- type HEnv = [(Name, Binder HTT)]
75+type Context = [(Name, Def)]
63−-- evalEnv :: Env -> HEnv
77+emptyContext = []
64−-- evalEnv = map (\ (n, b) -> (n, fmap (hoas []) b))
78+
65−--
79+addToCtxt :: Name -> Term -> Type -> Context -> Context
66−-- We just evaluate HOAS terms. Any de Bruijn indices will refer to higher
80+addToCtxt n tm ty ctxt = (n, Function (Fun ty (eval ctxt [] ty)
67−-- level things, so, if we remove a binder, we need to strengthen the
81+ tm (eval ctxt [] tm))) : ctxt
68−-- indices appropriately.
82+
69−
83+addConstant :: Name -> Type -> Context -> Context
70−-- eval :: Context -> HEnv -> HTT -> HTT
84+addConstant n ty ctxt = (n, Constant Ref ty (eval ctxt [] ty)) : ctxt
71−-- eval ctxt env t = ev t where
85+
72−-- ev (HP Ref n ty)
86+lookupTy :: Name -> Context -> Maybe Type
73−-- | Just v <- lookupVal n ctxt = v -- FIXME! Needs evalling
87+lookupTy n ctxt = do def <- lookup n ctxt
74−-- ev (HP nt n ty) = HP nt n (ev ty)
88+ case def of
75−-- {-
89+ (Function (Fun ty _ _ _)) -> return ty
76−-- ev (HV i)
90+ (Constant _ ty _) -> return ty
77−-- | i < length env = case env!! i of
91+
78−-- (n, Let t v) -> ev v
92+lookupP :: Name -> Context -> Maybe Term
79−-- _ -> HV i
93+lookupP n ctxt
94+ = do def <- lookup n ctxt
96+ (Function (Fun ty _ tm _)) -> return (P Ref n ty)
97+ (Constant nt ty hty) -> return (P nt n ty)
99+lookupVal :: Name -> Context -> Maybe Value
101+ = do def <- lookup n ctxt
103+ (Function (Fun _ _ _ htm)) -> return htm
104+ (Constant nt ty hty) -> return (VP nt n hty)
106+lookupTyEnv :: Name -> Env -> Maybe (Int, Type)
107+lookupTyEnv n env = li n 0 env where
110+ | n == x = Just (i, binderTy b)
80−-- | otherwise = HV i
111+ | otherwise = li n (i+1) xs
83−-- = evSpine f [(a, t)]
84−-- where evSpine (HApp f t a) sp = evSpine f ((a, t):sp)
85−-- evSpine f sp = evAp f sp
86−-- ev (HBind n (Let t v) sc) = weaken (-1) (ev (sc (weaken 1 (ev v))))
88−-- = HBind n (evB b) (\x -> ev (sc x))
89−-- where evB (Lam t) = Lam (ev t)
90−-- evB (Pi t) = Pi (ev t)
91−-- evB (Hole t) = Hole (ev t)
92−-- evB (Guess t v) = Guess (ev t) (ev v)
93−-- evB (PVar t) = PVar (ev t)
94−-- ev tm = tm -- Constructors + constants
96−-- -- TODO:add PE magic here - nothing is evaluated yet
98−-- evAp f sp = evAp' (ev f) sp
99−-- evAp' (HBind n (Lam ty) sc) ((a, t):sp)
100−-- = weaken (-1) (evAp' (sc (weaken 1 (ev a))) sp)
101−-- evAp' f sp = apply f (map (\ (a, t) -> (ev a, ev t)) sp)
104−-- apply f ((a, t):xs) = apply (HApp f t a) xs
106−-- -- Adjust de Bruijn indices across a term
108−-- weaken :: Int -> HTT -> HTT
109−-- weaken w (HV i) = HV (i + w)
110−-- weaken w (HBind n b sc)
111−-- = HBind n (weakenB b) (\x -> weaken w (sc x))
112−-- where weakenB (Lam t) = Lam (weaken w t)
113−-- weakenB (Pi t) = Pi (weaken w t)
114−-- weakenB (Let t v) = Let (weaken w t) (weaken w v)
115−-- weakenB (Hole t) = Hole (weaken w t)
116−-- weakenB (Guess t v) = Guess (weaken w t) (weaken w v)
117−-- weakenB (PVar t) = PVar (weaken w t)
118−-- weaken w (HApp f t a) = HApp (weaken w f) (weaken w t) (weaken w a)
121−-- quote :: Int -> HTT -> TT Name
122−-- quote env (HP nt n t) = P nt n (quote env t)
123−-- quote env (HV i) = V i
124−-- quote env (HBind n b sc)
125−-- = Bind n (quoteB b) (quote (env+1) (sc (HTmp (env+1))))
126−-- where quoteB (Lam t) = Lam (quote env t)
127−-- quoteB (Pi t) = Pi (quote env t)
128−-- quoteB (Let t v) = Let (quote env t) (quote env v)
129−-- quoteB (Hole t) = Hole (quote env t)
130−-- quoteB (Guess t v) = Guess (quote env t) (quote env v)
131−-- quoteB (PVar t) = PVar (quote env t)
132−-- quote env (HApp f t a) = App (quote env f) (quote env t) (quote env a)
133−-- quote env (HSet i) = Set i
134−-- quote env (HTmp i) = V (env-i)