@@ -3,11 +3,12 @@
4module Core.Evaluate(normalise, normaliseC, normaliseAll,
4module Core.Evaluate(normalise, normaliseC, normaliseAll,
5 simplify, specialise, hnf,
5 simplify, specialise, hnf,
6− Def(..), Accessibility(..),
6+ Def(..), Accessibility(..), Totality(..), PReason(..),
7 Context, initContext, ctxtAlist, uconstraints, next_tvar,
7 Context, initContext, ctxtAlist, uconstraints, next_tvar,
8− addToCtxt, setAccess, addCtxtDef, addTyDecl, addDatatype,
8+ addToCtxt, setAccess, setTotal, addCtxtDef, addTyDecl, addDatatype,
9 addCasedef, addOperator,
9 addCasedef, addOperator,
10− lookupTy, lookupP, lookupDef, lookupVal, lookupTyEnv, isConName,
10+ lookupTy, lookupP, lookupDef, lookupVal, lookupTotal,
11+ lookupTyEnv, isConName,
11 Value(..)) where
12 Value(..)) where
13import Debug.Trace
14import Debug.Trace
@@ -470,42 +471,63 @@ instance Binary (a -> b) where
471data Accessibility = Public | Frozen | Hidden
472data Accessibility = Public | Frozen | Hidden
472 deriving (Show, Eq)
473 deriving (Show, Eq)
475+data Totality = Total | Partial PReason
478+data PReason = Other [Name] | Itself | NotCovering | NotPositive
474deriving instance Binary Accessibility
482deriving instance Binary Accessibility
486+deriving instance Binary Totality
490+deriving instance Binary PReason
477data Context = MkContext { uconstraints :: [UConstraint],
493data Context = MkContext { uconstraints :: [UConstraint],
478 next_tvar :: Int,
494 next_tvar :: Int,
479− definitions :: Ctxt (Def, Accessibility) }
495+ definitions :: Ctxt (Def, Accessibility, Totality) }
481initContext = MkContext [] 0 emptyContext
497initContext = MkContext [] 0 emptyContext
483ctxtAlist :: Context -> [(Name, Def)]
499ctxtAlist :: 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)
486veval ctxt env t = evalState (eval ctxt emptyContext env t []) ()
502veval ctxt env t = evalState (eval ctxt emptyContext env t []) ()
488addToCtxt :: Name -> Term -> Type -> Context -> Context
504addToCtxt :: Name -> Term -> Type -> Context -> Context
489addToCtxt n tm ty uctxt
505addToCtxt n tm ty uctxt
490 = let ctxt = definitions uctxt
506 = 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
492 uctxt { definitions = ctxt' }
508 uctxt { definitions = ctxt' }
494setAccess :: Name -> Accessibility -> Context -> Context
510setAccess :: Name -> Accessibility -> Context -> Context
495setAccess n a uctxt
511setAccess n a uctxt
496 = let ctxt = definitions uctxt
512 = 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' }
516+setTotal :: Name -> Totality -> Context -> Context
518+ = let ctxt = definitions uctxt
519+ ctxt' = updateDef n (\ (d, a, _) -> (d, a, t)) ctxt in
498 uctxt { definitions = ctxt' }
520 uctxt { definitions = ctxt' }
500addCtxtDef :: Name -> Def -> Context -> Context
522addCtxtDef :: Name -> Def -> Context -> Context
501addCtxtDef n d c = let ctxt = definitions c
523addCtxtDef n d c = let ctxt = definitions c
502− ctxt' = addDef n (d, Public) ctxt in
524+ ctxt' = addDef n (d, Public, Total) ctxt in
503 c { definitions = ctxt' }
525 c { definitions = ctxt' }
505addTyDecl :: Name -> Type -> Context -> Context
527addTyDecl :: Name -> Type -> Context -> Context
506addTyDecl n ty uctxt
528addTyDecl n ty uctxt
507 = let ctxt = definitions uctxt
529 = 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
509 uctxt { definitions = ctxt' }
531 uctxt { definitions = ctxt' }
511addDatatype :: Datatype Name -> Context -> Context
533addDatatype :: Datatype Name -> Context -> Context
@@ -513,14 +535,14 @@ addDatatype (Data n tag ty cons) uctxt
513 = let ctxt = definitions uctxt
535 = let ctxt = definitions uctxt
514 ty' = normalise uctxt [] ty
536 ty' = normalise uctxt [] ty
515 ctxt' = addCons 0 cons (addDef n
537 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
517 uctxt { definitions = ctxt' }
539 uctxt { definitions = ctxt' }
519 addCons tag [] ctxt = ctxt
541 addCons tag [] ctxt = ctxt
520 addCons tag ((n, ty) : cons) ctxt
542 addCons tag ((n, ty) : cons) ctxt
521 = let ty' = normalise uctxt [] ty in
543 = let ty' = normalise uctxt [] ty in
522 addCons (tag+1) cons (addDef n
544 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)
525addCasedef :: Name -> Bool -> Bool -> Bool -> [(Term, Term)] -> [(Term, Term)] ->
547addCasedef :: Name -> Bool -> Bool -> Bool -> [(Term, Term)] -> [(Term, Term)] ->
526 Type -> Context -> Context
548 Type -> Context -> Context
@@ -532,7 +554,7 @@ addCasedef n alwaysInline tcase covering ps psrt ty uctxt
532 (CaseDef args sc _, CaseDef args' sc' _) ->
554 (CaseDef args sc _, CaseDef args' sc' _) ->
533 let inl = alwaysInline in
555 let inl = alwaysInline in
534 addDef n (CaseOp inl ty ps args sc args' sc',
556 addDef n (CaseOp inl ty ps args sc args' sc',
535− Public) ctxt in
557+ Public, Total) ctxt in
536 uctxt { definitions = ctxt' }
558 uctxt { definitions = ctxt' }
537 where simpl [] = []
559 where simpl [] = []
538 simpl ((l,r) : xs) = (l, simplify uctxt [] r) : simpl xs
560 simpl ((l,r) : xs) = (l, simplify uctxt [] r) : simpl xs
@@ -540,13 +562,15 @@ addCasedef n alwaysInline tcase covering ps psrt ty uctxt
540addOperator :: Name -> Type -> Int -> ([Value] -> Maybe Value) -> Context -> Context
562addOperator :: Name -> Type -> Int -> ([Value] -> Maybe Value) -> Context -> Context
541addOperator n ty a op uctxt
563addOperator n ty a op uctxt
542 = let ctxt = definitions uctxt
564 = 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
544 uctxt { definitions = ctxt' }
566 uctxt { definitions = ctxt' }
546lookupTy :: Maybe [String] -> Name -> Context -> [Type]
570lookupTy :: Maybe [String] -> Name -> Context -> [Type]
547lookupTy root n ctxt
571lookupTy root n ctxt
548 = do def <- lookupCtxt root n (definitions ctxt)
572 = do def <- lookupCtxt root n (definitions ctxt)
549− case fst def of
573+ case tfst def of
550 (Function ty _) -> return ty
574 (Function ty _) -> return ty
551 (TyDecl _ ty) -> return ty
575 (TyDecl _ ty) -> return ty
552 (Operator ty _ _) -> return ty
576 (Operator ty _ _) -> return ty
@@ -555,7 +579,7 @@ lookupTy root n ctxt
555isConName :: Maybe [String] -> Name -> Context -> Bool
579isConName :: Maybe [String] -> Name -> Context -> Bool
556isConName root n ctxt
580isConName root n ctxt
557 = or $ do def <- lookupCtxt root n (definitions ctxt)
581 = or $ do def <- lookupCtxt root n (definitions ctxt)
558− case fst def of
582+ case tfst def of
559 (TyDecl (DCon _ _) _) -> return True
583 (TyDecl (DCon _ _) _) -> return True
560 (TyDecl (TCon _ _) _) -> return True
584 (TyDecl (TCon _ _) _) -> return True
561 _ -> return False
585 _ -> return False
@@ -564,26 +588,30 @@ lookupP :: Maybe [String] -> Name -> Context -> [Term]
564lookupP root n ctxt
588lookupP root n ctxt
565 = do def <- lookupCtxt root n (definitions ctxt)
589 = do def <- lookupCtxt root n (definitions ctxt)
566 p <- case def of
590 p <- case def of
567− (Function ty tm, a) -> return (P Ref n ty, a)
591+ (Function ty tm, a, _) -> return (P Ref n ty, a)
568− (TyDecl nt ty, a) -> return (P nt n ty, a)
592+ (TyDecl nt ty, a, _) -> return (P nt n ty, a)
569− (CaseOp _ ty _ _ _ _ _, a) -> return (P Ref n ty, a)
593+ (CaseOp _ ty _ _ _ _ _, a, _) -> return (P Ref n ty, a)
570− (Operator ty _ _, a) -> return (P Ref n ty, a)
594+ (Operator ty _ _, a, _) -> return (P Ref n ty, a)
571 case snd p of
595 case snd p of
572 Hidden -> []
596 Hidden -> []
573 _ -> return (fst p)
597 _ -> return (fst p)
575lookupDef :: Maybe [String] -> Name -> Context -> [Def]
599lookupDef :: 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)
578lookupDefAcc :: Maybe [String] -> Name -> Bool -> Context -> [(Def, Accessibility)]
602lookupDefAcc :: Maybe [String] -> Name -> Bool -> Context -> [(Def, Accessibility)]
579lookupDefAcc root n mkpublic ctxt
603lookupDefAcc root n mkpublic ctxt
580 = map mkp $ lookupCtxt root n (definitions ctxt)
604 = 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)
607+lookupTotal :: Name -> Context -> [Totality]
608+lookupTotal n ctxt = map mkt $ lookupCtxt Nothing n (definitions ctxt)
609+ where mkt (d, a, t) = t
583lookupVal :: Maybe [String] -> Name -> Context -> [Value]
611lookupVal :: Maybe [String] -> Name -> Context -> [Value]
584lookupVal root n ctxt
612lookupVal root n ctxt
585 = do def <- lookupCtxt root n (definitions ctxt)
613 = do def <- lookupCtxt root n (definitions ctxt)
586− case fst def of
614+ case tfst def of
587 (Function _ htm) -> return (veval ctxt [] htm)
615 (Function _ htm) -> return (veval ctxt [] htm)
588 (TyDecl nt ty) -> return (VP nt n (veval ctxt [] ty))
616 (TyDecl nt ty) -> return (VP nt n (veval ctxt [] ty))