1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, DeriveFunctor #-}
5import Control.Monad.State
11-- To check conversion, normalise each term wrt the current environment.
12-- Since we haven't converted everything to de Bruijn indices yet, we'll have to
13-- deal with alpha conversion - we do this by making each inner term de Bruijn
14-- indexed with 'finalise'
16converts :: Context -> Env -> Term -> Term -> TC ()
17converts ctxt env x y = if (finalise (normalise ctxt env x) ==
18 finalise (normalise ctxt env y))
20 else fail ("Can't convert between " ++
21 showEnv env (finalise (normalise ctxt env x)) ++ " and " ++
22 showEnv env (finalise (normalise ctxt env y)))
24isSet :: Context -> Env -> Term -> TC ()
25isSet ctxt env tm = isSet' (normalise ctxt env tm)
26 where isSet' :: Term -> TC ()
27 isSet' (Set _) = return ()
28 isSet' tm = fail (showEnv env tm ++ " is not a Set")
30recheck :: Context -> Env -> Term -> TC (Term, Type)
31recheck ctxt env tm = check ctxt env (forget tm)
33check :: Context -> Env -> Raw -> TC (Term, Type)
35 | Just (i, ty) <- lookupTyEnv n env = return (P Bound n ty, ty)
36 | Just (P nt n' ty) <- lookupP n ctxt = return (P nt n' ty, ty)
37 | otherwise = do fail $ "No such variable " ++ show n
38check ctxt env (RApp f a)
39 = do (fv, fty) <- check ctxt env f
40 (av, aty) <- check ctxt env a
41 let fty' = normalise ctxt env fty
42-- trace (showEnv env fty ++ " ===> " ++ showEnv env fty') $
45 do converts ctxt env s aty
47 normalise ctxt env (Bind x (Let aty av) t))
48 t -> fail "Can't apply a non-function type"
49check ctxt env (RSet i) = return (Set i, Set i) -- LATER: (i+1))
50check ctxt env (RBind n b sc)
51 = do b' <- checkBinder b
52 (scv, sct) <- check ctxt ((n, b'):env) sc
53 discharge n b' (pToV n scv) (pToV n sct)
54 where checkBinder (Lam t)
55 = do (tv, tt) <- check ctxt env t
56 let tv' = normalise ctxt env tv
57 let tt' = normalise ctxt env tt
61 = do (tv, tt) <- check ctxt env t
62 let tv' = normalise ctxt env tv
63 let tt' = normalise ctxt env tt
67 = do (tv, tt) <- check ctxt env t
68 (vv, vt) <- check ctxt env v
69 let tv' = normalise ctxt env tv
70 let tt' = normalise ctxt env tt
71 converts ctxt env tv vt
74 checkBinder (NLet t v)
75 = do (tv, tt) <- check ctxt env t
76 (vv, vt) <- check ctxt env v
77 let tv' = normalise ctxt env tv
78 let tt' = normalise ctxt env tt
79 converts ctxt env tv vt
83 = do (tv, tt) <- check ctxt env t
84 let tv' = normalise ctxt env tv
85 let tt' = normalise ctxt env tt
88 checkBinder (Guess t v)
89 = do (tv, tt) <- check ctxt env t
90 (vv, vt) <- check ctxt env v
91 let tv' = normalise ctxt env tv
92 let tt' = normalise ctxt env tt
93 converts ctxt env tv vt
97 = do (tv, tt) <- check ctxt env t
98 let tv' = normalise ctxt env tv
99 let tt' = normalise ctxt env tt
103 discharge n (Lam t) scv sct
104 = return (Bind n (Lam t) scv, Bind n (Pi t) sct)
105 discharge n (Pi t) scv sct
106 = return (Bind n (Pi t) scv, sct)
107 discharge n (Let t v) scv sct
108 = return (Bind n (Let t v) scv, Bind n (Let t v) sct)
109 discharge n (NLet t v) scv sct
110 = return (Bind n (NLet t v) scv, Bind n (Let t v) sct)
111 discharge n (Hole t) scv sct
112 = do -- A hole can't appear in the type of its scope
114 return (Bind n (Hole t) scv, sct)
115 discharge n (Guess t v) scv sct
116 = do -- A hole can't appear in the type of its scope
118 return (Bind n (Guess t v) scv, sct)
119 discharge n (PVar t) scv sct
120 = return (Bind n (PVar t) scv, sct)
122 checkNotHoley i (V v)
123 | v == i = fail "You can't put a hole where a hole don't belong"
124 checkNotHoley i (App f a) = do checkNotHoley i f
126 checkNotHoley i (Bind n b sc) = checkNotHoley (i+1) sc
127 checkNotHoley _ _ = return ()
129pToV :: Eq n => n -> TT n -> TT n
130pToV n = pToV' 0 where
131 pToV' i (P _ x _) | n == x = V i
132 pToV' i (Bind x b sc)
133 | n == x = Bind x (fmap (pToV' i) b) sc
134 | otherwise = Bind x (fmap (pToV' i) b) (pToV' (i+1) sc)
135 pToV' i (App f a) = App (pToV' i f) (pToV' i a)
140 vToP' env (V i) = let (n, b) = (env !! i) in
141 P Bound n (binderTy b)
142 vToP' env (Bind n b sc) = let b' = fmap (vToP' env) b in
143 Bind n b' (vToP' ((n, b'):env) sc)
144 vToP' env (App f a) = App (vToP' env f) (vToP' env a)
147instantiate :: TT n -> TT n -> TT n
148instantiate e = subst 0 where
149 subst i (V x) | i == x = e
150 subst i (Bind x b sc) = Bind x (fmap (subst i) b) (subst (i+1) sc)
151 subst i (App f a) = App (subst i f) (subst i a)
154finalise :: Eq n => TT n -> TT n
155finalise (Bind x b sc) = Bind x (fmap finalise b) (pToV x (finalise sc))
156finalise (App f a) = App (finalise f) (finalise a)
159checkProgram :: Context -> RProgram -> TC Context
160checkProgram ctxt [] = return ctxt
161checkProgram ctxt ((n, RConst t) : xs)
162 = do (t', tt') <- trace (show n) $ check ctxt [] t
164 checkProgram (addConstant n t' ctxt) xs
165checkProgram ctxt ((n, RFunction (RawFun ty val)) : xs)
166 = do (ty', tyt') <- trace (show n) $ check ctxt [] ty
167 (val', valt') <- check ctxt [] val
169 converts ctxt [] ty' valt'
170 checkProgram (addToCtxt n val' ty' ctxt) xs
171checkProgram ctxt ((n, RData (RDatatype _ ty cons)) : xs)
172 = do (ty', tyt') <- trace (show n) $ check ctxt [] ty
174 -- add the tycon temporarily so we can check constructors
175 let ctxt' = addDatatype (Data n ty' []) ctxt
176 cons' <- mapM (checkCon ctxt') cons
177 checkProgram (addDatatype (Data n ty' cons') ctxt) xs
178 where checkCon ctxt (n, cty) = do (cty', ctyt') <- check ctxt [] cty
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list