@@ -18,6 +18,7 @@ import Idris.Core.Typecheck (check, recheck)
18import Idris.ErrReverse (errReverse)
18import Idris.ErrReverse (errReverse)
19import Idris.ElabQuasiquote (extractUnquotes)
19import Idris.ElabQuasiquote (extractUnquotes)
20import Idris.Elab.Utils
20import Idris.Elab.Utils
21+import Idris.Reflection
21import qualified Util.Pretty as U
22import qualified Util.Pretty as U
23import Control.Applicative ((<$>))
24import Control.Applicative ((<$>))
@@ -36,6 +37,32 @@ import Debug.Trace
36data ElabMode = ETyDecl | ELHS | ERHS
37data ElabMode = ETyDecl | ELHS | ERHS
37 deriving Eq
38 deriving Eq
41+ ElabResult { resultTerm :: Term -- ^ The term resulting from elaboration
42+ , resultMetavars :: [(Name, (Int, Maybe Name, Type))]
43+ -- ^ Information about new metavariables
44+ , resultCaseDecls :: [PDecl]
45+ -- ^ Deferred declarations as the meaning of case blocks
46+ , resultContext :: Context
47+ -- ^ The potentially extended context from new definitions
48+ , resultTyDecls :: [(Name, FC, [PArg], Type)]
49+ -- ^ Meta-info about the new type declarations
52+processTacticDecls :: [(Name, FC, [PArg], Type)] -> Idris ()
53+processTacticDecls info =
54+ forM_ info $ \(n, fc, impls, ty) ->
55+ do logLvl 3 $ "Declaration from tactics: " ++ show n ++ " : " ++ show ty
56+ logLvl 3 $ " It has impls " ++ show impls
57+ updateIState $ \i -> i { idris_implicits =
58+ addDef n impls (idris_implicits i) }
60+ ds <- checkDef fc iderr [(n, (-1, Nothing, ty))]
62+ let ds' = map (\(n, (i, top, t)) -> (n, (i, top, t, True))) ds
39-- Using the elaborator, convert a term in raw syntax to a fully
66-- Using the elaborator, convert a term in raw syntax to a fully
40-- elaborated, typechecked term.
67-- elaborated, typechecked term.
@@ -45,7 +72,7 @@ data ElabMode = ETyDecl | ELHS | ERHS
45-- Also find deferred names in the term and their types
72-- Also find deferred names in the term and their types
47build :: IState -> ElabInfo -> ElabMode -> FnOpts -> Name -> PTerm ->
74build :: IState -> ElabInfo -> ElabMode -> FnOpts -> Name -> PTerm ->
48− ElabD (Term, [(Name, (Int, Maybe Name, Type))], [PDecl])
75+ ElabD ElabResult
49build ist info emode opts fn tm
76build ist info emode opts fn tm
50 = do elab ist info emode opts fn tm
77 = do elab ist info emode opts fn tm
51 let tmIn = tm
78 let tmIn = tm
@@ -64,7 +91,7 @@ build ist info emode opts fn tm
64 mapM_ (\n -> when (n `elem` hs) $
91 mapM_ (\n -> when (n `elem` hs) $
65 do focus n
92 do focus n
67− try (resolveTC True 7 g fn ist)
94+ try (resolveTC True False 7 g fn ist)
68 (movelast n)) ivs
95 (movelast n)) ivs
69 ivs <- get_instances
96 ivs <- get_instances
70 hs <- get_holes
97 hs <- get_holes
@@ -72,7 +99,8 @@ build ist info emode opts fn tm
72 mapM_ (\n -> when (n `elem` hs) $
99 mapM_ (\n -> when (n `elem` hs) $
73 do focus n
100 do focus n
74 g <- goal
101 g <- goal
75− resolveTC True 7 g fn ist) ivs
103+ resolveTC True True 7 g fn ist) ivs
76 tm <- get_term
104 tm <- get_term
77 ctxt <- get_context
105 ctxt <- get_context
78 probs <- get_probs
106 probs <- get_probs
@@ -94,12 +122,13 @@ build ist info emode opts fn tm
94 when tydecl (do update_term orderPats
122 when tydecl (do update_term orderPats
96-- update_term liftPats)
124-- update_term liftPats)
97− EState is _ <- getAux
125+ EState is _ impls <- getAux
98 tt <- get_term
126 tt <- get_term
99 let (tm, ds) = runState (collectDeferred (Just fn) tt) []
127 let (tm, ds) = runState (collectDeferred (Just fn) tt) []
100 log <- getLog
128 log <- getLog
101− if (log /= "") then trace log $ return (tm, ds, is)
130+ if (log /= "") then trace log $ return (ElabResult tm ds is ctxt impls)
102− else return (tm, ds, is)
131+ else return (ElabResult tm ds is ctxt impls)
103 where pattern = emode == ELHS
132 where pattern = emode == ELHS
104 tydecl = emode == ETyDecl
133 tydecl = emode == ETyDecl
@@ -114,7 +143,7 @@ build ist info emode opts fn tm
114-- know about yet on the LHS of a pattern def)
143-- know about yet on the LHS of a pattern def)
116buildTC :: IState -> ElabInfo -> ElabMode -> FnOpts -> Name -> PTerm ->
145buildTC :: IState -> ElabInfo -> ElabMode -> FnOpts -> Name -> PTerm ->
117− ElabD (Term, [(Name, (Int, Maybe Name, Type))], [PDecl])
146+ ElabD ElabResult
118buildTC ist info emode opts fn tm
147buildTC ist info emode opts fn tm
119 = do -- set name supply to begin after highest index in tm
148 = do -- set name supply to begin after highest index in tm
120 let ns = allNamesIn tm
149 let ns = allNamesIn tm
@@ -135,12 +164,13 @@ buildTC ist info emode opts fn tm
135 -- unification
164 -- unification
136 when (not (null dots)) $
165 when (not (null dots)) $
137 lift (Error (CantMatch (getInferTerm tm)))
166 lift (Error (CantMatch (getInferTerm tm)))
138− EState is _ <- getAux
167+ EState is _ impls <- getAux
139 tt <- get_term
168 tt <- get_term
140 let (tm, ds) = runState (collectDeferred (Just fn) tt) []
169 let (tm, ds) = runState (collectDeferred (Just fn) tt) []
141 log <- getLog
170 log <- getLog
142− if (log /= "") then trace log $ return (tm, ds, is)
172+ if (log /= "") then trace log $ return (ElabResult tm ds is ctxt impls)
143− else return (tm, ds, is)
173+ else return (ElabResult tm ds is ctxt impls)
144 where pattern = emode == ELHS
174 where pattern = emode == ELHS
146-- return whether arguments of the given constructor name can be
176-- return whether arguments of the given constructor name can be
@@ -183,9 +213,9 @@ goal_polymorphic =
183 _ -> return True
213 _ -> return True
184 _ -> return False
214 _ -> return False
186−-- Returns the set of declarations we need to add to complete the definition
216+-- | Returns the set of declarations we need to add to complete the
187−-- (most likely case blocks to elaborate)
217+-- definition (most likely case blocks to elaborate) as well as
188−
218+-- declarations resulting from user tactic scripts (%runTactics)
189elab :: IState -> ElabInfo -> ElabMode -> FnOpts -> Name -> PTerm ->
219elab :: IState -> ElabInfo -> ElabMode -> FnOpts -> Name -> PTerm ->
191elab ist info emode opts fn tm
221elab ist info emode opts fn tm
@@ -321,7 +351,7 @@ elab ist info emode opts fn tm
321 UType _ -> elab' ina (Just fc) (PRef fc unitTy)
351 UType _ -> elab' ina (Just fc) (PRef fc unitTy)
322 _ -> elab' ina (Just fc) (PRef fc unitCon)
352 _ -> elab' ina (Just fc) (PRef fc unitCon)
323 elab' ina fc (PResolveTC (FC "HACK" _ _)) -- for chasing parent classes
353 elab' ina fc (PResolveTC (FC "HACK" _ _)) -- for chasing parent classes
324− = do g <- goal; resolveTC False 5 g fn ist
354+ = do g <- goal; resolveTC False False 5 g fn ist
325 elab' ina fc (PResolveTC fc')
355 elab' ina fc (PResolveTC fc')
326 = do c <- getNameFrom (sMN 0 "class")
356 = do c <- getNameFrom (sMN 0 "class")
327 instanceArg c
357 instanceArg c
@@ -540,7 +570,7 @@ elab ist info emode opts fn tm
540 g <- goal
570 g <- goal
541 hs <- get_holes
571 hs <- get_holes
542 if all (\n -> n == tyn || not (n `elem` hs)) (freeNames g)
572 if all (\n -> n == tyn || not (n `elem` hs)) (freeNames g)
543− then try (resolveTC True 7 g fn ist)
573+ then try (resolveTC True False 7 g fn ist)
544 (movelast n)
574 (movelast n)
545 else movelast n)
575 else movelast n)
546 (ivs' \\ ivs)
576 (ivs' \\ ivs)
@@ -700,7 +730,7 @@ elab ist info emode opts fn tm
700 env <- get_env
730 env <- get_env
701 hs <- get_holes
731 hs <- get_holes
702 if all (\n -> not (n `elem` hs)) (freeNames g)
732 if all (\n -> not (n `elem` hs)) (freeNames g)
703− then try (resolveTC False 7 g fn ist)
733+ then try (resolveTC False False 7 g fn ist)
704 (movelast n)
734 (movelast n)
705 else movelast n)
735 else movelast n)
706 (ivs' \\ ivs)
736 (ivs' \\ ivs)
@@ -965,17 +995,14 @@ elab ist info emode opts fn tm
965 elab' ina fc (PUnifyLog t) = do unifyLog True
995 elab' ina fc (PUnifyLog t) = do unifyLog True
966 elab' ina fc t
996 elab' ina fc t
967 unifyLog False
997 unifyLog False
968− elab' ina fc (PQuasiquote t goal)
998+ elab' ina fc (PQuasiquote t goalt)
969 = do -- First extract the unquoted subterms, replacing them with fresh
999 = do -- First extract the unquoted subterms, replacing them with fresh
970 -- names in the quasiquoted term. Claim their reflections to be
1000 -- names in the quasiquoted term. Claim their reflections to be
971 -- an inferred type (to support polytypic quasiquotes).
1001 -- an inferred type (to support polytypic quasiquotes).
972 (t, unq) <- extractUnquotes 0 t
1003 (t, unq) <- extractUnquotes 0 t
973 let unquoteNames = map fst unq
1004 let unquoteNames = map fst unq
974− mapM_ (\uqn -> do uqh <- getNameFrom (sMN 0 "uqh")
977− claim uqn (Var uqh)) unquoteNames
1005+ mapM_ (\uqn -> claim uqn (forget finalTy)) unquoteNames
980 -- Save the old state - we need a fresh proof state to avoid
1007 -- Save the old state - we need a fresh proof state to avoid
981 -- capturing lexically available variables in the quoted term.
1008 -- capturing lexically available variables in the quoted term.
@@ -1010,7 +1037,7 @@ elab ist info emode opts fn tm
1010 letbind nTm (Var qTy) (Var qTm)
1037 letbind nTm (Var qTy) (Var qTm)
1012 -- Fill out the goal type, if relevant
1039 -- Fill out the goal type, if relevant
1013− case goal of
1040+ case goalt of
1014 Nothing -> return ()
1041 Nothing -> return ()
1015 Just gTy -> do focus qTy
1042 Just gTy -> do focus qTy
1016 elabE (ina { e_qq = True }) fc gTy
1043 elabE (ina { e_qq = True }) fc gTy
@@ -1025,18 +1052,20 @@ elab ist info emode opts fn tm
1025 env <- get_env
1052 env <- get_env
1026 loadState
1053 loadState
1027 let quoted = fmap (explicitNames . binderVal) $ lookup nTm env
1054 let quoted = fmap (explicitNames . binderVal) $ lookup nTm env
1028−
1055+ isRaw = case unApply (normaliseAll ctxt env finalTy) of
1056+ (P _ n _, []) | n == reflm "Raw" -> True
1029 case quoted of
1058 case quoted of
1030 Just q -> do ctxt <- get_context
1059 Just q -> do ctxt <- get_context
1031 (q', _, _) <- lift $ recheck ctxt [(uq, Lam Erased) | uq <- unquoteNames] (forget q) q
1060 (q', _, _) <- lift $ recheck ctxt [(uq, Lam Erased) | uq <- unquoteNames] (forget q) q
1032 if pattern
1061 if pattern
1063+ then reflectRawQuotePattern unquoteNames (forget q')
1033− then do try' (reflectTTQuotePattern unquoteNames q')
1064+ else reflectTTQuotePattern unquoteNames q'
1034− (reflectRawQuotePattern unquoteNames (forget q'))
1065+ else do if isRaw
1036− else do try' (fill $ reflectTTQuote unquoteNames q')
1037− -- we forget q' instead of using q to ensure rechecking
1066+ then -- we forget q' instead of using q to ensure rechecking
1038− (fill $ reflectRawQuote unquoteNames (forget q'))
1067+ fill $ reflectRawQuote unquoteNames (forget q')
1039− True -- ignore errors like proof search does
1068+ else fill $ reflectTTQuote unquoteNames q'
1042 Nothing -> lift . tfail . Msg $ "Broken elaboration of quasiquote"
1071 Nothing -> lift . tfail . Msg $ "Broken elaboration of quasiquote"
@@ -1077,8 +1106,8 @@ elab ist info emode opts fn tm
1078 elab' ina (Just fc') tm
1107 elab' ina (Just fc') tm
1079 env <- get_env
1108 env <- get_env
1080− ctxt <- get_context
1081− runTactical (maybe fc' id fc) ctxt env (P Bound n' Erased)
1109+ runTactical (maybe fc' id fc) env (P Bound n' Erased)
1110+ EState _ _ todo <- getAux
1083 elab' ina fc x = fail $ "Unelaboratable syntactic form " ++ showTmImpls x
1112 elab' ina fc x = fail $ "Unelaboratable syntactic form " ++ showTmImpls x
@@ -1346,9 +1375,12 @@ findInstances :: IState -> Term -> [Name]
1346findInstances ist t
1375findInstances ist t
1347 | (P _ n _, _) <- unApply t
1376 | (P _ n _, _) <- unApply t
1348 = case lookupCtxt n (idris_classes ist) of
1377 = case lookupCtxt n (idris_classes ist) of
1349− [CI _ _ _ _ _ ins] -> ins
1378+ [CI _ _ _ _ _ ins _] -> filter accessible ins
1351 | otherwise = []
1380 | otherwise = []
1381+ where accessible n = case lookupDefAccExact n False (tt_ctxt ist) of
1382+ Just (_, Hidden) -> False
1353-- Try again to solve auto implicits
1385-- Try again to solve auto implicits
1354solveAuto :: IState -> Name -> Bool -> Name -> ElabD ()
1386solveAuto :: IState -> Name -> Bool -> Name -> ElabD ()
@@ -1376,14 +1408,26 @@ proofSearch' ist rec ambigok depth prv top n hints
1376-- Resolve type classes. This will only pick up 'normal' instances, never
1408-- Resolve type classes. This will only pick up 'normal' instances, never
1377-- named instances (hence using 'tcname' to check it's a generated instance
1409-- named instances (hence using 'tcname' to check it's a generated instance
1378-- name).
1410-- name).
1411+resolveTC :: Bool -- using default Int
1412+ -> Bool -- allow metavariables in the goal
1414+ -> Term -- top level goal
1415+ -> Name -- top level function name
1379−resolveTC :: Bool -> Int -> Term -> Name -> IState -> ElabD ()
1416+ -> IState -> ElabD ()
1380−resolveTC = resTC' []
1417+resolveTC def mvok depth top fn ist
1381−
1418+ = do hs <- get_holes
1419+ resTC' [] def hs depth top fn ist
1382−resTC' tcs def 0 topg fn ist = fail $ "Can't resolve type class"
1421+resTC' tcs def topholes 0 topg fn ist = fail $ "Can't resolve type class"
1383−resTC' tcs def 1 topg fn ist = try' (trivial' ist) (resolveTC def 0 topg fn ist) True
1422+resTC' tcs def topholes 1 topg fn ist = try' (trivial' ist) (resolveTC def False 0 topg fn ist) True
1384−resTC' tcs defaultOn depth topg fn ist
1423+resTC' tcs defaultOn topholes depth topg fn ist
1385− = do compute
1424+ = do compute
1386− g <- goal
1425+ g <- goal
1426+ let argsok = tcArgsOK g topholes
1427+-- trace (show (g,hs,argsok,topholes)) $
1428+ if not argsok -- && not mvok)
1429+ then lift $ tfail $ CantResolve True topg
1387 ptm <- get_term
1431 ptm <- get_term
1388 ulog <- getUnifyLog
1432 ulog <- getUnifyLog
1389 hs <- get_holes
1433 hs <- get_holes
@@ -1400,6 +1444,31 @@ resTC' tcs defaultOn depth topg fn ist
1400 let depth' = if scopeOnly then 2 else depth
1444 let depth' = if scopeOnly then 2 else depth
1401 blunderbuss t depth' stk (stk ++ insts)) True
1445 blunderbuss t depth' stk (stk ++ insts)) True
1447+ tcArgsOK ty hs | (P _ nc _, as) <- unApply ty, nc == numclass && defaultOn
1449+ tcArgsOK ty hs -- if any arguments are metavariables, postpone
1450+ = let (f, as) = unApply ty in
1452+ P _ cn _ -> case lookupCtxtExact cn (idris_classes ist) of
1453+ Just ci -> tcDetArgsOK 0 (class_determiners ci) hs as
1454+ Nothing -> not $ any (isMeta hs) as
1455+ _ -> not $ any (isMeta hs) as
1457+ tcDetArgsOK i ds hs (x : xs)
1458+ | i `elem` ds = not (isMeta hs x) && tcDetArgsOK (i + 1) ds hs xs
1459+ | otherwise = tcDetArgsOK (i + 1) ds hs xs
1460+ tcDetArgsOK _ _ _ [] = True
1462+ isMeta :: [Name] -> Term -> Bool
1463+ isMeta ns (P _ n _) = n `elem` ns
1466+ notHole hs (P _ n _, c)
1467+ | (P _ cn _, _) <- unApply c,
1468+ n `elem` hs && isConName cn (tt_ctxt ist) = False
1469+ | Constant _ <- c = not (n `elem` hs)
1403 elabTC n | n /= fn && tcname n = (resolve n depth, show n)
1472 elabTC n | n /= fn && tcname n = (resolve n depth, show n)
1404 | otherwise = (fail "Can't resolve", show n)
1473 | otherwise = (fail "Can't resolve", show n)
@@ -1427,10 +1496,13 @@ resTC' tcs defaultOn depth topg fn ist
1428 blunderbuss t d stk [] = do -- c <- get_env
1497 blunderbuss t d stk [] = do -- c <- get_env
1429 -- ps <- get_probs
1498 -- ps <- get_probs
1430− lift $ tfail $ CantResolve topg
1499+ lift $ tfail $ CantResolve False topg
1431 blunderbuss t d stk (n:ns)
1500 blunderbuss t d stk (n:ns)
1432 | n /= fn && (n `elem` stk || tcname n)
1501 | n /= fn && (n `elem` stk || tcname n)
1502+ = tryCatch (resolve n d)
1504+ CantResolve True _ -> lift $ tfail e
1433− = try' (resolve n d) (blunderbuss t d stk ns) True
1505+ _ -> blunderbuss t d stk ns)
1434 | otherwise = blunderbuss t d stk ns
1506 | otherwise = blunderbuss t d stk ns
1436 resolve n depth
1508 resolve n depth
@@ -1459,7 +1531,7 @@ resTC' tcs defaultOn depth topg fn ist
1459 let got = fst (unApply t)
1531 let got = fst (unApply t)
1460 let depth' = if tc' `elem` tcs
1532 let depth' = if tc' `elem` tcs
1461 then depth - 1 else depth
1533 then depth - 1 else depth
1462− resTC' (got : tcs) defaultOn depth' topg fn ist)
1534+ resTC' (got : tcs) defaultOn topholes depth' topg fn ist)
1463 (filter (\ (x, y) -> not x) (zip (map fst imps) args))
1535 (filter (\ (x, y) -> not x) (zip (map fst imps) args))
1464 -- if there's any arguments left, we've failed to resolve
1536 -- if there's any arguments left, we've failed to resolve
1465 hs <- get_holes
1537 hs <- get_holes
@@ -1505,11 +1577,17 @@ case_ ind autoSolve ist fn tm = do
1505 else casetac (forget val)
1577 else casetac (forget val)
1506 when autoSolve solveAll
1578 when autoSolve solveAll
1580+tacN :: String -> Name
1581+tacN str = sNS (sUN str) ["Tactical", "Reflection", "Language"]
1508−runTactical :: FC -> Context -> Env -> Term -> ElabD ()
1583+runTactical :: FC -> Env -> Term -> ElabD ()
1509−runTactical fc ctxt env tm = runTacTm (eval tm) >> return ()
1584+runTactical fc env tm = do tm' <- eval tm
1588+ eval tm = do ctxt <- get_context
1511− eval = normaliseAll ctxt env . finalise
1589+ return $ normaliseAll ctxt env (finalise tm)
1512− tacN str = sNS (sUN str) ["Tactical", "Reflection", "Language"]
1590+
1513 returnUnit = fmap fst $ get_type_val (Var unitCon)
1591 returnUnit = fmap fst $ get_type_val (Var unitCon)
1515 -- | Do a step in the reflected elaborator monad. The input is the
1593 -- | Do a step in the reflected elaborator monad. The input is the
@@ -1547,26 +1625,31 @@ runTactical fc ctxt env tm = runTacTm (eval tm) >> return ()
1547 = do env <- get_env
1625 = do env <- get_env
1548 fmap fst . get_type_val $ reflectEnv env
1626 fmap fst . get_type_val $ reflectEnv env
1549 | n == tacN "prim__Fail", [_a, errs] <- args
1627 | n == tacN "prim__Fail", [_a, errs] <- args
1628+ = do errs' <- eval errs
1550− = do parts <- reifyReportParts (eval errs)
1629+ parts <- reifyReportParts errs'
1551 lift . tfail $ ReflectionError [parts] (Msg "")
1630 lift . tfail $ ReflectionError [parts] (Msg "")
1552 | n == tacN "prim__PureTactical", [_a, tm] <- args
1631 | n == tacN "prim__PureTactical", [_a, tm] <- args
1553 = return tm
1632 = return tm
1554 | n == tacN "prim__BindTactical", [_a, _b, first, andThen] <- args
1633 | n == tacN "prim__BindTactical", [_a, _b, first, andThen] <- args
1555− = do let first' = eval first
1634+ = do first' <- eval first
1556 res <- runTacTm first'
1635 res <- runTacTm first'
1557− let next = eval (App andThen res)
1636+ next <- eval (App andThen res)
1558 runTacTm next
1637 runTacTm next
1559 | n == tacN "prim__Try", [_a, first, alt] <- args
1638 | n == tacN "prim__Try", [_a, first, alt] <- args
1560− = do let first' = eval first
1639+ = do first' <- eval first
1561− let alt' = eval alt
1640+ alt' <- eval alt
1562 try' (runTacTm first') (runTacTm alt') True
1641 try' (runTacTm first') (runTacTm alt') True
1563 | n == tacN "prim__Fill", [raw] <- args
1642 | n == tacN "prim__Fill", [raw] <- args
1564 = do raw' <- reifyRaw raw
1643 = do raw' <- reifyRaw raw
1565 apply raw' []
1644 apply raw' []
1566 returnUnit
1645 returnUnit
1567− | n == tacN "prim__Gensym", [eval -> Constant (Str hint)] <- args
1646+ | n == tacN "prim__Gensym", [hint] <- args
1647+ = do hintStr <- eval hint
1649+ Constant (Str h) -> do
1568− = do n <- getNameFrom (sMN 0 hint)
1650+ n <- getNameFrom (sMN 0 h)
1569− fmap fst $ get_type_val (reflectName n)
1651+ fmap fst $ get_type_val (reflectName n)
1652+ _ -> fail "no hint"
1570 | n == tacN "prim__Claim", [n, ty] <- args
1653 | n == tacN "prim__Claim", [n, ty] <- args
1571 = do n' <- reifyTTName n
1654 = do n' <- reifyTTName n
1572 ty' <- reifyRaw ty
1655 ty' <- reifyRaw ty
@@ -1596,6 +1679,29 @@ runTactical fc ctxt env tm = runTacTm (eval tm) >> return ()
1596 Just name -> fmap Just $ reifyTTName name
1679 Just name -> fmap Just $ reifyTTName name
1598 returnUnit
1681 returnUnit
1682+ | n == tacN "prim__DeclareType", [decl] <- args
1683+ = do (RDeclare n args res) <- reifyTyDecl decl
1684+ ctxt <- get_context
1685+ let mkPi arg res = RBind (argName arg)
1686+ (Pi Nothing (argTy arg) (RUType AllTypes))
1688+ rty = foldr mkPi res args
1689+ (checked, ty') <- lift $ check ctxt [] rty
1690+ case normaliseAll ctxt [] (finalise ty') of
1691+ TType _ -> lift . tfail . InternalMsg $
1692+ show checked ++ " is not a type: it's " ++ show ty'
1694+ case lookupDefExact n ctxt of
1695+ Just _ -> lift . tfail . InternalMsg $
1696+ show n ++ " is already defined."
1697+ Nothing -> return ()
1698+ let decl = TyDecl Ref checked
1699+ ctxt' = addCtxtDef n decl ctxt
1701+ updateAux $ \e -> e { new_tyDecls = (n, fc, map rArgToPArg args, checked) :
1599 | n == tacN "prim__Debug", [ty, msg] <- args
1705 | n == tacN "prim__Debug", [ty, msg] <- args
1600 = do let msg' = fromTTMaybe msg
1706 = do let msg' = fromTTMaybe msg
1601 case msg' of
1707 case msg' of
@@ -1850,6 +1956,7 @@ runTac autoSolve ist perhapsFC fn tac
1850 Just fc ->
1956 Just fc ->
1851 do fill $ reflectFC fc
1957 do fill $ reflectFC fc
1959+ runT Qed = lift . tfail $ Msg "The qed command is only valid in the interactive prover"
1853 runT x = fail $ "Not implemented " ++ show x
1960 runT x = fail $ "Not implemented " ++ show x
1855 runReflected t = do t' <- reify ist t
1962 runReflected t = do t' <- reify ist t
@@ -2361,7 +2468,7 @@ reflectTTQuote unq (P nt n t)
2361reflectTTQuote unq (V n)
2468reflectTTQuote unq (V n)
2362 = reflCall "V" [RConstant (I n)]
2469 = reflCall "V" [RConstant (I n)]
2363reflectTTQuote unq (Bind n b x)
2470reflectTTQuote unq (Bind n b x)
2364− = reflCall "Bind" [reflectName n, reflectBinderQuote reflectTTQuote unq b, reflectTTQuote unq x]
2471+ = reflCall "Bind" [reflectName n, reflectBinderQuote reflectTTQuote (reflm "TT") unq b, reflectTTQuote unq x]
2365reflectTTQuote unq (App f x)
2472reflectTTQuote unq (App f x)
2366 = reflCall "App" [reflectTTQuote unq f, reflectTTQuote unq x]
2473 = reflCall "App" [reflectTTQuote unq f, reflectTTQuote unq x]
2367reflectTTQuote unq (Constant c)
2474reflectTTQuote unq (Constant c)
@@ -2378,7 +2485,7 @@ reflectRawQuote unq (Var n)
2378 | n `elem` unq = Var n
2485 | n `elem` unq = Var n
2379 | otherwise = reflCall "Var" [reflectName n]
2486 | otherwise = reflCall "Var" [reflectName n]
2380reflectRawQuote unq (RBind n b r) =
2487reflectRawQuote unq (RBind n b r) =
2381− reflCall "RBind" [reflectName n, reflectBinderQuote reflectRawQuote unq b, reflectRawQuote unq r]
2488+ reflCall "RBind" [reflectName n, reflectBinderQuote reflectRawQuote (reflm "Raw") unq b, reflectRawQuote unq r]
2382reflectRawQuote unq (RApp f x) =
2489reflectRawQuote unq (RApp f x) =
2383 reflCall "RApp" [reflectRawQuote unq f, reflectRawQuote unq x]
2490 reflCall "RApp" [reflectRawQuote unq f, reflectRawQuote unq x]
2384reflectRawQuote unq RType = Var (reflm "RType")
2491reflectRawQuote unq RType = Var (reflm "RType")
@@ -2436,27 +2543,27 @@ reflectNameQuotePattern _ -- for all other names, match any
2438reflectBinder :: Binder Term -> Raw
2545reflectBinder :: Binder Term -> Raw
2439−reflectBinder = reflectBinderQuote reflectTTQuote []
2546+reflectBinder = reflectBinderQuote reflectTTQuote (reflm "TT") []
2441−reflectBinderQuote :: ([Name] -> a -> Raw) -> [Name] -> Binder a -> Raw
2548+reflectBinderQuote :: ([Name] -> a -> Raw) -> Name -> [Name] -> Binder a -> Raw
2442−reflectBinderQuote q unq (Lam t)
2549+reflectBinderQuote q ty unq (Lam t)
2443− = reflCall "Lam" [Var (reflm "TT"), q unq t]
2550+ = reflCall "Lam" [Var ty, q unq t]
2444−reflectBinderQuote q unq (Pi _ t k)
2551+reflectBinderQuote q ty unq (Pi _ t k)
2445− = reflCall "Pi" [Var (reflm "TT"), q unq t, q unq k]
2552+ = reflCall "Pi" [Var ty, q unq t, q unq k]
2446−reflectBinderQuote q unq (Let x y)
2553+reflectBinderQuote q ty unq (Let x y)
2447− = reflCall "Let" [Var (reflm "TT"), q unq x, q unq y]
2554+ = reflCall "Let" [Var ty, q unq x, q unq y]
2448−reflectBinderQuote q unq (NLet x y)
2555+reflectBinderQuote q ty unq (NLet x y)
2449− = reflCall "NLet" [Var (reflm "TT"), q unq x, q unq y]
2556+ = reflCall "NLet" [Var ty, q unq x, q unq y]
2450−reflectBinderQuote q unq (Hole t)
2557+reflectBinderQuote q ty unq (Hole t)
2451− = reflCall "Hole" [Var (reflm "TT"), q unq t]
2558+ = reflCall "Hole" [Var ty, q unq t]
2452−reflectBinderQuote q unq (GHole _ t)
2559+reflectBinderQuote q ty unq (GHole _ t)
2453− = reflCall "GHole" [Var (reflm "TT"), q unq t]
2560+ = reflCall "GHole" [Var ty, q unq t]
2454−reflectBinderQuote q unq (Guess x y)
2561+reflectBinderQuote q ty unq (Guess x y)
2455− = reflCall "Guess" [Var (reflm "TT"), q unq x, q unq y]
2562+ = reflCall "Guess" [Var ty, q unq x, q unq y]
2456−reflectBinderQuote q unq (PVar t)
2563+reflectBinderQuote q ty unq (PVar t)
2457− = reflCall "PVar" [Var (reflm "TT"), q unq t]
2564+ = reflCall "PVar" [Var ty, q unq t]
2458−reflectBinderQuote q unq (PVTy t)
2565+reflectBinderQuote q ty unq (PVTy t)
2459− = reflCall "PVTy" [Var (reflm "TT"), q unq t]
2566+ = reflCall "PVTy" [Var ty, q unq t]
2461mkList :: Raw -> [Raw] -> Raw
2568mkList :: Raw -> [Raw] -> Raw
2462mkList ty [] = RApp (Var (sNS (sUN "Nil") ["List", "Prelude"])) ty
2569mkList ty [] = RApp (Var (sNS (sUN "Nil") ["List", "Prelude"])) ty
@@ -2603,11 +2710,15 @@ reflectErr (NotInjective t1 t2 t3) =
2603 , reflect t2
2710 , reflect t2
2604 , reflect t3
2711 , reflect t3
2606−reflectErr (CantResolve t) = raw_apply (Var $ reflErrName "CantResolve") [reflect t]
2713+reflectErr (CantResolve _ t) = raw_apply (Var $ reflErrName "CantResolve") [reflect t]
2714+reflectErr (InvalidTCArg n t) = raw_apply (Var $ reflErrName "InvalidTCArg") [reflectName n, reflect t]
2607reflectErr (CantResolveAlts ss) =
2715reflectErr (CantResolveAlts ss) =
2608 raw_apply (Var $ reflErrName "CantResolveAlts")
2716 raw_apply (Var $ reflErrName "CantResolveAlts")
2609 [rawList (Var $ reflm "TTName") (map reflectName ss)]
2717 [rawList (Var $ reflm "TTName") (map reflectName ss)]
2610reflectErr (IncompleteTerm t) = raw_apply (Var $ reflErrName "IncompleteTerm") [reflect t]
2718reflectErr (IncompleteTerm t) = raw_apply (Var $ reflErrName "IncompleteTerm") [reflect t]
2719+reflectErr (NoEliminator str t)
2720+ = raw_apply (Var $ reflErrName "NoEliminator") [RConstant (Str str),
2611reflectErr UniverseError = Var $ reflErrName "UniverseError"
2722reflectErr UniverseError = Var $ reflErrName "UniverseError"
2612reflectErr ProgramLineComment = Var $ reflErrName "ProgramLineComment"
2723reflectErr ProgramLineComment = Var $ reflErrName "ProgramLineComment"
2613reflectErr (Inaccessible n) = raw_apply (Var $ reflErrName "Inaccessible") [reflectName n]
2724reflectErr (Inaccessible n) = raw_apply (Var $ reflErrName "Inaccessible") [reflectName n]
@@ -2748,6 +2859,28 @@ reifyReportPart (App (P (DCon _ _ _) n _) tm)
2748 Nothing -> Left . InternalMsg $ "could not reify subreport " ++ show tm
2859 Nothing -> Left . InternalMsg $ "could not reify subreport " ++ show tm
2749reifyReportPart x = Left . InternalMsg $ "could not reify " ++ show x
2860reifyReportPart x = Left . InternalMsg $ "could not reify " ++ show x
2862+reifyTyDecl :: Term -> ElabD RTyDecl
2863+reifyTyDecl (App (App (App (P (DCon _ _ _) n _) tyN) args) ret)
2864+ | n == tacN "Declare" =
2865+ do tyN' <- reifyTTName tyN
2866+ args' <- case unList args of
2867+ Nothing -> fail $ "Couldn't reify " ++ show args ++ " as an arglist."
2868+ Just xs -> mapM reifyRArg xs
2869+ ret' <- reifyRaw ret
2870+ return $ RDeclare tyN' args' ret'
2871+ where reifyRArg :: Term -> ElabD RArg
2872+ reifyRArg (App (App (P (DCon _ _ _) n _) argN) argTy)
2873+ | n == tacN "Explicit" = liftM2 RExplicit
2876+ | n == tacN "Implicit" = liftM2 RImplicit
2878+ (reifyRaw argTy) | n == tacN "Constraint" = liftM2 RConstraint
2881+ reifyRArg aTm = fail $ "Couldn't reify " ++ show aTm ++ " as an RArg."
2882+reifyTyDecl tm = fail $ "Couldn't reify " ++ show tm ++ " as a type declaration."
2751envTupleType :: Raw
2884envTupleType :: Raw
2752envTupleType
2885envTupleType
2753 = raw_apply (Var pairTy) [ (Var $ reflm "TTName")
2886 = raw_apply (Var pairTy) [ (Var $ reflm "TTName")