@@ -5,167 +5,170 @@ import Idris.Core.TT
55import Idris.AbsSyntax
66
77
8−extract1 :: (PTerm -> a) -> PTerm -> Elab' aux (a, [(Name, PTerm)])
9−extract1 c tm = do (tm', ex) <- extractUnquotes tm
10− return (c tm', ex)
8+extract1 :: Int -> (PTerm -> a) -> PTerm -> Elab' aux (a, [(Name, PTerm)])
9+extract1 n c tm = do (tm', ex) <- extractUnquotes n tm
10+ return (c tm', ex)
1111
12−extract2 :: (PTerm -> PTerm -> a) -> PTerm -> PTerm -> Elab' aux (a, [(Name, PTerm)])
13−extract2 c a b = do (a', ex1) <- extractUnquotes a
14− (b', ex2) <- extractUnquotes b
15− return (c a' b', ex1 ++ ex2)
12+extract2 :: Int -> (PTerm -> PTerm -> a) -> PTerm -> PTerm -> Elab' aux (a, [(Name, PTerm)])
13+extract2 n c a b = do (a', ex1) <- extractUnquotes n a
14+ (b', ex2) <- extractUnquotes n b
15+ return (c a' b', ex1 ++ ex2)
1616
17−extractTUnquotes :: PTactic -> Elab' aux (PTactic, [(Name, PTerm)])
18−extractTUnquotes (Rewrite t) = extract1 Rewrite t
19−extractTUnquotes (Induction t) = extract1 Induction t
20−extractTUnquotes (LetTac n t) = extract1 (LetTac n) t
21−extractTUnquotes (LetTacTy n t1 t2) = extract2 (LetTacTy n) t1 t2
22−extractTUnquotes (Exact tm) = extract1 Exact tm
23−extractTUnquotes (Try tac1 tac2)
24− = do (tac1', ex1) <- extractTUnquotes tac1
25− (tac2', ex2) <- extractTUnquotes tac2
17+extractTUnquotes :: Int -> PTactic -> Elab' aux (PTactic, [(Name, PTerm)])
18+extractTUnquotes n (Rewrite t) = extract1 n Rewrite t
19+extractTUnquotes n (Induction t) = extract1 n Induction t
20+extractTUnquotes n (LetTac name t) = extract1 n (LetTac name) t
21+extractTUnquotes n (LetTacTy name t1 t2) = extract2 n (LetTacTy name) t1 t2
22+extractTUnquotes n (Exact tm) = extract1 n Exact tm
23+extractTUnquotes n (Try tac1 tac2)
24+ = do (tac1', ex1) <- extractTUnquotes n tac1
25+ (tac2', ex2) <- extractTUnquotes n tac2
2626 return (Try tac1' tac2', ex1 ++ ex2)
27−extractTUnquotes (TSeq tac1 tac2)
28− = do (tac1', ex1) <- extractTUnquotes tac1
29− (tac2', ex2) <- extractTUnquotes tac2
27+extractTUnquotes n (TSeq tac1 tac2)
28+ = do (tac1', ex1) <- extractTUnquotes n tac1
29+ (tac2', ex2) <- extractTUnquotes n tac2
3030 return (TSeq tac1' tac2', ex1 ++ ex2)
31−extractTUnquotes (ApplyTactic t) = extract1 ApplyTactic t
32−extractTUnquotes (ByReflection t) = extract1 ByReflection t
33−extractTUnquotes (Reflect t) = extract1 Reflect t
34−extractTUnquotes (GoalType s tac)
35− = do (tac', ex) <- extractTUnquotes tac
31+extractTUnquotes n (ApplyTactic t) = extract1 n ApplyTactic t
32+extractTUnquotes n (ByReflection t) = extract1 n ByReflection t
33+extractTUnquotes n (Reflect t) = extract1 n Reflect t
34+extractTUnquotes n (GoalType s tac)
35+ = do (tac', ex) <- extractTUnquotes n tac
3636 return (GoalType s tac', ex)
37−extractTUnquotes (TCheck t) = extract1 TCheck t
38−extractTUnquotes (TEval t) = extract1 TEval t
39−extractTUnquotes tac = return (tac, []) -- the rest don't contain PTerms
37+extractTUnquotes n (TCheck t) = extract1 n TCheck t
38+extractTUnquotes n (TEval t) = extract1 n TEval t
39+extractTUnquotes n tac = return (tac, []) -- the rest don't contain PTerms
4040
41−extractPArgUnquotes :: PArg -> Elab' aux (PArg, [(Name, PTerm)])
42−extractPArgUnquotes (PImp p m opts n t) =
43− do (t', ex) <- extractUnquotes t
41+extractPArgUnquotes :: Int -> PArg -> Elab' aux (PArg, [(Name, PTerm)])
42+extractPArgUnquotes d (PImp p m opts n t) =
43+ do (t', ex) <- extractUnquotes d t
4444 return (PImp p m opts n t', ex)
45−extractPArgUnquotes (PExp p opts n t) =
46− do (t', ex) <- extractUnquotes t
45+extractPArgUnquotes d (PExp p opts n t) =
46+ do (t', ex) <- extractUnquotes d t
4747 return (PExp p opts n t', ex)
48−extractPArgUnquotes (PConstraint p opts n t) =
49− do (t', ex) <- extractUnquotes t
48+extractPArgUnquotes d (PConstraint p opts n t) =
49+ do (t', ex) <- extractUnquotes d t
5050 return (PConstraint p opts n t', ex)
51−extractPArgUnquotes (PTacImplicit p opts n scpt t) =
52− do (scpt', ex1) <- extractUnquotes scpt
53− (t', ex2) <- extractUnquotes t
51+extractPArgUnquotes d (PTacImplicit p opts n scpt t) =
52+ do (scpt', ex1) <- extractUnquotes d scpt
53+ (t', ex2) <- extractUnquotes d t
5454 return (PTacImplicit p opts n scpt' t', ex1 ++ ex2)
5555
56−extractDoUnquotes :: PDo -> Elab' aux (PDo, [(Name, PTerm)])
57−extractDoUnquotes (DoExp fc tm)
58− = do (tm', ex) <- extractUnquotes tm
56+extractDoUnquotes :: Int -> PDo -> Elab' aux (PDo, [(Name, PTerm)])
57+extractDoUnquotes d (DoExp fc tm)
58+ = do (tm', ex) <- extractUnquotes d tm
5959 return (DoExp fc tm', ex)
60−extractDoUnquotes (DoBind fc n tm)
61− = do (tm', ex) <- extractUnquotes tm
60+extractDoUnquotes d (DoBind fc n tm)
61+ = do (tm', ex) <- extractUnquotes d tm
6262 return (DoBind fc n tm', ex)
63−extractDoUnquotes (DoBindP fc t t' alts)
63+extractDoUnquotes d (DoBindP fc t t' alts)
6464 = fail "Pattern-matching binds cannot be quasiquoted"
65−extractDoUnquotes (DoLet fc n v b)
66− = do (v', ex1) <- extractUnquotes v
67− (b', ex2) <- extractUnquotes b
65+extractDoUnquotes d (DoLet fc n v b)
66+ = do (v', ex1) <- extractUnquotes d v
67+ (b', ex2) <- extractUnquotes d b
6868 return (DoLet fc n v' b', ex1 ++ ex2)
69−extractDoUnquotes (DoLetP fc t t') = fail "Pattern-matching lets cannot be quasiquoted"
69+extractDoUnquotes d (DoLetP fc t t') = fail "Pattern-matching lets cannot be quasiquoted"
7070
7171
72−extractUnquotes :: PTerm -> Elab' aux (PTerm, [(Name, PTerm)])
73−extractUnquotes (PLam n ty body)
74− = do (ty', ex1) <- extractUnquotes ty
75− (body', ex2) <- extractUnquotes body
76− return (PLam n ty' body', ex1 ++ ex2)
77−extractUnquotes (PPi plicity n ty body)
78− = do (ty', ex1) <- extractUnquotes ty
79− (body', ex2) <- extractUnquotes body
80− return (PPi plicity n ty' body', ex1 ++ ex2)
81−extractUnquotes (PLet n ty val body)
82− = do (ty', ex1) <- extractUnquotes ty
83− (val', ex2) <- extractUnquotes val
84− (body', ex3) <- extractUnquotes body
85− return (PLet n ty' val' body', ex1 ++ ex2 ++ ex3)
86−extractUnquotes (PTyped tm ty)
87− = do (tm', ex1) <- extractUnquotes tm
88− (ty', ex2) <- extractUnquotes ty
72+extractUnquotes :: Int -> PTerm -> Elab' aux (PTerm, [(Name, PTerm)])
73+extractUnquotes n (PLam name ty body)
74+ = do (ty', ex1) <- extractUnquotes n ty
75+ (body', ex2) <- extractUnquotes n body
76+ return (PLam name ty' body', ex1 ++ ex2)
77+extractUnquotes n (PPi plicity name ty body)
78+ = do (ty', ex1) <- extractUnquotes n ty
79+ (body', ex2) <- extractUnquotes n body
80+ return (PPi plicity name ty' body', ex1 ++ ex2)
81+extractUnquotes n (PLet name ty val body)
82+ = do (ty', ex1) <- extractUnquotes n ty
83+ (val', ex2) <- extractUnquotes n val
84+ (body', ex3) <- extractUnquotes n body
85+ return (PLet name ty' val' body', ex1 ++ ex2 ++ ex3)
86+extractUnquotes n (PTyped tm ty)
87+ = do (tm', ex1) <- extractUnquotes n tm
88+ (ty', ex2) <- extractUnquotes n ty
8989 return (PTyped tm' ty', ex1 ++ ex2)
90−extractUnquotes (PApp fc f args)
91− = do (f', ex1) <- extractUnquotes f
92− args' <- mapM extractPArgUnquotes args
90+extractUnquotes n (PApp fc f args)
91+ = do (f', ex1) <- extractUnquotes n f
92+ args' <- mapM (extractPArgUnquotes n) args
9393 let (args'', exs) = unzip args'
9494 return (PApp fc f' args'', ex1 ++ concat exs)
95−extractUnquotes (PAppBind fc f args)
96− = do (f', ex1) <- extractUnquotes f
97− args' <- mapM extractPArgUnquotes args
95+extractUnquotes n (PAppBind fc f args)
96+ = do (f', ex1) <- extractUnquotes n f
97+ args' <- mapM (extractPArgUnquotes n) args
9898 let (args'', exs) = unzip args'
9999 return (PAppBind fc f' args'', ex1 ++ concat exs)
100−extractUnquotes (PCase fc expr cases)
101− = do (expr', ex1) <- extractUnquotes expr
100+extractUnquotes n (PCase fc expr cases)
101+ = do (expr', ex1) <- extractUnquotes n expr
102102 let (pats, rhss) = unzip cases
103− (pats', exs1) <- fmap unzip $ mapM extractUnquotes pats
104− (rhss', exs2) <- fmap unzip $ mapM extractUnquotes rhss
103+ (pats', exs1) <- fmap unzip $ mapM (extractUnquotes n) pats
104+ (rhss', exs2) <- fmap unzip $ mapM (extractUnquotes n) rhss
105105 return (PCase fc expr' (zip pats' rhss'), ex1 ++ concat exs1 ++ concat exs2)
106−extractUnquotes (PRefl fc x)
107− = do (x', ex) <- extractUnquotes x
106+extractUnquotes n (PRefl fc x)
107+ = do (x', ex) <- extractUnquotes n x
108108 return (PRefl fc x', ex)
109−extractUnquotes (PEq fc at bt a b)
110− = do (at', ex1) <- extractUnquotes at
111− (bt', ex2) <- extractUnquotes bt
112− (a', ex1) <- extractUnquotes a
113− (b', ex2) <- extractUnquotes b
109+extractUnquotes n (PEq fc at bt a b)
110+ = do (at', ex1) <- extractUnquotes n at
111+ (bt', ex2) <- extractUnquotes n bt
112+ (a', ex1) <- extractUnquotes n a
113+ (b', ex2) <- extractUnquotes n b
114114 return (PEq fc at' bt' a' b', ex1 ++ ex2)
115−extractUnquotes (PRewrite fc x y z)
116− = do (x', ex1) <- extractUnquotes x
117− (y', ex2) <- extractUnquotes y
115+extractUnquotes n (PRewrite fc x y z)
116+ = do (x', ex1) <- extractUnquotes n x
117+ (y', ex2) <- extractUnquotes n y
118118 case z of
119− Just zz -> do (z', ex3) <- extractUnquotes zz
119+ Just zz -> do (z', ex3) <- extractUnquotes n zz
120120 return (PRewrite fc x' y' (Just z'), ex1 ++ ex2 ++ ex3)
121121 Nothing -> return (PRewrite fc x' y' Nothing, ex1 ++ ex2)
122−extractUnquotes (PPair fc info l r)
123− = do (l', ex1) <- extractUnquotes l
124− (r', ex2) <- extractUnquotes r
122+extractUnquotes n (PPair fc info l r)
123+ = do (l', ex1) <- extractUnquotes n l
124+ (r', ex2) <- extractUnquotes n r
125125 return (PPair fc info l' r', ex1 ++ ex2)
126−extractUnquotes (PDPair fc info a b c)
127− = do (a', ex1) <- extractUnquotes a
128− (b', ex2) <- extractUnquotes b
129− (c', ex3) <- extractUnquotes c
126+extractUnquotes n (PDPair fc info a b c)
127+ = do (a', ex1) <- extractUnquotes n a
128+ (b', ex2) <- extractUnquotes n b
129+ (c', ex3) <- extractUnquotes n c
130130 return (PDPair fc info a' b' c', ex1 ++ ex2 ++ ex3)
131−extractUnquotes (PAlternative b alts)
132− = do alts' <- mapM extractUnquotes alts
131+extractUnquotes n (PAlternative b alts)
132+ = do alts' <- mapM (extractUnquotes n) alts
133133 let (alts'', exs) = unzip alts'
134134 return (PAlternative b alts'', concat exs)
135−extractUnquotes (PHidden tm)
136− = do (tm', ex) <- extractUnquotes tm
135+extractUnquotes n (PHidden tm)
136+ = do (tm', ex) <- extractUnquotes n tm
137137 return (PHidden tm', ex)
138−extractUnquotes (PGoal fc a n b)
139− = do (a', ex1) <- extractUnquotes a
140− (b', ex2) <- extractUnquotes b
141− return (PGoal fc a' n b', ex1 ++ ex2)
142−extractUnquotes (PDoBlock steps)
143− = do steps' <- mapM extractDoUnquotes steps
138+extractUnquotes n (PGoal fc a name b)
139+ = do (a', ex1) <- extractUnquotes n a
140+ (b', ex2) <- extractUnquotes n b
141+ return (PGoal fc a' name b', ex1 ++ ex2)
142+extractUnquotes n (PDoBlock steps)
143+ = do steps' <- mapM (extractDoUnquotes n) steps
144144 let (steps'', exs) = unzip steps'
145145 return (PDoBlock steps'', concat exs)
146−extractUnquotes (PIdiom fc tm)
147− = fmap (\(tm', ex) -> (PIdiom fc tm', ex)) $ extractUnquotes tm
148−extractUnquotes (PProof tacs)
149− = do (tacs', exs) <- fmap unzip $ mapM extractTUnquotes tacs
146+extractUnquotes n (PIdiom fc tm)
147+ = fmap (\(tm', ex) -> (PIdiom fc tm', ex)) $ extractUnquotes n tm
148+extractUnquotes n (PProof tacs)
149+ = do (tacs', exs) <- fmap unzip $ mapM (extractTUnquotes n) tacs
150150 return (PProof tacs', concat exs)
151−extractUnquotes (PTactics tacs)
152− = do (tacs', exs) <- fmap unzip $ mapM extractTUnquotes tacs
151+extractUnquotes n (PTactics tacs)
152+ = do (tacs', exs) <- fmap unzip $ mapM (extractTUnquotes n) tacs
153153 return (PTactics tacs', concat exs)
154−extractUnquotes (PElabError err) = fail "Can't quasiquote an error"
155−extractUnquotes (PCoerced tm)
156− = do (tm', ex) <- extractUnquotes tm
154+extractUnquotes n (PElabError err) = fail "Can't quasiquote an error"
155+extractUnquotes n (PCoerced tm)
156+ = do (tm', ex) <- extractUnquotes n tm
157157 return (PCoerced tm', ex)
158−extractUnquotes (PDisamb ns tm)
159− = do (tm', ex) <- extractUnquotes tm
158+extractUnquotes n (PDisamb ns tm)
159+ = do (tm', ex) <- extractUnquotes n tm
160160 return (PDisamb ns tm', ex)
161−extractUnquotes (PUnifyLog tm)
162− = fmap (\(tm', ex) -> (PUnifyLog tm', ex)) $ extractUnquotes tm
163−extractUnquotes (PNoImplicits tm)
164− = fmap (\(tm', ex) -> (PNoImplicits tm', ex)) $ extractUnquotes tm
165−extractUnquotes (PQuasiquote _ _) = fail "Nested quasiquotes not supported"
166−extractUnquotes (PUnquote tm) =
167− do n <- getNameFrom (sMN 0 "unquotation")
168− return (PRef (fileFC "(unquote)") n, [(n, tm)])
169−extractUnquotes x = return (x, []) -- no subterms!
161+extractUnquotes n (PUnifyLog tm)
162+ = fmap (\(tm', ex) -> (PUnifyLog tm', ex)) $ extractUnquotes n tm
163+extractUnquotes n (PNoImplicits tm)
164+ = fmap (\(tm', ex) -> (PNoImplicits tm', ex)) $ extractUnquotes n tm
165+extractUnquotes n (PQuasiquote tm goal)
166+ = fmap (\(tm', ex) -> (PQuasiquote tm' goal, ex)) $ extractUnquotes (n+1) tm
167+extractUnquotes n (PUnquote tm)
168+ | n == 0 = do n <- getNameFrom (sMN 0 "unquotation")
169+ return (PRef (fileFC "(unquote)") n, [(n, tm)])
170+ | otherwise = fmap (\(tm', ex) -> (PUnquote tm', ex)) $
171+ extractUnquotes (n-1) tm
172+extractUnquotes n x = return (x, []) -- no subterms!
170173
171174