@@ -1,4 +1,4 @@
1−{-# LANGUAGE PatternGuards, ViewPatterns #-}
1+{-# LANGUAGE LambdaCase, PatternGuards, ViewPatterns #-}
22{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
33module Idris.Elab.Term where
44
@@ -10,6 +10,7 @@ import Idris.Error
1010import Idris.ProofSearch
1111import Idris.Output (pshow)
1212
13+import Idris.Core.CaseTree (SC, SC'(STerm))
1314import Idris.Core.Elaborate hiding (Tactic(..))
1415import Idris.Core.TT
1516import Idris.Core.Evaluate
@@ -35,6 +36,7 @@ import Debug.Trace
3536data ElabMode = ETyDecl | ELHS | ERHS
3637 deriving Eq
3738
39+
3840data ElabResult =
3941 ElabResult { resultTerm :: Term -- ^ The term resulting from elaboration
4042 , resultMetavars :: [(Name, (Int, Maybe Name, Type))]
@@ -43,23 +45,28 @@ data ElabResult =
4345 -- ^ Deferred declarations as the meaning of case blocks
4446 , resultContext :: Context
4547 -- ^ The potentially extended context from new definitions
46− , resultTyDecls :: [(Name, FC, [PArg], Type)]
48+ , resultTyDecls :: [RDeclInstructions]
4749 -- ^ Meta-info about the new type declarations
4850 }
4951
50−processTacticDecls :: [(Name, FC, [PArg], Type)] -> Idris ()
52+processTacticDecls :: [RDeclInstructions] -> Idris ()
5153processTacticDecls info =
52− forM_ info $ \(n, fc, impls, ty) ->
53− do logLvl 3 $ "Declaration from tactics: " ++ show n ++ " : " ++ show ty
54− logLvl 3 $ " It has impls " ++ show impls
55− updateIState $ \i -> i { idris_implicits =
56− addDef n impls (idris_implicits i) }
57− addIBC (IBCImp n)
58− ds <- checkDef fc iderr [(n, (-1, Nothing, ty))]
59− addIBC (IBCDef n)
60− let ds' = map (\(n, (i, top, t)) -> (n, (i, top, t, True))) ds
61− addDeferred ds'
62−
54+ forM_ info $ \case
55+ RTyDeclInstrs n fc impls ty ->
56+ do logLvl 3 $ "Declaration from tactics: " ++ show n ++ " : " ++ show ty
57+ logLvl 3 $ " It has impls " ++ show impls
58+ updateIState $ \i -> i { idris_implicits =
59+ addDef n impls (idris_implicits i) }
60+ addIBC (IBCImp n)
61+ ds <- checkDef fc (\_ e -> e) [(n, (-1, Nothing, ty))]
62+ addIBC (IBCDef n)
63+ let ds' = map (\(n, (i, top, t)) -> (n, (i, top, t, True))) ds
64+ addDeferred ds'
65+ RClausesInstrs n cs ->
66+ do logLvl 3 $ "Pattern-matching definition from tactics: " ++ show n
67+ solveDeferred n
68+ updateIState $ \i -> i { idris_patdefs = addDef n (cs, []) $ idris_patdefs i } -- TODO: missing clauses
69+ addIBC (IBCDef n)
6370
6471-- Using the elaborator, convert a term in raw syntax to a fully
6572-- elaborated, typechecked term.
@@ -213,7 +220,7 @@ goal_polymorphic =
213220
214221-- | Returns the set of declarations we need to add to complete the
215222-- definition (most likely case blocks to elaborate) as well as
216−-- declarations resulting from user tactic scripts (%runTactics)
223+-- declarations resulting from user tactic scripts (%runElab)
217224elab :: IState -> ElabInfo -> ElabMode -> FnOpts -> Name -> PTerm ->
218225 ElabD ()
219226elab ist info emode opts fn tm
@@ -1098,11 +1105,11 @@ elab ist info emode opts fn tm
10981105 delayElab $ do focus h
10991106 dotterm
11001107 elab' ina fc t
1101− elab' ina fc (PRunTactics fc' tm) =
1108+ elab' ina fc (PRunElab fc' tm) =
11021109 do attack
11031110 n <- getNameFrom (sMN 0 "tacticScript")
11041111 n' <- getNameFrom (sMN 0 "tacticExpr")
1105− let scriptTy = RApp (Var (sNS (sUN "Tactical") ["Tactical", "Reflection", "Language"])) (Var unitTy)
1112+ let scriptTy = RApp (Var (sNS (sUN "Elab") ["Elab", "Reflection", "Language"])) (Var unitTy)
11061113 claim n scriptTy
11071114 movelast n
11081115 letbind n' scriptTy (Var n)
@@ -1592,8 +1599,6 @@ case_ ind autoSolve ist fn tm = do
15921599 else casetac (forget val)
15931600 when autoSolve solveAll
15941601
1595−tacN :: String -> Name
1596−tacN str = sNS (sUN str) ["Tactical", "Reflection", "Language"]
15971602
15981603runTactical :: FC -> Env -> Term -> ElabD ()
15991604runTactical fc env tm = do tm' <- eval tm
@@ -1605,6 +1610,45 @@ runTactical fc env tm = do tm' <- eval tm
16051610
16061611 returnUnit = fmap fst $ get_type_val (Var unitCon)
16071612
1613+ patvars :: [Name] -> Term -> ([Name], Term)
1614+ patvars ns (Bind n (PVar t) sc) = patvars (n : ns) (instantiate (P Bound n t) sc)
1615+ patvars ns tm = (ns, tm)
1616+
1617+ pullVars :: (Term, Term) -> ([Name], Term, Term)
1618+ pullVars (lhs, rhs) = (fst (patvars [] lhs), snd (patvars [] lhs), snd (patvars [] rhs)) -- TODO alpha-convert rhs
1619+
1620+ defineFunction :: RFunDefn -> ElabD ()
1621+ defineFunction (RDefineFun n clauses) =
1622+ do ctxt <- get_context
1623+ ty <- maybe (fail "no type decl") return $ lookupTyExact n ctxt
1624+ let info = CaseInfo True True False -- TODO document and figure out
1625+ clauses' <- forM clauses (\case
1626+ RMkFunClause lhs rhs ->
1627+ do lhs' <- fmap fst . lift $ check ctxt [] lhs
1628+ rhs' <- fmap fst . lift $ check ctxt [] rhs
1629+ return $ Right (lhs', rhs')
1630+ RMkImpossibleClause lhs ->
1631+ do lhs' <- fmap fst . lift $ check ctxt [] lhs
1632+ return $ Left lhs')
1633+ let clauses'' = map (\case Right c -> pullVars c
1634+ Left lhs -> let (ns, lhs') = patvars [] lhs'
1635+ in (ns, lhs', Impossible))
1636+ clauses'
1637+ set_context $
1638+ addCasedef n (const [])
1639+ info False (STerm Erased)
1640+ True False -- TODO what are these?
1641+ [] [] -- TODO argument types, inaccessible types
1642+ clauses'
1643+ clauses''
1644+ clauses''
1645+ clauses''
1646+ clauses''
1647+ ty
1648+ ctxt
1649+ updateAux $ \e -> e { new_tyDecls = RClausesInstrs n clauses'' : new_tyDecls e}
1650+ return ()
1651+
16081652 -- | Do a step in the reflected elaborator monad. The input is the
16091653 -- step, the output is the (reflected) term returned.
16101654 runTacTm :: Term -> ElabD Term
@@ -1643,9 +1687,9 @@ runTactical fc env tm = do tm' <- eval tm
16431687 = do errs' <- eval errs
16441688 parts <- reifyReportParts errs'
16451689 lift . tfail $ ReflectionError [parts] (Msg "")
1646− | n == tacN "prim__PureTactical", [_a, tm] <- args
1690+ | n == tacN "prim__PureElab", [_a, tm] <- args
16471691 = return tm
1648− | n == tacN "prim__BindTactical", [_a, _b, first, andThen] <- args
1692+ | n == tacN "prim__BindElab", [_a, _b, first, andThen] <- args
16491693 = do first' <- eval first
16501694 res <- runTacTm first'
16511695 next <- eval (App Complete andThen res)
@@ -1672,7 +1716,7 @@ runTactical fc env tm = do tm' <- eval tm
16721716 returnUnit
16731717 | n == tacN "prim__Forget", [tt] <- args
16741718 = do tt' <- reifyTT tt
1675− fmap fst . get_type_val $ reflect tt'
1719+ fmap fst . get_type_val . reflectRaw $ forget tt'
16761720 | n == tacN "prim__Attack", [] <- args
16771721 = do attack
16781722 returnUnit
@@ -1713,10 +1757,14 @@ runTactical fc env tm = do tm' <- eval tm
17131757 let decl = TyDecl Ref checked
17141758 ctxt' = addCtxtDef n decl ctxt
17151759 set_context ctxt'
1716− updateAux $ \e -> e { new_tyDecls = (n, fc, map rArgToPArg args, checked) :
1760+ updateAux $ \e -> e { new_tyDecls = (RTyDeclInstrs n fc (map rArgToPArg args) checked) :
17171761 new_tyDecls e }
17181762 aux <- getAux
17191763 returnUnit
1764+ | n == tacN "prim__DefineFunction", [decl] <- args
1765+ = do defn <- reifyFunDefn decl
1766+ defineFunction defn
1767+ returnUnit
17201768 | n == tacN "prim__Debug", [ty, msg] <- args
17211769 = do let msg' = fromTTMaybe msg
17221770 case msg' of
@@ -1978,781 +2026,6 @@ runTac autoSolve ist perhapsFC fn tac
19782026 runReflected t = do t' <- reify ist t
19792027 runTac autoSolve ist perhapsFC fn t'
19802028
1981−-- | Prefix a name with the "Language.Reflection" namespace
1982−reflm :: String -> Name
1983−reflm n = sNS (sUN n) ["Reflection", "Language"]
1984−
1985−
1986−-- | Reify tactics from their reflected representation
1987−reify :: IState -> Term -> ElabD PTactic
1988−reify _ (P _ n _) | n == reflm "Intros" = return Intros
1989−reify _ (P _ n _) | n == reflm "Trivial" = return Trivial
1990−reify _ (P _ n _) | n == reflm "Instance" = return TCInstance
1991−reify _ (P _ n _) | n == reflm "Solve" = return Solve
1992−reify _ (P _ n _) | n == reflm "Compute" = return Compute
1993−reify _ (P _ n _) | n == reflm "Skip" = return Skip
1994−reify _ (P _ n _) | n == reflm "SourceFC" = return SourceFC
1995−reify _ (P _ n _) | n == reflm "Unfocus" = return Unfocus
1996−reify ist t@(App _ _ _)
1997− | (P _ f _, args) <- unApply t = reifyApp ist f args
1998−reify _ t = fail ("Unknown tactic " ++ show t)
1999−
2000−reifyApp :: IState -> Name -> [Term] -> ElabD PTactic
2001−reifyApp ist t [l, r] | t == reflm "Try" = liftM2 Try (reify ist l) (reify ist r)
2002−reifyApp _ t [Constant (I i)]
2003− | t == reflm "Search" = return (ProofSearch True True i Nothing [])
2004−reifyApp _ t [x]
2005− | t == reflm "Refine" = do n <- reifyTTName x
2006− return $ Refine n []
2007−reifyApp ist t [n, ty] | t == reflm "Claim" = do n' <- reifyTTName n
2008− goal <- reifyTT ty
2009− return $ Claim n' (delab ist goal)
2010−reifyApp ist t [l, r] | t == reflm "Seq" = liftM2 TSeq (reify ist l) (reify ist r)
2011−reifyApp ist t [Constant (Str n), x]
2012− | t == reflm "GoalType" = liftM (GoalType n) (reify ist x)
2013−reifyApp _ t [n] | t == reflm "Intro" = liftM (Intro . (:[])) (reifyTTName n)
2014−reifyApp ist t [t'] | t == reflm "Induction" = liftM (Induction . delab ist) (reifyTT t')
2015−reifyApp ist t [t'] | t == reflm "Case" = liftM (Induction . delab ist) (reifyTT t')
2016−reifyApp ist t [t']
2017− | t == reflm "ApplyTactic" = liftM (ApplyTactic . delab ist) (reifyTT t')
2018−reifyApp ist t [t']
2019− | t == reflm "Reflect" = liftM (Reflect . delab ist) (reifyTT t')
2020−reifyApp ist t [t']
2021− | t == reflm "ByReflection" = liftM (ByReflection . delab ist) (reifyTT t')
2022−reifyApp _ t [t']
2023− | t == reflm "Fill" = liftM (Fill . PQuote) (reifyRaw t')
2024−reifyApp ist t [t']
2025− | t == reflm "Exact" = liftM (Exact . delab ist) (reifyTT t')
2026−reifyApp ist t [x]
2027− | t == reflm "Focus" = liftM Focus (reifyTTName x)
2028−reifyApp ist t [t']
2029− | t == reflm "Rewrite" = liftM (Rewrite . delab ist) (reifyTT t')
2030−reifyApp ist t [n, t']
2031− | t == reflm "LetTac" = do n' <- reifyTTName n
2032− t'' <- reifyTT t'
2033− return $ LetTac n' (delab ist t')
2034−reifyApp ist t [n, tt', t']
2035− | t == reflm "LetTacTy" = do n' <- reifyTTName n
2036− tt'' <- reifyTT tt'
2037− t'' <- reifyTT t'
2038− return $ LetTacTy n' (delab ist tt'') (delab ist t'')
2039−reifyApp ist t [errs]
2040− | t == reflm "Fail" = fmap TFail (reifyReportParts errs)
2041−reifyApp _ f args = fail ("Unknown tactic " ++ show (f, args)) -- shouldn't happen
2042−
2043−reifyReportParts :: Term -> ElabD [ErrorReportPart]
2044−reifyReportParts errs =
2045− case unList errs of
2046− Nothing -> fail "Failed to reify errors"
2047− Just errs' ->
2048− let parts = mapM reifyReportPart errs' in
2049− case parts of
2050− Left err -> fail $ "Couldn't reify \"Fail\" tactic - " ++ show err
2051− Right errs'' ->
2052− return errs''
2053−
2054−-- | Reify terms from their reflected representation
2055−reifyTT :: Term -> ElabD Term
2056−reifyTT t@(App _ _ _)
2057− | (P _ f _, args) <- unApply t = reifyTTApp f args
2058−reifyTT t@(P _ n _)
2059− | n == reflm "Erased" = return $ Erased
2060−reifyTT t@(P _ n _)
2061− | n == reflm "Impossible" = return $ Impossible
2062−reifyTT t = fail ("Unknown reflection term: " ++ show t)
2063−
2064−reifyTTApp :: Name -> [Term] -> ElabD Term
2065−reifyTTApp t [nt, n, x]
2066− | t == reflm "P" = do nt' <- reifyTTNameType nt
2067− n' <- reifyTTName n
2068− x' <- reifyTT x
2069− return $ P nt' n' x'
2070−reifyTTApp t [Constant (I i)]
2071− | t == reflm "V" = return $ V i
2072−reifyTTApp t [n, b, x]
2073− | t == reflm "Bind" = do n' <- reifyTTName n
2074− b' <- reifyTTBinder reifyTT (reflm "TT") b
2075− x' <- reifyTT x
2076− return $ Bind n' b' x'
2077−reifyTTApp t [f, x]
2078− | t == reflm "App" = do f' <- reifyTT f
2079− x' <- reifyTT x
2080− return $ App Complete f' x'
2081−reifyTTApp t [c]
2082− | t == reflm "TConst" = liftM Constant (reifyTTConst c)
2083−reifyTTApp t [t', Constant (I i)]
2084− | t == reflm "Proj" = do t'' <- reifyTT t'
2085− return $ Proj t'' i
2086−reifyTTApp t [tt]
2087− | t == reflm "TType" = liftM TType (reifyTTUExp tt)
2088−reifyTTApp t args = fail ("Unknown reflection term: " ++ show (t, args))
2089−
2090−-- | Reify raw terms from their reflected representation
2091−reifyRaw :: Term -> ElabD Raw
2092−reifyRaw t@(App _ _ _)
2093− | (P _ f _, args) <- unApply t = reifyRawApp f args
2094−reifyRaw t@(P _ n _)
2095− | n == reflm "RType" = return $ RType
2096−reifyRaw t = fail ("Unknown reflection raw term in reifyRaw: " ++ show t)
2097−
2098−reifyRawApp :: Name -> [Term] -> ElabD Raw
2099−reifyRawApp t [n]
2100− | t == reflm "Var" = liftM Var (reifyTTName n)
2101−reifyRawApp t [n, b, x]
2102− | t == reflm "RBind" = do n' <- reifyTTName n
2103− b' <- reifyTTBinder reifyRaw (reflm "Raw") b
2104− x' <- reifyRaw x
2105− return $ RBind n' b' x'
2106−reifyRawApp t [f, x]
2107− | t == reflm "RApp" = liftM2 RApp (reifyRaw f) (reifyRaw x)
2108−reifyRawApp t [t']
2109− | t == reflm "RForce" = liftM RForce (reifyRaw t')
2110−reifyRawApp t [c]
2111− | t == reflm "RConstant" = liftM RConstant (reifyTTConst c)
2112−reifyRawApp t args = fail ("Unknown reflection raw term in reifyRawApp: " ++ show (t, args))
2113−
2114−reifyTTName :: Term -> ElabD Name
2115−reifyTTName t
2116− | (P _ f _, args) <- unApply t = reifyTTNameApp f args
2117−reifyTTName t = fail ("Unknown reflection term name: " ++ show t)
2118−
2119−reifyTTNameApp :: Name -> [Term] -> ElabD Name
2120−reifyTTNameApp t [Constant (Str n)]
2121− | t == reflm "UN" = return $ sUN n
2122−reifyTTNameApp t [n, ns]
2123− | t == reflm "NS" = do n' <- reifyTTName n
2124− ns' <- reifyTTNamespace ns
2125− return $ sNS n' ns'
2126−reifyTTNameApp t [Constant (I i), Constant (Str n)]
2127− | t == reflm "MN" = return $ sMN i n
2128−reifyTTNameApp t []
2129− | t == reflm "NErased" = return NErased
2130−reifyTTNameApp t args = fail ("Unknown reflection term name: " ++ show (t, args))
2131−
2132−reifyTTNamespace :: Term -> ElabD [String]
2133−reifyTTNamespace t@(App _ _ _)
2134− = case unApply t of
2135− (P _ f _, [Constant StrType])
2136− | f == sNS (sUN "Nil") ["List", "Prelude"] -> return []
2137− (P _ f _, [Constant StrType, Constant (Str n), ns])
2138− | f == sNS (sUN "::") ["List", "Prelude"] -> liftM (n:) (reifyTTNamespace ns)
2139− _ -> fail ("Unknown reflection namespace arg: " ++ show t)
2140−reifyTTNamespace t = fail ("Unknown reflection namespace arg: " ++ show t)
2141−
2142−reifyTTNameType :: Term -> ElabD NameType
2143−reifyTTNameType t@(P _ n _) | n == reflm "Bound" = return $ Bound
2144−reifyTTNameType t@(P _ n _) | n == reflm "Ref" = return $ Ref
2145−reifyTTNameType t@(App _ _ _)
2146− = case unApply t of
2147− (P _ f _, [Constant (I tag), Constant (I num)])
2148− | f == reflm "DCon" -> return $ DCon tag num False -- FIXME: Uniqueness!
2149− | f == reflm "TCon" -> return $ TCon tag num
2150− _ -> fail ("Unknown reflection name type: " ++ show t)
2151−reifyTTNameType t = fail ("Unknown reflection name type: " ++ show t)
2152−
2153−reifyTTBinder :: (Term -> ElabD a) -> Name -> Term -> ElabD (Binder a)
2154−reifyTTBinder reificator binderType t@(App _ _ _)
2155− = case unApply t of
2156− (P _ f _, bt:args) | forget bt == Var binderType
2157− -> reifyTTBinderApp reificator f args
2158− _ -> fail ("Mismatching binder reflection: " ++ show t)
2159−reifyTTBinder _ _ t = fail ("Unknown reflection binder: " ++ show t)
2160−
2161−reifyTTBinderApp :: (Term -> ElabD a) -> Name -> [Term] -> ElabD (Binder a)
2162−reifyTTBinderApp reif f [t]
2163− | f == reflm "Lam" = liftM Lam (reif t)
2164−reifyTTBinderApp reif f [t, k]
2165− | f == reflm "Pi" = liftM2 (Pi Nothing) (reif t) (reif k)
2166−reifyTTBinderApp reif f [x, y]
2167− | f == reflm "Let" = liftM2 Let (reif x) (reif y)
2168−reifyTTBinderApp reif f [x, y]
2169− | f == reflm "NLet" = liftM2 NLet (reif x) (reif y)
2170−reifyTTBinderApp reif f [t]
2171− | f == reflm "Hole" = liftM Hole (reif t)
2172−reifyTTBinderApp reif f [t]
2173− | f == reflm "GHole" = liftM (GHole 0) (reif t)
2174−reifyTTBinderApp reif f [x, y]
2175− | f == reflm "Guess" = liftM2 Guess (reif x) (reif y)
2176−reifyTTBinderApp reif f [t]
2177− | f == reflm "PVar" = liftM PVar (reif t)
2178−reifyTTBinderApp reif f [t]
2179− | f == reflm "PVTy" = liftM PVTy (reif t)
2180−reifyTTBinderApp _ f args = fail ("Unknown reflection binder: " ++ show (f, args))
2181−
2182−reifyTTConst :: Term -> ElabD Const
2183−reifyTTConst (P _ n _) | n == reflm "StrType" = return $ StrType
2184−reifyTTConst (P _ n _) | n == reflm "VoidType" = return $ VoidType
2185−reifyTTConst (P _ n _) | n == reflm "Forgot" = return $ Forgot
2186−reifyTTConst t@(App _ _ _)
2187− | (P _ f _, [arg]) <- unApply t = reifyTTConstApp f arg
2188−reifyTTConst t = fail ("Unknown reflection constant: " ++ show t)
2189−
2190−reifyTTConstApp :: Name -> Term -> ElabD Const
2191−reifyTTConstApp f aty
2192− | f == reflm "AType" = fmap AType (reifyArithTy aty)
2193−reifyTTConstApp f (Constant c@(I _))
2194− | f == reflm "I" = return $ c
2195−reifyTTConstApp f (Constant c@(BI _))
2196− | f == reflm "BI" = return $ c
2197−reifyTTConstApp f (Constant c@(Fl _))
2198− | f == reflm "Fl" = return $ c
2199−reifyTTConstApp f (Constant c@(I _))
2200− | f == reflm "Ch" = return $ c
2201−reifyTTConstApp f (Constant c@(Str _))
2202− | f == reflm "Str" = return $ c
2203−reifyTTConstApp f (Constant c@(B8 _))
2204− | f == reflm "B8" = return $ c
2205−reifyTTConstApp f (Constant c@(B16 _))
2206− | f == reflm "B16" = return $ c
2207−reifyTTConstApp f (Constant c@(B32 _))
2208− | f == reflm "B32" = return $ c
2209−reifyTTConstApp f (Constant c@(B64 _))
2210− | f == reflm "B64" = return $ c
2211−reifyTTConstApp f arg = fail ("Unknown reflection constant: " ++ show (f, arg))
2212−
2213−reifyArithTy :: Term -> ElabD ArithTy
2214−reifyArithTy (App _ (P _ n _) intTy) | n == reflm "ATInt" = fmap ATInt (reifyIntTy intTy)
2215−reifyArithTy (P _ n _) | n == reflm "ATFloat" = return ATFloat
2216−reifyArithTy x = fail ("Couldn't reify reflected ArithTy: " ++ show x)
2217−
2218−reifyNativeTy :: Term -> ElabD NativeTy
2219−reifyNativeTy (P _ n _) | n == reflm "IT8" = return IT8
2220−reifyNativeTy (P _ n _) | n == reflm "IT8" = return IT8
2221−reifyNativeTy (P _ n _) | n == reflm "IT8" = return IT8
2222−reifyNativeTy (P _ n _) | n == reflm "IT8" = return IT8
2223−reifyNativeTy x = fail $ "Couldn't reify reflected NativeTy " ++ show x
2224−
2225−reifyIntTy :: Term -> ElabD IntTy
2226−reifyIntTy (App _ (P _ n _) nt) | n == reflm "ITFixed" = fmap ITFixed (reifyNativeTy nt)
2227−reifyIntTy (P _ n _) | n == reflm "ITNative" = return ITNative
2228−reifyIntTy (P _ n _) | n == reflm "ITBig" = return ITBig
2229−reifyIntTy (P _ n _) | n == reflm "ITChar" = return ITChar
2230−reifyIntTy tm = fail $ "The term " ++ show tm ++ " is not a reflected IntTy"
2231−
2232−reifyTTUExp :: Term -> ElabD UExp
2233−reifyTTUExp t@(App _ _ _)
2234− = case unApply t of
2235− (P _ f _, [Constant (I i)]) | f == reflm "UVar" -> return $ UVar i
2236− (P _ f _, [Constant (I i)]) | f == reflm "UVal" -> return $ UVal i
2237− _ -> fail ("Unknown reflection type universe expression: " ++ show t)
2238−reifyTTUExp t = fail ("Unknown reflection type universe expression: " ++ show t)
2239−
2240−-- | Create a reflected call to a named function/constructor
2241−reflCall :: String -> [Raw] -> Raw
2242−reflCall funName args
2243− = raw_apply (Var (reflm funName)) args
2244−
2245−-- | Lift a term into its Language.Reflection.TT representation
2246−reflect :: Term -> Raw
2247−reflect = reflectTTQuote []
2248−
2249−-- | Lift a term into its Language.Reflection.Raw representation
2250−reflectRaw :: Raw -> Raw
2251−reflectRaw = reflectRawQuote []
2252−
2253−claimTT :: Name -> ElabD Name
2254−claimTT n = do n' <- getNameFrom n
2255− claim n' (Var (sNS (sUN "TT") ["Reflection", "Language"]))
2256− return n'
2257−
2258−-- | Convert a reflected term to a more suitable form for pattern-matching.
2259−-- In particular, the less-interesting bits are elaborated to _ patterns. This
2260−-- happens to NameTypes, universe levels, names that are bound but not used,
2261−-- and the type annotation field of the P constructor.
2262−reflectTTQuotePattern :: [Name] -> Term -> ElabD ()
2263−reflectTTQuotePattern unq (P _ n _)
2264− | n `elem` unq = -- the unquoted names have been claimed as TT already - just use them
2265− do fill (Var n) ; solve
2266− | otherwise =
2267− do tyannot <- claimTT (sMN 0 "pTyAnnot")
2268− movelast tyannot -- use a _ pattern here
2269− nt <- getNameFrom (sMN 0 "nt")
2270− claim nt (Var (reflm "NameType"))
2271− movelast nt -- use a _ pattern here
2272− n' <- getNameFrom (sMN 0 "n")
2273− claim n' (Var (reflm "TTName"))
2274− fill $ reflCall "P" [Var nt, Var n', Var tyannot]
2275− solve
2276− focus n'; reflectNameQuotePattern n
2277−reflectTTQuotePattern unq (V n)
2278− = do fill $ reflCall "V" [RConstant (I n)]
2279− solve
2280−reflectTTQuotePattern unq (Bind n b x)
2281− = do x' <- claimTT (sMN 0 "sc")
2282− movelast x'
2283− b' <- getNameFrom (sMN 0 "binder")
2284− claim b' (RApp (Var (sNS (sUN "Binder") ["Reflection", "Language"]))
2285− (Var (sNS (sUN "TT") ["Reflection", "Language"])))
2286− if n `elem` freeNames x
2287− then do fill $ reflCall "Bind"
2288− [reflectName n,
2289− Var b',
2290− Var x']
2291− solve
2292− else do any <- getNameFrom (sMN 0 "anyName")
2293− claim any (Var (reflm "TTName"))
2294− movelast any
2295− fill $ reflCall "Bind"
2296− [Var any,
2297− Var b',
2298− Var x']
2299− solve
2300− focus x'; reflectTTQuotePattern unq x
2301− focus b'; reflectBinderQuotePattern reflectTTQuotePattern unq b
2302−reflectTTQuotePattern unq (App _ f x)
2303− = do f' <- claimTT (sMN 0 "f"); movelast f'
2304− x' <- claimTT (sMN 0 "x"); movelast x'
2305− fill $ reflCall "App" [Var f', Var x']
2306− solve
2307− focus f'; reflectTTQuotePattern unq f
2308− focus x'; reflectTTQuotePattern unq x
2309−reflectTTQuotePattern unq (Constant c)
2310− = do fill $ reflCall "TConst" [reflectConstant c]
2311− solve
2312−reflectTTQuotePattern unq (Proj t i)
2313− = do t' <- claimTT (sMN 0 "t"); movelast t'
2314− fill $ reflCall "Proj" [Var t', RConstant (I i)]
2315− solve
2316− focus t'; reflectTTQuotePattern unq t
2317−reflectTTQuotePattern unq (Erased)
2318− = do erased <- claimTT (sMN 0 "erased")
2319− movelast erased
2320− fill $ (Var erased)
2321− solve
2322−reflectTTQuotePattern unq (Impossible)
2323− = do fill $ Var (reflm "Impossible")
2324− solve
2325−reflectTTQuotePattern unq (TType exp)
2326− = do ue <- getNameFrom (sMN 0 "uexp")
2327− claim ue (Var (sNS (sUN "TTUExp") ["Reflection", "Language"]))
2328− movelast ue
2329− fill $ reflCall "TType" [Var ue]
2330− solve
2331−reflectTTQuotePattern unq (UType u)
2332− = do uH <- getNameFrom (sMN 0 "someUniv")
2333− claim uH (Var (reflm "Universe"))
2334− movelast uH
2335− fill $ reflCall "UType" [Var uH]
2336− solve
2337− focus uH
2338− fill (Var (reflm (case u of
2339− NullType -> "NullType"
2340− UniqueType -> "UniqueType"
2341− AllTypes -> "AllTypes")))
2342− solve
2343−
2344−reflectRawQuotePattern :: [Name] -> Raw -> ElabD ()
2345−reflectRawQuotePattern unq (Var n)
2346− -- the unquoted names already have types, just use them
2347− | n `elem` unq = do fill (Var n); solve
2348− | otherwise = do fill (reflCall "Var" [reflectName n]); solve
2349−reflectRawQuotePattern unq (RBind n b sc) =
2350− do scH <- getNameFrom (sMN 0 "sc")
2351− claim scH (Var (reflm "Raw"))
2352− movelast scH
2353− bH <- getNameFrom (sMN 0 "binder")
2354− claim bH (RApp (Var (reflm "Binder"))
2355− (Var (reflm "Raw")))
2356− if n `elem` freeNamesR sc
2357− then do fill $ reflCall "RBind" [reflectName n,
2358− Var bH,
2359− Var scH]
2360− solve
2361− else do any <- getNameFrom (sMN 0 "anyName")
2362− claim any (Var (reflm "TTName"))
2363− movelast any
2364− fill $ reflCall "RBind" [Var any, Var bH, Var scH]
2365− solve
2366− focus scH; reflectRawQuotePattern unq sc
2367− focus bH; reflectBinderQuotePattern reflectRawQuotePattern unq b
2368− where freeNamesR (Var n) = [n]
2369− freeNamesR (RBind n (Let t v) body) = concat [freeNamesR v,
2370− freeNamesR body \\ [n],
2371− freeNamesR t]
2372− freeNamesR (RBind n b body) = freeNamesR (binderTy b) ++
2373− (freeNamesR body \\ [n])
2374− freeNamesR (RApp f x) = freeNamesR f ++ freeNamesR x
2375− freeNamesR RType = []
2376− freeNamesR (RUType _) = []
2377− freeNamesR (RForce r) = freeNamesR r
2378− freeNamesR (RConstant _) = []
2379−reflectRawQuotePattern unq (RApp f x) =
2380− do fH <- getNameFrom (sMN 0 "f")
2381− claim fH (Var (reflm "Raw"))
2382− movelast fH
2383− xH <- getNameFrom (sMN 0 "x")
2384− claim xH (Var (reflm "Raw"))
2385− movelast xH
2386− fill $ reflCall "RApp" [Var fH, Var xH]
2387− solve
2388− focus fH; reflectRawQuotePattern unq f
2389− focus xH; reflectRawQuotePattern unq x
2390−reflectRawQuotePattern unq RType =
2391− do fill (Var (reflm "RType"))
2392− solve
2393−reflectRawQuotePattern unq (RUType univ) =
2394− do uH <- getNameFrom (sMN 0 "universe")
2395− claim uH (Var (reflm "Universe"))
2396− movelast uH
2397− fill $ reflCall "RUType" [Var uH]
2398− solve
2399− focus uH; fill (reflectUniverse univ); solve
2400−reflectRawQuotePattern unq (RForce r) =
2401− do rH <- getNameFrom (sMN 0 "raw")
2402− claim rH (Var (reflm "Raw"))
2403− movelast rH
2404− fill $ reflCall "RForce" [Var rH]
2405− solve
2406− focus rH; reflectRawQuotePattern unq r
2407−reflectRawQuotePattern unq (RConstant c) =
2408− do cH <- getNameFrom (sMN 0 "const")
2409− claim cH (Var (reflm "Constant"))
2410− movelast cH
2411− fill (reflCall "RConstant" [Var cH]); solve
2412− focus cH
2413− fill (reflectConstant c); solve
2414−
2415−reflectBinderQuotePattern :: ([Name] -> a -> ElabD ()) -> [Name] -> Binder a -> ElabD ()
2416−reflectBinderQuotePattern q unq (Lam t)
2417− = do t' <- claimTT (sMN 0 "ty"); movelast t'
2418− fill $ reflCall "Lam" [Var (reflm "TT"), Var t']
2419− solve
2420− focus t'; q unq t
2421−reflectBinderQuotePattern q unq (Pi _ t k)
2422− = do t' <- claimTT (sMN 0 "ty") ; movelast t'
2423− k' <- claimTT (sMN 0 "k"); movelast k';
2424− fill $ reflCall "Pi" [Var (reflm "TT"), Var t', Var k']
2425− solve
2426− focus t'; q unq t
2427−reflectBinderQuotePattern q unq (Let x y)
2428− = do x' <- claimTT (sMN 0 "ty"); movelast x';
2429− y' <- claimTT (sMN 0 "v"); movelast y';
2430− fill $ reflCall "Let" [Var (reflm "TT"), Var x', Var y']
2431− solve
2432− focus x'; q unq x
2433− focus y'; q unq y
2434−reflectBinderQuotePattern q unq (NLet x y)
2435− = do x' <- claimTT (sMN 0 "ty"); movelast x'
2436− y' <- claimTT (sMN 0 "v"); movelast y'
2437− fill $ reflCall "NLet" [Var (reflm "TT"), Var x', Var y']
2438− solve
2439− focus x'; q unq x
2440− focus y'; q unq y
2441−reflectBinderQuotePattern q unq (Hole t)
2442− = do t' <- claimTT (sMN 0 "ty"); movelast t'
2443− fill $ reflCall "Hole" [Var (reflm "TT"), Var t']
2444− solve
2445− focus t'; q unq t
2446−reflectBinderQuotePattern q unq (GHole _ t)
2447− = do t' <- claimTT (sMN 0 "ty"); movelast t'
2448− fill $ reflCall "GHole" [Var (reflm "TT"), Var t']
2449− solve
2450− focus t'; q unq t
2451−reflectBinderQuotePattern q unq (Guess x y)
2452− = do x' <- claimTT (sMN 0 "ty"); movelast x'
2453− y' <- claimTT (sMN 0 "v"); movelast y'
2454− fill $ reflCall "Guess" [Var (reflm "TT"), Var x', Var y']
2455− solve
2456− focus x'; q unq x
2457− focus y'; q unq y
2458−reflectBinderQuotePattern q unq (PVar t)
2459− = do t' <- claimTT (sMN 0 "ty"); movelast t'
2460− fill $ reflCall "PVar" [Var (reflm "TT"), Var t']
2461− solve
2462− focus t'; q unq t
2463−reflectBinderQuotePattern q unq (PVTy t)
2464− = do t' <- claimTT (sMN 0 "ty"); movelast t'
2465− fill $ reflCall "PVTy" [Var (reflm "TT"), Var t']
2466− solve
2467− focus t'; q unq t
2468−
2469−reflectUniverse :: Universe -> Raw
2470−reflectUniverse u =
2471− (Var (reflm (case u of
2472− NullType -> "NullType"
2473− UniqueType -> "UniqueType"
2474− AllTypes -> "AllTypes")))
2475−
2476−-- | Create a reflected TT term, but leave refs to the provided name intact
2477−reflectTTQuote :: [Name] -> Term -> Raw
2478−reflectTTQuote unq (P nt n t)
2479− | n `elem` unq = Var n
2480− | otherwise = reflCall "P" [reflectNameType nt, reflectName n, reflectTTQuote unq t]
2481−reflectTTQuote unq (V n)
2482− = reflCall "V" [RConstant (I n)]
2483−reflectTTQuote unq (Bind n b x)
2484− = reflCall "Bind" [reflectName n, reflectBinderQuote reflectTTQuote (reflm "TT") unq b, reflectTTQuote unq x]
2485−reflectTTQuote unq (App _ f x)
2486− = reflCall "App" [reflectTTQuote unq f, reflectTTQuote unq x]
2487−reflectTTQuote unq (Constant c)
2488− = reflCall "TConst" [reflectConstant c]
2489−reflectTTQuote unq (Proj t i)
2490− = reflCall "Proj" [reflectTTQuote unq t, RConstant (I i)]
2491−reflectTTQuote unq (Erased) = Var (reflm "Erased")
2492−reflectTTQuote unq (Impossible) = Var (reflm "Impossible")
2493−reflectTTQuote unq (TType exp) = reflCall "TType" [reflectUExp exp]
2494−reflectTTQuote unq (UType u) = reflCall "UType" [reflectUniverse u]
2495−
2496−reflectRawQuote :: [Name] -> Raw -> Raw
2497−reflectRawQuote unq (Var n)
2498− | n `elem` unq = Var n
2499− | otherwise = reflCall "Var" [reflectName n]
2500−reflectRawQuote unq (RBind n b r) =
2501− reflCall "RBind" [reflectName n, reflectBinderQuote reflectRawQuote (reflm "Raw") unq b, reflectRawQuote unq r]
2502−reflectRawQuote unq (RApp f x) =
2503− reflCall "RApp" [reflectRawQuote unq f, reflectRawQuote unq x]
2504−reflectRawQuote unq RType = Var (reflm "RType")
2505−reflectRawQuote unq (RUType u) =
2506− reflCall "RUType" [reflectUniverse u]
2507−reflectRawQuote unq (RForce r) = reflCall "RForce" [reflectRawQuote unq r]
2508−reflectRawQuote unq (RConstant cst) = reflCall "RConstant" [reflectConstant cst]
2509−
2510−reflectNameType :: NameType -> Raw
2511−reflectNameType (Bound) = Var (reflm "Bound")
2512−reflectNameType (Ref) = Var (reflm "Ref")
2513−reflectNameType (DCon x y _)
2514− = reflCall "DCon" [RConstant (I x), RConstant (I y)] -- FIXME: Uniqueness!
2515−reflectNameType (TCon x y)
2516− = reflCall "TCon" [RConstant (I x), RConstant (I y)]
2517−
2518−reflectName :: Name -> Raw
2519−reflectName (UN s)
2520− = reflCall "UN" [RConstant (Str (str s))]
2521−reflectName (NS n ns)
2522− = reflCall "NS" [ reflectName n
2523− , foldr (\ n s ->
2524− raw_apply ( Var $ sNS (sUN "::") ["List", "Prelude"] )
2525− [ RConstant StrType, RConstant (Str n), s ])
2526− ( raw_apply ( Var $ sNS (sUN "Nil") ["List", "Prelude"] )
2527− [ RConstant StrType ])
2528− (map str ns)
2529− ]
2530−reflectName (MN i n)
2531− = reflCall "MN" [RConstant (I i), RConstant (Str (str n))]
2532−reflectName (NErased) = Var (reflm "NErased")
2533−reflectName n = Var (reflm "NErased") -- special name, not yet implemented
2534−
2535−-- | Elaborate a name to a pattern. This means that NS and UN will be intact.
2536−-- MNs corresponding to will care about the string but not the number. All
2537−-- others become _.
2538−reflectNameQuotePattern :: Name -> ElabD ()
2539−reflectNameQuotePattern n@(UN s)
2540− = do fill $ reflectName n
2541− solve
2542−reflectNameQuotePattern n@(NS _ _)
2543− = do fill $ reflectName n
2544− solve
2545−reflectNameQuotePattern (MN _ n)
2546− = do i <- getNameFrom (sMN 0 "mnCounter")
2547− claim i (RConstant (AType (ATInt ITNative)))
2548− movelast i
2549− fill $ reflCall "MN" [Var i, RConstant (Str $ T.unpack n)]
2550− solve
2551−reflectNameQuotePattern _ -- for all other names, match any
2552− = do nameHole <- getNameFrom (sMN 0 "name")
2553− claim nameHole (Var (reflm "TTName"))
2554− movelast nameHole
2555− fill (Var nameHole)
2556− solve
2557−
2558−reflectBinder :: Binder Term -> Raw
2559−reflectBinder = reflectBinderQuote reflectTTQuote (reflm "TT") []
2560−
2561−reflectBinderQuote :: ([Name] -> a -> Raw) -> Name -> [Name] -> Binder a -> Raw
2562−reflectBinderQuote q ty unq (Lam t)
2563− = reflCall "Lam" [Var ty, q unq t]
2564−reflectBinderQuote q ty unq (Pi _ t k)
2565− = reflCall "Pi" [Var ty, q unq t, q unq k]
2566−reflectBinderQuote q ty unq (Let x y)
2567− = reflCall "Let" [Var ty, q unq x, q unq y]
2568−reflectBinderQuote q ty unq (NLet x y)
2569− = reflCall "NLet" [Var ty, q unq x, q unq y]
2570−reflectBinderQuote q ty unq (Hole t)
2571− = reflCall "Hole" [Var ty, q unq t]
2572−reflectBinderQuote q ty unq (GHole _ t)
2573− = reflCall "GHole" [Var ty, q unq t]
2574−reflectBinderQuote q ty unq (Guess x y)
2575− = reflCall "Guess" [Var ty, q unq x, q unq y]
2576−reflectBinderQuote q ty unq (PVar t)
2577− = reflCall "PVar" [Var ty, q unq t]
2578−reflectBinderQuote q ty unq (PVTy t)
2579− = reflCall "PVTy" [Var ty, q unq t]
2580−
2581−mkList :: Raw -> [Raw] -> Raw
2582−mkList ty [] = RApp (Var (sNS (sUN "Nil") ["List", "Prelude"])) ty
2583−mkList ty (x:xs) = RApp (RApp (RApp (Var (sNS (sUN "::") ["List", "Prelude"])) ty)
2584− x)
2585− (mkList ty xs)
2586−
2587−reflectConstant :: Const -> Raw
2588−reflectConstant c@(I _) = reflCall "I" [RConstant c]
2589−reflectConstant c@(BI _) = reflCall "BI" [RConstant c]
2590−reflectConstant c@(Fl _) = reflCall "Fl" [RConstant c]
2591−reflectConstant c@(Ch _) = reflCall "Ch" [RConstant c]
2592−reflectConstant c@(Str _) = reflCall "Str" [RConstant c]
2593−reflectConstant c@(B8 _) = reflCall "B8" [RConstant c]
2594−reflectConstant c@(B16 _) = reflCall "B16" [RConstant c]
2595−reflectConstant c@(B32 _) = reflCall "B32" [RConstant c]
2596−reflectConstant c@(B64 _) = reflCall "B64" [RConstant c]
2597−reflectConstant (AType (ATInt ITNative)) = reflCall "AType" [reflCall "ATInt" [Var (reflm "ITNative")]]
2598−reflectConstant (AType (ATInt ITBig)) = reflCall "AType" [reflCall "ATInt" [Var (reflm "ITBig")]]
2599−reflectConstant (AType ATFloat) = reflCall "AType" [Var (reflm "ATFloat")]
2600−reflectConstant (AType (ATInt ITChar)) = reflCall "AType" [reflCall "ATInt" [Var (reflm "ITChar")]]
2601−reflectConstant StrType = Var (reflm "StrType")
2602−reflectConstant (AType (ATInt (ITFixed IT8))) = reflCall "AType" [reflCall "ATInt" [reflCall "ITFixed" [Var (reflm "IT8")]]]
2603−reflectConstant (AType (ATInt (ITFixed IT16))) = reflCall "AType" [reflCall "ATInt" [reflCall "ITFixed" [Var (reflm "IT16")]]]
2604−reflectConstant (AType (ATInt (ITFixed IT32))) = reflCall "AType" [reflCall "ATInt" [reflCall "ITFixed" [Var (reflm "IT32")]]]
2605−reflectConstant (AType (ATInt (ITFixed IT64))) = reflCall "AType" [reflCall "ATInt" [reflCall "ITFixed" [Var (reflm "IT64")]]]
2606−reflectConstant VoidType = Var (reflm "VoidType")
2607−reflectConstant Forgot = Var (reflm "Forgot")
2608−reflectConstant WorldType = Var (reflm "WorldType")
2609−reflectConstant TheWorld = Var (reflm "TheWorld")
2610−
2611−reflectUExp :: UExp -> Raw
2612−reflectUExp (UVar i) = reflCall "UVar" [RConstant (I i)]
2613−reflectUExp (UVal i) = reflCall "UVal" [RConstant (I i)]
2614−
2615−-- | Reflect the environment of a proof into a List (TTName, Binder TT)
2616−reflectEnv :: Env -> Raw
2617−reflectEnv = foldr consToEnvList emptyEnvList
2618− where
2619− consToEnvList :: (Name, Binder Term) -> Raw -> Raw
2620− consToEnvList (n, b) l
2621− = raw_apply (Var (sNS (sUN "::") ["List", "Prelude"]))
2622− [ envTupleType
2623− , raw_apply (Var pairCon) [ (Var $ reflm "TTName")
2624− , (RApp (Var $ reflm "Binder")
2625− (Var $ reflm "TT"))
2626− , reflectName n
2627− , reflectBinder b
2628− ]
2629− , l
2630− ]
2631−
2632− emptyEnvList :: Raw
2633− emptyEnvList = raw_apply (Var (sNS (sUN "Nil") ["List", "Prelude"]))
2634− [envTupleType]
2635−
2636−-- | Reflect an error into the internal datatype of Idris -- TODO
2637−rawBool :: Bool -> Raw
2638−rawBool True = Var (sNS (sUN "True") ["Bool", "Prelude"])
2639−rawBool False = Var (sNS (sUN "False") ["Bool", "Prelude"])
2640−
2641−rawNil :: Raw -> Raw
2642−rawNil ty = raw_apply (Var (sNS (sUN "Nil") ["List", "Prelude"])) [ty]
2643−
2644−rawCons :: Raw -> Raw -> Raw -> Raw
2645−rawCons ty hd tl = raw_apply (Var (sNS (sUN "::") ["List", "Prelude"])) [ty, hd, tl]
2646−
2647−rawList :: Raw -> [Raw] -> Raw
2648−rawList ty = foldr (rawCons ty) (rawNil ty)
2649−
2650−rawPairTy :: Raw -> Raw -> Raw
2651−rawPairTy t1 t2 = raw_apply (Var pairTy) [t1, t2]
2652−
2653−rawPair :: (Raw, Raw) -> (Raw, Raw) -> Raw
2654−rawPair (a, b) (x, y) = raw_apply (Var pairCon) [a, b, x, y]
2655−
2656−reflectCtxt :: [(Name, Type)] -> Raw
2657−reflectCtxt ctxt = rawList (rawPairTy (Var $ reflm "TTName") (Var $ reflm "TT"))
2658− (map (\ (n, t) -> (rawPair (Var $ reflm "TTName", Var $ reflm "TT")
2659− (reflectName n, reflect t)))
2660− ctxt)
2661−
2662−reflectErr :: Err -> Raw
2663−reflectErr (Msg msg) = raw_apply (Var $ reflErrName "Msg") [RConstant (Str msg)]
2664−reflectErr (InternalMsg msg) = raw_apply (Var $ reflErrName "InternalMsg") [RConstant (Str msg)]
2665−reflectErr (CantUnify b (t1,_) (t2,_) e ctxt i) =
2666− raw_apply (Var $ reflErrName "CantUnify")
2667− [ rawBool b
2668− , reflect t1
2669− , reflect t2
2670− , reflectErr e
2671− , reflectCtxt ctxt
2672− , RConstant (I i)]
2673−reflectErr (InfiniteUnify n tm ctxt) =
2674− raw_apply (Var $ reflErrName "InfiniteUnify")
2675− [ reflectName n
2676− , reflect tm
2677− , reflectCtxt ctxt
2678− ]
2679−reflectErr (CantConvert t t' ctxt) =
2680− raw_apply (Var $ reflErrName "CantConvert")
2681− [ reflect t
2682− , reflect t'
2683− , reflectCtxt ctxt
2684− ]
2685−reflectErr (CantSolveGoal t ctxt) =
2686− raw_apply (Var $ reflErrName "CantSolveGoal")
2687− [ reflect t
2688− , reflectCtxt ctxt
2689− ]
2690−reflectErr (UnifyScope n n' t ctxt) =
2691− raw_apply (Var $ reflErrName "UnifyScope")
2692− [ reflectName n
2693− , reflectName n'
2694− , reflect t
2695− , reflectCtxt ctxt
2696− ]
2697−reflectErr (CantInferType str) =
2698− raw_apply (Var $ reflErrName "CantInferType") [RConstant (Str str)]
2699−reflectErr (NonFunctionType t t') =
2700− raw_apply (Var $ reflErrName "NonFunctionType") [reflect t, reflect t']
2701−reflectErr (NotEquality t t') =
2702− raw_apply (Var $ reflErrName "NotEquality") [reflect t, reflect t']
2703−reflectErr (TooManyArguments n) = raw_apply (Var $ reflErrName "TooManyArguments") [reflectName n]
2704−reflectErr (CantIntroduce t) = raw_apply (Var $ reflErrName "CantIntroduce") [reflect t]
2705−reflectErr (NoSuchVariable n) = raw_apply (Var $ reflErrName "NoSuchVariable") [reflectName n]
2706−reflectErr (WithFnType t) = raw_apply (Var $ reflErrName "WithFnType") [reflect t]
2707−reflectErr (CantMatch t) = raw_apply (Var $ reflErrName "CantMatch") [reflect t]
2708−reflectErr (NoTypeDecl n) = raw_apply (Var $ reflErrName "NoTypeDecl") [reflectName n]
2709−reflectErr (NotInjective t1 t2 t3) =
2710− raw_apply (Var $ reflErrName "NotInjective")
2711− [ reflect t1
2712− , reflect t2
2713− , reflect t3
2714− ]
2715−reflectErr (CantResolve _ t) = raw_apply (Var $ reflErrName "CantResolve") [reflect t]
2716−reflectErr (InvalidTCArg n t) = raw_apply (Var $ reflErrName "InvalidTCArg") [reflectName n, reflect t]
2717−reflectErr (CantResolveAlts ss) =
2718− raw_apply (Var $ reflErrName "CantResolveAlts")
2719− [rawList (Var $ reflm "TTName") (map reflectName ss)]
2720−reflectErr (IncompleteTerm t) = raw_apply (Var $ reflErrName "IncompleteTerm") [reflect t]
2721−reflectErr (NoEliminator str t)
2722− = raw_apply (Var $ reflErrName "NoEliminator") [RConstant (Str str),
2723− reflect t]
2724−reflectErr UniverseError = Var $ reflErrName "UniverseError"
2725−reflectErr ProgramLineComment = Var $ reflErrName "ProgramLineComment"
2726−reflectErr (Inaccessible n) = raw_apply (Var $ reflErrName "Inaccessible") [reflectName n]
2727−reflectErr (NonCollapsiblePostulate n) = raw_apply (Var $ reflErrName "NonCollabsiblePostulate") [reflectName n]
2728−reflectErr (AlreadyDefined n) = raw_apply (Var $ reflErrName "AlreadyDefined") [reflectName n]
2729−reflectErr (ProofSearchFail e) = raw_apply (Var $ reflErrName "ProofSearchFail") [reflectErr e]
2730−reflectErr (NoRewriting tm) = raw_apply (Var $ reflErrName "NoRewriting") [reflect tm]
2731−reflectErr (ProviderError str) =
2732− raw_apply (Var $ reflErrName "ProviderError") [RConstant (Str str)]
2733−reflectErr (LoadingFailed str err) =
2734− raw_apply (Var $ reflErrName "LoadingFailed") [RConstant (Str str)]
2735−reflectErr x = raw_apply (Var (sNS (sUN "Msg") ["Errors", "Reflection", "Language"])) [RConstant . Str $ "Default reflection: " ++ show x]
2736−
2737−-- | Reflect a file context
2738−reflectFC :: FC -> Raw
2739−reflectFC fc = raw_apply (Var (reflm "FileLoc"))
2740− [ RConstant (Str (fc_fname fc))
2741− , raw_apply (Var pairCon) $
2742− [intTy, intTy] ++
2743− map (RConstant . I)
2744− [ fst (fc_start fc)
2745− , snd (fc_start fc)
2746− ]
2747− , raw_apply (Var pairCon) $
2748− [intTy, intTy] ++
2749− map (RConstant . I)
2750− [ fst (fc_end fc)
2751− , snd (fc_end fc)
2752− ]
2753− ]
2754− where intTy = RConstant (AType (ATInt ITNative))
2755−
27562029elaboratingArgErr :: [(Name, Name)] -> Err -> Err
27572030elaboratingArgErr [] err = err
27582031elaboratingArgErr ((f,x):during) err = fromMaybe err (rewrite err)
@@ -2823,70 +2096,4 @@ withErrorReflection x = idrisCatch x (\ e -> handle e >>= ierror)
28232096 [] -> e
28242097 parts -> ReflectionError errorparts e
28252098
2826−fromTTMaybe :: Term -> Maybe Term -- WARNING: Assumes the term has type Maybe a
2827−fromTTMaybe (App _ (App _ (P (DCon _ _ _) (NS (UN just) _) _) ty) tm)
2828− | just == txt "Just" = Just tm
2829−fromTTMaybe x = Nothing
2830−
2831−reflErrName :: String -> Name
2832−reflErrName n = sNS (sUN n) ["Errors", "Reflection", "Language"]
2833−
2834−-- | Attempt to reify a report part from TT to the internal
2835−-- representation. Not in Idris or ElabD monads because it should be usable
2836−-- from either.
2837−reifyReportPart :: Term -> Either Err ErrorReportPart
2838−reifyReportPart (App _ (P (DCon _ _ _) n _) (Constant (Str msg))) | n == reflm "TextPart" =
2839− Right (TextPart msg)
2840−reifyReportPart (App _ (P (DCon _ _ _) n _) ttn)
2841− | n == reflm "NamePart" =
2842− case runElab initEState (reifyTTName ttn) (initElaborator NErased initContext Erased) of
2843− Error e -> Left . InternalMsg $
2844− "could not reify name term " ++
2845− show ttn ++
2846− " when reflecting an error:" ++ show e
2847− OK (n', _)-> Right $ NamePart n'
2848−reifyReportPart (App _ (P (DCon _ _ _) n _) tm)
2849− | n == reflm "TermPart" =
2850− case runElab initEState (reifyTT tm) (initElaborator NErased initContext Erased) of
2851− Error e -> Left . InternalMsg $
2852− "could not reify reflected term " ++
2853− show tm ++
2854− " when reflecting an error:" ++ show e
2855− OK (tm', _) -> Right $ TermPart tm'
2856−reifyReportPart (App _ (P (DCon _ _ _) n _) tm)
2857− | n == reflm "SubReport" =
2858− case unList tm of
2859− Just xs -> do subParts <- mapM reifyReportPart xs
2860− Right (SubReport subParts)
2861− Nothing -> Left . InternalMsg $ "could not reify subreport " ++ show tm
2862−reifyReportPart x = Left . InternalMsg $ "could not reify " ++ show x
2863−
2864−reifyTyDecl :: Term -> ElabD RTyDecl
2865−reifyTyDecl (App _ (App _ (App _ (P (DCon _ _ _) n _) tyN) args) ret)
2866− | n == tacN "Declare" =
2867− do tyN' <- reifyTTName tyN
2868− args' <- case unList args of
2869− Nothing -> fail $ "Couldn't reify " ++ show args ++ " as an arglist."
2870− Just xs -> mapM reifyRArg xs
2871− ret' <- reifyRaw ret
2872− return $ RDeclare tyN' args' ret'
2873− where reifyRArg :: Term -> ElabD RArg
2874− reifyRArg (App _ (App _ (P (DCon _ _ _) n _) argN) argTy)
2875− | n == tacN "Explicit" = liftM2 RExplicit
2876− (reifyTTName argN)
2877− (reifyRaw argTy)
2878− | n == tacN "Implicit" = liftM2 RImplicit
2879− (reifyTTName argN)
2880− (reifyRaw argTy) | n == tacN "Constraint" = liftM2 RConstraint
2881− (reifyTTName argN)
2882− (reifyRaw argTy)
2883− reifyRArg aTm = fail $ "Couldn't reify " ++ show aTm ++ " as an RArg."
2884−reifyTyDecl tm = fail $ "Couldn't reify " ++ show tm ++ " as a type declaration."
2885−
2886−envTupleType :: Raw
2887−envTupleType
2888− = raw_apply (Var pairTy) [ (Var $ reflm "TTName")
2889− , (RApp (Var $ reflm "Binder") (Var $ reflm "TT"))
2890− ]
2891−
28922099solveAll = try (do solve; solveAll) (return ())