on master help sign in
Move quoters and unquoters to Idris.Reflection

This makes ElabTerm much smaller and easier to deal with.

commit367c037e4b9bd758337a4444ef7c2dbb4f0259f6 authorDavid Raymond Christiansen <david@davidchristiansen.dk> dateWed 15 Apr 2015, 11:07 (12 years ago) parent0ca1715
2 files changed+877−863
src/Idris/ElabTerm.hs−860 src/Idris/Reflection.hs+877−3
src/Idris/ElabTerm.hs−860
@@ -1589,8 +1589,6 @@ case_ ind autoSolve ist fn tm = do
15891589 else casetac (forget val)
15901590 when autoSolve solveAll
15911591
1592−tacN :: String -> Name
1593−tacN str = sNS (sUN str) ["Elab", "Reflection", "Language"]
15941592
15951593runTactical :: FC -> Env -> Term -> ElabD ()
15961594runTactical fc env tm = do tm' <- eval tm
@@ -2018,780 +2016,6 @@ runTac autoSolve ist perhapsFC fn tac
20182016 runReflected t = do t' <- reify ist t
20192017 runTac autoSolve ist perhapsFC fn t'
20202018
2021−-- | Prefix a name with the "Language.Reflection" namespace
2022−reflm :: String -> Name
2023−reflm n = sNS (sUN n) ["Reflection", "Language"]
2024−
2025−
2026−-- | Reify tactics from their reflected representation
2027−reify :: IState -> Term -> ElabD PTactic
2028−reify _ (P _ n _) | n == reflm "Intros" = return Intros
2029−reify _ (P _ n _) | n == reflm "Trivial" = return Trivial
2030−reify _ (P _ n _) | n == reflm "Instance" = return TCInstance
2031−reify _ (P _ n _) | n == reflm "Solve" = return Solve
2032−reify _ (P _ n _) | n == reflm "Compute" = return Compute
2033−reify _ (P _ n _) | n == reflm "Skip" = return Skip
2034−reify _ (P _ n _) | n == reflm "SourceFC" = return SourceFC
2035−reify _ (P _ n _) | n == reflm "Unfocus" = return Unfocus
2036−reify ist t@(App _ _)
2037− | (P _ f _, args) <- unApply t = reifyApp ist f args
2038−reify _ t = fail ("Unknown tactic " ++ show t)
2039−
2040−reifyApp :: IState -> Name -> [Term] -> ElabD PTactic
2041−reifyApp ist t [l, r] | t == reflm "Try" = liftM2 Try (reify ist l) (reify ist r)
2042−reifyApp _ t [Constant (I i)]
2043− | t == reflm "Search" = return (ProofSearch True True i Nothing [])
2044−reifyApp _ t [x]
2045− | t == reflm "Refine" = do n <- reifyTTName x
2046− return $ Refine n []
2047−reifyApp ist t [n, ty] | t == reflm "Claim" = do n' <- reifyTTName n
2048− goal <- reifyTT ty
2049− return $ Claim n' (delab ist goal)
2050−reifyApp ist t [l, r] | t == reflm "Seq" = liftM2 TSeq (reify ist l) (reify ist r)
2051−reifyApp ist t [Constant (Str n), x]
2052− | t == reflm "GoalType" = liftM (GoalType n) (reify ist x)
2053−reifyApp _ t [n] | t == reflm "Intro" = liftM (Intro . (:[])) (reifyTTName n)
2054−reifyApp ist t [t'] | t == reflm "Induction" = liftM (Induction . delab ist) (reifyTT t')
2055−reifyApp ist t [t'] | t == reflm "Case" = liftM (Induction . delab ist) (reifyTT t')
2056−reifyApp ist t [t']
2057− | t == reflm "ApplyTactic" = liftM (ApplyTactic . delab ist) (reifyTT t')
2058−reifyApp ist t [t']
2059− | t == reflm "Reflect" = liftM (Reflect . delab ist) (reifyTT t')
2060−reifyApp ist t [t']
2061− | t == reflm "ByReflection" = liftM (ByReflection . delab ist) (reifyTT t')
2062−reifyApp _ t [t']
2063− | t == reflm "Fill" = liftM (Fill . PQuote) (reifyRaw t')
2064−reifyApp ist t [t']
2065− | t == reflm "Exact" = liftM (Exact . delab ist) (reifyTT t')
2066−reifyApp ist t [x]
2067− | t == reflm "Focus" = liftM Focus (reifyTTName x)
2068−reifyApp ist t [t']
2069− | t == reflm "Rewrite" = liftM (Rewrite . delab ist) (reifyTT t')
2070−reifyApp ist t [n, t']
2071− | t == reflm "LetTac" = do n' <- reifyTTName n
2072− t'' <- reifyTT t'
2073− return $ LetTac n' (delab ist t')
2074−reifyApp ist t [n, tt', t']
2075− | t == reflm "LetTacTy" = do n' <- reifyTTName n
2076− tt'' <- reifyTT tt'
2077− t'' <- reifyTT t'
2078− return $ LetTacTy n' (delab ist tt'') (delab ist t'')
2079−reifyApp ist t [errs]
2080− | t == reflm "Fail" = fmap TFail (reifyReportParts errs)
2081−reifyApp _ f args = fail ("Unknown tactic " ++ show (f, args)) -- shouldn't happen
2082−
2083−reifyReportParts :: Term -> ElabD [ErrorReportPart]
2084−reifyReportParts errs =
2085− case unList errs of
2086− Nothing -> fail "Failed to reify errors"
2087− Just errs' ->
2088− let parts = mapM reifyReportPart errs' in
2089− case parts of
2090− Left err -> fail $ "Couldn't reify \"Fail\" tactic - " ++ show err
2091− Right errs'' ->
2092− return errs''
2093−
2094−-- | Reify terms from their reflected representation
2095−reifyTT :: Term -> ElabD Term
2096−reifyTT t@(App _ _)
2097− | (P _ f _, args) <- unApply t = reifyTTApp f args
2098−reifyTT t@(P _ n _)
2099− | n == reflm "Erased" = return $ Erased
2100−reifyTT t@(P _ n _)
2101− | n == reflm "Impossible" = return $ Impossible
2102−reifyTT t = fail ("Unknown reflection term: " ++ show t)
2103−
2104−reifyTTApp :: Name -> [Term] -> ElabD Term
2105−reifyTTApp t [nt, n, x]
2106− | t == reflm "P" = do nt' <- reifyTTNameType nt
2107− n' <- reifyTTName n
2108− x' <- reifyTT x
2109− return $ P nt' n' x'
2110−reifyTTApp t [Constant (I i)]
2111− | t == reflm "V" = return $ V i
2112−reifyTTApp t [n, b, x]
2113− | t == reflm "Bind" = do n' <- reifyTTName n
2114− b' <- reifyTTBinder reifyTT (reflm "TT") b
2115− x' <- reifyTT x
2116− return $ Bind n' b' x'
2117−reifyTTApp t [f, x]
2118− | t == reflm "App" = do f' <- reifyTT f
2119− x' <- reifyTT x
2120− return $ App f' x'
2121−reifyTTApp t [c]
2122− | t == reflm "TConst" = liftM Constant (reifyTTConst c)
2123−reifyTTApp t [t', Constant (I i)]
2124− | t == reflm "Proj" = do t'' <- reifyTT t'
2125− return $ Proj t'' i
2126−reifyTTApp t [tt]
2127− | t == reflm "TType" = liftM TType (reifyTTUExp tt)
2128−reifyTTApp t args = fail ("Unknown reflection term: " ++ show (t, args))
2129−
2130−-- | Reify raw terms from their reflected representation
2131−reifyRaw :: Term -> ElabD Raw
2132−reifyRaw t@(App _ _)
2133− | (P _ f _, args) <- unApply t = reifyRawApp f args
2134−reifyRaw t@(P _ n _)
2135− | n == reflm "RType" = return $ RType
2136−reifyRaw t = fail ("Unknown reflection raw term in reifyRaw: " ++ show t)
2137−
2138−reifyRawApp :: Name -> [Term] -> ElabD Raw
2139−reifyRawApp t [n]
2140− | t == reflm "Var" = liftM Var (reifyTTName n)
2141−reifyRawApp t [n, b, x]
2142− | t == reflm "RBind" = do n' <- reifyTTName n
2143− b' <- reifyTTBinder reifyRaw (reflm "Raw") b
2144− x' <- reifyRaw x
2145− return $ RBind n' b' x'
2146−reifyRawApp t [f, x]
2147− | t == reflm "RApp" = liftM2 RApp (reifyRaw f) (reifyRaw x)
2148−reifyRawApp t [t']
2149− | t == reflm "RForce" = liftM RForce (reifyRaw t')
2150−reifyRawApp t [c]
2151− | t == reflm "RConstant" = liftM RConstant (reifyTTConst c)
2152−reifyRawApp t args = fail ("Unknown reflection raw term in reifyRawApp: " ++ show (t, args))
2153−
2154−reifyTTName :: Term -> ElabD Name
2155−reifyTTName t
2156− | (P _ f _, args) <- unApply t = reifyTTNameApp f args
2157−reifyTTName t = fail ("Unknown reflection term name: " ++ show t)
2158−
2159−reifyTTNameApp :: Name -> [Term] -> ElabD Name
2160−reifyTTNameApp t [Constant (Str n)]
2161− | t == reflm "UN" = return $ sUN n
2162−reifyTTNameApp t [n, ns]
2163− | t == reflm "NS" = do n' <- reifyTTName n
2164− ns' <- reifyTTNamespace ns
2165− return $ sNS n' ns'
2166−reifyTTNameApp t [Constant (I i), Constant (Str n)]
2167− | t == reflm "MN" = return $ sMN i n
2168−reifyTTNameApp t []
2169− | t == reflm "NErased" = return NErased
2170−reifyTTNameApp t args = fail ("Unknown reflection term name: " ++ show (t, args))
2171−
2172−reifyTTNamespace :: Term -> ElabD [String]
2173−reifyTTNamespace t@(App _ _)
2174− = case unApply t of
2175− (P _ f _, [Constant StrType])
2176− | f == sNS (sUN "Nil") ["List", "Prelude"] -> return []
2177− (P _ f _, [Constant StrType, Constant (Str n), ns])
2178− | f == sNS (sUN "::") ["List", "Prelude"] -> liftM (n:) (reifyTTNamespace ns)
2179− _ -> fail ("Unknown reflection namespace arg: " ++ show t)
2180−reifyTTNamespace t = fail ("Unknown reflection namespace arg: " ++ show t)
2181−
2182−reifyTTNameType :: Term -> ElabD NameType
2183−reifyTTNameType t@(P _ n _) | n == reflm "Bound" = return $ Bound
2184−reifyTTNameType t@(P _ n _) | n == reflm "Ref" = return $ Ref
2185−reifyTTNameType t@(App _ _)
2186− = case unApply t of
2187− (P _ f _, [Constant (I tag), Constant (I num)])
2188− | f == reflm "DCon" -> return $ DCon tag num False -- FIXME: Uniqueness!
2189− | f == reflm "TCon" -> return $ TCon tag num
2190− _ -> fail ("Unknown reflection name type: " ++ show t)
2191−reifyTTNameType t = fail ("Unknown reflection name type: " ++ show t)
2192−
2193−reifyTTBinder :: (Term -> ElabD a) -> Name -> Term -> ElabD (Binder a)
2194−reifyTTBinder reificator binderType t@(App _ _)
2195− = case unApply t of
2196− (P _ f _, bt:args) | forget bt == Var binderType
2197− -> reifyTTBinderApp reificator f args
2198− _ -> fail ("Mismatching binder reflection: " ++ show t)
2199−reifyTTBinder _ _ t = fail ("Unknown reflection binder: " ++ show t)
2200−
2201−reifyTTBinderApp :: (Term -> ElabD a) -> Name -> [Term] -> ElabD (Binder a)
2202−reifyTTBinderApp reif f [t]
2203− | f == reflm "Lam" = liftM Lam (reif t)
2204−reifyTTBinderApp reif f [t, k]
2205− | f == reflm "Pi" = liftM2 (Pi Nothing) (reif t) (reif k)
2206−reifyTTBinderApp reif f [x, y]
2207− | f == reflm "Let" = liftM2 Let (reif x) (reif y)
2208−reifyTTBinderApp reif f [x, y]
2209− | f == reflm "NLet" = liftM2 NLet (reif x) (reif y)
2210−reifyTTBinderApp reif f [t]
2211− | f == reflm "Hole" = liftM Hole (reif t)
2212−reifyTTBinderApp reif f [t]
2213− | f == reflm "GHole" = liftM (GHole 0) (reif t)
2214−reifyTTBinderApp reif f [x, y]
2215− | f == reflm "Guess" = liftM2 Guess (reif x) (reif y)
2216−reifyTTBinderApp reif f [t]
2217− | f == reflm "PVar" = liftM PVar (reif t)
2218−reifyTTBinderApp reif f [t]
2219− | f == reflm "PVTy" = liftM PVTy (reif t)
2220−reifyTTBinderApp _ f args = fail ("Unknown reflection binder: " ++ show (f, args))
2221−
2222−reifyTTConst :: Term -> ElabD Const
2223−reifyTTConst (P _ n _) | n == reflm "StrType" = return $ StrType
2224−reifyTTConst (P _ n _) | n == reflm "VoidType" = return $ VoidType
2225−reifyTTConst (P _ n _) | n == reflm "Forgot" = return $ Forgot
2226−reifyTTConst t@(App _ _)
2227− | (P _ f _, [arg]) <- unApply t = reifyTTConstApp f arg
2228−reifyTTConst t = fail ("Unknown reflection constant: " ++ show t)
2229−
2230−reifyTTConstApp :: Name -> Term -> ElabD Const
2231−reifyTTConstApp f aty
2232− | f == reflm "AType" = fmap AType (reifyArithTy aty)
2233−reifyTTConstApp f (Constant c@(I _))
2234− | f == reflm "I" = return $ c
2235−reifyTTConstApp f (Constant c@(BI _))
2236− | f == reflm "BI" = return $ c
2237−reifyTTConstApp f (Constant c@(Fl _))
2238− | f == reflm "Fl" = return $ c
2239−reifyTTConstApp f (Constant c@(I _))
2240− | f == reflm "Ch" = return $ c
2241−reifyTTConstApp f (Constant c@(Str _))
2242− | f == reflm "Str" = return $ c
2243−reifyTTConstApp f (Constant c@(B8 _))
2244− | f == reflm "B8" = return $ c
2245−reifyTTConstApp f (Constant c@(B16 _))
2246− | f == reflm "B16" = return $ c
2247−reifyTTConstApp f (Constant c@(B32 _))
2248− | f == reflm "B32" = return $ c
2249−reifyTTConstApp f (Constant c@(B64 _))
2250− | f == reflm "B64" = return $ c
2251−reifyTTConstApp f arg = fail ("Unknown reflection constant: " ++ show (f, arg))
2252−
2253−reifyArithTy :: Term -> ElabD ArithTy
2254−reifyArithTy (App (P _ n _) intTy) | n == reflm "ATInt" = fmap ATInt (reifyIntTy intTy)
2255−reifyArithTy (P _ n _) | n == reflm "ATFloat" = return ATFloat
2256−reifyArithTy x = fail ("Couldn't reify reflected ArithTy: " ++ show x)
2257−
2258−reifyNativeTy :: Term -> ElabD NativeTy
2259−reifyNativeTy (P _ n _) | n == reflm "IT8" = return IT8
2260−reifyNativeTy (P _ n _) | n == reflm "IT8" = return IT8
2261−reifyNativeTy (P _ n _) | n == reflm "IT8" = return IT8
2262−reifyNativeTy (P _ n _) | n == reflm "IT8" = return IT8
2263−reifyNativeTy x = fail $ "Couldn't reify reflected NativeTy " ++ show x
2264−
2265−reifyIntTy :: Term -> ElabD IntTy
2266−reifyIntTy (App (P _ n _) nt) | n == reflm "ITFixed" = fmap ITFixed (reifyNativeTy nt)
2267−reifyIntTy (P _ n _) | n == reflm "ITNative" = return ITNative
2268−reifyIntTy (P _ n _) | n == reflm "ITBig" = return ITBig
2269−reifyIntTy (P _ n _) | n == reflm "ITChar" = return ITChar
2270−reifyIntTy tm = fail $ "The term " ++ show tm ++ " is not a reflected IntTy"
2271−
2272−reifyTTUExp :: Term -> ElabD UExp
2273−reifyTTUExp t@(App _ _)
2274− = case unApply t of
2275− (P _ f _, [Constant (I i)]) | f == reflm "UVar" -> return $ UVar i
2276− (P _ f _, [Constant (I i)]) | f == reflm "UVal" -> return $ UVal i
2277− _ -> fail ("Unknown reflection type universe expression: " ++ show t)
2278−reifyTTUExp t = fail ("Unknown reflection type universe expression: " ++ show t)
2279−
2280−-- | Create a reflected call to a named function/constructor
2281−reflCall :: String -> [Raw] -> Raw
2282−reflCall funName args
2283− = raw_apply (Var (reflm funName)) args
2284−
2285−-- | Lift a term into its Language.Reflection.TT representation
2286−reflect :: Term -> Raw
2287−reflect = reflectTTQuote []
2288−
2289−-- | Lift a term into its Language.Reflection.Raw representation
2290−reflectRaw :: Raw -> Raw
2291−reflectRaw = reflectRawQuote []
2292−
2293−claimTT :: Name -> ElabD Name
2294−claimTT n = do n' <- getNameFrom n
2295− claim n' (Var (sNS (sUN "TT") ["Reflection", "Language"]))
2296− return n'
2297−
2298−-- | Convert a reflected term to a more suitable form for pattern-matching.
2299−-- In particular, the less-interesting bits are elaborated to _ patterns. This
2300−-- happens to NameTypes, universe levels, names that are bound but not used,
2301−-- and the type annotation field of the P constructor.
2302−reflectTTQuotePattern :: [Name] -> Term -> ElabD ()
2303−reflectTTQuotePattern unq (P _ n _)
2304− | n `elem` unq = -- the unquoted names have been claimed as TT already - just use them
2305− do fill (Var n) ; solve
2306− | otherwise =
2307− do tyannot <- claimTT (sMN 0 "pTyAnnot")
2308− movelast tyannot -- use a _ pattern here
2309− nt <- getNameFrom (sMN 0 "nt")
2310− claim nt (Var (reflm "NameType"))
2311− movelast nt -- use a _ pattern here
2312− n' <- getNameFrom (sMN 0 "n")
2313− claim n' (Var (reflm "TTName"))
2314− fill $ reflCall "P" [Var nt, Var n', Var tyannot]
2315− solve
2316− focus n'; reflectNameQuotePattern n
2317−reflectTTQuotePattern unq (V n)
2318− = do fill $ reflCall "V" [RConstant (I n)]
2319− solve
2320−reflectTTQuotePattern unq (Bind n b x)
2321− = do x' <- claimTT (sMN 0 "sc")
2322− movelast x'
2323− b' <- getNameFrom (sMN 0 "binder")
2324− claim b' (RApp (Var (sNS (sUN "Binder") ["Reflection", "Language"]))
2325− (Var (sNS (sUN "TT") ["Reflection", "Language"])))
2326− if n `elem` freeNames x
2327− then do fill $ reflCall "Bind"
2328− [reflectName n,
2329− Var b',
2330− Var x']
2331− solve
2332− else do any <- getNameFrom (sMN 0 "anyName")
2333− claim any (Var (reflm "TTName"))
2334− movelast any
2335− fill $ reflCall "Bind"
2336− [Var any,
2337− Var b',
2338− Var x']
2339− solve
2340− focus x'; reflectTTQuotePattern unq x
2341− focus b'; reflectBinderQuotePattern reflectTTQuotePattern unq b
2342−reflectTTQuotePattern unq (App f x)
2343− = do f' <- claimTT (sMN 0 "f"); movelast f'
2344− x' <- claimTT (sMN 0 "x"); movelast x'
2345− fill $ reflCall "App" [Var f', Var x']
2346− solve
2347− focus f'; reflectTTQuotePattern unq f
2348− focus x'; reflectTTQuotePattern unq x
2349−reflectTTQuotePattern unq (Constant c)
2350− = do fill $ reflCall "TConst" [reflectConstant c]
2351− solve
2352−reflectTTQuotePattern unq (Proj t i)
2353− = do t' <- claimTT (sMN 0 "t"); movelast t'
2354− fill $ reflCall "Proj" [Var t', RConstant (I i)]
2355− solve
2356− focus t'; reflectTTQuotePattern unq t
2357−reflectTTQuotePattern unq (Erased)
2358− = do erased <- claimTT (sMN 0 "erased")
2359− movelast erased
2360− fill $ (Var erased)
2361− solve
2362−reflectTTQuotePattern unq (Impossible)
2363− = do fill $ Var (reflm "Impossible")
2364− solve
2365−reflectTTQuotePattern unq (TType exp)
2366− = do ue <- getNameFrom (sMN 0 "uexp")
2367− claim ue (Var (sNS (sUN "TTUExp") ["Reflection", "Language"]))
2368− movelast ue
2369− fill $ reflCall "TType" [Var ue]
2370− solve
2371−reflectTTQuotePattern unq (UType u)
2372− = do uH <- getNameFrom (sMN 0 "someUniv")
2373− claim uH (Var (reflm "Universe"))
2374− movelast uH
2375− fill $ reflCall "UType" [Var uH]
2376− solve
2377− focus uH
2378− fill (Var (reflm (case u of
2379− NullType -> "NullType"
2380− UniqueType -> "UniqueType"
2381− AllTypes -> "AllTypes")))
2382− solve
2383−
2384−reflectRawQuotePattern :: [Name] -> Raw -> ElabD ()
2385−reflectRawQuotePattern unq (Var n)
2386− -- the unquoted names already have types, just use them
2387− | n `elem` unq = do fill (Var n); solve
2388− | otherwise = do fill (reflCall "Var" [reflectName n]); solve
2389−reflectRawQuotePattern unq (RBind n b sc) =
2390− do scH <- getNameFrom (sMN 0 "sc")
2391− claim scH (Var (reflm "Raw"))
2392− movelast scH
2393− bH <- getNameFrom (sMN 0 "binder")
2394− claim bH (RApp (Var (reflm "Binder"))
2395− (Var (reflm "Raw")))
2396− if n `elem` freeNamesR sc
2397− then do fill $ reflCall "RBind" [reflectName n,
2398− Var bH,
2399− Var scH]
2400− solve
2401− else do any <- getNameFrom (sMN 0 "anyName")
2402− claim any (Var (reflm "TTName"))
2403− movelast any
2404− fill $ reflCall "RBind" [Var any, Var bH, Var scH]
2405− solve
2406− focus scH; reflectRawQuotePattern unq sc
2407− focus bH; reflectBinderQuotePattern reflectRawQuotePattern unq b
2408− where freeNamesR (Var n) = [n]
2409− freeNamesR (RBind n (Let t v) body) = concat [freeNamesR v,
2410− freeNamesR body \\ [n],
2411− freeNamesR t]
2412− freeNamesR (RBind n b body) = freeNamesR (binderTy b) ++
2413− (freeNamesR body \\ [n])
2414− freeNamesR (RApp f x) = freeNamesR f ++ freeNamesR x
2415− freeNamesR RType = []
2416− freeNamesR (RUType _) = []
2417− freeNamesR (RForce r) = freeNamesR r
2418− freeNamesR (RConstant _) = []
2419−reflectRawQuotePattern unq (RApp f x) =
2420− do fH <- getNameFrom (sMN 0 "f")
2421− claim fH (Var (reflm "Raw"))
2422− movelast fH
2423− xH <- getNameFrom (sMN 0 "x")
2424− claim xH (Var (reflm "Raw"))
2425− movelast xH
2426− fill $ reflCall "RApp" [Var fH, Var xH]
2427− solve
2428− focus fH; reflectRawQuotePattern unq f
2429− focus xH; reflectRawQuotePattern unq x
2430−reflectRawQuotePattern unq RType =
2431− do fill (Var (reflm "RType"))
2432− solve
2433−reflectRawQuotePattern unq (RUType univ) =
2434− do uH <- getNameFrom (sMN 0 "universe")
2435− claim uH (Var (reflm "Universe"))
2436− movelast uH
2437− fill $ reflCall "RUType" [Var uH]
2438− solve
2439− focus uH; fill (reflectUniverse univ); solve
2440−reflectRawQuotePattern unq (RForce r) =
2441− do rH <- getNameFrom (sMN 0 "raw")
2442− claim rH (Var (reflm "Raw"))
2443− movelast rH
2444− fill $ reflCall "RForce" [Var rH]
2445− solve
2446− focus rH; reflectRawQuotePattern unq r
2447−reflectRawQuotePattern unq (RConstant c) =
2448− do cH <- getNameFrom (sMN 0 "const")
2449− claim cH (Var (reflm "Constant"))
2450− movelast cH
2451− fill (reflCall "RConstant" [Var cH]); solve
2452− focus cH
2453− fill (reflectConstant c); solve
2454−
2455−reflectBinderQuotePattern :: ([Name] -> a -> ElabD ()) -> [Name] -> Binder a -> ElabD ()
2456−reflectBinderQuotePattern q unq (Lam t)
2457− = do t' <- claimTT (sMN 0 "ty"); movelast t'
2458− fill $ reflCall "Lam" [Var (reflm "TT"), Var t']
2459− solve
2460− focus t'; q unq t
2461−reflectBinderQuotePattern q unq (Pi _ t k)
2462− = do t' <- claimTT (sMN 0 "ty") ; movelast t'
2463− k' <- claimTT (sMN 0 "k"); movelast k';
2464− fill $ reflCall "Pi" [Var (reflm "TT"), Var t', Var k']
2465− solve
2466− focus t'; q unq t
2467−reflectBinderQuotePattern q unq (Let x y)
2468− = do x' <- claimTT (sMN 0 "ty"); movelast x';
2469− y' <- claimTT (sMN 0 "v"); movelast y';
2470− fill $ reflCall "Let" [Var (reflm "TT"), Var x', Var y']
2471− solve
2472− focus x'; q unq x
2473− focus y'; q unq y
2474−reflectBinderQuotePattern q unq (NLet x y)
2475− = do x' <- claimTT (sMN 0 "ty"); movelast x'
2476− y' <- claimTT (sMN 0 "v"); movelast y'
2477− fill $ reflCall "NLet" [Var (reflm "TT"), Var x', Var y']
2478− solve
2479− focus x'; q unq x
2480− focus y'; q unq y
2481−reflectBinderQuotePattern q unq (Hole t)
2482− = do t' <- claimTT (sMN 0 "ty"); movelast t'
2483− fill $ reflCall "Hole" [Var (reflm "TT"), Var t']
2484− solve
2485− focus t'; q unq t
2486−reflectBinderQuotePattern q unq (GHole _ t)
2487− = do t' <- claimTT (sMN 0 "ty"); movelast t'
2488− fill $ reflCall "GHole" [Var (reflm "TT"), Var t']
2489− solve
2490− focus t'; q unq t
2491−reflectBinderQuotePattern q unq (Guess x y)
2492− = do x' <- claimTT (sMN 0 "ty"); movelast x'
2493− y' <- claimTT (sMN 0 "v"); movelast y'
2494− fill $ reflCall "Guess" [Var (reflm "TT"), Var x', Var y']
2495− solve
2496− focus x'; q unq x
2497− focus y'; q unq y
2498−reflectBinderQuotePattern q unq (PVar t)
2499− = do t' <- claimTT (sMN 0 "ty"); movelast t'
2500− fill $ reflCall "PVar" [Var (reflm "TT"), Var t']
2501− solve
2502− focus t'; q unq t
2503−reflectBinderQuotePattern q unq (PVTy t)
2504− = do t' <- claimTT (sMN 0 "ty"); movelast t'
2505− fill $ reflCall "PVTy" [Var (reflm "TT"), Var t']
2506− solve
2507− focus t'; q unq t
2508−
2509−reflectUniverse :: Universe -> Raw
2510−reflectUniverse u =
2511− (Var (reflm (case u of
2512− NullType -> "NullType"
2513− UniqueType -> "UniqueType"
2514− AllTypes -> "AllTypes")))
2515−
2516−-- | Create a reflected TT term, but leave refs to the provided name intact
2517−reflectTTQuote :: [Name] -> Term -> Raw
2518−reflectTTQuote unq (P nt n t)
2519− | n `elem` unq = Var n
2520− | otherwise = reflCall "P" [reflectNameType nt, reflectName n, reflectTTQuote unq t]
2521−reflectTTQuote unq (V n)
2522− = reflCall "V" [RConstant (I n)]
2523−reflectTTQuote unq (Bind n b x)
2524− = reflCall "Bind" [reflectName n, reflectBinderQuote reflectTTQuote (reflm "TT") unq b, reflectTTQuote unq x]
2525−reflectTTQuote unq (App f x)
2526− = reflCall "App" [reflectTTQuote unq f, reflectTTQuote unq x]
2527−reflectTTQuote unq (Constant c)
2528− = reflCall "TConst" [reflectConstant c]
2529−reflectTTQuote unq (Proj t i)
2530− = reflCall "Proj" [reflectTTQuote unq t, RConstant (I i)]
2531−reflectTTQuote unq (Erased) = Var (reflm "Erased")
2532−reflectTTQuote unq (Impossible) = Var (reflm "Impossible")
2533−reflectTTQuote unq (TType exp) = reflCall "TType" [reflectUExp exp]
2534−reflectTTQuote unq (UType u) = reflCall "UType" [reflectUniverse u]
2535−
2536−reflectRawQuote :: [Name] -> Raw -> Raw
2537−reflectRawQuote unq (Var n)
2538− | n `elem` unq = Var n
2539− | otherwise = reflCall "Var" [reflectName n]
2540−reflectRawQuote unq (RBind n b r) =
2541− reflCall "RBind" [reflectName n, reflectBinderQuote reflectRawQuote (reflm "Raw") unq b, reflectRawQuote unq r]
2542−reflectRawQuote unq (RApp f x) =
2543− reflCall "RApp" [reflectRawQuote unq f, reflectRawQuote unq x]
2544−reflectRawQuote unq RType = Var (reflm "RType")
2545−reflectRawQuote unq (RUType u) =
2546− reflCall "RUType" [reflectUniverse u]
2547−reflectRawQuote unq (RForce r) = reflCall "RForce" [reflectRawQuote unq r]
2548−reflectRawQuote unq (RConstant cst) = reflCall "RConstant" [reflectConstant cst]
2549−
2550−reflectNameType :: NameType -> Raw
2551−reflectNameType (Bound) = Var (reflm "Bound")
2552−reflectNameType (Ref) = Var (reflm "Ref")
2553−reflectNameType (DCon x y _)
2554− = reflCall "DCon" [RConstant (I x), RConstant (I y)] -- FIXME: Uniqueness!
2555−reflectNameType (TCon x y)
2556− = reflCall "TCon" [RConstant (I x), RConstant (I y)]
2557−
2558−reflectName :: Name -> Raw
2559−reflectName (UN s)
2560− = reflCall "UN" [RConstant (Str (str s))]
2561−reflectName (NS n ns)
2562− = reflCall "NS" [ reflectName n
2563− , foldr (\ n s ->
2564− raw_apply ( Var $ sNS (sUN "::") ["List", "Prelude"] )
2565− [ RConstant StrType, RConstant (Str n), s ])
2566− ( raw_apply ( Var $ sNS (sUN "Nil") ["List", "Prelude"] )
2567− [ RConstant StrType ])
2568− (map str ns)
2569− ]
2570−reflectName (MN i n)
2571− = reflCall "MN" [RConstant (I i), RConstant (Str (str n))]
2572−reflectName (NErased) = Var (reflm "NErased")
2573−reflectName n = Var (reflm "NErased") -- special name, not yet implemented
2574−
2575−-- | Elaborate a name to a pattern. This means that NS and UN will be intact.
2576−-- MNs corresponding to will care about the string but not the number. All
2577−-- others become _.
2578−reflectNameQuotePattern :: Name -> ElabD ()
2579−reflectNameQuotePattern n@(UN s)
2580− = do fill $ reflectName n
2581− solve
2582−reflectNameQuotePattern n@(NS _ _)
2583− = do fill $ reflectName n
2584− solve
2585−reflectNameQuotePattern (MN _ n)
2586− = do i <- getNameFrom (sMN 0 "mnCounter")
2587− claim i (RConstant (AType (ATInt ITNative)))
2588− movelast i
2589− fill $ reflCall "MN" [Var i, RConstant (Str $ T.unpack n)]
2590− solve
2591−reflectNameQuotePattern _ -- for all other names, match any
2592− = do nameHole <- getNameFrom (sMN 0 "name")
2593− claim nameHole (Var (reflm "TTName"))
2594− movelast nameHole
2595− fill (Var nameHole)
2596− solve
2597−
2598−reflectBinder :: Binder Term -> Raw
2599−reflectBinder = reflectBinderQuote reflectTTQuote (reflm "TT") []
2600−
2601−reflectBinderQuote :: ([Name] -> a -> Raw) -> Name -> [Name] -> Binder a -> Raw
2602−reflectBinderQuote q ty unq (Lam t)
2603− = reflCall "Lam" [Var ty, q unq t]
2604−reflectBinderQuote q ty unq (Pi _ t k)
2605− = reflCall "Pi" [Var ty, q unq t, q unq k]
2606−reflectBinderQuote q ty unq (Let x y)
2607− = reflCall "Let" [Var ty, q unq x, q unq y]
2608−reflectBinderQuote q ty unq (NLet x y)
2609− = reflCall "NLet" [Var ty, q unq x, q unq y]
2610−reflectBinderQuote q ty unq (Hole t)
2611− = reflCall "Hole" [Var ty, q unq t]
2612−reflectBinderQuote q ty unq (GHole _ t)
2613− = reflCall "GHole" [Var ty, q unq t]
2614−reflectBinderQuote q ty unq (Guess x y)
2615− = reflCall "Guess" [Var ty, q unq x, q unq y]
2616−reflectBinderQuote q ty unq (PVar t)
2617− = reflCall "PVar" [Var ty, q unq t]
2618−reflectBinderQuote q ty unq (PVTy t)
2619− = reflCall "PVTy" [Var ty, q unq t]
2620−
2621−mkList :: Raw -> [Raw] -> Raw
2622−mkList ty [] = RApp (Var (sNS (sUN "Nil") ["List", "Prelude"])) ty
2623−mkList ty (x:xs) = RApp (RApp (RApp (Var (sNS (sUN "::") ["List", "Prelude"])) ty)
2624− x)
2625− (mkList ty xs)
2626−
2627−reflectConstant :: Const -> Raw
2628−reflectConstant c@(I _) = reflCall "I" [RConstant c]
2629−reflectConstant c@(BI _) = reflCall "BI" [RConstant c]
2630−reflectConstant c@(Fl _) = reflCall "Fl" [RConstant c]
2631−reflectConstant c@(Ch _) = reflCall "Ch" [RConstant c]
2632−reflectConstant c@(Str _) = reflCall "Str" [RConstant c]
2633−reflectConstant c@(B8 _) = reflCall "B8" [RConstant c]
2634−reflectConstant c@(B16 _) = reflCall "B16" [RConstant c]
2635−reflectConstant c@(B32 _) = reflCall "B32" [RConstant c]
2636−reflectConstant c@(B64 _) = reflCall "B64" [RConstant c]
2637−reflectConstant (AType (ATInt ITNative)) = reflCall "AType" [reflCall "ATInt" [Var (reflm "ITNative")]]
2638−reflectConstant (AType (ATInt ITBig)) = reflCall "AType" [reflCall "ATInt" [Var (reflm "ITBig")]]
2639−reflectConstant (AType ATFloat) = reflCall "AType" [Var (reflm "ATFloat")]
2640−reflectConstant (AType (ATInt ITChar)) = reflCall "AType" [reflCall "ATInt" [Var (reflm "ITChar")]]
2641−reflectConstant StrType = Var (reflm "StrType")
2642−reflectConstant (AType (ATInt (ITFixed IT8))) = reflCall "AType" [reflCall "ATInt" [reflCall "ITFixed" [Var (reflm "IT8")]]]
2643−reflectConstant (AType (ATInt (ITFixed IT16))) = reflCall "AType" [reflCall "ATInt" [reflCall "ITFixed" [Var (reflm "IT16")]]]
2644−reflectConstant (AType (ATInt (ITFixed IT32))) = reflCall "AType" [reflCall "ATInt" [reflCall "ITFixed" [Var (reflm "IT32")]]]
2645−reflectConstant (AType (ATInt (ITFixed IT64))) = reflCall "AType" [reflCall "ATInt" [reflCall "ITFixed" [Var (reflm "IT64")]]]
2646−reflectConstant VoidType = Var (reflm "VoidType")
2647−reflectConstant Forgot = Var (reflm "Forgot")
2648−reflectConstant WorldType = Var (reflm "WorldType")
2649−reflectConstant TheWorld = Var (reflm "TheWorld")
2650−
2651−reflectUExp :: UExp -> Raw
2652−reflectUExp (UVar i) = reflCall "UVar" [RConstant (I i)]
2653−reflectUExp (UVal i) = reflCall "UVal" [RConstant (I i)]
2654−
2655−-- | Reflect the environment of a proof into a List (TTName, Binder TT)
2656−reflectEnv :: Env -> Raw
2657−reflectEnv = foldr consToEnvList emptyEnvList
2658− where
2659− consToEnvList :: (Name, Binder Term) -> Raw -> Raw
2660− consToEnvList (n, b) l
2661− = raw_apply (Var (sNS (sUN "::") ["List", "Prelude"]))
2662− [ envTupleType
2663− , raw_apply (Var pairCon) [ (Var $ reflm "TTName")
2664− , (RApp (Var $ reflm "Binder")
2665− (Var $ reflm "TT"))
2666− , reflectName n
2667− , reflectBinder b
2668− ]
2669− , l
2670− ]
2671−
2672− emptyEnvList :: Raw
2673− emptyEnvList = raw_apply (Var (sNS (sUN "Nil") ["List", "Prelude"]))
2674− [envTupleType]
2675−
2676−-- | Reflect an error into the internal datatype of Idris -- TODO
2677−rawBool :: Bool -> Raw
2678−rawBool True = Var (sNS (sUN "True") ["Bool", "Prelude"])
2679−rawBool False = Var (sNS (sUN "False") ["Bool", "Prelude"])
2680−
2681−rawNil :: Raw -> Raw
2682−rawNil ty = raw_apply (Var (sNS (sUN "Nil") ["List", "Prelude"])) [ty]
2683−
2684−rawCons :: Raw -> Raw -> Raw -> Raw
2685−rawCons ty hd tl = raw_apply (Var (sNS (sUN "::") ["List", "Prelude"])) [ty, hd, tl]
2686−
2687−rawList :: Raw -> [Raw] -> Raw
2688−rawList ty = foldr (rawCons ty) (rawNil ty)
2689−
2690−rawPairTy :: Raw -> Raw -> Raw
2691−rawPairTy t1 t2 = raw_apply (Var pairTy) [t1, t2]
2692−
2693−rawPair :: (Raw, Raw) -> (Raw, Raw) -> Raw
2694−rawPair (a, b) (x, y) = raw_apply (Var pairCon) [a, b, x, y]
2695−
2696−reflectCtxt :: [(Name, Type)] -> Raw
2697−reflectCtxt ctxt = rawList (rawPairTy (Var $ reflm "TTName") (Var $ reflm "TT"))
2698− (map (\ (n, t) -> (rawPair (Var $ reflm "TTName", Var $ reflm "TT")
2699− (reflectName n, reflect t)))
2700− ctxt)
2701−
2702−reflectErr :: Err -> Raw
2703−reflectErr (Msg msg) = raw_apply (Var $ reflErrName "Msg") [RConstant (Str msg)]
2704−reflectErr (InternalMsg msg) = raw_apply (Var $ reflErrName "InternalMsg") [RConstant (Str msg)]
2705−reflectErr (CantUnify b (t1,_) (t2,_) e ctxt i) =
2706− raw_apply (Var $ reflErrName "CantUnify")
2707− [ rawBool b
2708− , reflect t1
2709− , reflect t2
2710− , reflectErr e
2711− , reflectCtxt ctxt
2712− , RConstant (I i)]
2713−reflectErr (InfiniteUnify n tm ctxt) =
2714− raw_apply (Var $ reflErrName "InfiniteUnify")
2715− [ reflectName n
2716− , reflect tm
2717− , reflectCtxt ctxt
2718− ]
2719−reflectErr (CantConvert t t' ctxt) =
2720− raw_apply (Var $ reflErrName "CantConvert")
2721− [ reflect t
2722− , reflect t'
2723− , reflectCtxt ctxt
2724− ]
2725−reflectErr (CantSolveGoal t ctxt) =
2726− raw_apply (Var $ reflErrName "CantSolveGoal")
2727− [ reflect t
2728− , reflectCtxt ctxt
2729− ]
2730−reflectErr (UnifyScope n n' t ctxt) =
2731− raw_apply (Var $ reflErrName "UnifyScope")
2732− [ reflectName n
2733− , reflectName n'
2734− , reflect t
2735− , reflectCtxt ctxt
2736− ]
2737−reflectErr (CantInferType str) =
2738− raw_apply (Var $ reflErrName "CantInferType") [RConstant (Str str)]
2739−reflectErr (NonFunctionType t t') =
2740− raw_apply (Var $ reflErrName "NonFunctionType") [reflect t, reflect t']
2741−reflectErr (NotEquality t t') =
2742− raw_apply (Var $ reflErrName "NotEquality") [reflect t, reflect t']
2743−reflectErr (TooManyArguments n) = raw_apply (Var $ reflErrName "TooManyArguments") [reflectName n]
2744−reflectErr (CantIntroduce t) = raw_apply (Var $ reflErrName "CantIntroduce") [reflect t]
2745−reflectErr (NoSuchVariable n) = raw_apply (Var $ reflErrName "NoSuchVariable") [reflectName n]
2746−reflectErr (WithFnType t) = raw_apply (Var $ reflErrName "WithFnType") [reflect t]
2747−reflectErr (CantMatch t) = raw_apply (Var $ reflErrName "CantMatch") [reflect t]
2748−reflectErr (NoTypeDecl n) = raw_apply (Var $ reflErrName "NoTypeDecl") [reflectName n]
2749−reflectErr (NotInjective t1 t2 t3) =
2750− raw_apply (Var $ reflErrName "NotInjective")
2751− [ reflect t1
2752− , reflect t2
2753− , reflect t3
2754− ]
2755−reflectErr (CantResolve _ t) = raw_apply (Var $ reflErrName "CantResolve") [reflect t]
2756−reflectErr (InvalidTCArg n t) = raw_apply (Var $ reflErrName "InvalidTCArg") [reflectName n, reflect t]
2757−reflectErr (CantResolveAlts ss) =
2758− raw_apply (Var $ reflErrName "CantResolveAlts")
2759− [rawList (Var $ reflm "TTName") (map reflectName ss)]
2760−reflectErr (IncompleteTerm t) = raw_apply (Var $ reflErrName "IncompleteTerm") [reflect t]
2761−reflectErr (NoEliminator str t)
2762− = raw_apply (Var $ reflErrName "NoEliminator") [RConstant (Str str),
2763− reflect t]
2764−reflectErr UniverseError = Var $ reflErrName "UniverseError"
2765−reflectErr ProgramLineComment = Var $ reflErrName "ProgramLineComment"
2766−reflectErr (Inaccessible n) = raw_apply (Var $ reflErrName "Inaccessible") [reflectName n]
2767−reflectErr (NonCollapsiblePostulate n) = raw_apply (Var $ reflErrName "NonCollabsiblePostulate") [reflectName n]
2768−reflectErr (AlreadyDefined n) = raw_apply (Var $ reflErrName "AlreadyDefined") [reflectName n]
2769−reflectErr (ProofSearchFail e) = raw_apply (Var $ reflErrName "ProofSearchFail") [reflectErr e]
2770−reflectErr (NoRewriting tm) = raw_apply (Var $ reflErrName "NoRewriting") [reflect tm]
2771−reflectErr (ProviderError str) =
2772− raw_apply (Var $ reflErrName "ProviderError") [RConstant (Str str)]
2773−reflectErr (LoadingFailed str err) =
2774− raw_apply (Var $ reflErrName "LoadingFailed") [RConstant (Str str)]
2775−reflectErr x = raw_apply (Var (sNS (sUN "Msg") ["Errors", "Reflection", "Language"])) [RConstant . Str $ "Default reflection: " ++ show x]
2776−
2777−-- | Reflect a file context
2778−reflectFC :: FC -> Raw
2779−reflectFC fc = raw_apply (Var (reflm "FileLoc"))
2780− [ RConstant (Str (fc_fname fc))
2781− , raw_apply (Var pairCon) $
2782− [intTy, intTy] ++
2783− map (RConstant . I)
2784− [ fst (fc_start fc)
2785− , snd (fc_start fc)
2786− ]
2787− , raw_apply (Var pairCon) $
2788− [intTy, intTy] ++
2789− map (RConstant . I)
2790− [ fst (fc_end fc)
2791− , snd (fc_end fc)
2792− ]
2793− ]
2794− where intTy = RConstant (AType (ATInt ITNative))
27952019
27962020elaboratingArgErr :: [(Name, Name)] -> Err -> Err
27972021elaboratingArgErr [] err = err
@@ -2863,88 +2087,4 @@ withErrorReflection x = idrisCatch x (\ e -> handle e >>= ierror)
28632087 [] -> e
28642088 parts -> ReflectionError errorparts e
28652089
2866−fromTTMaybe :: Term -> Maybe Term -- WARNING: Assumes the term has type Maybe a
2867−fromTTMaybe (App (App (P (DCon _ _ _) (NS (UN just) _) _) ty) tm)
2868− | just == txt "Just" = Just tm
2869−fromTTMaybe x = Nothing
2870−
2871−reflErrName :: String -> Name
2872−reflErrName n = sNS (sUN n) ["Errors", "Reflection", "Language"]
2873−
2874−-- | Attempt to reify a report part from TT to the internal
2875−-- representation. Not in Idris or ElabD monads because it should be usable
2876−-- from either.
2877−reifyReportPart :: Term -> Either Err ErrorReportPart
2878−reifyReportPart (App (P (DCon _ _ _) n _) (Constant (Str msg))) | n == reflm "TextPart" =
2879− Right (TextPart msg)
2880−reifyReportPart (App (P (DCon _ _ _) n _) ttn)
2881− | n == reflm "NamePart" =
2882− case runElab initEState (reifyTTName ttn) (initElaborator NErased initContext Erased) of
2883− Error e -> Left . InternalMsg $
2884− "could not reify name term " ++
2885− show ttn ++
2886− " when reflecting an error:" ++ show e
2887− OK (n', _)-> Right $ NamePart n'
2888−reifyReportPart (App (P (DCon _ _ _) n _) tm)
2889− | n == reflm "TermPart" =
2890− case runElab initEState (reifyTT tm) (initElaborator NErased initContext Erased) of
2891− Error e -> Left . InternalMsg $
2892− "could not reify reflected term " ++
2893− show tm ++
2894− " when reflecting an error:" ++ show e
2895− OK (tm', _) -> Right $ TermPart tm'
2896−reifyReportPart (App (P (DCon _ _ _) n _) tm)
2897− | n == reflm "SubReport" =
2898− case unList tm of
2899− Just xs -> do subParts <- mapM reifyReportPart xs
2900− Right (SubReport subParts)
2901− Nothing -> Left . InternalMsg $ "could not reify subreport " ++ show tm
2902−reifyReportPart x = Left . InternalMsg $ "could not reify " ++ show x
2903−
2904−reifyTyDecl :: Term -> ElabD RTyDecl
2905−reifyTyDecl (App (App (App (P (DCon _ _ _) n _) tyN) args) ret)
2906− | n == tacN "Declare" =
2907− do tyN' <- reifyTTName tyN
2908− args' <- case unList args of
2909− Nothing -> fail $ "Couldn't reify " ++ show args ++ " as an arglist."
2910− Just xs -> mapM reifyRArg xs
2911− ret' <- reifyRaw ret
2912− return $ RDeclare tyN' args' ret'
2913− where reifyRArg :: Term -> ElabD RArg
2914− reifyRArg (App (App (P (DCon _ _ _) n _) argN) argTy)
2915− | n == tacN "Explicit" = liftM2 RExplicit
2916− (reifyTTName argN)
2917− (reifyRaw argTy)
2918− | n == tacN "Implicit" = liftM2 RImplicit
2919− (reifyTTName argN)
2920− (reifyRaw argTy) | n == tacN "Constraint" = liftM2 RConstraint
2921− (reifyTTName argN)
2922− (reifyRaw argTy)
2923− reifyRArg aTm = fail $ "Couldn't reify " ++ show aTm ++ " as an RArg."
2924−reifyTyDecl tm = fail $ "Couldn't reify " ++ show tm ++ " as a type declaration."
2925−
2926−reifyFunDefn :: Term -> ElabD RFunDefn
2927−reifyFunDefn (App (App (P _ n _) fnN) clauses)
2928− | n == tacN "DefineFun" =
2929− do fnN' <- reifyTTName fnN
2930− clauses' <- case unList clauses of
2931− Nothing -> fail $ "Couldn't reify " ++ show clauses ++ " as a clause list"
2932− Just cs -> mapM reifyC cs
2933− return $ RDefineFun fnN' clauses'
2934− where reifyC :: Term -> ElabD RFunClause
2935− reifyC (App (App (P (DCon _ _ _) n _) lhs) rhs)
2936− | n == tacN "MkFunClause" = liftM2 RMkFunClause
2937− (reifyRaw lhs)
2938− (reifyRaw rhs)
2939− reifyC (App (P (DCon _ _ _) n _) lhs)
2940− | n == tacN "MkImpossibleClause" = fmap RMkImpossibleClause $ reifyRaw lhs
2941− reifyC tm = fail $ "Couldn't reify " ++ show tm ++ " as a clause."
2942−reifyFunDefn tm = fail $ "Couldn't reify " ++ show tm ++ " as a function declaration."
2943−
2944−envTupleType :: Raw
2945−envTupleType
2946− = raw_apply (Var pairTy) [ (Var $ reflm "TTName")
2947− , (RApp (Var $ reflm "Binder") (Var $ reflm "TT"))
2948− ]
2949−
29502090solveAll = try (do solve; solveAll) (return ())
src/Idris/Reflection.hs+877−3
@@ -1,10 +1,22 @@
1−{-|
2−Handy tools for doing reflection.
1+{-| Code related to Idris's reflection system. This module contains
2+quoters and unquoters along with some supporting datatypes.
33-}
4+{-# LANGUAGE PatternGuards #-}
5+{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
46module Idris.Reflection where
57
8+import Control.Monad (liftM, liftM2)
9+import Data.List ((\\))
10+import qualified Data.Text as T
11+
12+import Idris.Core.Elaborate (claim, fill, focus, getNameFrom, initElaborator,
13+ movelast, runElab, solve)
14+import Idris.Core.Evaluate (initContext)
615import Idris.Core.TT
7−import Idris.AbsSyntaxTree (PArg'(..), PArg, PTerm(Placeholder))
16+
17+import Idris.AbsSyntaxTree (ElabD, IState, PArg'(..), PArg, PTactic, PTactic'(..),
18+ PTerm(..), initEState, pairCon, pairTy)
19+import Idris.Delaborate (delab)
820
921data RArg = RExplicit { argName :: Name, argTy :: Raw }
1022 | RImplicit { argName :: Name, argTy :: Raw }
@@ -22,3 +34,865 @@ data RFunClause = RMkFunClause Raw Raw
2234 deriving Show
2335
2436data RFunDefn = RDefineFun Name [RFunClause] deriving Show
37+
38+-- | Prefix a name with the "Language.Reflection" namespace
39+reflm :: String -> Name
40+reflm n = sNS (sUN n) ["Reflection", "Language"]
41+
42+-- | Prefix a name with the "Language.Reflection.Elab" namespace
43+tacN :: String -> Name
44+tacN str = sNS (sUN str) ["Elab", "Reflection", "Language"]
45+
46+-- | Reify tactics from their reflected representation
47+reify :: IState -> Term -> ElabD PTactic
48+reify _ (P _ n _) | n == reflm "Intros" = return Intros
49+reify _ (P _ n _) | n == reflm "Trivial" = return Trivial
50+reify _ (P _ n _) | n == reflm "Instance" = return TCInstance
51+reify _ (P _ n _) | n == reflm "Solve" = return Solve
52+reify _ (P _ n _) | n == reflm "Compute" = return Compute
53+reify _ (P _ n _) | n == reflm "Skip" = return Skip
54+reify _ (P _ n _) | n == reflm "SourceFC" = return SourceFC
55+reify _ (P _ n _) | n == reflm "Unfocus" = return Unfocus
56+reify ist t@(App _ _)
57+ | (P _ f _, args) <- unApply t = reifyApp ist f args
58+reify _ t = fail ("Unknown tactic " ++ show t)
59+
60+reifyApp :: IState -> Name -> [Term] -> ElabD PTactic
61+reifyApp ist t [l, r] | t == reflm "Try" = liftM2 Try (reify ist l) (reify ist r)
62+reifyApp _ t [Constant (I i)]
63+ | t == reflm "Search" = return (ProofSearch True True i Nothing [])
64+reifyApp _ t [x]
65+ | t == reflm "Refine" = do n <- reifyTTName x
66+ return $ Refine n []
67+reifyApp ist t [n, ty] | t == reflm "Claim" = do n' <- reifyTTName n
68+ goal <- reifyTT ty
69+ return $ Claim n' (delab ist goal)
70+reifyApp ist t [l, r] | t == reflm "Seq" = liftM2 TSeq (reify ist l) (reify ist r)
71+reifyApp ist t [Constant (Str n), x]
72+ | t == reflm "GoalType" = liftM (GoalType n) (reify ist x)
73+reifyApp _ t [n] | t == reflm "Intro" = liftM (Intro . (:[])) (reifyTTName n)
74+reifyApp ist t [t'] | t == reflm "Induction" = liftM (Induction . delab ist) (reifyTT t')
75+reifyApp ist t [t'] | t == reflm "Case" = liftM (Induction . delab ist) (reifyTT t')
76+reifyApp ist t [t']
77+ | t == reflm "ApplyTactic" = liftM (ApplyTactic . delab ist) (reifyTT t')
78+reifyApp ist t [t']
79+ | t == reflm "Reflect" = liftM (Reflect . delab ist) (reifyTT t')
80+reifyApp ist t [t']
81+ | t == reflm "ByReflection" = liftM (ByReflection . delab ist) (reifyTT t')
82+reifyApp _ t [t']
83+ | t == reflm "Fill" = liftM (Fill . PQuote) (reifyRaw t')
84+reifyApp ist t [t']
85+ | t == reflm "Exact" = liftM (Exact . delab ist) (reifyTT t')
86+reifyApp ist t [x]
87+ | t == reflm "Focus" = liftM Focus (reifyTTName x)
88+reifyApp ist t [t']
89+ | t == reflm "Rewrite" = liftM (Rewrite . delab ist) (reifyTT t')
90+reifyApp ist t [n, t']
91+ | t == reflm "LetTac" = do n' <- reifyTTName n
92+ t'' <- reifyTT t'
93+ return $ LetTac n' (delab ist t')
94+reifyApp ist t [n, tt', t']
95+ | t == reflm "LetTacTy" = do n' <- reifyTTName n
96+ tt'' <- reifyTT tt'
97+ t'' <- reifyTT t'
98+ return $ LetTacTy n' (delab ist tt'') (delab ist t'')
99+reifyApp ist t [errs]
100+ | t == reflm "Fail" = fmap TFail (reifyReportParts errs)
101+reifyApp _ f args = fail ("Unknown tactic " ++ show (f, args)) -- shouldn't happen
102+
103+reifyReportParts :: Term -> ElabD [ErrorReportPart]
104+reifyReportParts errs =
105+ case unList errs of
106+ Nothing -> fail "Failed to reify errors"
107+ Just errs' ->
108+ let parts = mapM reifyReportPart errs' in
109+ case parts of
110+ Left err -> fail $ "Couldn't reify \"Fail\" tactic - " ++ show err
111+ Right errs'' ->
112+ return errs''
113+
114+-- | Reify terms from their reflected representation
115+reifyTT :: Term -> ElabD Term
116+reifyTT t@(App _ _)
117+ | (P _ f _, args) <- unApply t = reifyTTApp f args
118+reifyTT t@(P _ n _)
119+ | n == reflm "Erased" = return $ Erased
120+reifyTT t@(P _ n _)
121+ | n == reflm "Impossible" = return $ Impossible
122+reifyTT t = fail ("Unknown reflection term: " ++ show t)
123+
124+reifyTTApp :: Name -> [Term] -> ElabD Term
125+reifyTTApp t [nt, n, x]
126+ | t == reflm "P" = do nt' <- reifyTTNameType nt
127+ n' <- reifyTTName n
128+ x' <- reifyTT x
129+ return $ P nt' n' x'
130+reifyTTApp t [Constant (I i)]
131+ | t == reflm "V" = return $ V i
132+reifyTTApp t [n, b, x]
133+ | t == reflm "Bind" = do n' <- reifyTTName n
134+ b' <- reifyTTBinder reifyTT (reflm "TT") b
135+ x' <- reifyTT x
136+ return $ Bind n' b' x'
137+reifyTTApp t [f, x]
138+ | t == reflm "App" = do f' <- reifyTT f
139+ x' <- reifyTT x
140+ return $ App f' x'
141+reifyTTApp t [c]
142+ | t == reflm "TConst" = liftM Constant (reifyTTConst c)
143+reifyTTApp t [t', Constant (I i)]
144+ | t == reflm "Proj" = do t'' <- reifyTT t'
145+ return $ Proj t'' i
146+reifyTTApp t [tt]
147+ | t == reflm "TType" = liftM TType (reifyTTUExp tt)
148+reifyTTApp t args = fail ("Unknown reflection term: " ++ show (t, args))
149+
150+-- | Reify raw terms from their reflected representation
151+reifyRaw :: Term -> ElabD Raw
152+reifyRaw t@(App _ _)
153+ | (P _ f _, args) <- unApply t = reifyRawApp f args
154+reifyRaw t@(P _ n _)
155+ | n == reflm "RType" = return $ RType
156+reifyRaw t = fail ("Unknown reflection raw term in reifyRaw: " ++ show t)
157+
158+reifyRawApp :: Name -> [Term] -> ElabD Raw
159+reifyRawApp t [n]
160+ | t == reflm "Var" = liftM Var (reifyTTName n)
161+reifyRawApp t [n, b, x]
162+ | t == reflm "RBind" = do n' <- reifyTTName n
163+ b' <- reifyTTBinder reifyRaw (reflm "Raw") b
164+ x' <- reifyRaw x
165+ return $ RBind n' b' x'
166+reifyRawApp t [f, x]
167+ | t == reflm "RApp" = liftM2 RApp (reifyRaw f) (reifyRaw x)
168+reifyRawApp t [t']
169+ | t == reflm "RForce" = liftM RForce (reifyRaw t')
170+reifyRawApp t [c]
171+ | t == reflm "RConstant" = liftM RConstant (reifyTTConst c)
172+reifyRawApp t args = fail ("Unknown reflection raw term in reifyRawApp: " ++ show (t, args))
173+
174+reifyTTName :: Term -> ElabD Name
175+reifyTTName t
176+ | (P _ f _, args) <- unApply t = reifyTTNameApp f args
177+reifyTTName t = fail ("Unknown reflection term name: " ++ show t)
178+
179+reifyTTNameApp :: Name -> [Term] -> ElabD Name
180+reifyTTNameApp t [Constant (Str n)]
181+ | t == reflm "UN" = return $ sUN n
182+reifyTTNameApp t [n, ns]
183+ | t == reflm "NS" = do n' <- reifyTTName n
184+ ns' <- reifyTTNamespace ns
185+ return $ sNS n' ns'
186+reifyTTNameApp t [Constant (I i), Constant (Str n)]
187+ | t == reflm "MN" = return $ sMN i n
188+reifyTTNameApp t []
189+ | t == reflm "NErased" = return NErased
190+reifyTTNameApp t args = fail ("Unknown reflection term name: " ++ show (t, args))
191+
192+reifyTTNamespace :: Term -> ElabD [String]
193+reifyTTNamespace t@(App _ _)
194+ = case unApply t of
195+ (P _ f _, [Constant StrType])
196+ | f == sNS (sUN "Nil") ["List", "Prelude"] -> return []
197+ (P _ f _, [Constant StrType, Constant (Str n), ns])
198+ | f == sNS (sUN "::") ["List", "Prelude"] -> liftM (n:) (reifyTTNamespace ns)
199+ _ -> fail ("Unknown reflection namespace arg: " ++ show t)
200+reifyTTNamespace t = fail ("Unknown reflection namespace arg: " ++ show t)
201+
202+reifyTTNameType :: Term -> ElabD NameType
203+reifyTTNameType t@(P _ n _) | n == reflm "Bound" = return $ Bound
204+reifyTTNameType t@(P _ n _) | n == reflm "Ref" = return $ Ref
205+reifyTTNameType t@(App _ _)
206+ = case unApply t of
207+ (P _ f _, [Constant (I tag), Constant (I num)])
208+ | f == reflm "DCon" -> return $ DCon tag num False -- FIXME: Uniqueness!
209+ | f == reflm "TCon" -> return $ TCon tag num
210+ _ -> fail ("Unknown reflection name type: " ++ show t)
211+reifyTTNameType t = fail ("Unknown reflection name type: " ++ show t)
212+
213+reifyTTBinder :: (Term -> ElabD a) -> Name -> Term -> ElabD (Binder a)
214+reifyTTBinder reificator binderType t@(App _ _)
215+ = case unApply t of
216+ (P _ f _, bt:args) | forget bt == Var binderType
217+ -> reifyTTBinderApp reificator f args
218+ _ -> fail ("Mismatching binder reflection: " ++ show t)
219+reifyTTBinder _ _ t = fail ("Unknown reflection binder: " ++ show t)
220+
221+reifyTTBinderApp :: (Term -> ElabD a) -> Name -> [Term] -> ElabD (Binder a)
222+reifyTTBinderApp reif f [t]
223+ | f == reflm "Lam" = liftM Lam (reif t)
224+reifyTTBinderApp reif f [t, k]
225+ | f == reflm "Pi" = liftM2 (Pi Nothing) (reif t) (reif k)
226+reifyTTBinderApp reif f [x, y]
227+ | f == reflm "Let" = liftM2 Let (reif x) (reif y)
228+reifyTTBinderApp reif f [x, y]
229+ | f == reflm "NLet" = liftM2 NLet (reif x) (reif y)
230+reifyTTBinderApp reif f [t]
231+ | f == reflm "Hole" = liftM Hole (reif t)
232+reifyTTBinderApp reif f [t]
233+ | f == reflm "GHole" = liftM (GHole 0) (reif t)
234+reifyTTBinderApp reif f [x, y]
235+ | f == reflm "Guess" = liftM2 Guess (reif x) (reif y)
236+reifyTTBinderApp reif f [t]
237+ | f == reflm "PVar" = liftM PVar (reif t)
238+reifyTTBinderApp reif f [t]
239+ | f == reflm "PVTy" = liftM PVTy (reif t)
240+reifyTTBinderApp _ f args = fail ("Unknown reflection binder: " ++ show (f, args))
241+
242+reifyTTConst :: Term -> ElabD Const
243+reifyTTConst (P _ n _) | n == reflm "StrType" = return $ StrType
244+reifyTTConst (P _ n _) | n == reflm "VoidType" = return $ VoidType
245+reifyTTConst (P _ n _) | n == reflm "Forgot" = return $ Forgot
246+reifyTTConst t@(App _ _)
247+ | (P _ f _, [arg]) <- unApply t = reifyTTConstApp f arg
248+reifyTTConst t = fail ("Unknown reflection constant: " ++ show t)
249+
250+reifyTTConstApp :: Name -> Term -> ElabD Const
251+reifyTTConstApp f aty
252+ | f == reflm "AType" = fmap AType (reifyArithTy aty)
253+reifyTTConstApp f (Constant c@(I _))
254+ | f == reflm "I" = return $ c
255+reifyTTConstApp f (Constant c@(BI _))
256+ | f == reflm "BI" = return $ c
257+reifyTTConstApp f (Constant c@(Fl _))
258+ | f == reflm "Fl" = return $ c
259+reifyTTConstApp f (Constant c@(I _))
260+ | f == reflm "Ch" = return $ c
261+reifyTTConstApp f (Constant c@(Str _))
262+ | f == reflm "Str" = return $ c
263+reifyTTConstApp f (Constant c@(B8 _))
264+ | f == reflm "B8" = return $ c
265+reifyTTConstApp f (Constant c@(B16 _))
266+ | f == reflm "B16" = return $ c
267+reifyTTConstApp f (Constant c@(B32 _))
268+ | f == reflm "B32" = return $ c
269+reifyTTConstApp f (Constant c@(B64 _))
270+ | f == reflm "B64" = return $ c
271+reifyTTConstApp f arg = fail ("Unknown reflection constant: " ++ show (f, arg))
272+
273+reifyArithTy :: Term -> ElabD ArithTy
274+reifyArithTy (App (P _ n _) intTy) | n == reflm "ATInt" = fmap ATInt (reifyIntTy intTy)
275+reifyArithTy (P _ n _) | n == reflm "ATFloat" = return ATFloat
276+reifyArithTy x = fail ("Couldn't reify reflected ArithTy: " ++ show x)
277+
278+reifyNativeTy :: Term -> ElabD NativeTy
279+reifyNativeTy (P _ n _) | n == reflm "IT8" = return IT8
280+reifyNativeTy (P _ n _) | n == reflm "IT8" = return IT8
281+reifyNativeTy (P _ n _) | n == reflm "IT8" = return IT8
282+reifyNativeTy (P _ n _) | n == reflm "IT8" = return IT8
283+reifyNativeTy x = fail $ "Couldn't reify reflected NativeTy " ++ show x
284+
285+reifyIntTy :: Term -> ElabD IntTy
286+reifyIntTy (App (P _ n _) nt) | n == reflm "ITFixed" = fmap ITFixed (reifyNativeTy nt)
287+reifyIntTy (P _ n _) | n == reflm "ITNative" = return ITNative
288+reifyIntTy (P _ n _) | n == reflm "ITBig" = return ITBig
289+reifyIntTy (P _ n _) | n == reflm "ITChar" = return ITChar
290+reifyIntTy tm = fail $ "The term " ++ show tm ++ " is not a reflected IntTy"
291+
292+reifyTTUExp :: Term -> ElabD UExp
293+reifyTTUExp t@(App _ _)
294+ = case unApply t of
295+ (P _ f _, [Constant (I i)]) | f == reflm "UVar" -> return $ UVar i
296+ (P _ f _, [Constant (I i)]) | f == reflm "UVal" -> return $ UVal i
297+ _ -> fail ("Unknown reflection type universe expression: " ++ show t)
298+reifyTTUExp t = fail ("Unknown reflection type universe expression: " ++ show t)
299+
300+-- | Create a reflected call to a named function/constructor
301+reflCall :: String -> [Raw] -> Raw
302+reflCall funName args
303+ = raw_apply (Var (reflm funName)) args
304+
305+-- | Lift a term into its Language.Reflection.TT representation
306+reflect :: Term -> Raw
307+reflect = reflectTTQuote []
308+
309+-- | Lift a term into its Language.Reflection.Raw representation
310+reflectRaw :: Raw -> Raw
311+reflectRaw = reflectRawQuote []
312+
313+claimTT :: Name -> ElabD Name
314+claimTT n = do n' <- getNameFrom n
315+ claim n' (Var (sNS (sUN "TT") ["Reflection", "Language"]))
316+ return n'
317+
318+-- | Convert a reflected term to a more suitable form for pattern-matching.
319+-- In particular, the less-interesting bits are elaborated to _ patterns. This
320+-- happens to NameTypes, universe levels, names that are bound but not used,
321+-- and the type annotation field of the P constructor.
322+reflectTTQuotePattern :: [Name] -> Term -> ElabD ()
323+reflectTTQuotePattern unq (P _ n _)
324+ | n `elem` unq = -- the unquoted names have been claimed as TT already - just use them
325+ do fill (Var n) ; solve
326+ | otherwise =
327+ do tyannot <- claimTT (sMN 0 "pTyAnnot")
328+ movelast tyannot -- use a _ pattern here
329+ nt <- getNameFrom (sMN 0 "nt")
330+ claim nt (Var (reflm "NameType"))
331+ movelast nt -- use a _ pattern here
332+ n' <- getNameFrom (sMN 0 "n")
333+ claim n' (Var (reflm "TTName"))
334+ fill $ reflCall "P" [Var nt, Var n', Var tyannot]
335+ solve
336+ focus n'; reflectNameQuotePattern n
337+reflectTTQuotePattern unq (V n)
338+ = do fill $ reflCall "V" [RConstant (I n)]
339+ solve
340+reflectTTQuotePattern unq (Bind n b x)
341+ = do x' <- claimTT (sMN 0 "sc")
342+ movelast x'
343+ b' <- getNameFrom (sMN 0 "binder")
344+ claim b' (RApp (Var (sNS (sUN "Binder") ["Reflection", "Language"]))
345+ (Var (sNS (sUN "TT") ["Reflection", "Language"])))
346+ if n `elem` freeNames x
347+ then do fill $ reflCall "Bind"
348+ [reflectName n,
349+ Var b',
350+ Var x']
351+ solve
352+ else do any <- getNameFrom (sMN 0 "anyName")
353+ claim any (Var (reflm "TTName"))
354+ movelast any
355+ fill $ reflCall "Bind"
356+ [Var any,
357+ Var b',
358+ Var x']
359+ solve
360+ focus x'; reflectTTQuotePattern unq x
361+ focus b'; reflectBinderQuotePattern reflectTTQuotePattern unq b
362+reflectTTQuotePattern unq (App f x)
363+ = do f' <- claimTT (sMN 0 "f"); movelast f'
364+ x' <- claimTT (sMN 0 "x"); movelast x'
365+ fill $ reflCall "App" [Var f', Var x']
366+ solve
367+ focus f'; reflectTTQuotePattern unq f
368+ focus x'; reflectTTQuotePattern unq x
369+reflectTTQuotePattern unq (Constant c)
370+ = do fill $ reflCall "TConst" [reflectConstant c]
371+ solve
372+reflectTTQuotePattern unq (Proj t i)
373+ = do t' <- claimTT (sMN 0 "t"); movelast t'
374+ fill $ reflCall "Proj" [Var t', RConstant (I i)]
375+ solve
376+ focus t'; reflectTTQuotePattern unq t
377+reflectTTQuotePattern unq (Erased)
378+ = do erased <- claimTT (sMN 0 "erased")
379+ movelast erased
380+ fill $ (Var erased)
381+ solve
382+reflectTTQuotePattern unq (Impossible)
383+ = do fill $ Var (reflm "Impossible")
384+ solve
385+reflectTTQuotePattern unq (TType exp)
386+ = do ue <- getNameFrom (sMN 0 "uexp")
387+ claim ue (Var (sNS (sUN "TTUExp") ["Reflection", "Language"]))
388+ movelast ue
389+ fill $ reflCall "TType" [Var ue]
390+ solve
391+reflectTTQuotePattern unq (UType u)
392+ = do uH <- getNameFrom (sMN 0 "someUniv")
393+ claim uH (Var (reflm "Universe"))
394+ movelast uH
395+ fill $ reflCall "UType" [Var uH]
396+ solve
397+ focus uH
398+ fill (Var (reflm (case u of
399+ NullType -> "NullType"
400+ UniqueType -> "UniqueType"
401+ AllTypes -> "AllTypes")))
402+ solve
403+
404+reflectRawQuotePattern :: [Name] -> Raw -> ElabD ()
405+reflectRawQuotePattern unq (Var n)
406+ -- the unquoted names already have types, just use them
407+ | n `elem` unq = do fill (Var n); solve
408+ | otherwise = do fill (reflCall "Var" [reflectName n]); solve
409+reflectRawQuotePattern unq (RBind n b sc) =
410+ do scH <- getNameFrom (sMN 0 "sc")
411+ claim scH (Var (reflm "Raw"))
412+ movelast scH
413+ bH <- getNameFrom (sMN 0 "binder")
414+ claim bH (RApp (Var (reflm "Binder"))
415+ (Var (reflm "Raw")))
416+ if n `elem` freeNamesR sc
417+ then do fill $ reflCall "RBind" [reflectName n,
418+ Var bH,
419+ Var scH]
420+ solve
421+ else do any <- getNameFrom (sMN 0 "anyName")
422+ claim any (Var (reflm "TTName"))
423+ movelast any
424+ fill $ reflCall "RBind" [Var any, Var bH, Var scH]
425+ solve
426+ focus scH; reflectRawQuotePattern unq sc
427+ focus bH; reflectBinderQuotePattern reflectRawQuotePattern unq b
428+ where freeNamesR (Var n) = [n]
429+ freeNamesR (RBind n (Let t v) body) = concat [freeNamesR v,
430+ freeNamesR body \\ [n],
431+ freeNamesR t]
432+ freeNamesR (RBind n b body) = freeNamesR (binderTy b) ++
433+ (freeNamesR body \\ [n])
434+ freeNamesR (RApp f x) = freeNamesR f ++ freeNamesR x
435+ freeNamesR RType = []
436+ freeNamesR (RUType _) = []
437+ freeNamesR (RForce r) = freeNamesR r
438+ freeNamesR (RConstant _) = []
439+reflectRawQuotePattern unq (RApp f x) =
440+ do fH <- getNameFrom (sMN 0 "f")
441+ claim fH (Var (reflm "Raw"))
442+ movelast fH
443+ xH <- getNameFrom (sMN 0 "x")
444+ claim xH (Var (reflm "Raw"))
445+ movelast xH
446+ fill $ reflCall "RApp" [Var fH, Var xH]
447+ solve
448+ focus fH; reflectRawQuotePattern unq f
449+ focus xH; reflectRawQuotePattern unq x
450+reflectRawQuotePattern unq RType =
451+ do fill (Var (reflm "RType"))
452+ solve
453+reflectRawQuotePattern unq (RUType univ) =
454+ do uH <- getNameFrom (sMN 0 "universe")
455+ claim uH (Var (reflm "Universe"))
456+ movelast uH
457+ fill $ reflCall "RUType" [Var uH]
458+ solve
459+ focus uH; fill (reflectUniverse univ); solve
460+reflectRawQuotePattern unq (RForce r) =
461+ do rH <- getNameFrom (sMN 0 "raw")
462+ claim rH (Var (reflm "Raw"))
463+ movelast rH
464+ fill $ reflCall "RForce" [Var rH]
465+ solve
466+ focus rH; reflectRawQuotePattern unq r
467+reflectRawQuotePattern unq (RConstant c) =
468+ do cH <- getNameFrom (sMN 0 "const")
469+ claim cH (Var (reflm "Constant"))
470+ movelast cH
471+ fill (reflCall "RConstant" [Var cH]); solve
472+ focus cH
473+ fill (reflectConstant c); solve
474+
475+reflectBinderQuotePattern :: ([Name] -> a -> ElabD ()) -> [Name] -> Binder a -> ElabD ()
476+reflectBinderQuotePattern q unq (Lam t)
477+ = do t' <- claimTT (sMN 0 "ty"); movelast t'
478+ fill $ reflCall "Lam" [Var (reflm "TT"), Var t']
479+ solve
480+ focus t'; q unq t
481+reflectBinderQuotePattern q unq (Pi _ t k)
482+ = do t' <- claimTT (sMN 0 "ty") ; movelast t'
483+ k' <- claimTT (sMN 0 "k"); movelast k';
484+ fill $ reflCall "Pi" [Var (reflm "TT"), Var t', Var k']
485+ solve
486+ focus t'; q unq t
487+reflectBinderQuotePattern q unq (Let x y)
488+ = do x' <- claimTT (sMN 0 "ty"); movelast x';
489+ y' <- claimTT (sMN 0 "v"); movelast y';
490+ fill $ reflCall "Let" [Var (reflm "TT"), Var x', Var y']
491+ solve
492+ focus x'; q unq x
493+ focus y'; q unq y
494+reflectBinderQuotePattern q unq (NLet x y)
495+ = do x' <- claimTT (sMN 0 "ty"); movelast x'
496+ y' <- claimTT (sMN 0 "v"); movelast y'
497+ fill $ reflCall "NLet" [Var (reflm "TT"), Var x', Var y']
498+ solve
499+ focus x'; q unq x
500+ focus y'; q unq y
501+reflectBinderQuotePattern q unq (Hole t)
502+ = do t' <- claimTT (sMN 0 "ty"); movelast t'
503+ fill $ reflCall "Hole" [Var (reflm "TT"), Var t']
504+ solve
505+ focus t'; q unq t
506+reflectBinderQuotePattern q unq (GHole _ t)
507+ = do t' <- claimTT (sMN 0 "ty"); movelast t'
508+ fill $ reflCall "GHole" [Var (reflm "TT"), Var t']
509+ solve
510+ focus t'; q unq t
511+reflectBinderQuotePattern q unq (Guess x y)
512+ = do x' <- claimTT (sMN 0 "ty"); movelast x'
513+ y' <- claimTT (sMN 0 "v"); movelast y'
514+ fill $ reflCall "Guess" [Var (reflm "TT"), Var x', Var y']
515+ solve
516+ focus x'; q unq x
517+ focus y'; q unq y
518+reflectBinderQuotePattern q unq (PVar t)
519+ = do t' <- claimTT (sMN 0 "ty"); movelast t'
520+ fill $ reflCall "PVar" [Var (reflm "TT"), Var t']
521+ solve
522+ focus t'; q unq t
523+reflectBinderQuotePattern q unq (PVTy t)
524+ = do t' <- claimTT (sMN 0 "ty"); movelast t'
525+ fill $ reflCall "PVTy" [Var (reflm "TT"), Var t']
526+ solve
527+ focus t'; q unq t
528+
529+reflectUniverse :: Universe -> Raw
530+reflectUniverse u =
531+ (Var (reflm (case u of
532+ NullType -> "NullType"
533+ UniqueType -> "UniqueType"
534+ AllTypes -> "AllTypes")))
535+
536+-- | Create a reflected TT term, but leave refs to the provided name intact
537+reflectTTQuote :: [Name] -> Term -> Raw
538+reflectTTQuote unq (P nt n t)
539+ | n `elem` unq = Var n
540+ | otherwise = reflCall "P" [reflectNameType nt, reflectName n, reflectTTQuote unq t]
541+reflectTTQuote unq (V n)
542+ = reflCall "V" [RConstant (I n)]
543+reflectTTQuote unq (Bind n b x)
544+ = reflCall "Bind" [reflectName n, reflectBinderQuote reflectTTQuote (reflm "TT") unq b, reflectTTQuote unq x]
545+reflectTTQuote unq (App f x)
546+ = reflCall "App" [reflectTTQuote unq f, reflectTTQuote unq x]
547+reflectTTQuote unq (Constant c)
548+ = reflCall "TConst" [reflectConstant c]
549+reflectTTQuote unq (Proj t i)
550+ = reflCall "Proj" [reflectTTQuote unq t, RConstant (I i)]
551+reflectTTQuote unq (Erased) = Var (reflm "Erased")
552+reflectTTQuote unq (Impossible) = Var (reflm "Impossible")
553+reflectTTQuote unq (TType exp) = reflCall "TType" [reflectUExp exp]
554+reflectTTQuote unq (UType u) = reflCall "UType" [reflectUniverse u]
555+
556+reflectRawQuote :: [Name] -> Raw -> Raw
557+reflectRawQuote unq (Var n)
558+ | n `elem` unq = Var n
559+ | otherwise = reflCall "Var" [reflectName n]
560+reflectRawQuote unq (RBind n b r) =
561+ reflCall "RBind" [reflectName n, reflectBinderQuote reflectRawQuote (reflm "Raw") unq b, reflectRawQuote unq r]
562+reflectRawQuote unq (RApp f x) =
563+ reflCall "RApp" [reflectRawQuote unq f, reflectRawQuote unq x]
564+reflectRawQuote unq RType = Var (reflm "RType")
565+reflectRawQuote unq (RUType u) =
566+ reflCall "RUType" [reflectUniverse u]
567+reflectRawQuote unq (RForce r) = reflCall "RForce" [reflectRawQuote unq r]
568+reflectRawQuote unq (RConstant cst) = reflCall "RConstant" [reflectConstant cst]
569+
570+reflectNameType :: NameType -> Raw
571+reflectNameType (Bound) = Var (reflm "Bound")
572+reflectNameType (Ref) = Var (reflm "Ref")
573+reflectNameType (DCon x y _)
574+ = reflCall "DCon" [RConstant (I x), RConstant (I y)] -- FIXME: Uniqueness!
575+reflectNameType (TCon x y)
576+ = reflCall "TCon" [RConstant (I x), RConstant (I y)]
577+
578+reflectName :: Name -> Raw
579+reflectName (UN s)
580+ = reflCall "UN" [RConstant (Str (str s))]
581+reflectName (NS n ns)
582+ = reflCall "NS" [ reflectName n
583+ , foldr (\ n s ->
584+ raw_apply ( Var $ sNS (sUN "::") ["List", "Prelude"] )
585+ [ RConstant StrType, RConstant (Str n), s ])
586+ ( raw_apply ( Var $ sNS (sUN "Nil") ["List", "Prelude"] )
587+ [ RConstant StrType ])
588+ (map str ns)
589+ ]
590+reflectName (MN i n)
591+ = reflCall "MN" [RConstant (I i), RConstant (Str (str n))]
592+reflectName (NErased) = Var (reflm "NErased")
593+reflectName n = Var (reflm "NErased") -- special name, not yet implemented
594+
595+-- | Elaborate a name to a pattern. This means that NS and UN will be intact.
596+-- MNs corresponding to will care about the string but not the number. All
597+-- others become _.
598+reflectNameQuotePattern :: Name -> ElabD ()
599+reflectNameQuotePattern n@(UN s)
600+ = do fill $ reflectName n
601+ solve
602+reflectNameQuotePattern n@(NS _ _)
603+ = do fill $ reflectName n
604+ solve
605+reflectNameQuotePattern (MN _ n)
606+ = do i <- getNameFrom (sMN 0 "mnCounter")
607+ claim i (RConstant (AType (ATInt ITNative)))
608+ movelast i
609+ fill $ reflCall "MN" [Var i, RConstant (Str $ T.unpack n)]
610+ solve
611+reflectNameQuotePattern _ -- for all other names, match any
612+ = do nameHole <- getNameFrom (sMN 0 "name")
613+ claim nameHole (Var (reflm "TTName"))
614+ movelast nameHole
615+ fill (Var nameHole)
616+ solve
617+
618+reflectBinder :: Binder Term -> Raw
619+reflectBinder = reflectBinderQuote reflectTTQuote (reflm "TT") []
620+
621+reflectBinderQuote :: ([Name] -> a -> Raw) -> Name -> [Name] -> Binder a -> Raw
622+reflectBinderQuote q ty unq (Lam t)
623+ = reflCall "Lam" [Var ty, q unq t]
624+reflectBinderQuote q ty unq (Pi _ t k)
625+ = reflCall "Pi" [Var ty, q unq t, q unq k]
626+reflectBinderQuote q ty unq (Let x y)
627+ = reflCall "Let" [Var ty, q unq x, q unq y]
628+reflectBinderQuote q ty unq (NLet x y)
629+ = reflCall "NLet" [Var ty, q unq x, q unq y]
630+reflectBinderQuote q ty unq (Hole t)
631+ = reflCall "Hole" [Var ty, q unq t]
632+reflectBinderQuote q ty unq (GHole _ t)
633+ = reflCall "GHole" [Var ty, q unq t]
634+reflectBinderQuote q ty unq (Guess x y)
635+ = reflCall "Guess" [Var ty, q unq x, q unq y]
636+reflectBinderQuote q ty unq (PVar t)
637+ = reflCall "PVar" [Var ty, q unq t]
638+reflectBinderQuote q ty unq (PVTy t)
639+ = reflCall "PVTy" [Var ty, q unq t]
640+
641+mkList :: Raw -> [Raw] -> Raw
642+mkList ty [] = RApp (Var (sNS (sUN "Nil") ["List", "Prelude"])) ty
643+mkList ty (x:xs) = RApp (RApp (RApp (Var (sNS (sUN "::") ["List", "Prelude"])) ty)
644+ x)
645+ (mkList ty xs)
646+
647+reflectConstant :: Const -> Raw
648+reflectConstant c@(I _) = reflCall "I" [RConstant c]
649+reflectConstant c@(BI _) = reflCall "BI" [RConstant c]
650+reflectConstant c@(Fl _) = reflCall "Fl" [RConstant c]
651+reflectConstant c@(Ch _) = reflCall "Ch" [RConstant c]
652+reflectConstant c@(Str _) = reflCall "Str" [RConstant c]
653+reflectConstant c@(B8 _) = reflCall "B8" [RConstant c]
654+reflectConstant c@(B16 _) = reflCall "B16" [RConstant c]
655+reflectConstant c@(B32 _) = reflCall "B32" [RConstant c]
656+reflectConstant c@(B64 _) = reflCall "B64" [RConstant c]
657+reflectConstant (AType (ATInt ITNative)) = reflCall "AType" [reflCall "ATInt" [Var (reflm "ITNative")]]
658+reflectConstant (AType (ATInt ITBig)) = reflCall "AType" [reflCall "ATInt" [Var (reflm "ITBig")]]
659+reflectConstant (AType ATFloat) = reflCall "AType" [Var (reflm "ATFloat")]
660+reflectConstant (AType (ATInt ITChar)) = reflCall "AType" [reflCall "ATInt" [Var (reflm "ITChar")]]
661+reflectConstant StrType = Var (reflm "StrType")
662+reflectConstant (AType (ATInt (ITFixed IT8))) = reflCall "AType" [reflCall "ATInt" [reflCall "ITFixed" [Var (reflm "IT8")]]]
663+reflectConstant (AType (ATInt (ITFixed IT16))) = reflCall "AType" [reflCall "ATInt" [reflCall "ITFixed" [Var (reflm "IT16")]]]
664+reflectConstant (AType (ATInt (ITFixed IT32))) = reflCall "AType" [reflCall "ATInt" [reflCall "ITFixed" [Var (reflm "IT32")]]]
665+reflectConstant (AType (ATInt (ITFixed IT64))) = reflCall "AType" [reflCall "ATInt" [reflCall "ITFixed" [Var (reflm "IT64")]]]
666+reflectConstant VoidType = Var (reflm "VoidType")
667+reflectConstant Forgot = Var (reflm "Forgot")
668+reflectConstant WorldType = Var (reflm "WorldType")
669+reflectConstant TheWorld = Var (reflm "TheWorld")
670+
671+reflectUExp :: UExp -> Raw
672+reflectUExp (UVar i) = reflCall "UVar" [RConstant (I i)]
673+reflectUExp (UVal i) = reflCall "UVal" [RConstant (I i)]
674+
675+-- | Reflect the environment of a proof into a List (TTName, Binder TT)
676+reflectEnv :: Env -> Raw
677+reflectEnv = foldr consToEnvList emptyEnvList
678+ where
679+ consToEnvList :: (Name, Binder Term) -> Raw -> Raw
680+ consToEnvList (n, b) l
681+ = raw_apply (Var (sNS (sUN "::") ["List", "Prelude"]))
682+ [ envTupleType
683+ , raw_apply (Var pairCon) [ (Var $ reflm "TTName")
684+ , (RApp (Var $ reflm "Binder")
685+ (Var $ reflm "TT"))
686+ , reflectName n
687+ , reflectBinder b
688+ ]
689+ , l
690+ ]
691+
692+ emptyEnvList :: Raw
693+ emptyEnvList = raw_apply (Var (sNS (sUN "Nil") ["List", "Prelude"]))
694+ [envTupleType]
695+
696+-- | Reflect an error into the internal datatype of Idris -- TODO
697+rawBool :: Bool -> Raw
698+rawBool True = Var (sNS (sUN "True") ["Bool", "Prelude"])
699+rawBool False = Var (sNS (sUN "False") ["Bool", "Prelude"])
700+
701+rawNil :: Raw -> Raw
702+rawNil ty = raw_apply (Var (sNS (sUN "Nil") ["List", "Prelude"])) [ty]
703+
704+rawCons :: Raw -> Raw -> Raw -> Raw
705+rawCons ty hd tl = raw_apply (Var (sNS (sUN "::") ["List", "Prelude"])) [ty, hd, tl]
706+
707+rawList :: Raw -> [Raw] -> Raw
708+rawList ty = foldr (rawCons ty) (rawNil ty)
709+
710+rawPairTy :: Raw -> Raw -> Raw
711+rawPairTy t1 t2 = raw_apply (Var pairTy) [t1, t2]
712+
713+rawPair :: (Raw, Raw) -> (Raw, Raw) -> Raw
714+rawPair (a, b) (x, y) = raw_apply (Var pairCon) [a, b, x, y]
715+
716+reflectCtxt :: [(Name, Type)] -> Raw
717+reflectCtxt ctxt = rawList (rawPairTy (Var $ reflm "TTName") (Var $ reflm "TT"))
718+ (map (\ (n, t) -> (rawPair (Var $ reflm "TTName", Var $ reflm "TT")
719+ (reflectName n, reflect t)))
720+ ctxt)
721+
722+reflectErr :: Err -> Raw
723+reflectErr (Msg msg) = raw_apply (Var $ reflErrName "Msg") [RConstant (Str msg)]
724+reflectErr (InternalMsg msg) = raw_apply (Var $ reflErrName "InternalMsg") [RConstant (Str msg)]
725+reflectErr (CantUnify b (t1,_) (t2,_) e ctxt i) =
726+ raw_apply (Var $ reflErrName "CantUnify")
727+ [ rawBool b
728+ , reflect t1
729+ , reflect t2
730+ , reflectErr e
731+ , reflectCtxt ctxt
732+ , RConstant (I i)]
733+reflectErr (InfiniteUnify n tm ctxt) =
734+ raw_apply (Var $ reflErrName "InfiniteUnify")
735+ [ reflectName n
736+ , reflect tm
737+ , reflectCtxt ctxt
738+ ]
739+reflectErr (CantConvert t t' ctxt) =
740+ raw_apply (Var $ reflErrName "CantConvert")
741+ [ reflect t
742+ , reflect t'
743+ , reflectCtxt ctxt
744+ ]
745+reflectErr (CantSolveGoal t ctxt) =
746+ raw_apply (Var $ reflErrName "CantSolveGoal")
747+ [ reflect t
748+ , reflectCtxt ctxt
749+ ]
750+reflectErr (UnifyScope n n' t ctxt) =
751+ raw_apply (Var $ reflErrName "UnifyScope")
752+ [ reflectName n
753+ , reflectName n'
754+ , reflect t
755+ , reflectCtxt ctxt
756+ ]
757+reflectErr (CantInferType str) =
758+ raw_apply (Var $ reflErrName "CantInferType") [RConstant (Str str)]
759+reflectErr (NonFunctionType t t') =
760+ raw_apply (Var $ reflErrName "NonFunctionType") [reflect t, reflect t']
761+reflectErr (NotEquality t t') =
762+ raw_apply (Var $ reflErrName "NotEquality") [reflect t, reflect t']
763+reflectErr (TooManyArguments n) = raw_apply (Var $ reflErrName "TooManyArguments") [reflectName n]
764+reflectErr (CantIntroduce t) = raw_apply (Var $ reflErrName "CantIntroduce") [reflect t]
765+reflectErr (NoSuchVariable n) = raw_apply (Var $ reflErrName "NoSuchVariable") [reflectName n]
766+reflectErr (WithFnType t) = raw_apply (Var $ reflErrName "WithFnType") [reflect t]
767+reflectErr (CantMatch t) = raw_apply (Var $ reflErrName "CantMatch") [reflect t]
768+reflectErr (NoTypeDecl n) = raw_apply (Var $ reflErrName "NoTypeDecl") [reflectName n]
769+reflectErr (NotInjective t1 t2 t3) =
770+ raw_apply (Var $ reflErrName "NotInjective")
771+ [ reflect t1
772+ , reflect t2
773+ , reflect t3
774+ ]
775+reflectErr (CantResolve _ t) = raw_apply (Var $ reflErrName "CantResolve") [reflect t]
776+reflectErr (InvalidTCArg n t) = raw_apply (Var $ reflErrName "InvalidTCArg") [reflectName n, reflect t]
777+reflectErr (CantResolveAlts ss) =
778+ raw_apply (Var $ reflErrName "CantResolveAlts")
779+ [rawList (Var $ reflm "TTName") (map reflectName ss)]
780+reflectErr (IncompleteTerm t) = raw_apply (Var $ reflErrName "IncompleteTerm") [reflect t]
781+reflectErr (NoEliminator str t)
782+ = raw_apply (Var $ reflErrName "NoEliminator") [RConstant (Str str),
783+ reflect t]
784+reflectErr UniverseError = Var $ reflErrName "UniverseError"
785+reflectErr ProgramLineComment = Var $ reflErrName "ProgramLineComment"
786+reflectErr (Inaccessible n) = raw_apply (Var $ reflErrName "Inaccessible") [reflectName n]
787+reflectErr (NonCollapsiblePostulate n) = raw_apply (Var $ reflErrName "NonCollabsiblePostulate") [reflectName n]
788+reflectErr (AlreadyDefined n) = raw_apply (Var $ reflErrName "AlreadyDefined") [reflectName n]
789+reflectErr (ProofSearchFail e) = raw_apply (Var $ reflErrName "ProofSearchFail") [reflectErr e]
790+reflectErr (NoRewriting tm) = raw_apply (Var $ reflErrName "NoRewriting") [reflect tm]
791+reflectErr (ProviderError str) =
792+ raw_apply (Var $ reflErrName "ProviderError") [RConstant (Str str)]
793+reflectErr (LoadingFailed str err) =
794+ raw_apply (Var $ reflErrName "LoadingFailed") [RConstant (Str str)]
795+reflectErr x = raw_apply (Var (sNS (sUN "Msg") ["Errors", "Reflection", "Language"])) [RConstant . Str $ "Default reflection: " ++ show x]
796+
797+-- | Reflect a file context
798+reflectFC :: FC -> Raw
799+reflectFC fc = raw_apply (Var (reflm "FileLoc"))
800+ [ RConstant (Str (fc_fname fc))
801+ , raw_apply (Var pairCon) $
802+ [intTy, intTy] ++
803+ map (RConstant . I)
804+ [ fst (fc_start fc)
805+ , snd (fc_start fc)
806+ ]
807+ , raw_apply (Var pairCon) $
808+ [intTy, intTy] ++
809+ map (RConstant . I)
810+ [ fst (fc_end fc)
811+ , snd (fc_end fc)
812+ ]
813+ ]
814+ where intTy = RConstant (AType (ATInt ITNative))
815+
816+fromTTMaybe :: Term -> Maybe Term -- WARNING: Assumes the term has type Maybe a
817+fromTTMaybe (App (App (P (DCon _ _ _) (NS (UN just) _) _) ty) tm)
818+ | just == txt "Just" = Just tm
819+fromTTMaybe x = Nothing
820+
821+reflErrName :: String -> Name
822+reflErrName n = sNS (sUN n) ["Errors", "Reflection", "Language"]
823+
824+-- | Attempt to reify a report part from TT to the internal
825+-- representation. Not in Idris or ElabD monads because it should be usable
826+-- from either.
827+reifyReportPart :: Term -> Either Err ErrorReportPart
828+reifyReportPart (App (P (DCon _ _ _) n _) (Constant (Str msg))) | n == reflm "TextPart" =
829+ Right (TextPart msg)
830+reifyReportPart (App (P (DCon _ _ _) n _) ttn)
831+ | n == reflm "NamePart" =
832+ case runElab initEState (reifyTTName ttn) (initElaborator NErased initContext Erased) of
833+ Error e -> Left . InternalMsg $
834+ "could not reify name term " ++
835+ show ttn ++
836+ " when reflecting an error:" ++ show e
837+ OK (n', _)-> Right $ NamePart n'
838+reifyReportPart (App (P (DCon _ _ _) n _) tm)
839+ | n == reflm "TermPart" =
840+ case runElab initEState (reifyTT tm) (initElaborator NErased initContext Erased) of
841+ Error e -> Left . InternalMsg $
842+ "could not reify reflected term " ++
843+ show tm ++
844+ " when reflecting an error:" ++ show e
845+ OK (tm', _) -> Right $ TermPart tm'
846+reifyReportPart (App (P (DCon _ _ _) n _) tm)
847+ | n == reflm "SubReport" =
848+ case unList tm of
849+ Just xs -> do subParts <- mapM reifyReportPart xs
850+ Right (SubReport subParts)
851+ Nothing -> Left . InternalMsg $ "could not reify subreport " ++ show tm
852+reifyReportPart x = Left . InternalMsg $ "could not reify " ++ show x
853+
854+reifyTyDecl :: Term -> ElabD RTyDecl
855+reifyTyDecl (App (App (App (P (DCon _ _ _) n _) tyN) args) ret)
856+ | n == tacN "Declare" =
857+ do tyN' <- reifyTTName tyN
858+ args' <- case unList args of
859+ Nothing -> fail $ "Couldn't reify " ++ show args ++ " as an arglist."
860+ Just xs -> mapM reifyRArg xs
861+ ret' <- reifyRaw ret
862+ return $ RDeclare tyN' args' ret'
863+ where reifyRArg :: Term -> ElabD RArg
864+ reifyRArg (App (App (P (DCon _ _ _) n _) argN) argTy)
865+ | n == tacN "Explicit" = liftM2 RExplicit
866+ (reifyTTName argN)
867+ (reifyRaw argTy)
868+ | n == tacN "Implicit" = liftM2 RImplicit
869+ (reifyTTName argN)
870+ (reifyRaw argTy) | n == tacN "Constraint" = liftM2 RConstraint
871+ (reifyTTName argN)
872+ (reifyRaw argTy)
873+ reifyRArg aTm = fail $ "Couldn't reify " ++ show aTm ++ " as an RArg."
874+reifyTyDecl tm = fail $ "Couldn't reify " ++ show tm ++ " as a type declaration."
875+
876+reifyFunDefn :: Term -> ElabD RFunDefn
877+reifyFunDefn (App (App (P _ n _) fnN) clauses)
878+ | n == tacN "DefineFun" =
879+ do fnN' <- reifyTTName fnN
880+ clauses' <- case unList clauses of
881+ Nothing -> fail $ "Couldn't reify " ++ show clauses ++ " as a clause list"
882+ Just cs -> mapM reifyC cs
883+ return $ RDefineFun fnN' clauses'
884+ where reifyC :: Term -> ElabD RFunClause
885+ reifyC (App (App (P (DCon _ _ _) n _) lhs) rhs)
886+ | n == tacN "MkFunClause" = liftM2 RMkFunClause
887+ (reifyRaw lhs)
888+ (reifyRaw rhs)
889+ reifyC (App (P (DCon _ _ _) n _) lhs)
890+ | n == tacN "MkImpossibleClause" = fmap RMkImpossibleClause $ reifyRaw lhs
891+ reifyC tm = fail $ "Couldn't reify " ++ show tm ++ " as a clause."
892+reifyFunDefn tm = fail $ "Couldn't reify " ++ show tm ++ " as a function declaration."
893+
894+envTupleType :: Raw
895+envTupleType
896+ = raw_apply (Var pairTy) [ (Var $ reflm "TTName")
897+ , (RApp (Var $ reflm "Binder") (Var $ reflm "TT"))
898+ ]