Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assumption? #281

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions library/init/meta/interactive.lean
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,13 @@ meta def refine (q : parse texpr) : tactic unit :=
tactic.refine q

/--
This tactic looks in the local context for a hypothesis whose type is equal to the goal target. If it finds one, it uses it to prove the goal, and otherwise it fails.
This tactic looks in the local context for a hypothesis whose type is equal to the goal target.
If it finds one, it uses it to prove the goal, and otherwise it fails.
`assumption?` will print the name of the goal that it found.
-/
meta def assumption : tactic unit :=
tactic.assumption

meta def assumption (trace_result : parse $ optional (tk "?")) :=
tactic.assumption trace_result.is_some

/-- Try to apply `assumption` to all goals. -/
meta def assumption' : tactic unit :=
Expand Down
13 changes: 12 additions & 1 deletion library/init/meta/tactic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -948,10 +948,21 @@ meta def find_same_type : expr → list expr → tactic expr
meta def find_assumption (e : expr) : tactic expr :=
do ctx ← local_context, find_same_type e ctx

meta def assumption : tactic unit :=
meta def is_implicit : expr → bool
| (expr.pi _ binder_info.implicit _ _) := tt
| _ := ff

/-- Find a hypothesis matching the goal. `assumption tt` will print the goal that it found.
If the goal type starts with an implicit binder, prints an "@" in front. -/
meta def assumption (trace_result : bool := ff) : tactic unit :=
do { ctx ← local_context,
t ← target,
H ← find_same_type t ctx,
when trace_result $
do { pp_H ← pp H,
H_type ← tactic.infer_type H,
let H_format := if is_implicit H_type then ↑"@" ++ pp_H else pp_H,
trace ( ("Try this: exact " : format) ++ H_format) },
exact H }
<|> fail "assumption tactic failed"

Expand Down
9 changes: 9 additions & 0 deletions tests/lean/assumption.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
example (p : Prop) (h1 : p) : p :=
begin
assumption?,
end

example (p : Prop) (h2 : ∀ {x : ℕ}, p) : ∀ x : ℕ, p :=
begin
assumption?,
end
3 changes: 3 additions & 0 deletions tests/lean/assumption.lean.expected.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-{"msgs":[{"caption":"trace output","file_name":"assumption.lean","pos_col":2,"pos_line":3,"severity":"information","text":"Try this: h1\n"}],"response":"all_messages"}
-{"msgs":[{"caption":"trace output","file_name":"assumption.lean","pos_col":2,"pos_line":3,"severity":"information","text":""Try this: @h1\n"},"response":"all_messages"]
-{"message":"file invalidated","response":"ok","seq_num":0}