@@ -6,14 +6,15 @@ module Idris.Core.Evaluate(normalise, normaliseTrace, normaliseC,
66 normaliseAll, normaliseBlocking, toValue, quoteTerm,
77 rt_simplify, simplify, specialise, hnf, convEq, convEq',
88 Def(..), CaseInfo(..), CaseDefs(..),
9− Accessibility(..), Totality(..), PReason(..), MetaInformation(..),
9+ Accessibility(..), Injectivity, Totality(..), PReason(..), MetaInformation(..),
1010 Context, initContext, ctxtAlist, next_tvar,
11− addToCtxt, setAccess, setTotal, setMetaInformation, addCtxtDef, addTyDecl,
11+ addToCtxt, setAccess, setInjective, setTotal, setMetaInformation, addCtxtDef, addTyDecl,
1212 addDatatype, addCasedef, simplifyCasedef, addOperator,
1313 lookupNames, lookupTyName, lookupTyNameExact, lookupTy, lookupTyExact,
1414 lookupP, lookupP_all, lookupDef, lookupNameDef, lookupDefExact, lookupDefAcc, lookupDefAccExact, lookupVal,
1515 mapDefCtxt,
16− lookupTotal, lookupTotalExact, lookupNameTotal, lookupMetaInformation, lookupTyEnv, isTCDict, isDConName, canBeDConName, isTConName, isConName, isFnName,
16+ lookupTotal, lookupTotalExact, lookupInjectiveExact,
17+ lookupNameTotal, lookupMetaInformation, lookupTyEnv, isTCDict, isDConName, canBeDConName, isTConName, isConName, isFnName,
1718 Value(..), Quote(..), initEval, uniqueNameCtxt, uniqueBindersCtxt, definitions,
1819 isUniverse) where
1920
@@ -759,6 +760,8 @@ instance Show Accessibility where
759760 show Private = "private"
760761 show Hidden = "hidden"
761762
763+type Injectivity = Bool
764+
762765-- | The result of totality checking
763766data Totality = Total [Int] -- ^ well-founded arguments
764767 | Productive -- ^ productive
@@ -816,7 +819,7 @@ data MetaInformation =
816819-- universe constraints and existing definitions.
817820data Context = MkContext {
818821 next_tvar :: Int,
819− definitions :: Ctxt (Def, Accessibility, Totality, MetaInformation)
822+ definitions :: Ctxt (Def, Injectivity, Accessibility, Totality, MetaInformation)
820823 } deriving Show
821824
822825
@@ -826,47 +829,53 @@ initContext = MkContext 0 emptyContext
826829
827830mapDefCtxt :: (Def -> Def) -> Context -> Context
828831mapDefCtxt 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)
830833
831834-- | Get the definitions from a context
832835ctxtAlist :: 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)
834837
835838veval ctxt env t = evalState (eval False ctxt [] env t []) initEval
836839
837840addToCtxt :: Name -> Term -> Type -> Context -> Context
838841addToCtxt n tm ty uctxt
839842 = 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
841844 uctxt { definitions = ctxt' }
842845
843846setAccess :: Name -> Accessibility -> Context -> Context
844847setAccess n a uctxt
845848 = 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' }
851+
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
847856 uctxt { definitions = ctxt' }
848857
849858setTotal :: Name -> Totality -> Context -> Context
850859setTotal n t uctxt
851860 = 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
853862 uctxt { definitions = ctxt' }
854863
855864setMetaInformation :: Name -> MetaInformation -> Context -> Context
856865setMetaInformation n m uctxt
857866 = 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
859868 uctxt { definitions = ctxt' }
860869
861870addCtxtDef :: Name -> Def -> Context -> Context
862871addCtxtDef 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
864873 c { definitions = ctxt' }
865874
866875addTyDecl :: Name -> NameType -> Type -> Context -> Context
867876addTyDecl n nt ty uctxt
868877 = 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
870879 uctxt { definitions = ctxt' }
871880
872881addDatatype :: Datatype Name -> Context -> Context
@@ -874,14 +883,14 @@ addDatatype (Data n tag ty unique cons) uctxt
874883 = let ctxt = definitions uctxt
875884 ty' = normalise uctxt [] ty
876885 !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
878887 uctxt { definitions = ctxt' }
879888 where
880889 addCons tag [] ctxt = ctxt
881890 addCons tag ((n, ty) : cons) ctxt
882891 = let ty' = normalise uctxt [] ty in
883892 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)
885894
886895-- FIXME: Too many arguments! Refactor all these Bools.
887896--
@@ -923,7 +932,7 @@ addCasedef n ei ci@(CaseInfo inline alwaysInline tcdict)
923932 (args_rt, sc_rt)
924933 op = (CaseOp (ci { case_inlinable = inlc })
925934 ty argtys ps_in ps_tot cdef,
926− access, Unchecked, EmptyMI)
935+ False, access, Unchecked, EmptyMI)
927936 in return $ addDef n op ctxt
928937-- other -> tfail (Msg $ "Error adding case def: " ++ show other)
929938 return uctxt { definitions = ctxt' }
@@ -933,15 +942,15 @@ simplifyCasedef :: Name -> ErasureInfo -> Context -> TC Context
933942simplifyCasedef n ei uctxt
934943 = do let ctxt = definitions uctxt
935944 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)] ->
937946 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)] ->
939948 do let ps_in' = map simpl ps_in
940949 pdef = map debind ps_in'
941950 CaseDef args sc _ <- simpleCase False (STerm Erased) False CompileTime emptyFC [] atys pdef ei
942951 return $ addDef n (CaseOp ci
943952 ty atys ps_in' ps (cd { cases_totcheck = (args, sc) }),
944− acc, tot, metainf) ctxt
953+ inj, acc, tot, metainf) ctxt
945954
946955 _ -> return ctxt
947956 return uctxt { definitions = ctxt' }
@@ -961,10 +970,10 @@ addOperator :: Name -> Type -> Int -> ([Value] -> Maybe Value) ->
961970 Context -> Context
962971addOperator n ty a op uctxt
963972 = 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
965974 uctxt { definitions = ctxt' }
966975
967−tfst (a, _, _, _) = a
976+tfst (a, _, _, _, _) = a
968977
969978lookupNames :: Name -> Context -> [Name]
970979lookupNames n ctxt
@@ -1041,10 +1050,10 @@ lookupP_all :: Bool -> Bool -> Name -> Context -> [Term]
10411050lookupP_all all exact n ctxt
10421051 = do (n', def) <- names
10431052 p <- case def of
1044− (Function ty tm, a, _, _) -> return (P Ref n' ty, a)
1045− (TyDecl nt ty, a, _, _) -> return (P nt n' ty, a)
1046− (CaseOp _ ty _ _ _ _, a, _, _) -> return (P Ref n' ty, a)
1047− (Operator ty _ _, a, _, _) -> return (P Ref n' ty, a)
1053+ (Function ty tm, inj, a, _, _) -> return (P Ref n' ty, a)
1054+ (TyDecl nt ty, _, a, _, _) -> return (P nt n' ty, a)
1055+ (CaseOp _ ty _ _ _ _, inj, a, _, _) -> return (P Ref n' ty, a)
1056+ (Operator ty _ _, inj, a, _, _) -> return (P Ref n' ty, a)
10481057 case snd p of
10491058 Hidden -> if all then return (fst p) else []
10501059 Private -> if all then return (fst p) else []
@@ -1071,24 +1080,28 @@ lookupDefAcc :: Name -> Bool -> Context ->
10711080lookupDefAcc n mkpublic ctxt
10721081 = map mkp $ lookupCtxt n (definitions ctxt)
10731082 -- io_bind a special case for REPL prettiness
1074− where mkp (d, a, _, _) = if mkpublic && (not (n == sUN "io_bind" || n == sUN "io_return"))
1075− then (d, Public) else (d, a)
1083+ where mkp (d, inj, a, _, _) = if mkpublic && (not (n == sUN "io_bind" || n == sUN "io_return"))
1084+ then (d, Public) else (d, a)
10761085
10771086lookupDefAccExact :: Name -> Bool -> Context ->
10781087 Maybe (Def, Accessibility)
10791088lookupDefAccExact n mkpublic ctxt
10801089 = fmap mkp $ lookupCtxtExact n (definitions ctxt)
10811090 -- io_bind a special case for REPL prettiness
1082− where mkp (d, a, _, _) = if mkpublic && (not (n == sUN "io_bind" || n == sUN "io_return"))
1083− then (d, Public) else (d, a)
1091+ where mkp (d, inj, a, _, _) = if mkpublic && (not (n == sUN "io_bind" || n == sUN "io_return"))
1092+ then (d, Public) else (d, a)
10841093
10851094lookupTotal :: Name -> Context -> [Totality]
10861095lookupTotal 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
10881097
10891098lookupTotalExact :: Name -> Context -> Maybe Totality
10901099lookupTotalExact 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
1101+
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
10921105
10931106-- Check if a name is reducible in the type checker. Partial definitions
10941107-- are not reducible (so treated as a constant)
@@ -1100,10 +1113,10 @@ tcReducible n ctxt = case lookupTotalExact n ctxt of
11001113
11011114lookupMetaInformation :: Name -> Context -> [MetaInformation]
11021115lookupMetaInformation 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
11041117
11051118lookupNameTotal :: 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
11071120
11081121
11091122lookupVal :: Name -> Context -> [Value]