@@ -16,12 +16,14 @@ module Idris.Core.Evaluate(normalise, normaliseTrace, normaliseC,
16 Def(..), CaseInfo(..), CaseDefs(..),
16 Def(..), CaseInfo(..), CaseDefs(..),
17 Accessibility(..), Injectivity, Totality(..), PReason(..), MetaInformation(..),
17 Accessibility(..), Injectivity, Totality(..), PReason(..), MetaInformation(..),
18 Context, initContext, ctxtAlist, next_tvar,
18 Context, initContext, ctxtAlist, next_tvar,
19+ addToCtxt, setAccess, setInjective, setTotal, setRigCount,
19− addToCtxt, setAccess, setInjective, setTotal, setMetaInformation, addCtxtDef, addTyDecl,
20+ setMetaInformation, addCtxtDef, addTyDecl,
20 addDatatype, addCasedef, simplifyCasedef, addOperator,
21 addDatatype, addCasedef, simplifyCasedef, addOperator,
21 lookupNames, lookupTyName, lookupTyNameExact, lookupTy, lookupTyExact,
22 lookupNames, lookupTyName, lookupTyNameExact, lookupTy, lookupTyExact,
22 lookupP, lookupP_all, lookupDef, lookupNameDef, lookupDefExact, lookupDefAcc, lookupDefAccExact, lookupVal,
23 lookupP, lookupP_all, lookupDef, lookupNameDef, lookupDefExact, lookupDefAcc, lookupDefAccExact, lookupVal,
23 mapDefCtxt, tcReducible,
24 mapDefCtxt, tcReducible,
24 lookupTotal, lookupTotalExact, lookupInjectiveExact,
25 lookupTotal, lookupTotalExact, lookupInjectiveExact,
26+ lookupRigCount, lookupRigCountExact,
25 lookupNameTotal, lookupMetaInformation, lookupTyEnv, isTCDict,
27 lookupNameTotal, lookupMetaInformation, lookupTyEnv, isTCDict,
26 isCanonical, isDConName, canBeDConName, isTConName, isConName, isFnName,
28 isCanonical, isDConName, canBeDConName, isTConName, isConName, isFnName,
27 Value(..), Quote(..), initEval, uniqueNameCtxt, uniqueBindersCtxt, definitions,
29 Value(..), Quote(..), initEval, uniqueNameCtxt, uniqueBindersCtxt, definitions,
@@ -866,9 +868,11 @@ data MetaInformation =
867-- | Contexts used for global definitions and for proof state. They contain
869-- | Contexts used for global definitions and for proof state. They contain
868-- universe constraints and existing definitions.
870-- universe constraints and existing definitions.
871+-- Also store maximum RigCount of the name (can't bind a name at multiplicity
872+-- 1 in a RigW, for example)
869data Context = MkContext {
873data Context = MkContext {
870 next_tvar :: Int,
874 next_tvar :: Int,
871− definitions :: Ctxt (Def, Injectivity, Accessibility, Totality, MetaInformation)
875+ definitions :: Ctxt (Def, RigCount, Injectivity, Accessibility, Totality, MetaInformation)
872 } deriving (Show, Generic)
876 } deriving (Show, Generic)
@@ -878,53 +882,59 @@ initContext = MkContext 0 emptyContext
879mapDefCtxt :: (Def -> Def) -> Context -> Context
883mapDefCtxt :: (Def -> Def) -> Context -> Context
880mapDefCtxt f (MkContext t !defs) = MkContext t (mapCtxt f' defs)
884mapDefCtxt f (MkContext t !defs) = MkContext t (mapCtxt f' defs)
881− where f' (!d, i, a, t, m) = f' (f d, i, a, t, m)
885+ where f' (!d, r, i, a, t, m) = f' (f d, r, i, a, t, m)
883-- | Get the definitions from a context
887-- | Get the definitions from a context
884ctxtAlist :: Context -> [(Name, Def)]
888ctxtAlist :: Context -> [(Name, Def)]
885−ctxtAlist ctxt = map (\(n, (d, i, a, t, m)) -> (n, d)) $ toAlist (definitions ctxt)
889+ctxtAlist ctxt = map (\(n, (d, r, i, a, t, m)) -> (n, d)) $ toAlist (definitions ctxt)
887veval ctxt env t = evalState (eval False ctxt [] env t []) initEval
891veval ctxt env t = evalState (eval False ctxt [] env t []) initEval
889addToCtxt :: Name -> Term -> Type -> Context -> Context
893addToCtxt :: Name -> Term -> Type -> Context -> Context
890addToCtxt n tm ty uctxt
894addToCtxt n tm ty uctxt
891 = let ctxt = definitions uctxt
895 = let ctxt = definitions uctxt
892− !ctxt' = addDef n (Function ty tm, False, Public, Unchecked, EmptyMI) ctxt in
896+ !ctxt' = addDef n (Function ty tm, RigW, False, Public, Unchecked, EmptyMI) ctxt in
893 uctxt { definitions = ctxt' }
897 uctxt { definitions = ctxt' }
895setAccess :: Name -> Accessibility -> Context -> Context
899setAccess :: Name -> Accessibility -> Context -> Context
896setAccess n a uctxt
900setAccess n a uctxt
897 = let ctxt = definitions uctxt
901 = let ctxt = definitions uctxt
898− !ctxt' = updateDef n (\ (d, i, _, t, m) -> (d, i, a, t, m)) ctxt in
902+ !ctxt' = updateDef n (\ (d, r, i, _, t, m) -> (d, r, i, a, t, m)) ctxt in
899 uctxt { definitions = ctxt' }
903 uctxt { definitions = ctxt' }
901setInjective :: Name -> Injectivity -> Context -> Context
905setInjective :: Name -> Injectivity -> Context -> Context
902setInjective n i uctxt
906setInjective n i uctxt
903 = let ctxt = definitions uctxt
907 = let ctxt = definitions uctxt
904− !ctxt' = updateDef n (\ (d, _, a, t, m) -> (d, i, a, t, m)) ctxt in
908+ !ctxt' = updateDef n (\ (d, r, _, a, t, m) -> (d, r, i, a, t, m)) ctxt in
905 uctxt { definitions = ctxt' }
909 uctxt { definitions = ctxt' }
907setTotal :: Name -> Totality -> Context -> Context
911setTotal :: Name -> Totality -> Context -> Context
908setTotal n t uctxt
912setTotal n t uctxt
909 = let ctxt = definitions uctxt
913 = let ctxt = definitions uctxt
910− !ctxt' = updateDef n (\ (d, i, a, _, m) -> (d, i, a, t, m)) ctxt in
914+ !ctxt' = updateDef n (\ (d, r, i, a, _, m) -> (d, r, i, a, t, m)) ctxt in
915+ uctxt { definitions = ctxt' }
917+setRigCount :: Name -> RigCount -> Context -> Context
918+setRigCount n rc uctxt
919+ = let ctxt = definitions uctxt
920+ !ctxt' = updateDef n (\ (d, _, i, a, t, m) -> (d, rc, i, a, t, m)) ctxt in
911 uctxt { definitions = ctxt' }
921 uctxt { definitions = ctxt' }
913setMetaInformation :: Name -> MetaInformation -> Context -> Context
923setMetaInformation :: Name -> MetaInformation -> Context -> Context
914setMetaInformation n m uctxt
924setMetaInformation n m uctxt
915 = let ctxt = definitions uctxt
925 = let ctxt = definitions uctxt
916− !ctxt' = updateDef n (\ (d, i, a, t, _) -> (d, i, a, t, m)) ctxt in
926+ !ctxt' = updateDef n (\ (d, r, i, a, t, _) -> (d, r, i, a, t, m)) ctxt in
917 uctxt { definitions = ctxt' }
927 uctxt { definitions = ctxt' }
919addCtxtDef :: Name -> Def -> Context -> Context
929addCtxtDef :: Name -> Def -> Context -> Context
920addCtxtDef n d c = let ctxt = definitions c
930addCtxtDef n d c = let ctxt = definitions c
921− !ctxt' = addDef n (d, False, Public, Unchecked, EmptyMI) $! ctxt in
931+ !ctxt' = addDef n (d, RigW, False, Public, Unchecked, EmptyMI) $! ctxt in
922 c { definitions = ctxt' }
932 c { definitions = ctxt' }
924addTyDecl :: Name -> NameType -> Type -> Context -> Context
934addTyDecl :: Name -> NameType -> Type -> Context -> Context
925addTyDecl n nt ty uctxt
935addTyDecl n nt ty uctxt
926 = let ctxt = definitions uctxt
936 = let ctxt = definitions uctxt
927− !ctxt' = addDef n (TyDecl nt ty, False, Public, Unchecked, EmptyMI) ctxt in
937+ !ctxt' = addDef n (TyDecl nt ty, RigW, False, Public, Unchecked, EmptyMI) ctxt in
928 uctxt { definitions = ctxt' }
938 uctxt { definitions = ctxt' }
930addDatatype :: Datatype Name -> Context -> Context
940addDatatype :: Datatype Name -> Context -> Context
@@ -932,14 +942,14 @@ addDatatype (Data n tag ty unique cons) uctxt
932 = let ctxt = definitions uctxt
942 = let ctxt = definitions uctxt
933 ty' = normalise uctxt [] ty
943 ty' = normalise uctxt [] ty
934 !ctxt' = addCons 0 cons (addDef n
944 !ctxt' = addCons 0 cons (addDef n
935− (TyDecl (TCon tag (arity ty')) ty, True, Public, Unchecked, EmptyMI) ctxt) in
945+ (TyDecl (TCon tag (arity ty')) ty, RigW, True, Public, Unchecked, EmptyMI) ctxt) in
936 uctxt { definitions = ctxt' }
946 uctxt { definitions = ctxt' }
938 addCons tag [] ctxt = ctxt
948 addCons tag [] ctxt = ctxt
939 addCons tag ((n, ty) : cons) ctxt
949 addCons tag ((n, ty) : cons) ctxt
940 = let ty' = normalise uctxt [] ty in
950 = let ty' = normalise uctxt [] ty in
941 addCons (tag+1) cons (addDef n
951 addCons (tag+1) cons (addDef n
942− (TyDecl (DCon tag (arity ty') unique) ty, True, Public, Unchecked, EmptyMI) ctxt)
952+ (TyDecl (DCon tag (arity ty') unique) ty, RigW, True, Public, Unchecked, EmptyMI) ctxt)
944-- FIXME: Too many arguments! Refactor all these Bools.
954-- FIXME: Too many arguments! Refactor all these Bools.
@@ -973,7 +983,7 @@ addCasedef n ei ci@(CaseInfo inline alwaysInline tcdict)
973 (args_rt, sc_rt)
983 (args_rt, sc_rt)
974 op = (CaseOp (ci { case_inlinable = inlc })
984 op = (CaseOp (ci { case_inlinable = inlc })
975 ty argtys ps_in ps_ct cdef,
985 ty argtys ps_in ps_ct cdef,
976− False, access, Unchecked, EmptyMI)
986+ RigW, False, access, Unchecked, EmptyMI)
977 in return $ addDef n op ctxt
987 in return $ addDef n op ctxt
978-- other -> tfail (Msg $ "Error adding case def: " ++ show other)
988-- other -> tfail (Msg $ "Error adding case def: " ++ show other)
979 return uctxt { definitions = ctxt' }
989 return uctxt { definitions = ctxt' }
@@ -986,15 +996,15 @@ simplifyCasedef :: Name -> ErasureInfo -> Context -> TC Context
986simplifyCasedef n ei uctxt
996simplifyCasedef n ei uctxt
987 = do let ctxt = definitions uctxt
997 = do let ctxt = definitions uctxt
988 ctxt' <- case lookupCtxt n ctxt of
998 ctxt' <- case lookupCtxt n ctxt of
989− [(CaseOp ci ty atys [] ps _, inj, acc, tot, metainf)] ->
999+ [(CaseOp ci ty atys [] ps _, rc, inj, acc, tot, metainf)] ->
990 return ctxt -- nothing to simplify (or already done...)
1000 return ctxt -- nothing to simplify (or already done...)
991− [(CaseOp ci ty atys ps_in ps cd, inj, acc, tot, metainf)] ->
1001+ [(CaseOp ci ty atys ps_in ps cd, rc, inj, acc, tot, metainf)] ->
992 do let ps_in' = map simpl ps_in
1002 do let ps_in' = map simpl ps_in
993 pdef = map debind ps_in'
1003 pdef = map debind ps_in'
994 CaseDef args sc _ <- simpleCase False (STerm Erased) False CompileTime emptyFC [] atys pdef ei
1004 CaseDef args sc _ <- simpleCase False (STerm Erased) False CompileTime emptyFC [] atys pdef ei
995 return $ addDef n (CaseOp ci
1005 return $ addDef n (CaseOp ci
996 ty atys ps_in' ps (cd { cases_compiletime = (args, sc) }),
1006 ty atys ps_in' ps (cd { cases_compiletime = (args, sc) }),
997− inj, acc, tot, metainf) ctxt
1007+ rc, inj, acc, tot, metainf) ctxt
999 _ -> return ctxt
1009 _ -> return ctxt
1000 return uctxt { definitions = ctxt' }
1010 return uctxt { definitions = ctxt' }
@@ -1014,10 +1024,10 @@ addOperator :: Name -> Type -> Int -> ([Value] -> Maybe Value) ->
1014 Context -> Context
1024 Context -> Context
1015addOperator n ty a op uctxt
1025addOperator n ty a op uctxt
1016 = let ctxt = definitions uctxt
1026 = let ctxt = definitions uctxt
1017− ctxt' = addDef n (Operator ty a op, False, Public, Unchecked, EmptyMI) ctxt in
1027+ ctxt' = addDef n (Operator ty a op, RigW, False, Public, Unchecked, EmptyMI) ctxt in
1018 uctxt { definitions = ctxt' }
1028 uctxt { definitions = ctxt' }
1020−tfst (a, _, _, _, _) = a
1030+tfst (a, _, _, _, _, _) = a
1022lookupNames :: Name -> Context -> [Name]
1032lookupNames :: Name -> Context -> [Name]
1023lookupNames n ctxt
1033lookupNames n ctxt
@@ -1103,10 +1113,10 @@ lookupP_all :: Bool -> Bool -> Name -> Context -> [Term]
1103lookupP_all all exact n ctxt
1113lookupP_all all exact n ctxt
1104 = do (n', def) <- names
1114 = do (n', def) <- names
1105 p <- case def of
1115 p <- case def of
1106− (Function ty tm, inj, a, _, _) -> return (P Ref n' ty, a)
1116+ (Function ty tm, _, inj, a, _, _) -> return (P Ref n' ty, a)
1107− (TyDecl nt ty, _, a, _, _) -> return (P nt n' ty, a)
1117+ (TyDecl nt ty, _, _, a, _, _) -> return (P nt n' ty, a)
1108− (CaseOp _ ty _ _ _ _, inj, a, _, _) -> return (P Ref n' ty, a)
1118+ (CaseOp _ ty _ _ _ _, _, inj, a, _, _) -> return (P Ref n' ty, a)
1109− (Operator ty _ _, inj, a, _, _) -> return (P Ref n' ty, a)
1119+ (Operator ty _ _, _, inj, a, _, _) -> return (P Ref n' ty, a)
1110 case snd p of
1120 case snd p of
1111 Hidden -> if all then return (fst p) else []
1121 Hidden -> if all then return (fst p) else []
1112 Private -> if all then return (fst p) else []
1122 Private -> if all then return (fst p) else []
@@ -1133,28 +1143,36 @@ lookupDefAcc :: Name -> Bool -> Context ->
1133lookupDefAcc n mkpublic ctxt
1143lookupDefAcc n mkpublic ctxt
1134 = map mkp $ lookupCtxt n (definitions ctxt)
1144 = map mkp $ lookupCtxt n (definitions ctxt)
1135 -- io_bind a special case for REPL prettiness
1145 -- io_bind a special case for REPL prettiness
1136− where mkp (d, inj, a, _, _) = if mkpublic && (not (n == sUN "io_bind" || n == sUN "io_pure"))
1146+ where mkp (d, _, inj, a, _, _) = if mkpublic && (not (n == sUN "io_bind" || n == sUN "io_pure"))
1137− then (d, Public) else (d, a)
1147+ then (d, Public) else (d, a)
1139lookupDefAccExact :: Name -> Bool -> Context ->
1149lookupDefAccExact :: Name -> Bool -> Context ->
1140 Maybe (Def, Accessibility)
1150 Maybe (Def, Accessibility)
1141lookupDefAccExact n mkpublic ctxt
1151lookupDefAccExact n mkpublic ctxt
1142 = fmap mkp $ lookupCtxtExact n (definitions ctxt)
1152 = fmap mkp $ lookupCtxtExact n (definitions ctxt)
1143 -- io_bind a special case for REPL prettiness
1153 -- io_bind a special case for REPL prettiness
1144− where mkp (d, inj, a, _, _) = if mkpublic && (not (n == sUN "io_bind" || n == sUN "io_pure"))
1154+ where mkp (d, _, inj, a, _, _) = if mkpublic && (not (n == sUN "io_bind" || n == sUN "io_pure"))
1145− then (d, Public) else (d, a)
1155+ then (d, Public) else (d, a)
1147lookupTotal :: Name -> Context -> [Totality]
1157lookupTotal :: Name -> Context -> [Totality]
1148lookupTotal n ctxt = map mkt $ lookupCtxt n (definitions ctxt)
1158lookupTotal n ctxt = map mkt $ lookupCtxt n (definitions ctxt)
1149− where mkt (d, inj, a, t, m) = t
1159+ where mkt (d, _, inj, a, t, m) = t
1151lookupTotalExact :: Name -> Context -> Maybe Totality
1161lookupTotalExact :: Name -> Context -> Maybe Totality
1152lookupTotalExact n ctxt = fmap mkt $ lookupCtxtExact n (definitions ctxt)
1162lookupTotalExact n ctxt = fmap mkt $ lookupCtxtExact n (definitions ctxt)
1153− where mkt (d, inj, a, t, m) = t
1163+ where mkt (d, _, inj, a, t, m) = t
1165+lookupRigCount :: Name -> Context -> [Totality]
1166+lookupRigCount n ctxt = map mkt $ lookupCtxt n (definitions ctxt)
1167+ where mkt (d, _, inj, a, t, m) = t
1169+lookupRigCountExact :: Name -> Context -> Maybe RigCount
1170+lookupRigCountExact n ctxt = fmap mkt $ lookupCtxtExact n (definitions ctxt)
1171+ where mkt (d, rc, inj, a, t, m) = rc
1155lookupInjectiveExact :: Name -> Context -> Maybe Injectivity
1173lookupInjectiveExact :: Name -> Context -> Maybe Injectivity
1156lookupInjectiveExact n ctxt = fmap mkt $ lookupCtxtExact n (definitions ctxt)
1174lookupInjectiveExact n ctxt = fmap mkt $ lookupCtxtExact n (definitions ctxt)
1157− where mkt (d, inj, a, t, m) = inj
1175+ where mkt (d, _, inj, a, t, m) = inj
1159-- Check if a name is reducible in the type checker. Partial definitions
1177-- Check if a name is reducible in the type checker. Partial definitions
1160-- are not reducible (so treated as a constant)
1178-- are not reducible (so treated as a constant)
@@ -1166,10 +1184,10 @@ tcReducible n ctxt = case lookupTotalExact n ctxt of
1167lookupMetaInformation :: Name -> Context -> [MetaInformation]
1185lookupMetaInformation :: Name -> Context -> [MetaInformation]
1168lookupMetaInformation n ctxt = map mkm $ lookupCtxt n (definitions ctxt)
1186lookupMetaInformation n ctxt = map mkm $ lookupCtxt n (definitions ctxt)
1169− where mkm (d, inj, a, t, m) = m
1187+ where mkm (d, _, inj, a, t, m) = m
1171lookupNameTotal :: Name -> Context -> [(Name, Totality)]
1189lookupNameTotal :: Name -> Context -> [(Name, Totality)]
1172−lookupNameTotal n = map (\(n, (_, _, _, t, _)) -> (n, t)) . lookupCtxtName n . definitions
1190+lookupNameTotal n = map (\(n, (_, _, _, _, t, _)) -> (n, t)) . lookupCtxtName n . definitions
1175lookupVal :: Name -> Context -> [Value]
1193lookupVal :: Name -> Context -> [Value]