@@ -6,14 +6,15 @@ module Idris.Core.Evaluate(normalise, normaliseTrace, normaliseC,
6 normaliseAll, normaliseBlocking, toValue, quoteTerm,
6 normaliseAll, normaliseBlocking, toValue, quoteTerm,
7 rt_simplify, simplify, specialise, hnf, convEq, convEq',
7 rt_simplify, simplify, specialise, hnf, convEq, convEq',
8 Def(..), CaseInfo(..), CaseDefs(..),
8 Def(..), CaseInfo(..), CaseDefs(..),
9− Accessibility(..), Totality(..), PReason(..), MetaInformation(..),
9+ Accessibility(..), Injectivity, Totality(..), PReason(..), MetaInformation(..),
10 Context, initContext, ctxtAlist, next_tvar,
10 Context, initContext, ctxtAlist, next_tvar,
11− addToCtxt, setAccess, setTotal, setMetaInformation, addCtxtDef, addTyDecl,
11+ addToCtxt, setAccess, setInjective, setTotal, setMetaInformation, addCtxtDef, addTyDecl,
12 addDatatype, addCasedef, simplifyCasedef, addOperator,
12 addDatatype, addCasedef, simplifyCasedef, addOperator,
13 lookupNames, lookupTyName, lookupTyNameExact, lookupTy, lookupTyExact,
13 lookupNames, lookupTyName, lookupTyNameExact, lookupTy, lookupTyExact,
14 lookupP, lookupP_all, lookupDef, lookupNameDef, lookupDefExact, lookupDefAcc, lookupDefAccExact, lookupVal,
14 lookupP, lookupP_all, lookupDef, lookupNameDef, lookupDefExact, lookupDefAcc, lookupDefAccExact, lookupVal,
15 mapDefCtxt,
15 mapDefCtxt,
16+ lookupTotal, lookupTotalExact, lookupInjectiveExact,
16− lookupTotal, lookupTotalExact, lookupNameTotal, lookupMetaInformation, lookupTyEnv, isTCDict, isDConName, canBeDConName, isTConName, isConName, isFnName,
17+ lookupNameTotal, lookupMetaInformation, lookupTyEnv, isTCDict, isDConName, canBeDConName, isTConName, isConName, isFnName,
17 Value(..), Quote(..), initEval, uniqueNameCtxt, uniqueBindersCtxt, definitions,
18 Value(..), Quote(..), initEval, uniqueNameCtxt, uniqueBindersCtxt, definitions,
18 isUniverse) where
19 isUniverse) where
@@ -759,6 +760,8 @@ instance Show Accessibility where
759 show Private = "private"
760 show Private = "private"
760 show Hidden = "hidden"
761 show Hidden = "hidden"
763+type Injectivity = Bool
762-- | The result of totality checking
765-- | The result of totality checking
763data Totality = Total [Int] -- ^ well-founded arguments
766data Totality = Total [Int] -- ^ well-founded arguments
764 | Productive -- ^ productive
767 | Productive -- ^ productive
@@ -816,7 +819,7 @@ data MetaInformation =
816-- universe constraints and existing definitions.
819-- universe constraints and existing definitions.
817data Context = MkContext {
820data Context = MkContext {
818 next_tvar :: Int,
821 next_tvar :: Int,
819− definitions :: Ctxt (Def, Accessibility, Totality, MetaInformation)
822+ definitions :: Ctxt (Def, Injectivity, Accessibility, Totality, MetaInformation)
820 } deriving Show
823 } deriving Show
@@ -826,47 +829,53 @@ initContext = MkContext 0 emptyContext
827mapDefCtxt :: (Def -> Def) -> Context -> Context
830mapDefCtxt :: (Def -> Def) -> Context -> Context
828mapDefCtxt f (MkContext t !defs) = MkContext t (mapCtxt f' defs)
831mapDefCtxt f (MkContext t !defs) = MkContext t (mapCtxt f' defs)
829− where f' (!d, a, t, m) = f' (f d, a, t, m)
832+ where f' (!d, i, a, t, m) = f' (f d, i, a, t, m)
831-- | Get the definitions from a context
834-- | Get the definitions from a context
832ctxtAlist :: Context -> [(Name, Def)]
835ctxtAlist :: Context -> [(Name, Def)]
833−ctxtAlist ctxt = map (\(n, (d, a, t, m)) -> (n, d)) $ toAlist (definitions ctxt)
836+ctxtAlist ctxt = map (\(n, (d, i, a, t, m)) -> (n, d)) $ toAlist (definitions ctxt)
835veval ctxt env t = evalState (eval False ctxt [] env t []) initEval
838veval ctxt env t = evalState (eval False ctxt [] env t []) initEval
837addToCtxt :: Name -> Term -> Type -> Context -> Context
840addToCtxt :: Name -> Term -> Type -> Context -> Context
838addToCtxt n tm ty uctxt
841addToCtxt n tm ty uctxt
839 = let ctxt = definitions uctxt
842 = let ctxt = definitions uctxt
840− !ctxt' = addDef n (Function ty tm, Public, Unchecked, EmptyMI) ctxt in
843+ !ctxt' = addDef n (Function ty tm, False, Public, Unchecked, EmptyMI) ctxt in
841 uctxt { definitions = ctxt' }
844 uctxt { definitions = ctxt' }
843setAccess :: Name -> Accessibility -> Context -> Context
846setAccess :: Name -> Accessibility -> Context -> Context
844setAccess n a uctxt
847setAccess n a uctxt
845 = let ctxt = definitions uctxt
848 = let ctxt = definitions uctxt
846− !ctxt' = updateDef n (\ (d, _, t, m) -> (d, a, t, m)) ctxt in
849+ !ctxt' = updateDef n (\ (d, i, _, t, m) -> (d, i, a, t, m)) ctxt in
850+ uctxt { definitions = ctxt' }
852+setInjective :: Name -> Injectivity -> Context -> Context
853+setInjective n i uctxt
854+ = let ctxt = definitions uctxt
855+ !ctxt' = updateDef n (\ (d, _, a, t, m) -> (d, i, a, t, m)) ctxt in
847 uctxt { definitions = ctxt' }
856 uctxt { definitions = ctxt' }
849setTotal :: Name -> Totality -> Context -> Context
858setTotal :: Name -> Totality -> Context -> Context
850setTotal n t uctxt
859setTotal n t uctxt
851 = let ctxt = definitions uctxt
860 = let ctxt = definitions uctxt
852− !ctxt' = updateDef n (\ (d, a, _, m) -> (d, a, t, m)) ctxt in
861+ !ctxt' = updateDef n (\ (d, i, a, _, m) -> (d, i, a, t, m)) ctxt in
853 uctxt { definitions = ctxt' }
862 uctxt { definitions = ctxt' }
855setMetaInformation :: Name -> MetaInformation -> Context -> Context
864setMetaInformation :: Name -> MetaInformation -> Context -> Context
856setMetaInformation n m uctxt
865setMetaInformation n m uctxt
857 = let ctxt = definitions uctxt
866 = let ctxt = definitions uctxt
858− !ctxt' = updateDef n (\ (d, a, t, _) -> (d, a, t, m)) ctxt in
867+ !ctxt' = updateDef n (\ (d, i, a, t, _) -> (d, i, a, t, m)) ctxt in
859 uctxt { definitions = ctxt' }
868 uctxt { definitions = ctxt' }
861addCtxtDef :: Name -> Def -> Context -> Context
870addCtxtDef :: Name -> Def -> Context -> Context
862addCtxtDef n d c = let ctxt = definitions c
871addCtxtDef n d c = let ctxt = definitions c
863− !ctxt' = addDef n (d, Public, Unchecked, EmptyMI) $! ctxt in
872+ !ctxt' = addDef n (d, False, Public, Unchecked, EmptyMI) $! ctxt in
864 c { definitions = ctxt' }
873 c { definitions = ctxt' }
866addTyDecl :: Name -> NameType -> Type -> Context -> Context
875addTyDecl :: Name -> NameType -> Type -> Context -> Context
867addTyDecl n nt ty uctxt
876addTyDecl n nt ty uctxt
868 = let ctxt = definitions uctxt
877 = let ctxt = definitions uctxt
869− !ctxt' = addDef n (TyDecl nt ty, Public, Unchecked, EmptyMI) ctxt in
878+ !ctxt' = addDef n (TyDecl nt ty, False, Public, Unchecked, EmptyMI) ctxt in
870 uctxt { definitions = ctxt' }
879 uctxt { definitions = ctxt' }
872addDatatype :: Datatype Name -> Context -> Context
881addDatatype :: Datatype Name -> Context -> Context
@@ -874,14 +883,14 @@ addDatatype (Data n tag ty unique cons) uctxt
874 = let ctxt = definitions uctxt
883 = let ctxt = definitions uctxt
875 ty' = normalise uctxt [] ty
884 ty' = normalise uctxt [] ty
876 !ctxt' = addCons 0 cons (addDef n
885 !ctxt' = addCons 0 cons (addDef n
877− (TyDecl (TCon tag (arity ty')) ty, Public, Unchecked, EmptyMI) ctxt) in
886+ (TyDecl (TCon tag (arity ty')) ty, True, Public, Unchecked, EmptyMI) ctxt) in
878 uctxt { definitions = ctxt' }
887 uctxt { definitions = ctxt' }
880 addCons tag [] ctxt = ctxt
889 addCons tag [] ctxt = ctxt
881 addCons tag ((n, ty) : cons) ctxt
890 addCons tag ((n, ty) : cons) ctxt
882 = let ty' = normalise uctxt [] ty in
891 = let ty' = normalise uctxt [] ty in
883 addCons (tag+1) cons (addDef n
892 addCons (tag+1) cons (addDef n
884− (TyDecl (DCon tag (arity ty') unique) ty, Public, Unchecked, EmptyMI) ctxt)
893+ (TyDecl (DCon tag (arity ty') unique) ty, True, Public, Unchecked, EmptyMI) ctxt)
886-- FIXME: Too many arguments! Refactor all these Bools.
895-- FIXME: Too many arguments! Refactor all these Bools.
@@ -923,7 +932,7 @@ addCasedef n ei ci@(CaseInfo inline alwaysInline tcdict)
923 (args_rt, sc_rt)
932 (args_rt, sc_rt)
924 op = (CaseOp (ci { case_inlinable = inlc })
933 op = (CaseOp (ci { case_inlinable = inlc })
925 ty argtys ps_in ps_tot cdef,
934 ty argtys ps_in ps_tot cdef,
926− access, Unchecked, EmptyMI)
935+ False, access, Unchecked, EmptyMI)
927 in return $ addDef n op ctxt
936 in return $ addDef n op ctxt
928-- other -> tfail (Msg $ "Error adding case def: " ++ show other)
937-- other -> tfail (Msg $ "Error adding case def: " ++ show other)
929 return uctxt { definitions = ctxt' }
938 return uctxt { definitions = ctxt' }
@@ -933,15 +942,15 @@ simplifyCasedef :: Name -> ErasureInfo -> Context -> TC Context
933simplifyCasedef n ei uctxt
942simplifyCasedef n ei uctxt
934 = do let ctxt = definitions uctxt
943 = do let ctxt = definitions uctxt
935 ctxt' <- case lookupCtxt n ctxt of
944 ctxt' <- case lookupCtxt n ctxt of
936− [(CaseOp ci ty atys [] ps _, acc, tot, metainf)] ->
945+ [(CaseOp ci ty atys [] ps _, inj, acc, tot, metainf)] ->
937 return ctxt -- nothing to simplify (or already done...)
946 return ctxt -- nothing to simplify (or already done...)
938− [(CaseOp ci ty atys ps_in ps cd, acc, tot, metainf)] ->
947+ [(CaseOp ci ty atys ps_in ps cd, inj, acc, tot, metainf)] ->
939 do let ps_in' = map simpl ps_in
948 do let ps_in' = map simpl ps_in
940 pdef = map debind ps_in'
949 pdef = map debind ps_in'
941 CaseDef args sc _ <- simpleCase False (STerm Erased) False CompileTime emptyFC [] atys pdef ei
950 CaseDef args sc _ <- simpleCase False (STerm Erased) False CompileTime emptyFC [] atys pdef ei
942 return $ addDef n (CaseOp ci
951 return $ addDef n (CaseOp ci
943 ty atys ps_in' ps (cd { cases_totcheck = (args, sc) }),
952 ty atys ps_in' ps (cd { cases_totcheck = (args, sc) }),
944− acc, tot, metainf) ctxt
953+ inj, acc, tot, metainf) ctxt
946 _ -> return ctxt
955 _ -> return ctxt
947 return uctxt { definitions = ctxt' }
956 return uctxt { definitions = ctxt' }
@@ -961,10 +970,10 @@ addOperator :: Name -> Type -> Int -> ([Value] -> Maybe Value) ->
961 Context -> Context
970 Context -> Context
962addOperator n ty a op uctxt
971addOperator n ty a op uctxt
963 = let ctxt = definitions uctxt
972 = let ctxt = definitions uctxt
964− ctxt' = addDef n (Operator ty a op, Public, Unchecked, EmptyMI) ctxt in
973+ ctxt' = addDef n (Operator ty a op, False, Public, Unchecked, EmptyMI) ctxt in
965 uctxt { definitions = ctxt' }
974 uctxt { definitions = ctxt' }
967−tfst (a, _, _, _) = a
976+tfst (a, _, _, _, _) = a
969lookupNames :: Name -> Context -> [Name]
978lookupNames :: Name -> Context -> [Name]
970lookupNames n ctxt
979lookupNames n ctxt
@@ -1041,10 +1050,10 @@ lookupP_all :: Bool -> Bool -> Name -> Context -> [Term]
1041lookupP_all all exact n ctxt
1050lookupP_all all exact n ctxt
1042 = do (n', def) <- names
1051 = do (n', def) <- names
1043 p <- case def of
1052 p <- case def of
1044− (Function ty tm, a, _, _) -> return (P Ref n' ty, a)
1053+ (Function ty tm, inj, a, _, _) -> return (P Ref n' ty, a)
1045− (TyDecl nt ty, a, _, _) -> return (P nt n' ty, a)
1054+ (TyDecl nt ty, _, a, _, _) -> return (P nt n' ty, a)
1046− (CaseOp _ ty _ _ _ _, a, _, _) -> return (P Ref n' ty, a)
1055+ (CaseOp _ ty _ _ _ _, inj, a, _, _) -> return (P Ref n' ty, a)
1047− (Operator ty _ _, a, _, _) -> return (P Ref n' ty, a)
1056+ (Operator ty _ _, inj, a, _, _) -> return (P Ref n' ty, a)
1048 case snd p of
1057 case snd p of
1049 Hidden -> if all then return (fst p) else []
1058 Hidden -> if all then return (fst p) else []
1050 Private -> if all then return (fst p) else []
1059 Private -> if all then return (fst p) else []
@@ -1071,24 +1080,28 @@ lookupDefAcc :: Name -> Bool -> Context ->
1071lookupDefAcc n mkpublic ctxt
1080lookupDefAcc n mkpublic ctxt
1072 = map mkp $ lookupCtxt n (definitions ctxt)
1081 = map mkp $ lookupCtxt n (definitions ctxt)
1073 -- io_bind a special case for REPL prettiness
1082 -- io_bind a special case for REPL prettiness
1074− where mkp (d, a, _, _) = if mkpublic && (not (n == sUN "io_bind" || n == sUN "io_return"))
1083+ where mkp (d, inj, a, _, _) = if mkpublic && (not (n == sUN "io_bind" || n == sUN "io_return"))
1075− then (d, Public) else (d, a)
1084+ then (d, Public) else (d, a)
1077lookupDefAccExact :: Name -> Bool -> Context ->
1086lookupDefAccExact :: Name -> Bool -> Context ->
1078 Maybe (Def, Accessibility)
1087 Maybe (Def, Accessibility)
1079lookupDefAccExact n mkpublic ctxt
1088lookupDefAccExact n mkpublic ctxt
1080 = fmap mkp $ lookupCtxtExact n (definitions ctxt)
1089 = fmap mkp $ lookupCtxtExact n (definitions ctxt)
1081 -- io_bind a special case for REPL prettiness
1090 -- io_bind a special case for REPL prettiness
1082− where mkp (d, a, _, _) = if mkpublic && (not (n == sUN "io_bind" || n == sUN "io_return"))
1091+ where mkp (d, inj, a, _, _) = if mkpublic && (not (n == sUN "io_bind" || n == sUN "io_return"))
1083− then (d, Public) else (d, a)
1092+ then (d, Public) else (d, a)
1085lookupTotal :: Name -> Context -> [Totality]
1094lookupTotal :: Name -> Context -> [Totality]
1086lookupTotal n ctxt = map mkt $ lookupCtxt n (definitions ctxt)
1095lookupTotal n ctxt = map mkt $ lookupCtxt n (definitions ctxt)
1087− where mkt (d, a, t, m) = t
1096+ where mkt (d, inj, a, t, m) = t
1089lookupTotalExact :: Name -> Context -> Maybe Totality
1098lookupTotalExact :: Name -> Context -> Maybe Totality
1090lookupTotalExact n ctxt = fmap mkt $ lookupCtxtExact n (definitions ctxt)
1099lookupTotalExact n ctxt = fmap mkt $ lookupCtxtExact n (definitions ctxt)
1091− where mkt (d, a, t, m) = t
1100+ where mkt (d, inj, a, t, m) = t
1102+lookupInjectiveExact :: Name -> Context -> Maybe Injectivity
1103+lookupInjectiveExact n ctxt = fmap mkt $ lookupCtxtExact n (definitions ctxt)
1104+ where mkt (d, inj, a, t, m) = inj
1093-- Check if a name is reducible in the type checker. Partial definitions
1106-- Check if a name is reducible in the type checker. Partial definitions
1094-- are not reducible (so treated as a constant)
1107-- are not reducible (so treated as a constant)
@@ -1100,10 +1113,10 @@ tcReducible n ctxt = case lookupTotalExact n ctxt of
1101lookupMetaInformation :: Name -> Context -> [MetaInformation]
1114lookupMetaInformation :: Name -> Context -> [MetaInformation]
1102lookupMetaInformation n ctxt = map mkm $ lookupCtxt n (definitions ctxt)
1115lookupMetaInformation n ctxt = map mkm $ lookupCtxt n (definitions ctxt)
1103− where mkm (d, a, t, m) = m
1116+ where mkm (d, inj, a, t, m) = m
1105lookupNameTotal :: Name -> Context -> [(Name, Totality)]
1118lookupNameTotal :: Name -> Context -> [(Name, Totality)]
1106−lookupNameTotal n = map (\(n, (_, _, t, _)) -> (n, t)) . lookupCtxtName n . definitions
1119+lookupNameTotal n = map (\(n, (_, _, _, t, _)) -> (n, t)) . lookupCtxtName n . definitions
1109lookupVal :: Name -> Context -> [Value]
1122lookupVal :: Name -> Context -> [Value]