forked from leanprover/lean4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatchUtil.lean
68 lines (57 loc) · 1.97 KB
/
MatchUtil.lean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Util.Recognizers
import Lean.Meta.Basic
namespace Lean.Meta
@[inline] def testHelper (e : Expr) (p : Expr → MetaM Bool) : MetaM Bool := do
if (← p e) then
return true
else
p (← whnf e)
@[inline] def matchHelper? (e : Expr) (p? : Expr → MetaM (Option α)) : MetaM (Option α) := do
match (← p? e) with
| none => p? (← whnf e)
| s => return s
/-- Matches `e` with `lhs = (rhs : α)` and returns `(α, lhs, rhs)`. -/
def matchEq? (e : Expr) : MetaM (Option (Expr × Expr × Expr)) :=
matchHelper? e fun e => return Expr.eq? e
def matchHEq? (e : Expr) : MetaM (Option (Expr × Expr × Expr × Expr)) :=
matchHelper? e fun e => return Expr.heq? e
/--
Return `some (α, lhs, rhs)` if `e` is of the form `@Eq α lhs rhs` or `@HEq α lhs α rhs`
-/
def matchEqHEq? (e : Expr) : MetaM (Option (Expr × Expr × Expr)) := do
if let some r ← matchEq? e then
return some r
else if let some (α, lhs, β, rhs) ← matchHEq? e then
if (← isDefEq α β) then
return some (α, lhs, rhs)
return none
else
return none
def matchFalse (e : Expr) : MetaM Bool := do
testHelper e fun e => return e.isConstOf ``False
def matchNot? (e : Expr) : MetaM (Option Expr) :=
matchHelper? e fun e => do
if let some e := e.not? then
return e
else if let some (a, b) := e.arrow? then
if (← matchFalse b) then return some a else return none
else
return none
def matchNe? (e : Expr) : MetaM (Option (Expr × Expr × Expr)) :=
matchHelper? e fun e => do
if let some r := e.ne? then
return r
else if let some e ← matchNot? e then
matchEq? e
else
return none
def matchConstructorApp? (e : Expr) : MetaM (Option ConstructorVal) := do
let env ← getEnv
matchHelper? e fun e =>
return e.isConstructorApp? env
end Lean.Meta