1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances,
4module Core.Evaluate(normalise, normaliseTrace, normaliseC, normaliseAll,
5 simplify, specialise, hnf, convEq, convEq',
6 Def(..), Accessibility(..), Totality(..), PReason(..),
7 Context, initContext, ctxtAlist, uconstraints, next_tvar,
8 addToCtxt, setAccess, setTotal, addCtxtDef, addTyDecl,
9 addDatatype, addCasedef, simplifyCasedef, addOperator,
10 lookupNames, lookupTy, lookupP, lookupDef, lookupVal,
11 lookupTotal, lookupTyEnv, isConName, isFnName,
12 Value(..), Quote(..), evalState, initEval) where
15import Control.Monad.State
16import qualified Data.Binary as B
17import Data.Binary hiding (get, put)
22data EvalState = ES { limited :: [(Name, Int)],
25type Eval a = State EvalState a
27data EvalOpt = Spec | HNF | Simplify Bool | AtREPL
32-- VALUES (as HOAS) ---------------------------------------------------------
33-- | A HOAS representation of values
34data Value = VP NameType Name Value
36 -- True for Bool indicates safe to reduce
37 | VBind Bool Name (Binder Value) (Value -> Eval Value)
38 -- For frozen let bindings when simplifying
39 | VBLet Int Name Value Value Value
44-- | VLazy Env [Value] Term
47data HNF = HP NameType Name (TT Name)
49 | HBind Name (Binder HNF) (HNF -> Eval HNF)
50 | HApp HNF [HNF] [TT Name]
56instance Show Value where
57 show x = show $ evalState (quote 100 x) initEval
59instance Show (a -> b) where
62-- THE EVALUATOR ------------------------------------------------------------
64-- The environment is assumed to be "locally named" - i.e., not de Bruijn
66-- i.e. it's an intermediate environment that we have while type checking or
67-- while building a proof.
69-- | Normalise fully type checked terms (so, assume all names/let bindings resolved)
70normaliseC :: Context -> Env -> TT Name -> TT Name
72 = evalState (do val <- eval False ctxt [] env t []
75normaliseAll :: Context -> Env -> TT Name -> TT Name
76normaliseAll ctxt env t
77 = evalState (do val <- eval False ctxt [] env t [AtREPL]
80normalise :: Context -> Env -> TT Name -> TT Name
81normalise = normaliseTrace False
83normaliseTrace :: Bool -> Context -> Env -> TT Name -> TT Name
84normaliseTrace tr ctxt env t
85 = evalState (do val <- eval tr ctxt [] (map finalEntry env) (finalise t) []
88specialise :: Context -> Env -> [(Name, Int)] -> TT Name -> TT Name
89specialise ctxt env limits t
90 = evalState (do val <- eval False ctxt limits (map finalEntry env) (finalise t) []
91 quote 0 val) (initEval { limited = limits })
93-- | Like normalise, but we only reduce functions that are marked as okay to
94-- inline (and probably shouldn't reduce lets?)
95simplify :: Context -> Bool -> Env -> TT Name -> TT Name
96simplify ctxt runtime env t
97 = evalState (do val <- eval False ctxt [(UN "lazy", 0),
98 (UN "assert_smaller", 0),
101 (map finalEntry env) (finalise t)
103 quote 0 val) initEval
105-- | Reduce a term to head normal form
106hnf :: Context -> Env -> TT Name -> TT Name
108 = evalState (do val <- eval False ctxt []
111 quote 0 val) initEval
114-- unbindEnv env (quote 0 (eval ctxt (bindEnv env t)))
116finalEntry :: (Name, Binder (TT Name)) -> (Name, Binder (TT Name))
117finalEntry (n, b) = (n, fmap finalise b)
119bindEnv :: EnvTT n -> TT n -> TT n
121bindEnv ((n, Let t v):bs) tm = Bind n (NLet t v) (bindEnv bs tm)
122bindEnv ((n, b):bs) tm = Bind n b (bindEnv bs tm)
124unbindEnv :: EnvTT n -> TT n -> TT n
126unbindEnv (_:bs) (Bind n b sc) = unbindEnv bs sc
128usable :: Bool -> Name -> [(Name, Int)] -> (Bool, [(Name, Int)])
129usable _ _ ns@((MN 0 "STOP", _) : _) = (False, ns)
130usable True (UN "io_bind") ns = (False, ns)
131usable s n [] = (True, [])
132usable s n ns = case lookup n ns of
133 Just 0 -> (False, ns)
134 Just i -> (True, (n, abs (i-1)) : filter (\ (n', _) -> n/=n') ns)
135 _ -> if s then (True, (n, 0) : filter (\ (n', _) -> n/=n') ns)
136 else (True, (n, 100) : filter (\ (n', _) -> n/=n') ns)
138-- | Evaluate in a context of locally named things (i.e. not de Bruijn indexed,
139-- such as we might have during construction of a proof)
140eval :: Bool -> Context -> [(Name, Int)] -> Env -> TT Name ->
141 [EvalOpt] -> Eval Value
142eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
143 spec = Spec `elem` opts
144 simpl = Simplify True `elem` opts
145 atRepl = AtREPL `elem` opts
146 hnf = HNF `elem` opts
148 canSimplify inl inr n stk
149 = (Simplify False `elem` opts && (not inl || elem n stk))
150 || (Simplify True `elem` opts && (not inr || elem n stk))
152 ev ntimes stk top env (P _ n ty)
153 | Just (Let t v) <- lookup n genv = ev ntimes stk top env v
154 ev ntimes_in stk top env (P Ref n ty)
155 | not top && hnf = liftM (VP Ref n) (ev ntimes stk top env ty)
156 | (True, ntimes) <- usable simpl n ntimes_in
157 = do let val = lookupDefAcc n atRepl ctxt
159 [(Function _ tm, Public)] ->
160 ev ntimes (n:stk) True env tm
161 [(TyDecl nt ty, _)] -> do vty <- ev ntimes stk True env ty
163 [(CaseOp inl inr _ _ _ [] tree _ _, acc)]
164 | acc == Public || simpl -> -- unoptimised version
165 if canSimplify inl inr n stk
166 then liftM (VP Ref n) (ev ntimes stk top env ty)
167 else do c <- evCase ntimes (n:stk) top env [] [] tree
169 (Nothing, _) -> liftM (VP Ref n) (ev ntimes stk top env ty)
170 (Just v, _) -> return v
171 _ -> liftM (VP Ref n) (ev ntimes stk top env ty)
172 ev ntimes stk top env (P nt n ty) = liftM (VP nt n) (ev ntimes stk top env ty)
173 ev ntimes stk top env (V i)
174 | i < length env && i >= 0 = return $ env !! i
175 | otherwise = return $ VV i
176 ev ntimes stk top env (Bind n (Let t v) sc)
177 | not simpl -- - || vinstances 0 sc < 2
178 = do v' <- ev ntimes stk top env v --(finalise v)
179 sc' <- ev ntimes stk top (v' : env) sc
181 | otherwise -- put this back when the Bind works properly
182 = do t' <- ev ntimes stk top env t
183 v' <- ev ntimes stk top env v --(finalise v)
184 -- use Tmp as a placeholder, then make it a variable reference
185 -- again when evaluation finished
188 put (hs { nexthole = vd + 1 })
189 sc' <- ev ntimes stk top (VP Bound (MN vd "vlet") VErased : env) sc
190 return $ VBLet vd n t' v' sc'
191 ev ntimes stk top env (Bind n (NLet t v) sc)
192 = do t' <- ev ntimes stk top env (finalise t)
193 v' <- ev ntimes stk top env (finalise v)
194 sc' <- ev ntimes stk top (v' : env) sc
195 return $ VBind True n (Let t' v') (\x -> return sc')
196 ev ntimes stk top env (Bind n b sc)
197 = do b' <- vbind env b
198 return $ VBind (not simpl || vinstances 0 sc < 2)
199 n b' (\x -> ev ntimes stk False (x:env) sc)
202 = fmapMB (\tm -> ev ((MN 0 "STOP", 0) : ntimes)
203 stk top env (finalise tm)) t
205 = fmapMB (\tm -> ev ntimes stk top env (finalise tm)) t
206-- ev ntimes stk top env (App (App (P _ laz _) _) a)
208-- = trace (showEnvDbg genv a) $ ev ntimes stk top env a
209 ev ntimes stk top env (App f a)
210 = do f' <- ev ntimes stk False env f
211 a' <- ev ntimes stk False env a
212 evApply ntimes stk top env [a'] f'
213 ev ntimes stk top env (Constant c) = return $ VConstant c
214 ev ntimes stk top env Erased = return VErased
215 ev ntimes stk top env (TType i) = return $ VType i
217 evApply ntimes stk top env args (VApp f a) =
218 evApply ntimes stk top env (a:args) f
219 evApply ntimes stk top env args f = apply ntimes stk top env f args
221 apply ntimes stk top env (VBind True n (Lam t) sc) (a:as)
223 app <- apply ntimes stk top env a' as
225-- apply ntimes stk False env f args
226-- | spec = specApply ntimes stk env f args
227 apply ntimes_in stk top env f@(VP Ref n ty) args
228 | not top && hnf = case args of
230 _ -> return $ unload env f args
231 | (True, ntimes) <- usable simpl n ntimes_in
232 = traceWhen traceon (show stk) $
233 do let val = lookupDefAcc n atRepl ctxt
235 [(CaseOp inl inr _ _ _ ns tree _ _, acc)]
236 | acc == Public -> -- unoptimised version
237 if canSimplify inl inr n stk
238 then return $ unload env (VP Ref n ty) args
239 else do c <- evCase ntimes (n:stk) top env ns args tree
241 (Nothing, _) -> return $ unload env (VP Ref n ty) args
242 (Just v, rest) -> evApply ntimes stk top env rest v
243 [(Operator _ i op, _)] ->
244 if (i <= length args)
245 then case op (take i args) of
246 Nothing -> return $ unload env (VP Ref n ty) args
247 Just v -> evApply ntimes stk top env (drop i args) v
248 else return $ unload env (VP Ref n ty) args
251 _ -> return $ unload env f args
252 apply ntimes stk top env f (a:as) = return $ unload env f (a:as)
253 apply ntimes stk top env f [] = return f
255-- specApply stk env f@(VP Ref n ty) args
256-- = case lookupCtxt n statics of
258-- then trace (show (n, map fst (filter (\ (_, s) -> s) (zip args as)))) $
259-- return $ unload env f args
260-- else return $ unload env f args
261-- _ -> return $ unload env f args
262-- specApply stk env f args = return $ unload env f args
265 unload env f (a:as) = unload env (VApp f a) as
267 evCase ntimes stk top env ns args tree
268 | length ns <= length args
269 = do let args' = take (length ns) args
270 let rest = drop (length ns) args
271 t <- evTree ntimes stk top env
272 (zipWith (\n t -> (n, t)) ns args') tree
274 | otherwise = return (Nothing, args)
276 evTree :: [(Name, Int)] -> [Name] -> Bool ->
277 [Value] -> [(Name, Value)] -> SC -> Eval (Maybe Value)
278 evTree ntimes stk top env amap (UnmatchedCase str) = return Nothing
279 evTree ntimes stk top env amap (STerm tm)
280 = do let etm = pToVs (map fst amap) tm
281 etm' <- ev ntimes stk (not (conHeaded tm))
282 (map snd amap ++ env) etm
284 evTree ntimes stk top env amap (Case n alts)
285 = case lookup n amap of
286 Just v -> do c <- chooseAlt env v (getValArgs v) alts amap
288 Just (altmap, sc) -> evTree ntimes stk top env altmap sc
289 _ -> do c' <- chooseAlt' ntimes stk env v (getValArgs v) alts amap
291 Just (altmap, sc) -> evTree ntimes stk top env altmap sc
294 evTree ntimes stk top env amap ImpossibleCase = return Nothing
296 conHeaded tm@(App _ _)
297 | (P (DCon _ _) _ _, args) <- unApply tm = True
300 chooseAlt' ntimes stk env _ (f, args) alts amap
301 = do f' <- apply ntimes stk True env f args
302 chooseAlt env f' (getValArgs f') alts amap
304 chooseAlt :: [Value] -> Value -> (Value, [Value]) -> [CaseAlt] -> [(Name, Value)] ->
305 Eval (Maybe ([(Name, Value)], SC))
306 chooseAlt env _ (VP (DCon i a) _ _, args) alts amap
307 | Just (ns, sc) <- findTag i alts = return $ Just (updateAmap (zip ns args) amap, sc)
308 | Just v <- findDefault alts = return $ Just (amap, v)
309 chooseAlt env _ (VP (TCon i a) _ _, args) alts amap
310 | Just (ns, sc) <- findTag i alts = return $ Just (updateAmap (zip ns args) amap, sc)
311 | Just v <- findDefault alts = return $ Just (amap, v)
312 chooseAlt env _ (VConstant c, []) alts amap
313 | Just v <- findConst c alts = return $ Just (amap, v)
314 | Just v <- findDefault alts = return $ Just (amap, v)
315 chooseAlt _ _ _ _ _ = return Nothing
317 -- Replace old variable names in the map with new matches
318 -- (This is possibly unnecessary since we make unique names and don't
319 -- allow repeated variables...?)
321 = newm ++ filter (\ (x, _) -> not (elem x (map fst newm))) amap
322 findTag i [] = Nothing
323 findTag i (ConCase n j ns sc : xs) | i == j = Just (ns, sc)
324 findTag i (_ : xs) = findTag i xs
326 findDefault [] = Nothing
327 findDefault (DefaultCase sc : xs) = Just sc
328 findDefault (_ : xs) = findDefault xs
330 findConst c [] = Nothing
331 findConst c (ConstCase c' v : xs) | c == c' = Just v
332 findConst IType (ConCase n 1 [] v : xs) = Just v
333 findConst FlType (ConCase n 2 [] v : xs) = Just v
334 findConst ChType (ConCase n 3 [] v : xs) = Just v
335 findConst StrType (ConCase n 4 [] v : xs) = Just v
336 findConst PtrType (ConCase n 5 [] v : xs) = Just v
337 findConst c (_ : xs) = findConst c xs
339 getValArgs tm = getValArgs' tm []
340 getValArgs' (VApp f a) as = getValArgs' f (a:as)
341 getValArgs' f as = (f, as)
343-- tmpToV i vd (VLetHole j) | vd == j = return $ VV i
344-- tmpToV i vd (VP nt n v) = liftM (VP nt n) (tmpToV i vd v)
345-- tmpToV i vd (VBind n b sc) = do b' <- fmapMB (tmpToV i vd) b
346-- let sc' = \x -> do x' <- sc x
347-- tmpToV (i + 1) vd x'
348-- return (VBind n b' sc')
349-- tmpToV i vd (VApp f a) = liftM2 VApp (tmpToV i vd f) (tmpToV i vd a)
350-- tmpToV i vd x = return x
353 quote :: Int -> a -> Eval (TT Name)
355instance Quote Value where
356 quote i (VP nt n v) = liftM (P nt n) (quote i v)
357 quote i (VV x) = return $ V x
358 quote i (VBind _ n b sc) = do sc' <- sc (VTmp i)
360 liftM (Bind n b') (quote (i+1) sc')
361 where quoteB t = fmapMB (quote i) t
362 quote i (VBLet vd n t v sc)
363 = do sc' <- quote i sc
366 let sc'' = pToV (MN vd "vlet") (addBinder sc')
367 return (Bind n (Let t' v') sc'')
368 quote i (VApp f a) = liftM2 App (quote i f) (quote i a)
369 quote i (VType u) = return $ TType u
370 quote i VErased = return $ Erased
371 quote i (VConstant c) = return $ Constant c
372 quote i (VTmp x) = return $ V (i - x - 1)
374instance Quote HNF where
375 quote i (HP nt n t) = return (P nt n t)
376 quote i (HV x) = return $ V x
377 quote i (HBind n b sc) = do sc' <- sc (HTmp i)
379 liftM (Bind n b') (quote (i+1) sc')
380 where quoteB t = fmapMB (quote i) t
381 quote i (HApp f env as) = do f' <- quote i f
382 as' <- mapM (iEnv env) as
383 return $ mkApp f' as'
384 where iEnv [] a = return a
385 iEnv (x:xs) a = do x' <- quote i x
386 iEnv xs (weakenTm (-1) (instantiate x' a))
387 quote i (HType u) = return $ TType u
388 quote i (HConstant c) = return $ Constant c
389 quote i (HTmp x) = return $ V (i - x - 1)
391wknV :: Int -> Value -> Eval Value
392wknV i (VV x) = return $ VV (x + i)
393wknV i (VBind red n b sc) = do b' <- fmapMB (wknV i) b
394 return $ VBind red n b' (\x -> do x' <- sc x
396wknV i (VApp f a) = liftM2 VApp (wknV i f) (wknV i a)
399wknH :: Int -> HNF -> Eval HNF
400wknH i (HV x) = return $ HV (x + i)
401wknH i (HBind n b sc) = do b' <- fmapMB (wknH i) b
402 return $ HBind n b' (\x -> do x' <- sc x
404wknH i (HApp f env as) = liftM3 HApp (wknH i f) (return env)
408-- HEAD NORMAL FORM ---------------------------------------------------------
410eval_hnf :: Context -> Ctxt [Bool] -> Env -> TT Name -> Eval HNF
411eval_hnf ctxt statics genv tm = ev [] tm where
412 ev :: [HNF] -> TT Name -> Eval HNF
414 | Just (Let t v) <- lookup n genv = ev env v
415 ev env (P Ref n ty) = case lookupDef n ctxt of
416 [Function _ t] -> ev env t
417 [TyDecl nt ty] -> return $ HP nt n ty
418 [CaseOp inl _ _ _ _ [] tree _ _] ->
419 do c <- evCase env [] [] tree
421 (Nothing, _, _) -> return $ HP Ref n ty
422 (Just v, _, _) -> return v
423 _ -> return $ HP Ref n ty
424 ev env (P nt n ty) = return $ HP nt n ty
425 ev env (V i) | i < length env = return $ env !! i
426 | otherwise = return $ HV i
427 ev env (Bind n (Let t v) sc)
428 = do v' <- ev env (finalise v)
429 sc' <- ev (v' : env) sc
432 = do b' <- hbind env b
433 return $ HBind n b' (\x -> ev (x : env) sc)
434 where hbind env t = fmapMB (\tm -> ev env (finalise tm)) t
435 ev env (App f a) = evApply env [a] f
436 ev env (Constant c) = return $ HConstant c
437 ev env (TType i) = return $ HType i
439 evApply env args (App f a) = evApply env (a : args) f
440 evApply env args f = do f' <- ev env f
443 apply env (HBind n (Lam t) sc) (a:as) = do a' <- ev env a
445 app <- apply env sc' as
447 apply env (HP Ref n ty) args
448 | [CaseOp _ _ _ _ _ ns tree _ _] <- lookupDef n ctxt
449 = do c <- evCase env ns args tree
451 (Nothing, _, env') -> return $ unload env' (HP Ref n ty) args
452 (Just v, rest, env') -> do v' <- quote 0 v
454-- | Just (Operator _ i op) <- lookupDef n ctxt
455-- = if (i <= length args)
456-- then case op (take i args) of
457-- Nothing -> return $ unload env (HP Ref n ty) args
458-- Just v -> evApply env (drop i args) v
459-- else return $ unload env (HP Ref n ty) args
460 apply env f (a:as) = return $ unload env f (a:as)
461 apply env f [] = return f
464 unload env f as = HApp f env as
466 evCase env ns args tree
467 | length ns <= length args
468 = do let args' = take (length ns) args
469 let rest = drop (length ns) args
470 (t, env') <- evTree env (zipWith (\n t -> (n, t)) ns args') tree
471 return (t, rest, env')
472 | otherwise = return (Nothing, args, env)
474 evTree :: [HNF] -> [(Name, TT Name)] -> SC -> Eval (Maybe HNF, [HNF])
475 evTree env amap (UnmatchedCase str) = return (Nothing, env)
476 evTree env amap (STerm tm)
477 = do let etm = pToVs (map fst amap) tm
478 amap' <- mapM (ev env) (map snd amap)
479 envw <- mapM (wknH (length amap)) env
480 let env' = amap' ++ envw
481 etm' <- trace (show etm) $ ev env' etm
483 trace ("Ev: " ++ show (etm, etmq)) $ return $ (Just etm', env')
484 evTree env amap (Case n alts)
485 = case lookup n amap of
486 Just v -> do v' <- ev env v
487 case chooseAlt v' (getValArgs v') alts amap of
488 Just (altmap, sc) -> evTree env altmap sc
489 _ -> return (Nothing, env)
491 chooseAlt :: HNF -> (HNF, [HNF], [TT Name]) ->
492 [CaseAlt] -> [(Name, TT Name)] ->
493 Maybe ([(Name, TT Name)], SC)
494 chooseAlt _ (HP (DCon i a) _ _, env, args) alts amap
495 | Just (ns, sc) <- findTag i alts = Just (updateAmap (zip ns args) amap, sc)
496 | Just v <- findDefault alts = Just (amap, v)
497 chooseAlt _ (HP (TCon i a) _ _, env, args) alts amap
498 | Just (ns, sc) <- findTag i alts = Just (updateAmap (zip ns args) amap, sc)
499 | Just v <- findDefault alts = Just (amap, v)
500 chooseAlt _ (HConstant c, env, []) alts amap
501 | Just v <- findConst c alts = Just (amap, v)
502 | Just v <- findDefault alts = Just (amap, v)
503 chooseAlt _ _ _ _ = Nothing
505 -- Replace old variable names in the map with new matches
506 -- (This is possibly unnecessary since we make unique names and don't
507 -- allow repeated variables...?)
509 = newm ++ filter (\ (x, _) -> not (elem x (map fst newm))) amap
510 findTag i [] = Nothing
511 findTag i (ConCase n j ns sc : xs) | i == j = Just (ns, sc)
512 findTag i (_ : xs) = findTag i xs
514 findDefault [] = Nothing
515 findDefault (DefaultCase sc : xs) = Just sc
516 findDefault (_ : xs) = findDefault xs
518 findConst c [] = Nothing
519 findConst c (ConstCase c' v : xs) | c == c' = Just v
520 findConst IType (ConCase n 1 [] v : xs) = Just v
521 findConst FlType (ConCase n 2 [] v : xs) = Just v
522 findConst ChType (ConCase n 3 [] v : xs) = Just v
523 findConst StrType (ConCase n 4 [] v : xs) = Just v
524 findConst PtrType (ConCase n 5 [] v : xs) = Just v
525 findConst BIType (ConCase n 6 [] v : xs) = Just v
526 findConst B8Type (ConCase n 7 [] v : xs) = Just v
527 findConst B16Type (ConCase n 8 [] v : xs) = Just v
528 findConst B32Type (ConCase n 9 [] v : xs) = Just v
529 findConst B64Type (ConCase n 10 [] v : xs) = Just v
530 findConst c (_ : xs) = findConst c xs
532 getValArgs (HApp t env args) = (t, env, args)
533 getValArgs t = (t, [], [])
535convEq' ctxt x y = evalStateT (convEq ctxt x y) (0, [])
537convEq :: Context -> TT Name -> TT Name -> StateT UCs TC Bool
538convEq ctxt = ceq [] where
539 ceq :: [(Name, Name)] -> TT Name -> TT Name -> StateT UCs TC Bool
540 ceq ps (P xt x _) (P yt y _)
541 | x == y || (x, y) `elem` ps || (y,x) `elem` ps = return True
542 | otherwise = sameDefs ps x y
543 ceq ps x (Bind n (Lam t) (App y (V 0))) = ceq ps x y
544 ceq ps (Bind n (Lam t) (App x (V 0))) y = ceq ps x y
545 ceq ps x (Bind n (Lam t) (App y (P Bound n' _)))
546 | n == n' = ceq ps x y
547 ceq ps (Bind n (Lam t) (App x (P Bound n' _))) y
548 | n == n' = ceq ps x y
549 ceq ps (V x) (V y) = return (x == y)
550 ceq ps (Bind _ xb xs) (Bind _ yb ys)
551 = liftM2 (&&) (ceqB ps xb yb) (ceq ps xs ys)
553 ceqB ps (Let v t) (Let v' t') = liftM2 (&&) (ceq ps v v') (ceq ps t t')
554 ceqB ps (Guess v t) (Guess v' t') = liftM2 (&&) (ceq ps v v') (ceq ps t t')
555 ceqB ps b b' = ceq ps (binderTy b) (binderTy b')
556 ceq ps (App fx ax) (App fy ay) = liftM2 (&&) (ceq ps fx fy) (ceq ps ax ay)
557 ceq ps (Constant x) (Constant y) = return (x == y)
558 ceq ps (TType x) (TType y) = do (v, cs) <- get
559 put (v, ULE x y : cs)
561 ceq ps Erased _ = return True
562 ceq ps _ Erased = return True
563 ceq ps _ _ = return False
565 caseeq ps (Case n cs) (Case n' cs') = caseeqA ((n,n'):ps) cs cs'
567 caseeqA ps (ConCase x i as sc : rest) (ConCase x' i' as' sc' : rest')
568 = do q1 <- caseeq (zip as as' ++ ps) sc sc'
569 q2 <- caseeqA ps rest rest'
570 return $ x == x' && i == i' && q1 && q2
571 caseeqA ps (ConstCase x sc : rest) (ConstCase x' sc' : rest')
572 = do q1 <- caseeq ps sc sc'
573 q2 <- caseeqA ps rest rest'
574 return $ x == x' && q1 && q2
575 caseeqA ps (DefaultCase sc : rest) (DefaultCase sc' : rest')
576 = liftM2 (&&) (caseeq ps sc sc') (caseeqA ps rest rest')
577 caseeqA ps [] [] = return True
578 caseeqA ps _ _ = return False
579 caseeq ps (STerm x) (STerm y) = ceq ps x y
580 caseeq ps (UnmatchedCase _) (UnmatchedCase _) = return True
581 caseeq ps _ _ = return False
583 sameDefs ps x y = case (lookupDef x ctxt, lookupDef y ctxt) of
584 ([Function _ xdef], [Function _ ydef])
585 -> ceq ((x,y):ps) xdef ydef
586 ([CaseOp _ _ _ _ _ _ xdef _ _],
587 [CaseOp _ _ _ _ _ _ ydef _ _])
588 -> caseeq ((x,y):ps) xdef ydef
591-- SPECIALISATION -----------------------------------------------------------
592-- We need too much control to be able to do this by tweaking the main
595spec :: Context -> Ctxt [Bool] -> Env -> TT Name -> Eval (TT Name)
596spec ctxt statics genv tm = error "spec undefined"
598-- CONTEXTS -----------------------------------------------------------------
600{-| A definition is either a simple function (just an expression with a type),
601 a constant, which could be a data or type constructor, an axiom or as an
602 yet undefined function, or an Operator.
603 An Operator is a function which explains how to reduce.
604 A CaseOp is a function defined by a simple case tree -}
606data Def = Function Type Term
607 | TyDecl NameType Type
608 | Operator Type Int ([Value] -> Maybe Value)
609 | CaseOp Bool Bool -- compile-time/run-time inlinable
611 [Either Term (Term, Term)] -- original definition
612 [([Name], Term, Term)] -- simplified definition
613 [Name] SC -- Compile time case definition
614 [Name] SC -- Run time cae definitions
616deriving instance Binary Def
619instance Show Def where
620 show (Function ty tm) = "Function: " ++ show (ty, tm)
621 show (TyDecl nt ty) = "TyDecl: " ++ show nt ++ " " ++ show ty
622 show (Operator ty _ _) = "Operator: " ++ show ty
623 show (CaseOp inlc inlr ty ps_in ps ns sc ns' sc')
624 = "Case: " ++ show ty ++ " " ++ show ps ++ "\n" ++
625 show ns ++ " " ++ show sc ++ "\n" ++
626 show ns' ++ " " ++ show sc' ++ "\n" ++
627 if inlc then "Inlinable\n" else "Not inlinable\n"
629-- We need this for serialising Def. Fortunately, it never gets used because
630-- we'll never serialise a primitive operator
632instance Binary (a -> b) where
634 get = error "Getting a function"
638-- Frozen => doesn't reduce
639-- Hidden => doesn't reduce and invisible to type checker
641data Accessibility = Public | Frozen | Hidden
644-- | The result of totality checking
645data Totality = Total [Int] -- ^ well-founded arguments
646 | Productive -- ^ productive
651-- | Reasons why a function may not be total
652data PReason = Other [Name] | Itself | NotCovering | NotPositive | UseUndef Name
653 | BelieveMe | Mutual [Name] | NotProductive
656instance Show Totality where
657 show (Total args)= "Total" -- ++ show args ++ " decreasing arguments"
658 show Productive = "Productive" -- ++ show args ++ " decreasing arguments"
659 show Unchecked = "not yet checked for totality"
660 show (Partial Itself) = "possibly not total as it is not well founded"
661 show (Partial NotCovering) = "not total as there are missing cases"
662 show (Partial NotPositive) = "not strictly positive"
663 show (Partial NotProductive) = "not productive"
664 show (Partial BelieveMe) = "not total due to use of believe_me in proof"
665 show (Partial (Other ns)) = "possibly not total due to: " ++ showSep ", " (map show ns)
666 show (Partial (Mutual ns)) = "possibly not total due to recursive path " ++
667 showSep " --> " (map show ns)
670deriving instance Binary Accessibility
674deriving instance Binary Totality
678deriving instance Binary PReason
681-- | Contexts used for global definitions and for proof state. They contain
682-- universe constraints and existing definitions.
683data Context = MkContext {
684 uconstraints :: [UConstraint],
686 definitions :: Ctxt (Def, Accessibility, Totality)
689-- | The initial empty context
690initContext = MkContext [] 0 emptyContext
692-- | Get the definitions from a context
693ctxtAlist :: Context -> [(Name, Def)]
694ctxtAlist ctxt = map (\(n, (d, a, t)) -> (n, d)) $ toAlist (definitions ctxt)
696veval ctxt env t = evalState (eval False ctxt [] env t []) initEval
698addToCtxt :: Name -> Term -> Type -> Context -> Context
699addToCtxt n tm ty uctxt
700 = let ctxt = definitions uctxt
701 ctxt' = addDef n (Function ty tm, Public, Unchecked) ctxt in
702 uctxt { definitions = ctxt' }
704setAccess :: Name -> Accessibility -> Context -> Context
706 = let ctxt = definitions uctxt
707 ctxt' = updateDef n (\ (d, _, t) -> (d, a, t)) ctxt in
708 uctxt { definitions = ctxt' }
710setTotal :: Name -> Totality -> Context -> Context
712 = let ctxt = definitions uctxt
713 ctxt' = updateDef n (\ (d, a, _) -> (d, a, t)) ctxt in
714 uctxt { definitions = ctxt' }
716addCtxtDef :: Name -> Def -> Context -> Context
717addCtxtDef n d c = let ctxt = definitions c
718 ctxt' = addDef n (d, Public, Unchecked) ctxt in
719 c { definitions = ctxt' }
721addTyDecl :: Name -> NameType -> Type -> Context -> Context
722addTyDecl n nt ty uctxt
723 = let ctxt = definitions uctxt
724 ctxt' = addDef n (TyDecl nt ty, Public, Unchecked) ctxt in
725 uctxt { definitions = ctxt' }
727addDatatype :: Datatype Name -> Context -> Context
728addDatatype (Data n tag ty cons) uctxt
729 = let ctxt = definitions uctxt
730 ty' = normalise uctxt [] ty
731 ctxt' = addCons 0 cons (addDef n
732 (TyDecl (TCon tag (arity ty')) ty, Public, Unchecked) ctxt) in
733 uctxt { definitions = ctxt' }
735 addCons tag [] ctxt = ctxt
736 addCons tag ((n, ty) : cons) ctxt
737 = let ty' = normalise uctxt [] ty in
738 addCons (tag+1) cons (addDef n
739 (TyDecl (DCon tag (arity ty')) ty, Public, Unchecked) ctxt)
741addCasedef :: Name -> Bool -> Bool -> Bool -> Bool ->
742 [Either Term (Term, Term)] ->
743 [([Name], Term, Term)] ->
744 [([Name], Term, Term)] ->
745 Type -> Context -> Context
746addCasedef n alwaysInline tcase covering asserted ps_in ps psrt ty uctxt
747 = let ctxt = definitions uctxt
748 access = case lookupDefAcc n False uctxt of
751 ctxt' = case (simpleCase tcase covering CompileTime (FC "" 0) ps,
752 simpleCase tcase covering RunTime (FC "" 0) psrt) of
753 (OK (CaseDef args sc _), OK (CaseDef args' sc' _)) ->
754 let inl = alwaysInline
755 inlc = (inl || small n args sc') && (not asserted)
756 inlr = inl || small n args sc' in
757 addDef n (CaseOp inlc inlr
758 ty ps_in ps args sc args' sc',
759 access, Unchecked) ctxt in
760 uctxt { definitions = ctxt' }
762simplifyCasedef :: Name -> Context -> Context
763simplifyCasedef n uctxt
764 = let ctxt = definitions uctxt
765 ctxt' = case lookupCtxt n ctxt of
766 [(CaseOp inl inr ty [] ps args sc args' sc', acc, tot)] ->
767 ctxt -- nothing to simplify (or already done...)
768 [(CaseOp inl inr ty ps_in ps args sc args' sc', acc, tot)] ->
769 let ps_in' = map simpl ps_in
770 pdef = map debind ps_in' in
771 case simpleCase False True CompileTime (FC "" 0) pdef of
772 OK (CaseDef args sc _) ->
773 addDef n (CaseOp inl inr
774 ty ps_in' ps args sc args' sc',
777 uctxt { definitions = ctxt' }
779 depat acc (Bind n (PVar t) sc)
780 = depat (n : acc) (instantiate (P Bound n t) sc)
781 depat acc x = (acc, x)
782 debind (Right (x, y)) = let (vs, x') = depat [] x
783 (_, y') = depat [] y in
785 debind (Left x) = let (vs, x') = depat [] x in
787 simpl (Right (x, y)) = Right (x, simplify uctxt False [] y)
790addOperator :: Name -> Type -> Int -> ([Value] -> Maybe Value) ->
792addOperator n ty a op uctxt
793 = let ctxt = definitions uctxt
794 ctxt' = addDef n (Operator ty a op, Public, Unchecked) ctxt in
795 uctxt { definitions = ctxt' }
799lookupNames :: Name -> Context -> [Name]
801 = let ns = lookupCtxtName n (definitions ctxt) in
804lookupTy :: Name -> Context -> [Type]
806 = do def <- lookupCtxt n (definitions ctxt)
808 (Function ty _) -> return ty
809 (TyDecl _ ty) -> return ty
810 (Operator ty _ _) -> return ty
811 (CaseOp _ _ ty _ _ _ _ _ _) -> return ty
813isConName :: Name -> Context -> Bool
815 = or $ do def <- lookupCtxt n (definitions ctxt)
817 (TyDecl (DCon _ _) _) -> return True
818 (TyDecl (TCon _ _) _) -> return True
821isFnName :: Name -> Context -> Bool
823 = or $ do def <- lookupCtxt n (definitions ctxt)
825 (Function _ _) -> return True
826 (Operator _ _ _) -> return True
827 (CaseOp _ _ _ _ _ _ _ _ _) -> return True
830lookupP :: Name -> Context -> [Term]
832 = do def <- lookupCtxt n (definitions ctxt)
834 (Function ty tm, a, _) -> return (P Ref n ty, a)
835 (TyDecl nt ty, a, _) -> return (P nt n ty, a)
836 (CaseOp _ _ ty _ _ _ _ _ _, a, _) -> return (P Ref n ty, a)
837 (Operator ty _ _, a, _) -> return (P Ref n ty, a)
842lookupDef :: Name -> Context -> [Def]
843lookupDef n ctxt = map tfst $ lookupCtxt n (definitions ctxt)
845lookupDefAcc :: Name -> Bool -> Context ->
846 [(Def, Accessibility)]
847lookupDefAcc n mkpublic ctxt
848 = map mkp $ lookupCtxt n (definitions ctxt)
849 where mkp (d, a, _) = if mkpublic then (d, Public) else (d, a)
851lookupTotal :: Name -> Context -> [Totality]
852lookupTotal n ctxt = map mkt $ lookupCtxt n (definitions ctxt)
853 where mkt (d, a, t) = t
855lookupVal :: Name -> Context -> [Value]
857 = do def <- lookupCtxt n (definitions ctxt)
859 (Function _ htm) -> return (veval ctxt [] htm)
860 (TyDecl nt ty) -> return (VP nt n (veval ctxt [] ty))
862lookupTyEnv :: Name -> Env -> Maybe (Int, Type)
863lookupTyEnv n env = li n 0 env where
866 | n == x = Just (i, binderTy b)
867 | otherwise = li n (i+1) xs
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