1{-# LANGUAGE PatternGuards #-}
2module Idris.Elab.Transform where
13import Idris.Primitives
15import Idris.PartialEval
17import Idris.Output (iputStrLn, pshow, iWarn, sendHighlighting)
20import Idris.Elab.Utils
24import Idris.Core.Elaborate hiding (Tactic(..))
25import Idris.Core.Evaluate
26import Idris.Core.Execute
27import Idris.Core.Typecheck
28import Idris.Core.CaseTree
30import Idris.Docstrings
32import Prelude hiding (id, (.))
33import Control.Category
35import Control.Applicative hiding (Const)
38import Control.Monad.State.Strict as State
43import qualified Data.Map as Map
44import qualified Data.Set as S
45import qualified Data.Text as T
46import Data.Char(isLetter, toLower)
47import Data.List.Split (splitOn)
49import Util.Pretty(pretty, text)
51elabTransform :: ElabInfo -> FC -> Bool -> PTerm -> PTerm -> Idris (Term, Term)
52elabTransform info fc safe lhs_in@(PApp _ (PRef _ _ tf) _) rhs_in
53 = do ctxt <- getContext
55 let lhs = addImplPat i lhs_in
56 logLvl 5 ("Transform LHS input: " ++ showTmImpls lhs)
57 (ElabResult lhs' dlhs [] ctxt' newDecls highlights, _) <-
58 tclift $ elaborate ctxt (idris_datatypes i) (sMN 0 "transLHS") infP initEState
59 (erun fc (buildTC i info ETransLHS [] (sUN "transform")
62 processTacticDecls info newDecls
63 sendHighlighting highlights
64 let lhs_tm = orderPats (getInferTerm lhs')
65 let lhs_ty = getInferType lhs'
66 let newargs = pvars i lhs_tm
68 (clhs_tm_in, clhs_ty) <- recheckC fc id [] lhs_tm
69 let clhs_tm = renamepats pnames clhs_tm_in
70 logLvl 3 ("Transform LHS " ++ show clhs_tm)
71 logLvl 3 ("Transform type " ++ show clhs_ty)
73 let rhs = addImplBound i (map fst newargs) rhs_in
74 logLvl 5 ("Transform RHS input: " ++ showTmImpls rhs)
76 ((rhs', defer, ctxt', newDecls), _) <-
77 tclift $ elaborate ctxt (idris_datatypes i) (sMN 0 "transRHS") clhs_ty initEState
80 (ElabResult _ _ _ ctxt' newDecls highlights) <- erun fc (build i info ERHS [] (sUN "transform") rhs)
81 erun fc $ psolve lhs_tm
83 let (rhs', defer) = runState (collectDeferred Nothing [] ctxt tt) []
84 return (rhs', defer, ctxt', newDecls))
86 processTacticDecls info newDecls
87 sendHighlighting highlights
89 (crhs_tm_in, crhs_ty) <- recheckC fc id [] rhs'
90 let crhs_tm = renamepats pnames crhs_tm_in
91 logLvl 3 ("Transform RHS " ++ show crhs_tm)
93 -- Types must always convert
94 case converts ctxt [] clhs_ty crhs_ty of
96 Error e -> ierror (At fc (CantUnify False (clhs_tm, Nothing) (crhs_tm, Nothing) e [] 0))
97 -- In safe mode, values must convert (Thinks: This is probably not
98 -- useful as is, perhaps it should require a proof of equality instead)
99 when safe $ case converts ctxt [] clhs_tm crhs_tm of
101 Error e -> ierror (At fc (CantUnify False (clhs_tm, Nothing) (crhs_tm, Nothing) e [] 0))
103 case unApply (depat clhs_tm) of
104 (P _ tfname _, _) -> do addTrans tfname (clhs_tm, crhs_tm)
105 addIBC (IBCTrans tf (clhs_tm, crhs_tm))
106 _ -> ierror (At fc (Msg "Invalid transformation rule (must be function application)"))
107 return (clhs_tm, crhs_tm)
110 depat (Bind n (PVar t) sc) = depat (instantiate (P Bound n t) sc)
113 renamepats (n' : ns) (Bind n (PVar t) sc)
114 = Bind n' (PVar t) (renamepats ns sc) -- all Vs
117 -- names for transformation variables. Need to ensure these don't clash
118 -- with any other names when applying rules, so rename here.
119 pnames = map (\i -> sMN i ("tvar" ++ show i)) [0..]
121elabTransform info fc safe lhs_in rhs_in
122 = ierror (At fc (Msg "Invalid transformation rule (must be function application)"))
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list