@@ -3,11 +3,12 @@
33
44module Core.Evaluate(normalise, normaliseC, normaliseAll,
55 simplify, specialise, hnf,
6− Def(..), Accessibility(..),
6+ Def(..), Accessibility(..), Totality(..), PReason(..),
77 Context, initContext, ctxtAlist, uconstraints, next_tvar,
8− addToCtxt, setAccess, addCtxtDef, addTyDecl, addDatatype,
8+ addToCtxt, setAccess, setTotal, addCtxtDef, addTyDecl, addDatatype,
99 addCasedef, addOperator,
10− lookupTy, lookupP, lookupDef, lookupVal, lookupTyEnv, isConName,
10+ lookupTy, lookupP, lookupDef, lookupVal, lookupTotal,
11+ lookupTyEnv, isConName,
1112 Value(..)) where
1213
1314import Debug.Trace
@@ -470,42 +471,63 @@ instance Binary (a -> b) where
470471
471472data Accessibility = Public | Frozen | Hidden
472473 deriving (Show, Eq)
474+
475+data Totality = Total | Partial PReason
476+ deriving (Show, Eq)
477+
478+data PReason = Other [Name] | Itself | NotCovering | NotPositive
479+ deriving (Show, Eq)
480+
473481{-!
474482deriving instance Binary Accessibility
475483!-}
476484
485+{-!
486+deriving instance Binary Totality
487+!-}
488+
489+{-!
490+deriving instance Binary PReason
491+!-}
492+
477493data Context = MkContext { uconstraints :: [UConstraint],
478494 next_tvar :: Int,
479− definitions :: Ctxt (Def, Accessibility) }
495+ definitions :: Ctxt (Def, Accessibility, Totality) }
480496
481497initContext = MkContext [] 0 emptyContext
482498
483499ctxtAlist :: Context -> [(Name, Def)]
484−ctxtAlist ctxt = map (\(n, (d, a)) -> (n, d)) $ toAlist (definitions ctxt)
500+ctxtAlist ctxt = map (\(n, (d, a, t)) -> (n, d)) $ toAlist (definitions ctxt)
485501
486502veval ctxt env t = evalState (eval ctxt emptyContext env t []) ()
487503
488504addToCtxt :: Name -> Term -> Type -> Context -> Context
489505addToCtxt n tm ty uctxt
490506 = let ctxt = definitions uctxt
491− ctxt' = addDef n (Function ty tm, Public) ctxt in
507+ ctxt' = addDef n (Function ty tm, Public, Total) ctxt in
492508 uctxt { definitions = ctxt' }
493509
494510setAccess :: Name -> Accessibility -> Context -> Context
495511setAccess n a uctxt
496512 = let ctxt = definitions uctxt
497− ctxt' = updateDef n (\ (d, _) -> (d, a)) ctxt in
513+ ctxt' = updateDef n (\ (d, _, t) -> (d, a, t)) ctxt in
514+ uctxt { definitions = ctxt' }
515+
516+setTotal :: Name -> Totality -> Context -> Context
517+setTotal n t uctxt
518+ = let ctxt = definitions uctxt
519+ ctxt' = updateDef n (\ (d, a, _) -> (d, a, t)) ctxt in
498520 uctxt { definitions = ctxt' }
499521
500522addCtxtDef :: Name -> Def -> Context -> Context
501523addCtxtDef n d c = let ctxt = definitions c
502− ctxt' = addDef n (d, Public) ctxt in
524+ ctxt' = addDef n (d, Public, Total) ctxt in
503525 c { definitions = ctxt' }
504526
505527addTyDecl :: Name -> Type -> Context -> Context
506528addTyDecl n ty uctxt
507529 = let ctxt = definitions uctxt
508− ctxt' = addDef n (TyDecl Ref ty, Public) ctxt in
530+ ctxt' = addDef n (TyDecl Ref ty, Public, Total) ctxt in
509531 uctxt { definitions = ctxt' }
510532
511533addDatatype :: Datatype Name -> Context -> Context
@@ -513,14 +535,14 @@ addDatatype (Data n tag ty cons) uctxt
513535 = let ctxt = definitions uctxt
514536 ty' = normalise uctxt [] ty
515537 ctxt' = addCons 0 cons (addDef n
516− (TyDecl (TCon tag (arity ty')) ty, Public) ctxt) in
538+ (TyDecl (TCon tag (arity ty')) ty, Public, Total) ctxt) in
517539 uctxt { definitions = ctxt' }
518540 where
519541 addCons tag [] ctxt = ctxt
520542 addCons tag ((n, ty) : cons) ctxt
521543 = let ty' = normalise uctxt [] ty in
522544 addCons (tag+1) cons (addDef n
523− (TyDecl (DCon tag (arity ty')) ty, Public) ctxt)
545+ (TyDecl (DCon tag (arity ty')) ty, Public, Total) ctxt)
524546
525547addCasedef :: Name -> Bool -> Bool -> Bool -> [(Term, Term)] -> [(Term, Term)] ->
526548 Type -> Context -> Context
@@ -532,7 +554,7 @@ addCasedef n alwaysInline tcase covering ps psrt ty uctxt
532554 (CaseDef args sc _, CaseDef args' sc' _) ->
533555 let inl = alwaysInline in
534556 addDef n (CaseOp inl ty ps args sc args' sc',
535− Public) ctxt in
557+ Public, Total) ctxt in
536558 uctxt { definitions = ctxt' }
537559 where simpl [] = []
538560 simpl ((l,r) : xs) = (l, simplify uctxt [] r) : simpl xs
@@ -540,13 +562,15 @@ addCasedef n alwaysInline tcase covering ps psrt ty uctxt
540562addOperator :: Name -> Type -> Int -> ([Value] -> Maybe Value) -> Context -> Context
541563addOperator n ty a op uctxt
542564 = let ctxt = definitions uctxt
543− ctxt' = addDef n (Operator ty a op, Public) ctxt in
565+ ctxt' = addDef n (Operator ty a op, Public, Total) ctxt in
544566 uctxt { definitions = ctxt' }
545567
568+tfst (a, _, _) = a
569+
546570lookupTy :: Maybe [String] -> Name -> Context -> [Type]
547571lookupTy root n ctxt
548572 = do def <- lookupCtxt root n (definitions ctxt)
549− case fst def of
573+ case tfst def of
550574 (Function ty _) -> return ty
551575 (TyDecl _ ty) -> return ty
552576 (Operator ty _ _) -> return ty
@@ -555,7 +579,7 @@ lookupTy root n ctxt
555579isConName :: Maybe [String] -> Name -> Context -> Bool
556580isConName root n ctxt
557581 = or $ do def <- lookupCtxt root n (definitions ctxt)
558− case fst def of
582+ case tfst def of
559583 (TyDecl (DCon _ _) _) -> return True
560584 (TyDecl (TCon _ _) _) -> return True
561585 _ -> return False
@@ -564,26 +588,30 @@ lookupP :: Maybe [String] -> Name -> Context -> [Term]
564588lookupP root n ctxt
565589 = do def <- lookupCtxt root n (definitions ctxt)
566590 p <- case def of
567− (Function ty tm, a) -> return (P Ref n ty, a)
568− (TyDecl nt ty, a) -> return (P nt n ty, a)
569− (CaseOp _ ty _ _ _ _ _, a) -> return (P Ref n ty, a)
570− (Operator ty _ _, a) -> return (P Ref n ty, a)
591+ (Function ty tm, a, _) -> return (P Ref n ty, a)
592+ (TyDecl nt ty, a, _) -> return (P nt n ty, a)
593+ (CaseOp _ ty _ _ _ _ _, a, _) -> return (P Ref n ty, a)
594+ (Operator ty _ _, a, _) -> return (P Ref n ty, a)
571595 case snd p of
572596 Hidden -> []
573597 _ -> return (fst p)
574598
575599lookupDef :: Maybe [String] -> Name -> Context -> [Def]
576−lookupDef root n ctxt = map fst $ lookupCtxt root n (definitions ctxt)
600+lookupDef root n ctxt = map tfst $ lookupCtxt root n (definitions ctxt)
577601
578602lookupDefAcc :: Maybe [String] -> Name -> Bool -> Context -> [(Def, Accessibility)]
579603lookupDefAcc root n mkpublic ctxt
580604 = map mkp $ lookupCtxt root n (definitions ctxt)
581− where mkp (d, a) = if mkpublic then (d, Public) else (d, a)
605+ where mkp (d, a, _) = if mkpublic then (d, Public) else (d, a)
606+
607+lookupTotal :: Name -> Context -> [Totality]
608+lookupTotal n ctxt = map mkt $ lookupCtxt Nothing n (definitions ctxt)
609+ where mkt (d, a, t) = t
582610
583611lookupVal :: Maybe [String] -> Name -> Context -> [Value]
584612lookupVal root n ctxt
585613 = do def <- lookupCtxt root n (definitions ctxt)
586− case fst def of
614+ case tfst def of
587615 (Function _ htm) -> return (veval ctxt [] htm)
588616 (TyDecl nt ty) -> return (VP nt n (veval ctxt [] ty))
589617