on master help sign in
Merge pull request #4099 from joom/master

Replace splitPair with construct tactic

commita3583be821f168403fd51f5256dffb27ec5babd5 authorDavid Christiansen <david@davidchristiansen.dk> dateWed 4 Oct 2017, 09:35 (9 years ago) parenteabb92288fd351
3 files changed+32−15
CHANGELOG.md+1−1 libs/pruviloj/Pruviloj/Core.idr+18−14 test/pruviloj001/pruviloj001.idr+13
CHANGELOG.md+1−1
@@ -13,7 +13,7 @@
1313
1414+ Added `Text.Literate`, a module for working with literate source files.
1515+ Added `Data.IORef`, for working with mutable references in `IO`.
16−+ Added `discriminate` and `splitPair` tactics to Pruviloj.
16++ Added `discriminate` and `construct` tactics to Pruviloj.
1717
1818## Tool Updates
1919+ Private functions are no longer visible in the REPL except for modules
libs/pruviloj/Pruviloj/Core.idr+18−14
@@ -256,20 +256,24 @@ unproduct tm =
256256 try (unproduct (Var n1))
257257 try (unproduct (Var n2))
258258
259−||| If the goal is a tuple or a dependent pair, then split it into
260−||| two goals and return the new goal names.
261−||| It fails if the goal is neither a `Pair` nor a `DPair`.
262−splitPair : Elab (TTName, TTName)
263−splitPair =
264− let bug = [TextPart "Splitting created wrong number of holes"] in
265− case !goalType of
266− `(Pair ~A ~B) => do [x, y] <- apply `(MkPair {A=~A} {B=~B}) [False, False]
267− | _ => fail bug
268− solve ; pure (x, y)
269− `(DPair ~A ~Pr) => do [x, y] <- apply `(MkDPair {a=~A} {P=~Pr}) [False, False]
270− | _ => fail bug
271− solve ; pure (x, y)
272− _ => fail [TextPart "Goal is not a pair"]
259+||| Try to apply the constructors of the goal data type one by one,
260+||| and apply the first one that works. If one of the constructors work,
261+||| the explicit arguments to the constructor are created as new holes and
262+||| the hole names are returned in a list. The parameters of the type and
263+||| the implicit arguments of the constructor will be solved by unification.
264+||| Similar to `constructor` in Coq.
265+construct : Elab (List TTName)
266+construct = case headName !goalType of
267+ Nothing =>
268+ fail [TextPart "Goal is not of a type declared with the data keyword"]
269+ Just h =>
270+ choiceMap (\(n, xs, _) => apply (Var n) (map shouldUnify xs) <* solve)
271+ !(constructors <$> lookupDatatypeExact h)
272+ <|> fail [TextPart "No constructors apply"]
273+ where
274+ shouldUnify : CtorArg -> Bool
275+ shouldUnify (CtorField (MkFunArg _ _ Explicit _)) = False
276+ shouldUnify _ = True
273277
274278||| A special-purpose tactic that attempts to solve a goal using
275279||| `Refl`. This is useful for ensuring that goals in fact are trivial
test/pruviloj001/pruviloj001.idr+13
@@ -66,6 +66,19 @@ appendNilRightId : (l : List a) ->
6666 l ++ [] = l
6767appendNilRightId = %runElab mush
6868
69+pair : a -> b -> (a, b)
70+pair = %runElab (do intros ; construct ; hypothesis ; hypothesis)
71+
72+data Even : Nat -> Type where
73+ EvenZ : Even Z
74+ EvenSS : Even n -> Even (S (S n))
75+
76+even6 : Even 6
77+even6 = %runElab (do construct ; construct ; construct ; construct ; skip)
78+
79+even6' : Even 6
80+even6' = %runElab (repeatUntilFail (ignore construct))
81+
6982-- Local Variables:
7083-- idris-load-packages: ("pruviloj")
7184-- End: