on master help sign in
Changed evaluator
commit867f0928cbb7a31bd7e4bce43b31c5ba3c2ada58 authorEdwin Brady <eb@cs.st-andrews.ac.uk> dateSat 24 Sep 2011, 14:11 (15 years ago) parent56309a5
8 files changed+148−90
src/Core.hs+12−7 src/CoreParser.hs+1 src/Evaluate.hs+119−69 src/Main.hs+5−3 src/ProofShell.hs+4−3 src/ProofState.hs+2−2 src/Typecheck.hs+4−5 src/test.mi+1−1
src/Core.hs+12−7
@@ -69,10 +69,12 @@ type RProgram = [(Name, RDef)]
6969data NameType = Ref | DCon Int Int | TCon Int
7070 deriving (Show, Eq)
7171
72+-- FIXME: Consider changing this to birectional, with type annotations.
73+
7274data TT n = P NameType n (TT n) -- embed type
7375 | V Int
7476 | Bind n (Binder (TT n)) (TT n)
75− | App (TT n) (TT n) (TT n) -- function, function type, arg
77+ | App (TT n) (TT n) -- function, function type, arg
7678 | Set Int
7779 deriving (Functor, Eq)
7880
@@ -105,7 +107,7 @@ showEnv' env t dbg = se 10 env t where
105107 if dbg then "{" ++ show i ++ "}" else ""
106108 | otherwise = "!!V " ++ show i ++ "!!"
107109 se p env (Bind n b sc) = bracket p 2 $ sb env n b ++ se 10 ((n,b):env) sc
108− se p env (App f t a) = bracket p 1 $ se 1 env f ++ " " ++ se 0 env a
110+ se p env (App f a) = bracket p 1 $ se 1 env f ++ " " ++ se 0 env a
109111 se p env (Set i) = "Set" ++ show i
110112
111113 sb env n (Lam t) = showb env "\\ " " => " n t
@@ -125,7 +127,7 @@ showEnv' env t dbg = se 10 env t where
125127-- Check whether a term has any holes in it - impure if so
126128
127129pureTerm :: TT n -> Bool
128−pureTerm (App f t a) = pureTerm f && pureTerm t && pureTerm a
130+pureTerm (App f a) = pureTerm f && pureTerm a
129131pureTerm (Bind n b sc) = pureBinder b && pureTerm sc where
130132 pureBinder (Hole _) = False
131133 pureBinder (Guess _ _) = False
@@ -138,7 +140,7 @@ pureTerm _ = True
138140weakenTm :: Int -> TT n -> TT n
139141weakenTm i t = wk i 0 t
140142 where wk i min (V x) | x >= min = V (i + x)
141− wk i m (App f t a) = App (wk i m f) (wk i m t) (wk i m a)
143+ wk i m (App f a) = App (wk i m f) (wk i m a)
142144 wk i m (Bind x b sc) = Bind x (wkb i m b) (wk i (m + 1) sc)
143145 wk i m t = t
144146 wkb i m (Let t v) = Let (wk i m t) (wk i m v)
@@ -148,7 +150,7 @@ weakenTm i t = wk i 0 t
148150-- weaken an environment so that all the de Bruijn indices are correct according
149151-- to the latest bound variable
150152
151−weakenEnv :: Show n => EnvTT n -> EnvTT n
153+weakenEnv :: EnvTT n -> EnvTT n
152154weakenEnv env = wk (length env - 1) env
153155 where wk i [] = []
154156 wk i ((n, b) : bs) = (n, weakenTmB i b) : wk (i - 1) bs
@@ -156,12 +158,15 @@ weakenEnv env = wk (length env - 1) env
156158 weakenTmB i (Guess t v) = Guess (weakenTm i t) (weakenTm i v)
157159 weakenTmB i t = t { binderTy = weakenTm i (binderTy t) }
158160
161+weakenTmEnv :: Int -> EnvTT n -> EnvTT n
162+weakenTmEnv i = map (\ (n, b) -> (n, fmap (weakenTm i) b))
163+
159164-- WELL TYPED TERMS AS HOAS -------------------------------------------------
160165
161166data HTT = HP NameType Name HTT
162167 | HV Int
163168 | HBind Name (Binder HTT) (HTT -> HTT)
164− | HApp HTT HTT HTT
169+ | HApp HTT HTT
165170 | HSet Int
166171 | HTmp Int
167172
@@ -179,7 +184,7 @@ hoas env (Bind n b sc) = HBind n (hbind b) (\x -> hoas (x:env) sc)
179184 hbind (PVar t) = PVar (hoas env t)
180185 hbind (Let v t) = Let (hoas env v) (hoas env t)
181186 hbind (Guess v t) = Guess (hoas env v) (hoas env t)
182−hoas env (App f t a) = HApp (hoas env f) (hoas env t) (hoas env a)
187+hoas env (App f a) = HApp (hoas env f) (hoas env a)
183188hoas env (Set i) = HSet i
184189
185190-- CONTEXTS -----------------------------------------------------------------
src/CoreParser.hs+1
@@ -96,6 +96,7 @@ pExp = do lchar '\\'; x <- iName; lchar ':'; ty <- pTerm
9696 x <- iName; lchar ':'; ty <- pTerm
9797 lchar '=';
9898 val <- pTerm
99+ reserved "in";
99100 sc <- pTerm
100101 return (RBind x (Let ty val) sc))
101102 <|> try (do lchar '_';
src/Evaluate.hs+119−69
@@ -6,79 +6,129 @@ import Debug.Trace
66import Core
77
88normalise :: Context -> Env -> TT Name -> TT Name
9−normalise ctxt env t = quote 0 (eval ctxt (evalEnv env) (hoas [] t))
10−
11−type HEnv = [(Name, Binder HTT)]
9+normalise ctxt env t = quote 0 (eval ctxt (weakenEnv env) t)
1210
13−evalEnv :: Env -> HEnv
14−evalEnv = map (\ (n, b) -> (n, fmap (hoas []) b))
11+data Value = VP NameType Name Value
12+ | VV Int
13+ | VBind Name (Binder Value) (Value -> Value)
14+ | VApp Value Value
15+ | VSet Int
16+ | VTmp Int
1517
16−-- We just evaluate HOAS terms. Any de Bruijn indices will refer to higher
17−-- level things, so, if we remove a binder, we need to strengthen the
18−-- indices appropriately.
19−
20−eval :: Context -> HEnv -> HTT -> HTT
21−eval ctxt env t = ev t where
22− ev (HP Ref n ty)
23− | Just v <- lookupVal n ctxt = v -- FIXME! Needs evalling
24− ev (HP nt n ty) = HP nt n (ev ty)
25−{-
26− ev (HV i)
27− | i < length env = case env!! i of
28− (n, Let t v) -> ev v
29− _ -> HV i
30− | otherwise = HV i
31−-}
32− ev (HApp f t a)
33− = evSpine f [(a, t)]
34− where evSpine (HApp f t a) sp = evSpine f ((a, t):sp)
35− evSpine f sp = evAp f sp
36− ev (HBind n (Let t v) sc) = weaken (-1) (ev (sc (weaken 1 (ev v))))
37− ev (HBind n b sc)
38− = HBind n (evB b) (\x -> ev (sc x))
39− where evB (Lam t) = Lam (ev t)
40− evB (Pi t) = Pi (ev t)
41− evB (Hole t) = Hole (ev t)
42− evB (Guess t v) = Guess (ev t) (ev v)
43− evB (PVar t) = PVar (ev t)
44− ev tm = tm -- Constructors + constants
18+eval :: Context -> Env -> TT Name -> Value
19+eval ctxt genv tm = ev [] tm where
20+-- ev env (P Ref n ty)
21+-- | Just v <- lookupP n ctxt = v -- FIXME! Needs evalling
22+ ev env (P nt n ty) = VP nt n (ev env ty)
23+ ev env (V i) | i < length env = env !! i
24+ | i < length env + length genv
25+ = case genv !! (i - length env) of
26+ (_, Let t v) -> ev env v
27+ _ -> VV i
28+ | otherwise = error $ "Internal error: V" ++ show i
29+ ev env (Bind n (Let t v) sc)
30+ = ev (ev env v : env) sc
31+ 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)
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
38+ ev env (Set i) = VSet i
39+
40+ evApply env args (App f a) = evApply env (a:args) f
41+ evApply env args f = apply env (ev env f) (reverse args)
4542
46− -- TODO:add PE magic here - nothing is evaluated yet
43+ 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)
45+ apply env f [] = f
4746
48− evAp f sp = evAp' (ev f) sp
49− evAp' (HBind n (Lam ty) sc) ((a, t):sp)
50− = weaken (-1) (evAp' (sc (weaken 1 (ev a))) sp)
51− evAp' f sp = apply f (map (\ (a, t) -> (ev a, ev t)) sp)
47+ unload env f [] = f
48+ unload env f (a:as) = unload env (VApp f (ev env a)) as
5249
53− apply f [] = f
54− apply f ((a, t):xs) = apply (HApp f t a) xs
50+quote :: Int -> Value -> TT Name
51+quote i (VP nt n v) = P nt n (quote i v)
52+quote i (VV x) = V x
53+quote i (VBind n b sc) = Bind n (quoteB b) (quote (i+1) (sc (VTmp (i+1))))
54+ where quoteB t = fmap (quote i) t
55+quote i (VApp f a) = App (quote i f) (quote i a)
56+quote i (VSet u) = Set u
57+quote i (VTmp x) = V (i - x)
5558
56−-- Adjust de Bruijn indices across a term
57−
58−weaken :: Int -> HTT -> HTT
59−weaken w (HV i) = HV (i + w)
60−weaken w (HBind n b sc)
61− = HBind n (weakenB b) (\x -> weaken w (sc x))
62− where weakenB (Lam t) = Lam (weaken w t)
63− weakenB (Pi t) = Pi (weaken w t)
64− weakenB (Let t v) = Let (weaken w t) (weaken w v)
65− weakenB (Hole t) = Hole (weaken w t)
66− weakenB (Guess t v) = Guess (weaken w t) (weaken w v)
67− weakenB (PVar t) = PVar (weaken w t)
68−weaken w (HApp f t a) = HApp (weaken w f) (weaken w t) (weaken w a)
69−weaken w tm = tm
59+-- normalise ctxt env t = quote 0 (eval ctxt (evalEnv env) (hoas [] t))
60+--
61+-- type HEnv = [(Name, Binder HTT)]
62+--
63+-- evalEnv :: Env -> HEnv
64+-- evalEnv = map (\ (n, b) -> (n, fmap (hoas []) b))
65+--
66+-- We just evaluate HOAS terms. Any de Bruijn indices will refer to higher
67+-- level things, so, if we remove a binder, we need to strengthen the
68+-- indices appropriately.
7069
71−quote :: Int -> HTT -> TT Name
72−quote env (HP nt n t) = P nt n (quote env t)
73−quote env (HV i) = V i
74−quote env (HBind n b sc)
75− = Bind n (quoteB b) (quote (env+1) (sc (HTmp (env+1))))
76− where quoteB (Lam t) = Lam (quote env t)
77− quoteB (Pi t) = Pi (quote env t)
78− quoteB (Let t v) = Let (quote env t) (quote env v)
79− quoteB (Hole t) = Hole (quote env t)
80− quoteB (Guess t v) = Guess (quote env t) (quote env v)
81− quoteB (PVar t) = PVar (quote env t)
82−quote env (HApp f t a) = App (quote env f) (quote env t) (quote env a)
83−quote env (HSet i) = Set i
84−quote env (HTmp i) = V (env-i)
70+-- eval :: Context -> HEnv -> HTT -> HTT
71+-- eval ctxt env t = ev t where
72+-- ev (HP Ref n ty)
73+-- | Just v <- lookupVal n ctxt = v -- FIXME! Needs evalling
74+-- ev (HP nt n ty) = HP nt n (ev ty)
75+-- {-
76+-- ev (HV i)
77+-- | i < length env = case env!! i of
78+-- (n, Let t v) -> ev v
79+-- _ -> HV i
80+-- | otherwise = HV i
81+-- -}
82+-- ev (HApp f t a)
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))))
87+-- ev (HBind n b sc)
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
95+--
96+-- -- TODO:add PE magic here - nothing is evaluated yet
97+--
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)
102+--
103+-- apply f [] = f
104+-- apply f ((a, t):xs) = apply (HApp f t a) xs
105+--
106+-- -- Adjust de Bruijn indices across a term
107+--
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)
119+-- weaken w tm = tm
120+--
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)
src/Main.hs+5−3
@@ -10,6 +10,8 @@ import ProofShell
1010main = do f <- readFile "test.mi"
1111 let Right defs = parseFile f
1212 print defs
13− let OK ctxt = checkProgram [] defs
14− print ctxt
15− runShell (initState ctxt)
13+ case checkProgram [] defs of
14+ OK ctxt -> do print ctxt
15+ runShell (initState ctxt)
16+ return ()
17+ err -> print err
src/ProofShell.hs+4−3
@@ -27,9 +27,10 @@ processCommand (Theorem n ty) state
2727processCommand Quit state = (state { exitNow = True }, "Bye bye")
2828processCommand (Eval t) state =
2929 case check (ctxt state) [] t of
30− OK (val, _) ->
31− let nf = normalise (ctxt state) [] val in
32− (state, show nf)
30+ OK (val, ty) ->
31+ let nf = normalise (ctxt state) [] val
32+ tnf = normalise (ctxt state) [] ty in
33+ (state, show nf ++ " : " ++ show ty)
3334 err -> (state, show err)
3435processCommand (Tac t) state
3536 | Just ps <- prf state = case processTactic t ps of
src/ProofState.hs+2−2
@@ -89,7 +89,7 @@ goal :: Hole -> Term -> Context -> TC Goal
8989goal h tm ctxt = g [] tm where
9090 g env (Bind n b sc) | hole b && same h n = return $ GD env b
9191 | otherwise = gb env b `mplus` g ((n,b):env) sc
92− g env (App f t a) = g env f `mplus` g env t `mplus` g env a
92+ g env (App f a) = g env f `mplus` g env a
9393 g env t = Error "Can't find hole"
9494
9595 gb env (Let t v) = g env t `mplus` g env v
@@ -104,7 +104,7 @@ tactic h ps ctxt f = do let tm = pterm ps
104104 atH env binder@(Bind n b sc)
105105 | hole b && same h n = f ctxt (weakenEnv env) binder
106106 | otherwise = pure Bind <*> pure n <*> atHb env b <*> atH ((n,b):env) sc
107− atH env (App f t a) = pure App <*> atH env f <*> atH env t <*> atH env a
107+ atH env (App f a) = pure App <*> atH env f <*> atH env a
108108 atH env t = return t
109109
110110 atHb env (Let t v) = pure Let <*> atH env t <*> atH env v
src/Typecheck.hs+4−5
@@ -55,8 +55,8 @@ check ctxt env (RApp f a)
5555 case fty' of
5656 Bind x (Pi s) t ->
5757 do converts ctxt env s aty
58− return (App fv fty av,
59− normalise ctxt env (Bind x (Let av aty) t))
58+ return (App fv av,
59+ normalise ctxt env (Bind x (Let aty av) t))
6060 t -> fail "Can't apply a non-function type"
6161check ctxt env (RSet i) = return (Set i, Set (i+1))
6262check ctxt env (RBind n b sc)
@@ -123,9 +123,8 @@ check ctxt env (RBind n b sc)
123123
124124 checkNotHoley i (V v)
125125 | v == i = fail "You can't put a hole where a hole don't belong"
126− checkNotHoley i (App f t a) = do checkNotHoley i f
127− checkNotHoley i t
128− checkNotHoley i a
126+ checkNotHoley i (App f a) = do checkNotHoley i f
127+ checkNotHoley i a
129128 checkNotHoley i (Bind n b sc) = checkNotHoley (i+1) sc
130129 checkNotHoley _ _ = return ()
131130
src/test.mi+1−1
@@ -3,7 +3,7 @@ O : N;
33S : N -> N;
44
55one : N = S O;
6−two : N = S one;
6+two : N = S (S O);
77three : N = S two;
88
99Vect : (A:Set 0) -> (n:N) -> Set 0;