@@ -32,14 +32,25 @@ toplevel = EInfo [] emptyContext id Nothing
32build :: IState -> ElabInfo -> Bool -> Name -> PTerm ->
32build :: IState -> ElabInfo -> Bool -> Name -> PTerm ->
33 Elab (Term, [(Name, Type)])
33 Elab (Term, [(Name, Type)])
34build ist info pattern fn tm
34build ist info pattern fn tm
35− = do elab ist info pattern fn tm
35+ = do elab ist info pattern False fn tm
36 tt <- get_term
36 tt <- get_term
37 return $ runState (collectDeferred tt) []
37 return $ runState (collectDeferred tt) []
39+-- Build a term autogenerated as a typeclass method definition
40+-- (Separate, so we don't go overboard resolving things that we don't
41+-- know about yet on the LHS of a pattern def)
43+buildTC :: IState -> ElabInfo -> Bool -> Bool -> Name -> PTerm ->
44+ Elab (Term, [(Name, Type)])
45+buildTC ist info pattern tcgen fn tm
46+ = do elab ist info pattern tcgen fn tm
48+ return $ runState (collectDeferred tt) []
39−elab :: IState -> ElabInfo -> Bool -> Name -> PTerm ->
50+elab :: IState -> ElabInfo -> Bool -> Bool -> Name -> PTerm ->
41−elab ist info pattern fn tm
52+elab ist info pattern tcgen fn tm
42− = do elabE tm
53+ = do elabE False tm
43 when pattern -- convert remaining holes to pattern vars
54 when pattern -- convert remaining holes to pattern vars
@@ -47,66 +58,66 @@ elab ist info pattern fn tm
47 Placeholder -> (True, priority arg)
58 Placeholder -> (True, priority arg)
48 _ -> (False, priority arg)
59 _ -> (False, priority arg)
50− toElab arg = case getTm arg of
61+ toElab ina arg = case getTm arg of
51 Placeholder -> Nothing
62 Placeholder -> Nothing
52− v -> Just (priority arg, elabE v)
63+ v -> Just (priority arg, elabE ina v)
54 mkPat = do hs <- get_holes
65 mkPat = do hs <- get_holes
55 case hs of
66 case hs of
56 (h: hs) -> do patvar h; mkPat
67 (h: hs) -> do patvar h; mkPat
57 [] -> return ()
68 [] -> return ()
59− elabE t = {- do g <- goal
70+ elabE ina t = {- do g <- goal
60 tm <- get_term
71 tm <- get_term
61 trace ("Elaborating " ++ show t ++ " : " ++ show g ++ "\n\tin " ++ show tm)
72 trace ("Elaborating " ++ show t ++ " : " ++ show g ++ "\n\tin " ++ show tm)
62− $ -} elab' t
73+ $ -} elab' ina t
64 local f = do e <- get_env
75 local f = do e <- get_env
65 return (f `elem` map fst e)
76 return (f `elem` map fst e)
67− elab' PSet = do fill RSet; solve
78+ elab' ina PSet = do fill RSet; solve
68− elab' (PConstant c) = do apply (RConstant c) []; solve
79+ elab' ina (PConstant c) = do apply (RConstant c) []; solve
69− elab' (PQuote r) = do fill r; solve
80+ elab' ina (PQuote r) = do fill r; solve
70− elab' (PTrue fc) = try (elab' (PRef fc unitCon))
81+ elab' ina (PTrue fc) = try (elab' ina (PRef fc unitCon))
71− (elab' (PRef fc unitTy))
82+ (elab' ina (PRef fc unitTy))
72− elab' (PFalse fc) = elab' (PRef fc falseTy)
83+ elab' ina (PFalse fc) = elab' ina (PRef fc falseTy)
73− elab' (PResolveTC (FC "HACK" _)) -- for chasing parent classes
84+ elab' ina (PResolveTC (FC "HACK" _)) -- for chasing parent classes
74 = resolveTC 2 fn ist
85 = resolveTC 2 fn ist
75− elab' (PResolveTC fc) = do c <- unique_hole (MN 0 "c")
86+ elab' ina (PResolveTC fc) = do c <- unique_hole (MN 0 "c")
76− instanceArg c
87+ instanceArg c
77− elab' (PRefl fc) = elab' (PApp fc (PRef fc eqCon) [pimp (MN 0 "a") Placeholder,
88+ elab' ina (PRefl fc) = elab' ina (PApp fc (PRef fc eqCon) [pimp (MN 0 "a") Placeholder,
78 pimp (MN 0 "x") Placeholder])
89 pimp (MN 0 "x") Placeholder])
79− elab' (PEq fc l r) = elab' (PApp fc (PRef fc eqTy) [pimp (MN 0 "a") Placeholder,
90+ elab' ina (PEq fc l r) = elab' ina (PApp fc (PRef fc eqTy) [pimp (MN 0 "a") Placeholder,
80 pimp (MN 0 "b") Placeholder,
91 pimp (MN 0 "b") Placeholder,
81 pexp l, pexp r])
92 pexp l, pexp r])
82− elab' (PPair fc l r) = try (elabE (PApp fc (PRef fc pairTy)
93+ elab' ina (PPair fc l r) = try (elabE True (PApp fc (PRef fc pairTy)
83 [pexp l,pexp r]))
94 [pexp l,pexp r]))
84− (elabE (PApp fc (PRef fc pairCon)
95+ (elabE True (PApp fc (PRef fc pairCon)
85 [pimp (MN 0 "A") Placeholder,
96 [pimp (MN 0 "A") Placeholder,
86 pimp (MN 0 "B") Placeholder,
97 pimp (MN 0 "B") Placeholder,
87 pexp l, pexp r]))
98 pexp l, pexp r]))
88− elab' (PDPair fc l@(PRef _ n) t r)
99+ elab' ina (PDPair fc l@(PRef _ n) t r)
89 = case t of
100 = case t of
90 Placeholder -> try asType asValue
101 Placeholder -> try asType asValue
91 _ -> asType
102 _ -> asType
92− where asType = elab' (PApp fc (PRef fc sigmaTy)
103+ where asType = elab' ina (PApp fc (PRef fc sigmaTy)
94 pexp (PLam n Placeholder r)])
105 pexp (PLam n Placeholder r)])
95− asValue = elab' (PApp fc (PRef fc existsCon)
106+ asValue = elab' ina (PApp fc (PRef fc existsCon)
96 [pimp (MN 0 "a") t,
107 [pimp (MN 0 "a") t,
97 pimp (MN 0 "P") Placeholder,
108 pimp (MN 0 "P") Placeholder,
98 pexp l, pexp r])
109 pexp l, pexp r])
99− elab' (PDPair fc l t r) = elab' (PApp fc (PRef fc existsCon)
110+ elab' ina (PDPair fc l t r) = elab' ina (PApp fc (PRef fc existsCon)
100 [pimp (MN 0 "a") t,
111 [pimp (MN 0 "a") t,
101 pimp (MN 0 "P") Placeholder,
112 pimp (MN 0 "P") Placeholder,
102 pexp l, pexp r])
113 pexp l, pexp r])
103− elab' (PAlternative as)
114+ elab' ina (PAlternative as)
104 = let as' = pruneAlt as in
115 = let as' = pruneAlt as in
105− try (tryAll (zip (map elab' as') (map showHd as')))
116+ try (tryAll (zip (map (elab' ina) as') (map showHd as')))
106− (tryAll (zip (map elab' as) (map showHd as)))
117+ (tryAll (zip (map (elab' ina) as) (map showHd as)))
107 where showHd (PApp _ h _) = show h
118 where showHd (PApp _ h _) = show h
108 showHd x = show x
119 showHd x = show x
109− elab' (PRef fc n) | pattern && not (inparamBlock n)
120+ elab' ina (PRef fc n) | pattern && not (inparamBlock n)
110 = do ctxt <- get_context
121 = do ctxt <- get_context
111 let sc = case lookupTy Nothing n ctxt of
122 let sc = case lookupTy Nothing n ctxt of
112 [] -> True
123 [] -> True
@@ -119,22 +130,22 @@ elab ist info pattern fn tm
119 where inparamBlock n = case lookupCtxtName Nothing n (inblock info) of
130 where inparamBlock n = case lookupCtxtName Nothing n (inblock info) of
120 [] -> False
131 [] -> False
121 _ -> True
132 _ -> True
122− elab' (PRef fc n) = erun fc $ do apply (Var n) []; solve
133+ elab' ina (PRef fc n) = erun fc $ do apply (Var n) []; solve
123− elab' (PLam n Placeholder sc)
134+ elab' ina (PLam n Placeholder sc)
124− = do attack; intro (Just n); elabE sc; solve
135+ = do attack; intro (Just n); elabE True sc; solve
125− elab' (PLam n ty sc)
136+ elab' ina (PLam n ty sc)
126 = do tyn <- unique_hole (MN 0 "lamty")
137 = do tyn <- unique_hole (MN 0 "lamty")
127 claim tyn RSet
138 claim tyn RSet
129 introTy (Var tyn) (Just n)
140 introTy (Var tyn) (Just n)
130 -- end_unify
141 -- end_unify
131 focus tyn
142 focus tyn
132− elabE ty
143+ elabE True ty
133− elabE sc
144+ elabE True sc
135− elab' (PPi _ n Placeholder sc)
146+ elab' ina (PPi _ n Placeholder sc)
136− = do attack; arg n (MN 0 "ty"); elabE sc; solve
147+ = do attack; arg n (MN 0 "ty"); elabE True sc; solve
137− elab' (PPi _ n ty sc)
148+ elab' ina (PPi _ n ty sc)
138 = do attack; tyn <- unique_hole (MN 0 "ty")
149 = do attack; tyn <- unique_hole (MN 0 "ty")
139 claim tyn RSet
150 claim tyn RSet
140 n' <- case n of
151 n' <- case n of
@@ -142,10 +153,10 @@ elab ist info pattern fn tm
142 _ -> return n
153 _ -> return n
143 forall n' (Var tyn)
154 forall n' (Var tyn)
144 focus tyn
155 focus tyn
145− elabE ty
156+ elabE True ty
146− elabE sc
157+ elabE True sc
148− elab' (PLet n ty val sc)
159+ elab' ina (PLet n ty val sc)
149 = do attack;
160 = do attack;
150 tyn <- unique_hole (MN 0 "letty")
161 tyn <- unique_hole (MN 0 "letty")
151 claim tyn RSet
162 claim tyn RSet
@@ -155,27 +166,31 @@ elab ist info pattern fn tm
155 case ty of
166 case ty of
156 Placeholder -> return ()
167 Placeholder -> return ()
157 _ -> do focus tyn
168 _ -> do focus tyn
158− elabE ty
169+ elabE True ty
159 focus valn
170 focus valn
160− elabE val
171+ elabE True val
161− elabE sc
172+ elabE True sc
163− elab' (PApp fc (PRef _ f) args')
174+ elab' ina (PApp fc (PRef _ f) args')
164 = do let args = {- case lookupCtxt f (inblock info) of
175 = do let args = {- case lookupCtxt f (inblock info) of
165 Just ps -> (map (pexp . (PRef fc)) ps ++ args')
176 Just ps -> (map (pexp . (PRef fc)) ps ++ args')
166 _ ->-} args'
177 _ ->-} args'
167 ivs <- get_instances
178 ivs <- get_instances
179+ -- HACK: we shouldn't resolve type classes if we're defining an instance
180+ -- function or default defition.
181+ let isinf = f == inferCon || tcname f
168 try (do ns <- apply (Var f) (map isph args)
182 try (do ns <- apply (Var f) (map isph args)
170 let (ns', eargs)
184 let (ns', eargs)
171 = unzip $
185 = unzip $
172 sortBy (\(_,x) (_,y) -> compare (priority x) (priority y))
186 sortBy (\(_,x) (_,y) -> compare (priority x) (priority y))
173 (zip ns args)
187 (zip ns args)
188+ elabArgs (ina || not isinf)
174− elabArgs [] False ns' (map (\x -> (lazyarg x, getTm x)) eargs))
189+ [] False ns' (map (\x -> (lazyarg x, getTm x)) eargs))
175− (do apply_elab f (map toElab args)
190+ (do apply_elab f (map (toElab (ina || not isinf)) args)
177 ivs' <- get_instances
192 ivs' <- get_instances
178− when (not pattern) $
193+ when (not pattern || (ina && not tcgen)) $
179 mapM_ (\n -> do focus n
194 mapM_ (\n -> do focus n
180 resolveTC 7 fn ist) (ivs' \\ ivs)
195 resolveTC 7 fn ist) (ivs' \\ ivs)
181-- ivs <- get_instances
196-- ivs <- get_instances
@@ -187,37 +202,37 @@ elab ist info pattern fn tm
187 where tcArg (n, PConstraint _ _ Placeholder) = True
202 where tcArg (n, PConstraint _ _ Placeholder) = True
188 tcArg _ = False
203 tcArg _ = False
190− elab' (PApp fc f [arg])
205+ elab' a (PApp fc f [arg])
191 = erun fc $
206 = erun fc $
192− do simple_app (elabE f) (elabE (getTm arg))
207+ do simple_app (elabE a f) (elabE True (getTm arg))
194− elab' Placeholder = do (h : hs) <- get_holes
209+ elab' ina Placeholder = do (h : hs) <- get_holes
195− movelast h
210+ movelast h
196− elab' (PMetavar n) = let n' = case namespace info of
211+ elab' ina (PMetavar n) = let n' = case namespace info of
197 Just xs@(_:_) -> NS n xs
212 Just xs@(_:_) -> NS n xs
198 _ -> n in
213 _ -> n in
199− do attack; defer n'; solve
214+ do attack; defer n'; solve
200− elab' (PProof ts) = do mapM_ (runTac True ist) ts
215+ elab' ina (PProof ts) = do mapM_ (runTac True ist) ts
201− elab' (PTactics ts) = do mapM_ (runTac False ist) ts
216+ elab' ina (PTactics ts) = do mapM_ (runTac False ist) ts
202− elab' (PElabError e) = fail e
217+ elab' ina (PElabError e) = fail e
203− elab' x = fail $ "Something's gone wrong. Did you miss a semi-colon somewhere?"
218+ elab' ina x = fail $ "Something's gone wrong. Did you miss a semi-colon somewhere?"
205− elabArgs failed retry [] _
220+ elabArgs ina failed retry [] _
206 | retry = let (ns, ts) = unzip (reverse failed) in
221 | retry = let (ns, ts) = unzip (reverse failed) in
207− elabArgs [] False ns ts
222+ elabArgs ina [] False ns ts
208 | otherwise = return ()
223 | otherwise = return ()
209− elabArgs failed r (n:ns) ((_, Placeholder) : args)
224+ elabArgs ina failed r (n:ns) ((_, Placeholder) : args)
210− = elabArgs failed r ns args
225+ = elabArgs ina failed r ns args
211− elabArgs failed r (n:ns) ((lazy, t) : args)
226+ elabArgs ina failed r (n:ns) ((lazy, t) : args)
212 | lazy && not pattern
227 | lazy && not pattern
213 = do elabArg n (PApp bi (PRef bi (UN "lazy"))
228 = do elabArg n (PApp bi (PRef bi (UN "lazy"))
214 [pimp (UN "a") Placeholder,
229 [pimp (UN "a") Placeholder,
215 pexp t]);
230 pexp t]);
216 | otherwise = elabArg n t
231 | otherwise = elabArg n t
217 where elabArg n t = if r
232 where elabArg n t = if r
218− then try (do focus n; elabE t; elabArgs failed r ns args)
233+ then try (do focus n; elabE ina t; elabArgs ina failed r ns args)
219− (elabArgs ((n,(lazy, t)):failed) r ns args)
234+ (elabArgs ina ((n,(lazy, t)):failed) r ns args)
220− else do focus n; elabE t; elabArgs failed r ns args
235+ else do focus n; elabE ina t; elabArgs ina failed r ns args
222pruneAlt :: [PTerm] -> [PTerm]
237pruneAlt :: [PTerm] -> [PTerm]
223pruneAlt xs = map prune xs
238pruneAlt xs = map prune xs
@@ -233,12 +248,13 @@ pruneAlt xs = map prune xs
233 headIs f _ = True -- keep if it's not an application
248 headIs f _ = True -- keep if it's not an application
235trivial :: IState -> Elab ()
250trivial :: IState -> Elab ()
236−trivial ist = try (elab ist toplevel False (MN 0 "tac") (PRefl (FC "prf" 0)))
251+trivial ist = try (elab ist toplevel False False (MN 0 "tac") (PRefl (FC "prf" 0)))
237 (do env <- get_env
252 (do env <- get_env
238 tryAll (map fst env))
253 tryAll (map fst env))
240 tryAll [] = fail "No trivial solution"
255 tryAll [] = fail "No trivial solution"
241− tryAll (x:xs) = try (elab ist toplevel False (MN 0 "tac") (PRef (FC "prf" 0) x))
256+ tryAll (x:xs) = try (elab ist toplevel False False
257+ (MN 0 "tac") (PRef (FC "prf" 0) x))
242 (tryAll xs)
258 (tryAll xs)
244resolveTC :: Int -> Name -> IState -> Elab ()
260resolveTC :: Int -> Name -> IState -> Elab ()
@@ -271,9 +287,6 @@ resolveTC depth fn ist
271 blunderbuss t (n:ns) | n /= fn && tcname n = try (resolve n depth)
287 blunderbuss t (n:ns) | n /= fn && tcname n = try (resolve n depth)
272 (blunderbuss t ns)
288 (blunderbuss t ns)
273 | otherwise = blunderbuss t ns
289 | otherwise = blunderbuss t ns
274− tcname (UN ('@':_)) = True
275− tcname (NS n _) = tcname n
278 resolve n depth
291 resolve n depth
279 | depth == 0 = fail $ "Can't resolve type class"
292 | depth == 0 = fail $ "Can't resolve type class"
@@ -316,7 +329,7 @@ runTac autoSolve ist tac = runT (fmap (addImpl ist) tac) where
316 bname (Bind n _ _) = Just n
329 bname (Bind n _ _) = Just n
317 bname _ = Nothing
330 bname _ = Nothing
318 runT (Intro xs) = mapM_ (\x -> do attack; intro (Just x)) xs
331 runT (Intro xs) = mapM_ (\x -> do attack; intro (Just x)) xs
319− runT (Exact tm) = do elab ist toplevel False (MN 0 "tac") tm
332+ runT (Exact tm) = do elab ist toplevel False False (MN 0 "tac") tm
320 when autoSolve solveAll
333 when autoSolve solveAll
321 runT (Refine fn [])
334 runT (Refine fn [])
322 = do (fn', imps) <- case lookupCtxtName Nothing fn (idris_implicits ist) of
335 = do (fn', imps) <- case lookupCtxtName Nothing fn (idris_implicits ist) of
@@ -345,7 +358,7 @@ runTac autoSolve ist tac = runT (fmap (addImpl ist) tac) where
345 letn <- unique_hole (MN 0 "rewrite_rule")
358 letn <- unique_hole (MN 0 "rewrite_rule")
346 letbind letn (Var tyn) (Var valn)
359 letbind letn (Var tyn) (Var valn)
347 focus valn
360 focus valn
348− elab ist toplevel False (MN 0 "tac") tm
361+ elab ist toplevel False False (MN 0 "tac") tm
349 rewrite (Var letn)
362 rewrite (Var letn)
350 when autoSolve solveAll
363 when autoSolve solveAll
351 runT (LetTac n tm)
364 runT (LetTac n tm)
@@ -357,7 +370,7 @@ runTac autoSolve ist tac = runT (fmap (addImpl ist) tac) where
357 letn <- unique_hole n
370 letn <- unique_hole n
358 letbind letn (Var tyn) (Var valn)
371 letbind letn (Var tyn) (Var valn)
359 focus valn
372 focus valn
360− elab ist toplevel False (MN 0 "tac") tm
373+ elab ist toplevel False False (MN 0 "tac") tm
361 when autoSolve solveAll
374 when autoSolve solveAll
362 runT Compute = compute
375 runT Compute = compute
363 runT Trivial = do trivial ist; when autoSolve solveAll
376 runT Trivial = do trivial ist; when autoSolve solveAll