@@ -8,32 +8,14 @@ import Debug.Trace
88import Core
99import Evaluate
1010
11−data TC a = OK a
12− | Error String -- TMP! Make this an informative data structure
13− deriving (Eq, Functor)
14−
15−instance Show a => Show (TC a) where
16− show (OK x) = show x
17− show (Error str) = "Error: " ++ str
18−
19−-- at some point, this instance should also carry type checking options
20−-- (e.g. Set:Set)
21−
22−instance Monad TC where
23− return = OK
24− x >>= k = case x of
25− OK v -> k v
26− Error e -> Error e
27− fail = Error
28−
29−instance MonadPlus TC where
30− mzero = Error "Unknown error"
31− (OK x) `mplus` _ = OK x
32− _ `mplus` (OK y) = OK y
33− err `mplus` _ = err
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'
3415
3516converts :: Context -> Env -> Term -> Term -> TC ()
36−converts ctxt env x y = if (normalise ctxt env x == normalise ctxt env y)
17+converts ctxt env x y = if (finalise (normalise ctxt env x) ==
18+ finalise (normalise ctxt env y))
3719 then return ()
3820 else fail ("Can't convert between " ++
3921 showEnv env (normalise ctxt env x) ++ " and " ++
@@ -173,14 +155,24 @@ finalise t = t
173155
174156checkProgram :: Context -> RProgram -> TC Context
175157checkProgram ctxt [] = return ctxt
176−checkProgram ctxt ((n, RConst t):xs)
158+checkProgram ctxt ((n, RConst t) : xs)
177159 = do (t', tt') <- trace (show n) $ check ctxt [] t
178160 isSet ctxt [] tt'
179161 checkProgram (addConstant n t' ctxt) xs
180−checkProgram ctxt ((n, RFunction (RawFun ty val)):xs)
162+checkProgram ctxt ((n, RFunction (RawFun ty val)) : xs)
181163 = do (ty', tyt') <- trace (show n) $ check ctxt [] ty
182164 (val', valt') <- check ctxt [] val
183165 isSet ctxt [] tyt'
184166 converts ctxt [] ty' valt'
185167 checkProgram (addToCtxt n val' ty' ctxt) xs
168+checkProgram ctxt ((n, RData (RDatatype _ ty cons)) : xs)
169+ = do (ty', tyt') <- trace (show n) $ check ctxt [] ty
170+ isSet ctxt [] tyt'
171+ -- add the tycon temporarily so we can check constructors
172+ let ctxt' = addDatatype (Data n ty' []) ctxt
173+ cons' <- mapM (checkCon ctxt') cons
174+ checkProgram (addDatatype (Data n ty' cons') ctxt) xs
175+ where checkCon ctxt (n, cty) = do (cty', ctyt') <- check ctxt [] cty
176+ return (n, cty')
177+
186178