-
Notifications
You must be signed in to change notification settings - Fork 829
Expand file tree
/
Copy pathBasic.lean
More file actions
324 lines (277 loc) · 12.1 KB
/
Basic.lean
File metadata and controls
324 lines (277 loc) · 12.1 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
module
prelude
public import Lean.Meta.CollectFVars
public import Lean.Meta.Match.CaseArraySizes
public section
namespace Lean.Meta.Match
def mkNamedPattern (x h p : Expr) : MetaM Expr :=
mkAppM ``namedPattern #[x, p, h]
def isNamedPattern (e : Expr) : Bool :=
let e := e.consumeMData
e.getAppNumArgs == 4 && e.getAppFn.consumeMData.isConstOf ``namedPattern
def isNamedPattern? (e : Expr) : Option Expr :=
let e := e.consumeMData
if e.getAppNumArgs == 4 && e.getAppFn.consumeMData.isConstOf ``namedPattern then
some e
else
none
inductive Pattern : Type where
| inaccessible (e : Expr) : Pattern
| var (fvarId : FVarId) : Pattern
| ctor (ctorName : Name) (us : List Level) (params : List Expr) (fields : List Pattern) : Pattern
| val (e : Expr) : Pattern
| arrayLit (type : Expr) (xs : List Pattern) : Pattern
| as (varId : FVarId) (p : Pattern) (hId : FVarId) : Pattern
deriving Inhabited
namespace Pattern
partial def toMessageData : Pattern → MessageData
| inaccessible e => m!".({e})"
| var varId => mkFVar varId
| ctor ctorName _ _ [] => ctorName
| ctor ctorName _ _ pats => m!"({ctorName}{pats.foldl (fun (msg : MessageData) pat => msg ++ " " ++ toMessageData pat) Format.nil})"
| val e => e
| arrayLit _ pats => m!"#[{MessageData.joinSep (pats.map toMessageData) ", "}]"
| as varId p _ => m!"{mkFVar varId}@{toMessageData p}"
partial def toExpr (p : Pattern) (annotate := false) : MetaM Expr :=
visit p
where
visit (p : Pattern) := do
match p with
| inaccessible e =>
if annotate then
pure (mkInaccessible e)
else
pure e
| var fvarId => pure $ mkFVar fvarId
| val e => pure e
| as fvarId p hId =>
-- TODO
if annotate then
mkNamedPattern (mkFVar fvarId) (mkFVar hId) (← visit p)
else
visit p
| arrayLit type xs =>
let xs ← xs.mapM visit
mkArrayLit type xs
| ctor ctorName us params fields =>
let fields ← fields.mapM visit
pure $ mkAppN (mkConst ctorName us) (params ++ fields).toArray
/-- Apply the free variable substitution `s` to the given pattern -/
partial def applyFVarSubst (s : FVarSubst) : Pattern → Pattern
| inaccessible e => inaccessible $ s.apply e
| ctor n us ps fs => ctor n us (ps.map s.apply) $ fs.map (applyFVarSubst s)
| val e => val $ s.apply e
| arrayLit t xs => arrayLit (s.apply t) $ xs.map (applyFVarSubst s)
| var fvarId => match s.find? fvarId with
| some e => inaccessible e
| none => var fvarId
| as fvarId p hId => match s.find? fvarId with
| none => as fvarId (applyFVarSubst s p) hId
| some _ => applyFVarSubst s p
def replaceFVarId (fvarId : FVarId) (v : Expr) (p : Pattern) : Pattern :=
let s : FVarSubst := {}
p.applyFVarSubst (s.insert fvarId v)
partial def hasExprMVar : Pattern → Bool
| inaccessible e => e.hasExprMVar
| ctor _ _ ps fs => ps.any (·.hasExprMVar) || fs.any hasExprMVar
| val e => e.hasExprMVar
| as _ p _ => hasExprMVar p
| arrayLit t xs => t.hasExprMVar || xs.any hasExprMVar
| _ => false
partial def collectFVars (p : Pattern) : StateRefT CollectFVars.State MetaM Unit := do
match p with
| inaccessible e => e.collectFVars
| ctor _ _ ps fs =>
ps.forM fun p => p.collectFVars
fs.forM collectFVars
| val e => e.collectFVars
| arrayLit t xs => t.collectFVars; xs.forM collectFVars
| as fvarId₁ p fvarId₂ => modify (·.add fvarId₁ |>.add fvarId₂); p.collectFVars
| var fvarId => modify (·.add fvarId)
end Pattern
partial def instantiatePatternMVars : Pattern → MetaM Pattern
| Pattern.inaccessible e => return Pattern.inaccessible (← instantiateMVars e)
| Pattern.val e => return Pattern.val (← instantiateMVars e)
| Pattern.ctor n us ps fields => return Pattern.ctor n us (← ps.mapM instantiateMVars) (← fields.mapM instantiatePatternMVars)
| Pattern.as x p h => return Pattern.as x (← instantiatePatternMVars p) h
| Pattern.arrayLit t xs => return Pattern.arrayLit (← instantiateMVars t) (← xs.mapM instantiatePatternMVars)
| p => return p
structure AltLHS where
ref : Syntax
fvarDecls : List LocalDecl -- Free variables used in the patterns.
patterns : List Pattern -- We use `List Pattern` since we have nary match-expressions.
deriving Inhabited
def AltLHS.collectFVars (altLHS: AltLHS) : StateRefT CollectFVars.State MetaM Unit := do
altLHS.fvarDecls.forM fun fvarDecl => fvarDecl.collectFVars
altLHS.patterns.forM fun p => p.collectFVars
def instantiateAltLHSMVars (altLHS : AltLHS) : MetaM AltLHS :=
return { altLHS with
fvarDecls := (← altLHS.fvarDecls.mapM instantiateLocalDeclMVars),
patterns := (← altLHS.patterns.mapM instantiatePatternMVars)
}
/-- `Match` alternative -/
structure Alt where
/-- `Syntax` object for providing position information -/
ref : Syntax
/--
Original alternative index. Alternatives can be split, this index is the original
position of the alternative that generated this one.
-/
idx : Nat
/--
Right-hand-side of the alternative.
-/
rhs : Expr
/--
Alternative pattern variables.
-/
fvarDecls : List LocalDecl
/--
Alternative patterns.
-/
patterns : List Pattern
/--
Pending constraints `lhs ≋ rhs` that need to be solved before the alternative
is considered acceptable. We generate them when processing inaccessible patterns.
Note that `lhs` and `rhs` often have different types.
After we perform additional case analysis, their types become definitionally equal.
-/
cnstrs : List (Expr × Expr)
deriving Inhabited
namespace Alt
partial def toMessageData (alt : Alt) : MetaM MessageData := do
withExistingLocalDecls alt.fvarDecls do
let msg := alt.fvarDecls.map fun d => m!"{d.toExpr}:({d.type})"
let mut msg := m!"{msg} |- {alt.patterns.map Pattern.toMessageData} => {alt.rhs}"
for (lhs, rhs) in alt.cnstrs do
msg := m!"{msg}\n | {lhs} ≋ {rhs}"
addMessageContext msg
def applyFVarSubst (s : FVarSubst) (alt : Alt) : Alt :=
{ alt with
patterns := alt.patterns.map fun p => p.applyFVarSubst s,
fvarDecls := alt.fvarDecls.map fun d => d.applyFVarSubst s,
rhs := alt.rhs.applyFVarSubst s
cnstrs := alt.cnstrs.map fun (lhs, rhs) => (lhs.applyFVarSubst s, rhs.applyFVarSubst s) }
def replaceFVarId (fvarId : FVarId) (v : Expr) (alt : Alt) : Alt :=
{ alt with
patterns := alt.patterns.map fun p => p.replaceFVarId fvarId v,
rhs := alt.rhs.replaceFVarId fvarId v
fvarDecls :=
let decls := alt.fvarDecls.filter fun d => d.fvarId != fvarId
decls.map (·.replaceFVarId fvarId v)
cnstrs := alt.cnstrs.map fun (lhs, rhs) => (lhs.replaceFVarId fvarId v, rhs.replaceFVarId fvarId v) }
/-- Return `true` if `fvarId` is one of the alternative pattern variables -/
def isLocalDecl (fvarId : FVarId) (alt : Alt) : Bool :=
alt.fvarDecls.any fun d => d.fvarId == fvarId
end Alt
inductive Example where
| var : FVarId → Example
| underscore : Example
| ctor : Name → List Example → Example
| val : Expr → Example
| arrayLit : List Example → Example
namespace Example
partial def replaceFVarId (fvarId : FVarId) (ex : Example) : Example → Example
| var x => if x == fvarId then ex else var x
| ctor n exs => ctor n $ exs.map (replaceFVarId fvarId ex)
| arrayLit exs => arrayLit $ exs.map (replaceFVarId fvarId ex)
| ex => ex
partial def applyFVarSubst (s : FVarSubst) : Example → Example
| var fvarId =>
match s.get fvarId with
| Expr.fvar fvarId' => var fvarId'
| _ => underscore
| ctor n exs => ctor n $ exs.map (applyFVarSubst s)
| arrayLit exs => arrayLit $ exs.map (applyFVarSubst s)
| ex => ex
partial def varsToUnderscore : Example → Example
| var _ => underscore
| ctor n exs => ctor n $ exs.map varsToUnderscore
| arrayLit exs => arrayLit $ exs.map varsToUnderscore
| ex => ex
partial def toMessageData : Example → MessageData
| var fvarId => mkFVar fvarId
| ctor ctorName [] => mkConst ctorName
| ctor ctorName exs => m!"({.ofConstName ctorName}{exs.foldl (fun msg pat => m!"{msg} {toMessageData pat}") Format.nil})"
| arrayLit exs => "#" ++ MessageData.ofList (exs.map toMessageData)
| val e => e
| underscore => "_"
end Example
def examplesToMessageData (cex : List Example) : MessageData :=
MessageData.joinSep (cex.map (Example.toMessageData ∘ Example.varsToUnderscore)) ", "
structure Problem where
mvarId : MVarId
vars : List Expr
alts : List Alt
examples : List Example
deriving Inhabited
def withGoalOf {α} (p : Problem) (x : MetaM α) : MetaM α :=
p.mvarId.withContext x
def Problem.toMessageData (p : Problem) : MetaM MessageData :=
withGoalOf p do
let alts ← p.alts.mapM Alt.toMessageData
let vars ← p.vars.mapM fun x => do let xType ← inferType x; pure m!"{x}:({xType})"
return m!"remaining variables: {vars}\nalternatives:{indentD (MessageData.joinSep alts Format.line)}\nexamples:{examplesToMessageData p.examples}\n"
abbrev CounterExample := List Example
def counterExampleToMessageData (cex : CounterExample) : MessageData :=
examplesToMessageData cex
def counterExamplesToMessageData (cexs : List CounterExample) : MessageData :=
MessageData.joinSep (cexs.map counterExampleToMessageData) Format.line
structure MatcherResult where
matcher : Expr -- The matcher. It is not just `Expr.const matcherName` because the type of the major premises may contain free variables.
counterExamples : List CounterExample
unusedAltIdxs : List Nat
addMatcher : MetaM Unit
/--
Convert a expression occurring as the argument of a `match` motive application back into a `Pattern`
For example, we can use this method to convert `x::y::xs` at
```
...
(motive : List Nat → Sort u_1) (xs : List Nat) (h_1 : (x y : Nat) → (xs : List Nat) → motive (x :: y :: xs))
...
```
into a pattern object
-/
partial def toPattern (e : Expr) : MetaM Pattern := do
match inaccessible? e with
| some t => return Pattern.inaccessible t
| none =>
match e.arrayLit? with
| some (α, lits) =>
return Pattern.arrayLit α (← lits.mapM toPattern)
| none =>
if let some e := isNamedPattern? e then
let p ← toPattern <| e.getArg! 2
match e.getArg! 1, e.getArg! 3 with
| Expr.fvar x, Expr.fvar h => return Pattern.as x p h
| _, _ => throwError "Unexpected occurrence of auxiliary declaration 'namedPattern'"
else if (← isMatchValue e) then
return Pattern.val e
else if e.isFVar then
return Pattern.var e.fvarId!
else
let newE ← whnf e
if newE != e then
toPattern newE
else matchConstCtor e.getAppFn (fun _ => throwError "Unexpected pattern{indentExpr e}") fun v us => do
let args := e.getAppArgs
unless args.size == v.numParams + v.numFields do
throwError "Unexpected pattern{indentExpr e}"
let params := args.extract 0 v.numParams
let fields := args.extract v.numParams args.size
let fields ← fields.mapM toPattern
return Pattern.ctor v.name us params.toList fields.toList
/-! Match congruence equational theorem names helper declarations and functions -/
def congrEqnThmSuffixBase := "congr_eq"
def congrEqnThmSuffixBasePrefix := congrEqnThmSuffixBase ++ "_"
def congrEqn1ThmSuffix := congrEqnThmSuffixBasePrefix ++ "1"
example : congrEqn1ThmSuffix = "congr_eq_1" := rfl
/-- Returns `true` if `s` is of the form `congr_eq_<idx>` -/
def isCongrEqnReservedNameSuffix (s : String) : Bool :=
congrEqnThmSuffixBasePrefix.isPrefixOf s && (s.drop congrEqnThmSuffixBasePrefix.length).isNat
end Lean.Meta.Match