@@ -6,9 +6,9 @@ quoters and unquoters along with some supporting datatypes.
6module Idris.Reflection where
6module Idris.Reflection where
8import Control.Applicative ((<$>), (<*>), pure)
8import Control.Applicative ((<$>), (<*>), pure)
9−import Control.Monad (liftM, liftM2, liftM3)
9+import Control.Monad (liftM, liftM2, liftM4)
10import Data.Maybe (catMaybes)
10import Data.Maybe (catMaybes)
11−import Data.List ((\\))
11+import Data.List ((\\), findIndex)
12import qualified Data.Text as T
12import qualified Data.Text as T
14import Idris.Core.Elaborate (claim, fill, focus, getNameFrom, initElaborator,
14import Idris.Core.Elaborate (claim, fill, focus, getNameFrom, initElaborator,
@@ -23,27 +23,33 @@ import Idris.Delaborate (delab)
24data RErasure = RErased | RNotErased deriving Show
24data RErasure = RErased | RNotErased deriving Show
26−data RArg = RExplicit { argName :: Name, argErased :: RErasure, argTy :: Raw }
26+data RPlicity = RExplicit | RImplicit | RConstraint deriving Show
27− | RImplicit { argName :: Name, argErased :: RErasure, argTy :: Raw }
27+
28− | RConstraint { argName :: Name, argErased :: RErasure, argTy :: Raw }
28+data RFunArg = RFunArg { argName :: Name
30+ , argPlicity :: RPlicity
31+ , erasure :: RErasure
29 deriving Show
33 deriving Show
31−data RTyDecl = RDeclare Name [RArg] Raw deriving Show
35+data RTyDecl = RDeclare Name [RFunArg] Raw deriving Show
33−data RTyConArg = RParameter { tcArgName :: Name, tcArgErased :: RErasure, tcArgTy :: Raw }
37+data RTyConArg = RParameter RFunArg
34− | RIndex { tcArgName :: Name, tcArgErased :: RErasure, tcArgTy :: Raw }
38+ | RIndex RFunArg
35 deriving Show
39 deriving Show
41+data RCtorArg = RCtorParameter RFunArg | RCtorField RFunArg deriving Show
37−data RDatatype = RDatatype Name [RTyConArg] Raw [(Name, [RArg], Raw)] deriving Show
43+data RDatatype = RDatatype Name [RTyConArg] Raw [(Name, [RCtorArg], Raw)] deriving Show
39rArgOpts :: RErasure -> [ArgOpt]
45rArgOpts :: RErasure -> [ArgOpt]
40rArgOpts RErased = [InaccessibleArg]
46rArgOpts RErased = [InaccessibleArg]
41rArgOpts _ = []
47rArgOpts _ = []
43−rArgToPArg :: RArg -> PArg
49+rFunArgToPArg :: RFunArg -> PArg
44−rArgToPArg (RExplicit n e _) = PExp 0 (rArgOpts e) n Placeholder
50+rFunArgToPArg (RFunArg n _ RExplicit e) = PExp 0 (rArgOpts e) n Placeholder
45−rArgToPArg (RImplicit n e _) = PImp 0 False (rArgOpts e) n Placeholder
51+rFunArgToPArg (RFunArg n _ RImplicit e) = PImp 0 False (rArgOpts e) n Placeholder
46−rArgToPArg (RConstraint n e _) = PConstraint 0 (rArgOpts e) n Placeholder
52+rFunArgToPArg (RFunArg n _ RConstraint e) = PConstraint 0 (rArgOpts e) n Placeholder
48data RFunClause = RMkFunClause Raw Raw
54data RFunClause = RMkFunClause Raw Raw
49 | RMkImpossibleClause Raw
55 | RMkImpossibleClause Raw
@@ -795,6 +801,9 @@ reflectEnv = foldr consToEnvList emptyEnvList
795 emptyEnvList = raw_apply (Var (sNS (sUN "Nil") ["List", "Prelude"]))
801 emptyEnvList = raw_apply (Var (sNS (sUN "Nil") ["List", "Prelude"]))
796 [envTupleType]
802 [envTupleType]
804+reifyEnv :: Term -> ElabD Env
805+reifyEnv = reifyList (reifyPair reifyTTName (reifyTTBinder reifyTT (reflm "TT")))
798-- | Reflect an error into the internal datatype of Idris -- TODO
807-- | Reflect an error into the internal datatype of Idris -- TODO
799rawBool :: Bool -> Raw
808rawBool :: Bool -> Raw
800rawBool True = Var (sNS (sUN "True") ["Bool", "Prelude"])
809rawBool True = Var (sNS (sUN "True") ["Bool", "Prelude"])
@@ -977,29 +986,31 @@ reifyErasure (P (DCon _ _ _) n _)
977 | n == tacN "NotErased" = return RNotErased
986 | n == tacN "NotErased" = return RNotErased
978reifyErasure tm = fail $ "Can't reify " ++ show tm ++ " as erasure info."
987reifyErasure tm = fail $ "Can't reify " ++ show tm ++ " as erasure info."
989+reifyPlicity :: Term -> ElabD RPlicity
990+reifyPlicity (P (DCon _ _ _) n _)
991+ | n == tacN "Explicit" = return RExplicit
992+ | n == tacN "Implicit" = return RImplicit
993+ | n == tacN "Constraint" = return RConstraint
994+reifyPlicity tm = fail $ "Couldn't reify " ++ show tm ++ " as RPlicity."
996+reifyRFunArg :: Term -> ElabD RFunArg
997+reifyRFunArg (App _ (App _ (App _ (App _ (P (DCon _ _ _) n _) argN) argTy) argPl) argE)
998+ | n == tacN "MkFunArg" = liftM4 RFunArg
1001+ (reifyPlicity argPl)
1002+ (reifyErasure argE)
1003+reifyRFunArg aTm = fail $ "Couldn't reify " ++ show aTm ++ " as an RArg."
980reifyTyDecl :: Term -> ElabD RTyDecl
1005reifyTyDecl :: Term -> ElabD RTyDecl
981reifyTyDecl (App _ (App _ (App _ (P (DCon _ _ _) n _) tyN) args) ret)
1006reifyTyDecl (App _ (App _ (App _ (P (DCon _ _ _) n _) tyN) args) ret)
982 | n == tacN "Declare" =
1007 | n == tacN "Declare" =
983 do tyN' <- reifyTTName tyN
1008 do tyN' <- reifyTTName tyN
984 args' <- case unList args of
1009 args' <- case unList args of
985 Nothing -> fail $ "Couldn't reify " ++ show args ++ " as an arglist."
1010 Nothing -> fail $ "Couldn't reify " ++ show args ++ " as an arglist."
986− Just xs -> mapM reifyRArg xs
1011+ Just xs -> mapM reifyRFunArg xs
987 ret' <- reifyRaw ret
1012 ret' <- reifyRaw ret
988 return $ RDeclare tyN' args' ret'
1013 return $ RDeclare tyN' args' ret'
989− where reifyRArg :: Term -> ElabD RArg
990− reifyRArg (App _ (App _ (App _ (P (DCon _ _ _) n _) argN) argE) argTy)
991− | n == tacN "Explicit" = liftM3 RExplicit
995− | n == tacN "Implicit" = liftM3 RImplicit
998− (reifyRaw argTy) | n == tacN "Constraint" = liftM3 RConstraint
1000− (reifyErasure argE)
1002− reifyRArg aTm = fail $ "Couldn't reify " ++ show aTm ++ " as an RArg."
1003reifyTyDecl tm = fail $ "Couldn't reify " ++ show tm ++ " as a type declaration."
1014reifyTyDecl tm = fail $ "Couldn't reify " ++ show tm ++ " as a type declaration."
1005reifyFunDefn :: Term -> ElabD RFunDefn
1016reifyFunDefn :: Term -> ElabD RFunDefn
@@ -1028,19 +1039,25 @@ envTupleType
1029-- | Apply Idris's implicit info to get a signature. The [PArg] should
1040-- | Apply Idris's implicit info to get a signature. The [PArg] should
1030-- come from a lookup in idris_implicits on IState.
1041-- come from a lookup in idris_implicits on IState.
1031−getArgs :: [PArg] -> Raw -> ([RArg], Raw)
1042+getArgs :: [PArg] -> Raw -> ([RFunArg], Raw)
1032getArgs [] r = ([], r)
1043getArgs [] r = ([], r)
1033getArgs (a:as) (RBind n (Pi _ ty _) sc) =
1044getArgs (a:as) (RBind n (Pi _ ty _) sc) =
1034 let (args, res) = getArgs as sc
1045 let (args, res) = getArgs as sc
1035 erased = if InaccessibleArg `elem` argopts a then RErased else RNotErased
1046 erased = if InaccessibleArg `elem` argopts a then RErased else RNotErased
1036 arg' = case a of
1047 arg' = case a of
1037− PImp {} -> RImplicit n erased ty
1048+ PImp {} -> RFunArg n ty RImplicit erased
1038− PExp {} -> RExplicit n erased ty
1049+ PExp {} -> RFunArg n ty RExplicit erased
1039− PConstraint {} -> RConstraint n erased ty
1050+ PConstraint {} -> RFunArg n ty RConstraint erased
1040− PTacImplicit {} -> RImplicit n erased ty -- TODO?
1051+ PTacImplicit {} -> RFunArg n ty RImplicit erased
1041 in (arg':args, res)
1052 in (arg':args, res)
1042getArgs _ r = ([], r)
1053getArgs _ r = ([], r)
1055+unApplyRaw :: Raw -> (Raw, [Raw])
1056+unApplyRaw tm = ua [] tm
1058+ ua args (RApp f a) = ua (a:args) f
1059+ ua args t = (t, args)
1044-- | Build the reflected datatype definition(s) that correspond(s) to
1061-- | Build the reflected datatype definition(s) that correspond(s) to
1045-- a provided unqualified name
1062-- a provided unqualified name
1046buildDatatypes :: IState -> Name -> [RDatatype]
1063buildDatatypes :: IState -> Name -> [RDatatype]
@@ -1052,44 +1069,62 @@ buildDatatypes ist n =
1052 ctxt = tt_ctxt ist
1069 ctxt = tt_ctxt ist
1053 impls = idris_implicits ist
1070 impls = idris_implicits ist
1055− ctorSig cn = do cty <- fmap forget (lookupTyExact cn ctxt)
1072+ ctorSig params cn = do cty <- fmap forget (lookupTyExact cn ctxt)
1056− argInfo <- lookupCtxtExact cn impls
1073+ argInfo <- lookupCtxtExact cn impls
1057− let (args, res) = getArgs argInfo cty
1074+ let (args, res) = getArgs argInfo cty
1058− return (cn, args, res)
1075+ return (cn, ctorArgsStatus args res params, res)
1077+ argPos n [] res = findPos n res
1078+ where findPos n app = case unApplyRaw app of
1079+ (_, argL) -> findIndex (== Var n) argL
1080+ argPos n (arg:args) res = if n == argName arg
1082+ else argPos n args res
1084+ ctorArgsStatus :: [RFunArg] -> Raw -> [Int] -> [RCtorArg]
1085+ ctorArgsStatus [] _ _ = []
1086+ ctorArgsStatus (arg:args) res params =
1087+ case argPos (argName arg) args res of
1088+ Nothing -> RCtorField arg : ctorArgsStatus args res params
1089+ Just i -> if i `elem` params
1090+ then RCtorParameter arg : ctorArgsStatus args res params
1091+ else RCtorField arg : ctorArgsStatus args res params
1060 mkDataType name (TI {param_pos = params, con_names = constrs}) =
1093 mkDataType name (TI {param_pos = params, con_names = constrs}) =
1061 do (TyDecl (TCon _ _) ty) <- lookupDefExact name ctxt
1094 do (TyDecl (TCon _ _) ty) <- lookupDefExact name ctxt
1062 implInfo <- lookupCtxtExact name impls
1095 implInfo <- lookupCtxtExact name impls
1063− let (tcargs, tcres) = getTCArgs params implInfo (forget ty) 0
1096+ let (tcargs, tcres) = getTCArgs params implInfo (forget ty)
1064− ctors <- mapM ctorSig constrs
1097+ ctors <- mapM (ctorSig params) constrs
1065 return $ RDatatype name tcargs tcres ctors
1098 return $ RDatatype name tcargs tcres ctors
1067− getTCArgs params implInfo (RBind an (Pi _ ty _) body) i =
1100+ getTCArgs :: [Int] -> [PArg] -> Raw -> ([RTyConArg], Raw)
1068− let (erased, implInfo') = case implInfo of
1101+ getTCArgs params implInfo tcTy =
1069− [] -> (RNotErased, [])
1070− (a:as) -> (if InaccessibleArg `elem` argopts a
1074− (args, res) = getTCArgs params implInfo' body (i+1)
1102+ let (args, res) = getArgs implInfo tcTy
1075− in if i `elem` params
1103+ in (tcArg args 0, res)
1076− then (RParameter an erased ty : args, res )
1104+ where tcArg [] _ = []
1077− else (RIndex an erased ty : args, res)
1105+ tcArg (arg:args) i | i `elem` params = RParameter arg : tcArg args (i+1)
1078− getTCArgs _ implInfo tm _ = ([], tm)
1106+ | otherwise = RIndex arg : tcArg args (i+1)
1080reflectErasure :: RErasure -> Raw
1109reflectErasure :: RErasure -> Raw
1081reflectErasure RErased = Var (tacN "Erased")
1110reflectErasure RErased = Var (tacN "Erased")
1082reflectErasure RNotErased = Var (tacN "NotErased")
1111reflectErasure RNotErased = Var (tacN "NotErased")
1084−reflectArg :: RArg -> Raw
1113+reflectPlicity :: RPlicity -> Raw
1086− RApp (RApp (RApp (Var con) (reflectName $ argName arg))
1087− (reflectErasure $ argErased arg))
1088− (reflectRaw $ argTy arg)
1089− where con = case arg of
1090− RExplicit{} -> tacN "Explicit"
1114+reflectPlicity RExplicit = Var (tacN "Explicit")
1091− RImplicit{} -> tacN "Implicit"
1115+reflectPlicity RImplicit = Var (tacN "Implicit")
1092− RConstraint{} -> tacN "Constraint"
1116+reflectPlicity RConstraint = Var (tacN "Constraint")
1118+reflectArg :: RFunArg -> Raw
1119+reflectArg (RFunArg n ty plic erasure) =
1120+ RApp (RApp (RApp (RApp (Var $ tacN "MkFunArg") (reflectName n))
1122+ (reflectPlicity plic))
1123+ (reflectErasure erasure)
1125+reflectCtorArg :: RCtorArg -> Raw
1126+reflectCtorArg (RCtorParameter arg) = RApp (Var $ tacN "CtorParameter") (reflectArg arg)
1127+reflectCtorArg (RCtorField arg) = RApp (Var $ tacN "CtorField") (reflectArg arg)
1094reflectDatatype :: RDatatype -> Raw
1129reflectDatatype :: RDatatype -> Raw
1095reflectDatatype (RDatatype tyn tyConArgs tyConRes constrs) =
1130reflectDatatype (RDatatype tyn tyConArgs tyConRes constrs) =
@@ -1097,18 +1132,18 @@ reflectDatatype (RDatatype tyn tyConArgs tyConRes constrs) =
1097 , rawList (Var $ tacN "TyConArg") (map reflectConArg tyConArgs)
1132 , rawList (Var $ tacN "TyConArg") (map reflectConArg tyConArgs)
1098 , reflectRaw tyConRes
1133 , reflectRaw tyConRes
1099 , rawList (rawTripleTy (Var $ reflm "TTName")
1134 , rawList (rawTripleTy (Var $ reflm "TTName")
1100− (RApp (Var (sNS (sUN "List") ["List", "Prelude"])) (Var $ tacN "Arg"))
1135+ (RApp (Var (sNS (sUN "List") ["List", "Prelude"])) (Var $ tacN "CtorArg"))
1101 (Var $ reflm "Raw"))
1136 (Var $ reflm "Raw"))
1102 [ rawTriple ((Var $ reflm "TTName"),
1137 [ rawTriple ((Var $ reflm "TTName"),
1103− (RApp (Var (sNS (sUN "List") ["List", "Prelude"])) (Var $ tacN "Arg")),
1138+ (RApp (Var (sNS (sUN "List") ["List", "Prelude"])) (Var $ tacN "CtorArg")),
1104 (Var $ reflm "Raw"))
1139 (Var $ reflm "Raw"))
1105 (reflectName cn,
1140 (reflectName cn,
1106− rawList (Var $ tacN "Arg") (map reflectArg cargs),
1141+ rawList (Var $ tacN "CtorArg") (map reflectCtorArg cargs),
1107 reflectRaw cty)
1142 reflectRaw cty)
1108 | (cn, cargs, cty) <- constrs
1143 | (cn, cargs, cty) <- constrs
1111− where reflectConArg (RParameter n e t) =
1146+ where reflectConArg (RParameter a) =
1112− raw_apply (Var $ tacN "Parameter") [reflectName n, reflectErasure e, reflectRaw t]
1147+ RApp (Var $ tacN "TyConParameter") (reflectArg a)
1113− reflectConArg (RIndex n e t) =
1148+ reflectConArg (RIndex a) =
1114− raw_apply (Var $ tacN "Index") [reflectName n, reflectErasure e, reflectRaw t]
1149+ RApp (Var $ tacN "TyConIndex") (reflectArg a)