on master help sign in
Allow new-style tactics to declare function signatures

Docs forthcoming.

commitc2dc6ec7dd9fc8b679b201258b2067e23129eb9f authorDavid Raymond Christiansen <david@davidchristiansen.dk> dateTue 17 Mar 2015, 21:22 (12 years ago) parente6d1cfd
14 files changed+258−76
idris.cabal+1 libs/prelude/Language/Reflection/Tactical.idr+14 src/Idris/AbsSyntax.hs+14−1 src/Idris/AbsSyntaxTree.hs+9−3 src/Idris/Core/TT.hs+1−1 src/Idris/Elab/Clause.hs+34−22 src/Idris/Elab/Instance.hs+5−3 src/Idris/Elab/Transform.hs+10−4 src/Idris/Elab/Type.hs+10−6 src/Idris/Elab/Utils.hs+10−8 src/Idris/Elab/Value.hs+8−3 src/Idris/ElabTerm.hs+110−25 src/Idris/Reflection.hs+18 test/tactics001/Tactics.idr+14
idris.cabal+1
@@ -725,6 +725,7 @@ Library
725725 , Idris.ProofSearch
726726 , Idris.Prover
727727 , Idris.Providers
728+ , Idris.Reflection
728729 , Idris.REPL
729730 , Idris.REPLParser
730731 , Idris.Transforms
libs/prelude/Language/Reflection/Tactical.idr+14
@@ -8,6 +8,14 @@ import Prelude.Maybe
88import Prelude.Monad
99import Language.Reflection
1010
11+data Arg : Type where
12+ Explicit : TTName -> Raw -> Arg
13+ Implicit : TTName -> Raw -> Arg
14+ Constraint : TTName -> Raw -> Arg
15+
16+data TyDecl : Type where
17+ Declare : TTName -> List Arg -> Raw -> TyDecl
18+
1119abstract
1220data Tactical : Type -> Type where
1321 -- obligatory control stuff
@@ -39,8 +47,11 @@ data Tactical : Type -> Type where
3947 prim__Claim : TTName -> Raw -> Tactical ()
4048 prim__Intro : Maybe TTName -> Tactical ()
4149
50+ prim__DeclareType : TyDecl -> Tactical ()
51+
4252 prim__Debug : {a : Type} -> Maybe String -> Tactical a
4353
54+
4455-------------
4556-- Public API
4657-------------
@@ -117,6 +128,9 @@ sourceLocation = do loc <- getSourceLocation
117128rewriteWith : Raw -> Tactical ()
118129rewriteWith rule = prim__Rewrite rule
119130
131+declareType : TyDecl -> Tactical ()
132+declareType decl = prim__DeclareType decl
133+
120134debug : Tactical a
121135debug = prim__Debug Nothing
122136
src/Idris/AbsSyntax.hs+14−1
@@ -455,6 +455,10 @@ getIState = get
455455putIState :: IState -> Idris ()
456456putIState = put
457457
458+updateIState :: (IState -> IState) -> Idris ()
459+updateIState f = do i <- getIState
460+ putIState $ f i
461+
458462withContext :: (IState -> Ctxt a) -> Name -> b -> (a -> Idris b) -> Idris b
459463withContext ctx name dflt action = do
460464 ist <- getIState
@@ -547,7 +551,16 @@ addConstraints fc (v, cs)
547551addDeferred = addDeferred' Ref
548552addDeferredTyCon = addDeferred' (TCon 0 0)
549553
550−addDeferred' :: NameType -> [(Name, (Int, Maybe Name, Type, Bool))] -> Idris ()
554+-- | Save information about a name that is not yet defined
555+addDeferred' :: NameType
556+ -> [(Name, (Int, Maybe Name, Type, Bool))]
557+ -- ^ The Name is the name being made into a metavar,
558+ -- the Int is the number of vars that are part of a
559+ -- putative proof context, the Maybe Name is the
560+ -- top-level function containing the new metavariable,
561+ -- the Type is its type, and the Bool is whether :p is
562+ -- allowed
563+ -> Idris ()
551564addDeferred' nt ns
552565 = do mapM_ (\(n, (i, _, t, _)) -> updateContext (addTyDecl n nt (tidyNames S.empty t))) ns
553566 mapM_ (\(n, _) -> when (not (n `elem` primDefs)) $ addIBC (IBCMetavar n)) ns
src/Idris/AbsSyntaxTree.hs+9−3
@@ -184,7 +184,11 @@ data IState = IState {
184184 idris_name :: Int,
185185 idris_lineapps :: [((FilePath, Int), PTerm)],
186186 -- ^ Full application LHS on source line
187− idris_metavars :: [(Name, (Maybe Name, Int, Bool))], -- ^ The currently defined but not proven metavariables
187+ idris_metavars :: [(Name, (Maybe Name, Int, Bool))],
188+ -- ^ The currently defined but not proven metavariables. The Int
189+ -- is the number of vars to display as a context, the Maybe Name
190+ -- is its top-level function, and the Bool is whether :p is
191+ -- allowed
188192 idris_coercions :: [Name],
189193 idris_errRev :: [(Term, Term)],
190194 syntax_rules :: SyntaxRules,
@@ -631,10 +635,12 @@ deriving instance NFData PDecl'
631635-- For elaborator state
632636data EState = EState {
633637 case_decls :: [PDecl],
634− delayed_elab :: [Elab' EState ()]
638+ delayed_elab :: [Elab' EState ()],
639+ new_tyDecls :: [(Name, FC, [PArg], Type)]
635640 }
636641
637−initEState = EState [] []
642+initEState :: EState
643+initEState = EState [] [] []
638644
639645type ElabD a = Elab' EState a
640646
src/Idris/Core/TT.hs+1−1
@@ -507,7 +507,7 @@ toAlist :: Ctxt a -> [(Name, a)]
507507toAlist ctxt = let allns = map snd (Map.toList ctxt) in
508508 concatMap (Map.toList) allns
509509
510−addAlist :: Show a => [(Name, a)] -> Ctxt a -> Ctxt a
510+addAlist :: [(Name, a)] -> Ctxt a -> Ctxt a
511511addAlist [] ctxt = ctxt
512512addAlist ((n, tm) : ds) ctxt = addDef n tm (addAlist ds ctxt)
513513
src/Idris/Elab/Clause.hs+34−22
@@ -455,8 +455,10 @@ checkPossible info fc tcgen fname lhs_in
455455 -- if the LHS type checks, it is possible
456456 case elaborate ctxt (sMN 0 "patLHS") infP initEState
457457 (erun fc (buildTC i info ELHS [] fname (infTerm lhs))) of
458− OK ((lhs', _, _), _) ->
459− do let lhs_tm = orderPats (getInferTerm lhs')
458+ OK (ElabResult lhs' _ _ ctxt' newDecls, _) ->
459+ do setContext ctxt'
460+ processTacticDecls newDecls
461+ let lhs_tm = orderPats (getInferTerm lhs')
460462 case recheck ctxt [] (forget lhs_tm) lhs_tm of
461463 OK _ -> return True
462464 err -> return False
@@ -579,14 +581,15 @@ elabClause info opts (cnum, PClause fc fname lhs_in_as withs rhs_in_as wherebloc
579581 logLvl 4 ("Fixed parameters: " ++ show params ++ " from " ++ show lhs_in ++
580582 "\n" ++ show (fn_ty, fn_is))
581583
582− (((lhs', dlhs, []), probs, inj), _) <-
583− tclift $ elaborate ctxt (sMN 0 "patLHS") infP initEState
584− (do res <- errAt "left hand side of " fname
585− (erun fc (buildTC i info ELHS opts fname (infTerm lhs)))
586− probs <- get_probs
587− inj <- get_inj
588− return (res, probs, inj))
589−
584+ ((ElabResult lhs' dlhs [] ctxt' newDecls, probs, inj), _) <-
585+ tclift $ elaborate ctxt (sMN 0 "patLHS") infP initEState
586+ (do res <- errAt "left hand side of " fname
587+ (erun fc (buildTC i info ELHS opts fname (infTerm lhs)))
588+ probs <- get_probs
589+ inj <- get_inj
590+ return (res, probs, inj))
591+ setContext ctxt'
592+ processTacticDecls newDecls
590593 when inf $ addTyInfConstraints fc (map (\(x,y,_,_,_,_,_) -> (x,y)) probs)
591594
592595 let lhs_tm = orderPats (getInferTerm lhs')
@@ -653,13 +656,14 @@ elabClause info opts (cnum, PClause fc fname lhs_in_as withs rhs_in_as wherebloc
653656 logLvl 2 $ "RHS: " ++ show (map fst newargs_all) ++ " " ++ showTmImpls rhs
654657 ctxt <- getContext -- new context with where block added
655658 logLvl 5 "STARTING CHECK"
656− ((rhs', defer, is, probs), _) <-
659+ ((rhs', defer, is, probs, ctxt', newDecls), _) <-
657660 tclift $ elaborate ctxt (sMN 0 "patRHS") clhsty initEState
658661 (do pbinds ist lhs_tm
659662 mapM_ setinj (nub (params ++ inj))
660663 setNextName
661− (_, _, is) <- errAt "right hand side of " fname
662− (erun fc (build i winfo ERHS opts fname rhs))
664+ (ElabResult _ _ is ctxt' newDecls) <-
665+ errAt "right hand side of " fname
666+ (erun fc (build i winfo ERHS opts fname rhs))
663667 errAt "right hand side of " fname
664668 (erun fc $ psolve lhs_tm)
665669 hs <- get_holes
@@ -668,8 +672,9 @@ elabClause info opts (cnum, PClause fc fname lhs_in_as withs rhs_in_as wherebloc
668672 tt <- get_term
669673 let (tm, ds) = runState (collectDeferred (Just fname) tt) []
670674 probs <- get_probs
671− return (tm, ds, is, probs))
672−
675+ return (tm, ds, is, probs, ctxt', newDecls))
676+ setContext ctxt'
677+ processTacticDecls newDecls
673678 when inf $ addTyInfConstraints fc (map (\(x,y,_,_,_,_,_) -> (x,y)) probs)
674679
675680 logLvl 5 "DONE CHECK"
@@ -812,10 +817,12 @@ elabClause info opts (_, PWith fc fname lhs_in withs wval_in pn_in withblock)
812817 let params = getParamsInType i [] fn_is fn_ty
813818 let lhs = stripLinear i $ stripUnmatchable i $ propagateParams i params fn_ty (addImplPat i lhs_in)
814819 logLvl 2 ("LHS: " ++ show lhs)
815− ((lhs', dlhs, []), _) <-
820+ (ElabResult lhs' dlhs [] ctxt' newDecls, _) <-
816821 tclift $ elaborate ctxt (sMN 0 "patLHS") infP initEState
817822 (errAt "left hand side of with in " fname
818823 (erun fc (buildTC i info ELHS opts fname (infTerm lhs))) )
824+ setContext ctxt'
825+ processTacticDecls newDecls
819826 let lhs_tm = orderPats (getInferTerm lhs')
820827 let lhs_ty = getInferType lhs'
821828 let ret_ty = getRetTy (explicitNames (normalise ctxt [] lhs_ty))
@@ -827,17 +834,19 @@ elabClause info opts (_, PWith fc fname lhs_in withs wval_in pn_in withblock)
827834 let wval = addImplBound i (map fst bargs) wval_in
828835 logLvl 5 ("Checking " ++ showTmImpls wval)
829836 -- Elaborate wval in this context
830− ((wval', defer, is), _) <-
837+ ((wval', defer, is, ctxt', newDecls), _) <-
831838 tclift $ elaborate ctxt (sMN 0 "withRHS")
832839 (bindTyArgs PVTy bargs infP) initEState
833840 (do pbinds i lhs_tm
834841 setNextName
835842 -- TODO: may want where here - see winfo abpve
836− (_', d, is) <- errAt "with value in " fname
843+ (ElabResult _ d is ctxt' newDecls) <- errAt "with value in " fname
837844 (erun fc (build i info ERHS opts fname (infTerm wval)))
838845 erun fc $ psolve lhs_tm
839846 tt <- get_term
840− return (tt, d, is))
847+ return (tt, d, is, ctxt', newDecls))
848+ setContext ctxt'
849+ processTacticDecls newDecls
841850 def' <- checkDef fc defer
842851 let def'' = map (\(n, (i, top, t)) -> (n, (i, top, t, False))) def'
843852 addDeferred def''
@@ -923,14 +932,17 @@ elabClause info opts (_, PWith fc fname lhs_in withs wval_in pn_in withblock)
923932 logLvl 5 ("New RHS " ++ showTmImpls rhs)
924933 ctxt <- getContext -- New context with block added
925934 i <- getIState
926− ((rhs', defer, is), _) <-
935+ ((rhs', defer, is, ctxt', newDecls), _) <-
927936 tclift $ elaborate ctxt (sMN 0 "wpatRHS") clhsty initEState
928937 (do pbinds i lhs_tm
929938 setNextName
930− (_, d, is) <- erun fc (build i info ERHS opts fname rhs)
939+ (ElabResult _ d is ctxt' newDecls) <- erun fc (build i info ERHS opts fname rhs)
931940 psolve lhs_tm
932941 tt <- get_term
933− return (tt, d, is))
942+ return (tt, d, is, ctxt', newDecls))
943+ setContext ctxt'
944+ processTacticDecls newDecls
945+
934946 def' <- checkDef fc defer
935947 let def'' = map (\(n, (i, top, t)) -> (n, (i, top, t, False))) def'
936948 addDeferred def''
src/Idris/Elab/Instance.hs+5−3
@@ -169,9 +169,11 @@ elabInstance info syn doc argDocs what fc cs n ps t expn ds = do
169169 ty' <- implicit info syn iname ty'
170170 let ty = addImpl i ty'
171171 ctxt <- getContext
172− ((tyT, _, _), _) <-
173− tclift $ elaborate ctxt iname (TType (UVal 0)) initEState
174− (errAt "type of " iname (erun fc (build i info ERHS [] iname ty)))
172+ (ElabResult tyT _ _ ctxt' newDecls, _) <-
173+ tclift $ elaborate ctxt iname (TType (UVal 0)) initEState
174+ (errAt "type of " iname (erun fc (build i info ERHS [] iname ty)))
175+ setContext ctxt'
176+ processTacticDecls newDecls
175177 ctxt <- getContext
176178 (cty, _) <- recheckC fc [] tyT
177179 let nty = normalise ctxt [] cty
src/Idris/Elab/Transform.hs+10−4
@@ -53,10 +53,12 @@ elabTransform info fc safe lhs_in@(PApp _ (PRef _ tf) _) rhs_in
5353 = do ctxt <- getContext
5454 i <- getIState
5555 let lhs = addImplPat i lhs_in
56− ((lhs', dlhs, []), _) <-
56+ (ElabResult lhs' dlhs [] ctxt' newDecls, _) <-
5757 tclift $ elaborate ctxt (sMN 0 "transLHS") infP initEState
5858 (erun fc (buildTC i info ELHS [] (sUN "transform")
5959 (infTerm lhs)))
60+ setContext ctxt'
61+ processTacticDecls newDecls
6062 let lhs_tm = orderPats (getInferTerm lhs')
6163 let lhs_ty = getInferType lhs'
6264 let newargs = pvars i lhs_tm
@@ -66,14 +68,18 @@ elabTransform info fc safe lhs_in@(PApp _ (PRef _ tf) _) rhs_in
6668 logLvl 3 ("Transform LHS " ++ show clhs_tm)
6769
6870 let rhs = addImplBound i (map fst newargs) rhs_in
69− ((rhs', defer), _) <-
71+ ((rhs', defer, ctxt', newDecls), _) <-
7072 tclift $ elaborate ctxt (sMN 0 "transRHS") clhs_ty initEState
7173 (do pbinds i lhs_tm
7274 setNextName
73− erun fc (build i info ERHS [] (sUN "transform") rhs)
75+ (ElabResult _ _ _ ctxt' newDecls) <- erun fc (build i info ERHS [] (sUN "transform") rhs)
7476 erun fc $ psolve lhs_tm
7577 tt <- get_term
76− return (runState (collectDeferred Nothing tt) []))
78+ let (rhs', defer) = runState (collectDeferred Nothing tt) []
79+ return (rhs', defer, ctxt', newDecls))
80+ setContext ctxt'
81+ processTacticDecls newDecls
82+
7783 (crhs_tm_in, crhs_ty) <- recheckC fc [] rhs'
7884 let crhs_tm = renamepats pnames crhs_tm_in
7985 logLvl 3 ("Transform RHS " ++ show crhs_tm)
src/Idris/Elab/Type.hs+10−6
@@ -65,9 +65,11 @@ buildType info syn fc opts n ty' = do
6565 logLvl 3 $ show n ++ " type pre-addimpl " ++ showTmImpls ty'
6666 logLvl 3 $ show n ++ " type " ++ show (using syn) ++ "\n" ++ showTmImpls ty
6767
68− ((tyT', defer, is), log) <-
69− tclift $ elaborate ctxt n (TType (UVal 0)) initEState
70− (errAt "type of " n (erun fc (build i info ETyDecl [] n ty)))
68+ (ElabResult tyT' defer is ctxt' newDecls, log) <-
69+ tclift $ elaborate ctxt n (TType (UVal 0)) initEState
70+ (errAt "type of " n (erun fc (build i info ETyDecl [] n ty)))
71+ setContext ctxt'
72+ processTacticDecls newDecls
7173
7274 let tyT = patToImp tyT'
7375
@@ -114,12 +116,14 @@ buildType info syn fc opts n ty' = do
114116 param_pos i ns t = []
115117
116118-- | Elaborate a top-level type declaration - for example, "foo : Int -> Int".
117−elabType :: ElabInfo -> SyntaxInfo -> Docstring (Either Err PTerm)-> [(Name, Docstring (Either Err PTerm))] ->
118− FC -> FnOpts -> Name -> PTerm -> Idris Type
119+elabType :: ElabInfo -> SyntaxInfo
120+ -> Docstring (Either Err PTerm) -> [(Name, Docstring (Either Err PTerm))]
121+ -> FC -> FnOpts -> Name -> PTerm -> Idris Type
119122elabType = elabType' False
120123
121124elabType' :: Bool -> -- normalise it
122− ElabInfo -> SyntaxInfo -> Docstring (Either Err PTerm) -> [(Name, Docstring (Either Err PTerm))] ->
125+ ElabInfo -> SyntaxInfo ->
126+ Docstring (Either Err PTerm) -> [(Name, Docstring (Either Err PTerm))] ->
123127 FC -> FnOpts -> Name -> PTerm -> Idris Type
124128elabType' norm info syn doc argDocs fc opts n ty' = {- let ty' = piBind (params info) ty_in
125129 n = liftname info n_in in -}
src/Idris/Elab/Utils.hs+10−8
@@ -22,7 +22,7 @@ import Debug.Trace
2222
2323import qualified Data.Map as Map
2424
25−recheckC = recheckC_borrowing False []
25+recheckC = recheckC_borrowing False []
2626
2727recheckC_borrowing uniq_check bs fc env t
2828 = do -- t' <- applyOpts (forget t) (doesn't work, or speed things up...)
@@ -34,9 +34,13 @@ recheckC_borrowing uniq_check bs fc env t
3434 addConstraints fc cs
3535 return (tm, ty)
3636
37−
37+checkDef :: FC -> [(Name, (Int, Maybe Name, Type))]
38+ -> Idris [(Name, (Int, Maybe Name, Type))]
3839checkDef fc ns = checkAddDef False True fc ns
3940
41+checkAddDef :: Bool -> Bool -> FC
42+ -> [(Name, (Int, Maybe Name, Type))]
43+ -> Idris [(Name, (Int, Maybe Name, Type))]
4044checkAddDef add toplvl fc [] = return []
4145checkAddDef add toplvl fc ((n, (i, top, t)) : ns)
4246 = do ctxt <- getContext
@@ -46,7 +50,7 @@ checkAddDef add toplvl fc ((n, (i, top, t)) : ns)
4650 ns' <- checkAddDef add toplvl fc ns
4751 return ((n, (i, top, t')) : ns')
4852
49−-- Get the list of (index, name) of inaccessible arguments from an elaborated
53+-- | Get the list of (index, name) of inaccessible arguments from an elaborated
5054-- type
5155inaccessibleImps :: Int -> Type -> [Bool] -> [(Int, Name)]
5256inaccessibleImps i (Bind n (Pi _ t _) sc) (inacc : ins)
@@ -54,7 +58,7 @@ inaccessibleImps i (Bind n (Pi _ t _) sc) (inacc : ins)
5458 | otherwise = inaccessibleImps (i + 1) sc ins
5559inaccessibleImps _ _ _ = []
5660
57−-- Get the list of (index, name) of inaccessible arguments from the type.
61+-- | Get the list of (index, name) of inaccessible arguments from the type.
5862inaccessibleArgs :: Int -> PTerm -> [(Int, Name)]
5963inaccessibleArgs i (PPi (Imp _ _ _ _) n Placeholder t)
6064 = (i,n) : inaccessibleArgs (i+1) t -- unbound implicit
@@ -74,10 +78,9 @@ elabCaseBlock info opts d@(PClauses f o n ps)
7478 when (AssertTotal `elem` opts) $ setFlags n [AssertTotal]
7579 rec_elabDecl info EAll info (PClauses f opts' n ps )
7680
77−-- Check that the result of type checking matches what the programmer wrote
81+-- | Check that the result of type checking matches what the programmer wrote
7882-- (i.e. - if we inferred any arguments that the user provided, make sure
7983-- they are the same!)
80−
8184checkInferred :: FC -> PTerm -> PTerm -> Idris ()
8285checkInferred fc inf user =
8386 do logLvl 6 $ "Checked to\n" ++ showTmImpls inf ++ "\n\nFROM\n\n" ++
@@ -92,9 +95,8 @@ checkInferred fc inf user =
9295 logLvl 10 $ "Checked match"
9396-- ++ "\n" ++ showImp True inf ++ "\n" ++ showImp True user)
9497
95−-- Return whether inferred term is different from given term
98+-- | Return whether inferred term is different from given term
9699-- (as above, but return a Bool)
97−
98100inferredDiff :: FC -> PTerm -> PTerm -> Idris Bool
99101inferredDiff fc inf user =
100102 do i <- getIState
src/Idris/Elab/Value.hs+8−3
@@ -63,9 +63,14 @@ elabValBind info aspat norm tm_in
6363 -- * elaboration as a Type
6464 -- * elaboration as a function a -> b
6565
66− ((tm', defer, is), _) <-
67− tclift (elaborate ctxt (sMN 0 "val") infP initEState
68− (build i info aspat [Reflection] (sMN 0 "val") (infTerm tm)))
66+ (ElabResult tm' defer is ctxt' newDecls, _) <-
67+ tclift (elaborate ctxt (sMN 0 "val") infP initEState
68+ (build i info aspat [Reflection] (sMN 0 "val") (infTerm tm)))
69+
70+ -- Extend the context with new definitions created
71+ setContext ctxt'
72+ processTacticDecls newDecls
73+
6974 let vtm = orderPats (getInferTerm tm')
7075
7176 def' <- checkDef (fileFC "(input)") defer
src/Idris/ElabTerm.hs+110−25
@@ -18,6 +18,7 @@ import Idris.Core.Typecheck (check, recheck)
1818import Idris.ErrReverse (errReverse)
1919import Idris.ElabQuasiquote (extractUnquotes)
2020import Idris.Elab.Utils
21+import Idris.Reflection
2122import qualified Util.Pretty as U
2223
2324import Control.Applicative ((<$>))
@@ -36,6 +37,32 @@ import Debug.Trace
3637data ElabMode = ETyDecl | ELHS | ERHS
3738 deriving Eq
3839
40+data ElabResult =
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
50+ }
51+
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) }
59+ addIBC (IBCImp n)
60+ ds <- checkDef fc [(n, (-1, Nothing, ty))]
61+ addIBC (IBCDef n)
62+ let ds' = map (\(n, (i, top, t)) -> (n, (i, top, t, True))) ds
63+ addDeferred ds'
64+
65+
3966-- Using the elaborator, convert a term in raw syntax to a fully
4067-- elaborated, typechecked term.
4168--
@@ -45,7 +72,7 @@ data ElabMode = ETyDecl | ELHS | ERHS
4572-- Also find deferred names in the term and their types
4673
4774build :: IState -> ElabInfo -> ElabMode -> FnOpts -> Name -> PTerm ->
48− ElabD (Term, [(Name, (Int, Maybe Name, Type))], [PDecl])
75+ ElabD ElabResult
4976build ist info emode opts fn tm
5077 = do elab ist info emode opts fn tm
5178 let tmIn = tm
@@ -94,12 +121,13 @@ build ist info emode opts fn tm
94121 when tydecl (do update_term orderPats
95122 mkPat)
96123-- update_term liftPats)
97− EState is _ <- getAux
124+ EState is _ impls <- getAux
98125 tt <- get_term
99126 let (tm, ds) = runState (collectDeferred (Just fn) tt) []
100127 log <- getLog
101− if (log /= "") then trace log $ return (tm, ds, is)
102− else return (tm, ds, is)
128+ ctxt <- get_context
129+ if (log /= "") then trace log $ return (ElabResult tm ds is ctxt impls)
130+ else return (ElabResult tm ds is ctxt impls)
103131 where pattern = emode == ELHS
104132 tydecl = emode == ETyDecl
105133
@@ -114,7 +142,7 @@ build ist info emode opts fn tm
114142-- know about yet on the LHS of a pattern def)
115143
116144buildTC :: IState -> ElabInfo -> ElabMode -> FnOpts -> Name -> PTerm ->
117− ElabD (Term, [(Name, (Int, Maybe Name, Type))], [PDecl])
145+ ElabD ElabResult
118146buildTC ist info emode opts fn tm
119147 = do -- set name supply to begin after highest index in tm
120148 let ns = allNamesIn tm
@@ -135,12 +163,13 @@ buildTC ist info emode opts fn tm
135163 -- unification
136164 when (not (null dots)) $
137165 lift (Error (CantMatch (getInferTerm tm)))
138− EState is _ <- getAux
166+ EState is _ impls <- getAux
139167 tt <- get_term
140168 let (tm, ds) = runState (collectDeferred (Just fn) tt) []
141169 log <- getLog
142− if (log /= "") then trace log $ return (tm, ds, is)
143− else return (tm, ds, is)
170+ ctxt <- get_context
171+ if (log /= "") then trace log $ return (ElabResult tm ds is ctxt impls)
172+ else return (ElabResult tm ds is ctxt impls)
144173 where pattern = emode == ELHS
145174
146175-- return whether arguments of the given constructor name can be
@@ -183,9 +212,9 @@ goal_polymorphic =
183212 _ -> return True
184213 _ -> return False
185214
186−-- Returns the set of declarations we need to add to complete the definition
187−-- (most likely case blocks to elaborate)
188−
215+-- | Returns the set of declarations we need to add to complete the
216+-- definition (most likely case blocks to elaborate) as well as
217+-- declarations resulting from user tactic scripts (%runTactics)
189218elab :: IState -> ElabInfo -> ElabMode -> FnOpts -> Name -> PTerm ->
190219 ElabD ()
191220elab ist info emode opts fn tm
@@ -1077,8 +1106,8 @@ elab ist info emode opts fn tm
10771106 focus n
10781107 elab' ina (Just fc') tm
10791108 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
10821111 solve
10831112 elab' ina fc x = fail $ "Unelaboratable syntactic form " ++ showTmImpls x
10841113
@@ -1505,11 +1534,17 @@ case_ ind autoSolve ist fn tm = do
15051534 else casetac (forget val)
15061535 when autoSolve solveAll
15071536
1508−runTactical :: FC -> Context -> Env -> Term -> ElabD ()
1509−runTactical fc ctxt env tm = runTacTm (eval tm) >> return ()
1537+tacN :: String -> Name
1538+tacN str = sNS (sUN str) ["Tactical", "Reflection", "Language"]
1539+
1540+runTactical :: FC -> Env -> Term -> ElabD ()
1541+runTactical fc env tm = do tm' <- eval tm
1542+ runTacTm tm'
1543+ return ()
15101544 where
1511− eval = normaliseAll ctxt env . finalise
1512− tacN str = sNS (sUN str) ["Tactical", "Reflection", "Language"]
1545+ eval tm = do ctxt <- get_context
1546+ return $ normaliseAll ctxt env (finalise tm)
1547+
15131548 returnUnit = fmap fst $ get_type_val (Var unitCon)
15141549
15151550 -- | Do a step in the reflected elaborator monad. The input is the
@@ -1547,26 +1582,31 @@ runTactical fc ctxt env tm = runTacTm (eval tm) >> return ()
15471582 = do env <- get_env
15481583 fmap fst . get_type_val $ reflectEnv env
15491584 | n == tacN "prim__Fail", [_a, errs] <- args
1550− = do parts <- reifyReportParts (eval errs)
1585+ = do errs' <- eval errs
1586+ parts <- reifyReportParts errs'
15511587 lift . tfail $ ReflectionError [parts] (Msg "")
15521588 | n == tacN "prim__PureTactical", [_a, tm] <- args
15531589 = return tm
15541590 | n == tacN "prim__BindTactical", [_a, _b, first, andThen] <- args
1555− = do let first' = eval first
1591+ = do first' <- eval first
15561592 res <- runTacTm first'
1557− let next = eval (App andThen res)
1593+ next <- eval (App andThen res)
15581594 runTacTm next
15591595 | n == tacN "prim__Try", [_a, first, alt] <- args
1560− = do let first' = eval first
1561− let alt' = eval alt
1596+ = do first' <- eval first
1597+ alt' <- eval alt
15621598 try' (runTacTm first') (runTacTm alt') True
15631599 | n == tacN "prim__Fill", [raw] <- args
15641600 = do raw' <- reifyRaw raw
15651601 apply raw' []
15661602 returnUnit
1567− | n == tacN "prim__Gensym", [eval -> Constant (Str hint)] <- args
1568− = do n <- getNameFrom (sMN 0 hint)
1569− fmap fst $ get_type_val (reflectName n)
1603+ | n == tacN "prim__Gensym", [hint] <- args
1604+ = do hintStr <- eval hint
1605+ case hintStr of
1606+ Constant (Str h) -> do
1607+ n <- getNameFrom (sMN 0 h)
1608+ fmap fst $ get_type_val (reflectName n)
1609+ _ -> fail "no hint"
15701610 | n == tacN "prim__Claim", [n, ty] <- args
15711611 = do n' <- reifyTTName n
15721612 ty' <- reifyRaw ty
@@ -1596,6 +1636,29 @@ runTactical fc ctxt env tm = runTacTm (eval tm) >> return ()
15961636 Just name -> fmap Just $ reifyTTName name
15971637 intro n
15981638 returnUnit
1639+ | n == tacN "prim__DeclareType", [decl] <- args
1640+ = do (RDeclare n args res) <- reifyTyDecl decl
1641+ ctxt <- get_context
1642+ let mkPi arg res = RBind (argName arg)
1643+ (Pi Nothing (argTy arg) (RUType AllTypes))
1644+ res
1645+ rty = foldr mkPi res args
1646+ (checked, ty') <- lift $ check ctxt [] rty
1647+ case normaliseAll ctxt [] (finalise ty') of
1648+ TType _ -> lift . tfail . InternalMsg $
1649+ show checked ++ " is not a type: it's " ++ show ty'
1650+ _ -> return ()
1651+ case lookupDefExact n ctxt of
1652+ Just _ -> lift . tfail . InternalMsg $
1653+ show n ++ " is already defined."
1654+ Nothing -> return ()
1655+ let decl = TyDecl Ref checked
1656+ ctxt' = addCtxtDef n decl ctxt
1657+ set_context ctxt'
1658+ updateAux $ \e -> e { new_tyDecls = (n, fc, map rArgToPArg args, checked) :
1659+ new_tyDecls e }
1660+ aux <- getAux
1661+ returnUnit
15991662 | n == tacN "prim__Debug", [ty, msg] <- args
16001663 = do let msg' = fromTTMaybe msg
16011664 case msg' of
@@ -2749,6 +2812,28 @@ reifyReportPart (App (P (DCon _ _ _) n _) tm)
27492812 Nothing -> Left . InternalMsg $ "could not reify subreport " ++ show tm
27502813reifyReportPart x = Left . InternalMsg $ "could not reify " ++ show x
27512814
2815+reifyTyDecl :: Term -> ElabD RTyDecl
2816+reifyTyDecl (App (App (App (P (DCon _ _ _) n _) tyN) args) ret)
2817+ | n == tacN "Declare" =
2818+ do tyN' <- reifyTTName tyN
2819+ args' <- case unList args of
2820+ Nothing -> fail $ "Couldn't reify " ++ show args ++ " as an arglist."
2821+ Just xs -> mapM reifyRArg xs
2822+ ret' <- reifyRaw ret
2823+ return $ RDeclare tyN' args' ret'
2824+ where reifyRArg :: Term -> ElabD RArg
2825+ reifyRArg (App (App (P (DCon _ _ _) n _) argN) argTy)
2826+ | n == tacN "Explicit" = liftM2 RExplicit
2827+ (reifyTTName argN)
2828+ (reifyRaw argTy)
2829+ | n == tacN "Implicit" = liftM2 RImplicit
2830+ (reifyTTName argN)
2831+ (reifyRaw argTy) | n == tacN "Constraint" = liftM2 RConstraint
2832+ (reifyTTName argN)
2833+ (reifyRaw argTy)
2834+ reifyRArg aTm = fail $ "Couldn't reify " ++ show aTm ++ " as an RArg."
2835+reifyTyDecl tm = fail $ "Couldn't reify " ++ show tm ++ " as a type declaration."
2836+
27522837envTupleType :: Raw
27532838envTupleType
27542839 = raw_apply (Var pairTy) [ (Var $ reflm "TTName")
src/Idris/Reflection.hsnew file+18
@@ -0,0 +1,18 @@
1+{-|
2+Handy tools for doing reflection.
3+-}
4+module Idris.Reflection where
5+
6+import Idris.Core.TT
7+import Idris.AbsSyntaxTree (PArg'(..), PArg, PTerm(Placeholder))
8+
9+data RArg = RExplicit { argName :: Name, argTy :: Raw }
10+ | RImplicit { argName :: Name, argTy :: Raw }
11+ | RConstraint { argName :: Name, argTy :: Raw }
12+
13+data RTyDecl = RDeclare Name [RArg] Raw
14+
15+rArgToPArg :: RArg -> PArg
16+rArgToPArg (RExplicit n _) = PExp 0 [] n Placeholder
17+rArgToPArg (RImplicit n _) = PImp 0 False [] n Placeholder
18+rArgToPArg (RConstraint n _) = PConstraint 0 [] n Placeholder
test/tactics001/Tactics.idr+14
@@ -2,6 +2,9 @@ module Tactics
22
33import Language.Reflection.Tactical
44
5+import Data.Vect
6+
7+
58%default total
69
710-- Test that basic proofs with new-style tactics work
@@ -25,3 +28,14 @@ plusSuccRightSuccNew (S j) k =
2528 %runTactics (do rewriteWith `(sym $ plusSuccRightSuccNew ~(Var "j") ~(Var (UN "k")))
2629 fill $ refl `(Nat : Type) `(S (S (plus ~(Var (UN "j")) ~(Var (UN "k")))))
2730 solve)
31+
32+-- Test that side effects in new tactics work
33+
34+mkDecl : ()
35+mkDecl = %runTactics (do declareType $ Declare (NS (UN "repUnit") ["Tactics"])
36+ [Implicit (UN "z") `(Nat)]
37+ `(Vect ~(Var (UN "z")) ())
38+ fill `(() : ())
39+ solve)
40+
41+repUnit = pure ()