on master help sign in
Add andThen and refine to Pruviloj

andThen is a subgoal sequencing combinator, and refine is a bit like Coq's eapply.

commitc9973eb7c369b7a143a5b02ada6bde6fdf2215c3 authorDavid Raymond Christiansen <david@davidchristiansen.dk> dateThu 1 Oct 2015, 18:28 (11 years ago) parent210db7d
2 files changed+36−9
libs/pruviloj/Pruviloj/Core.idr+35 test/pruviloj001/pruviloj001.idr+1−9
libs/pruviloj/Pruviloj/Core.idr+35
@@ -160,3 +160,38 @@ inferType tac =
160160 | err => fail [TextPart "Type inference failure"]
161161 solve
162162 focus tmH
163+
164+||| Given one tactic that produces a list of subgoal names and another
165+||| that produces some result, run the second tactic in each hole
166+||| produced by the first and return the resulting values.
167+|||
168+||| Elab has no built-in notion of "subgoals", so this simulates the
169+||| Coq or JonPRL semicolon operators.
170+|||
171+||| @ first run this tactic to produce subgoals
172+||| @ after run this tactic in each subgoal
173+andThen : (first : Elab (List TTName)) -> (after : Elab a) -> Elab (List a)
174+andThen first after =
175+ do hs <- first
176+ catMaybes <$> for hs (flip inHole after)
177+
178+||| Refine the current goal using some term, constructing holes for
179+||| all arguments that can't be inferred. Return the list of generated
180+||| holes.
181+|||
182+||| @ tm the term to apply to some number of goals
183+refine : (tm : Raw) -> Elab (List TTName)
184+refine tm =
185+ do ty <- (snd <$> check tm) >>= forgetTypes
186+ g <- goalType
187+
188+ -- we don't care about negative results because it'll just fail anyway
189+ let argCount = minus (countPi ty) (countPi g)
190+ newHoles <- apply tm (replicate argCount (True, 0))
191+ solve
192+ actualHoles <- getHoles
193+ return (filter (flip elem actualHoles) (map snd newHoles))
194+
195+ where countPi : Raw -> Nat
196+ countPi (RBind _ (Pi _ _) body) = S (countPi body)
197+ countPi _ = Z
test/pruviloj001/pruviloj001.idr+1−9
@@ -19,14 +19,6 @@ auto = do compute
1919 hypothesis <|> search
2020 solve
2121
22−
23−||| ; in Coq (roughly)
24−andThen : Elab (List TTName) -> Elab a -> Elab ()
25−andThen tac1 tac2 =
26− do hs <- tac1
27− for_ hs $ \h =>
28− inHole h $ ignore tac2
29−
3022||| Common pattern of proofs by induction.
3123partial
3224mush : Elab ()
@@ -34,7 +26,7 @@ mush =
3426 do n <- gensym "j"
3527 intro n
3628 try intros
37− induction (Var n) `andThen` auto
29+ ignore $ induction (Var n) `andThen` auto
3830
3931plusAssoc : (j, k, l : Nat) -> plus (plus j k) l = plus j (plus k l)
4032plusAssoc = %runElab mush