You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lean/main/04_metam.lean
+69-69Lines changed: 69 additions & 69 deletions
Original file line number
Diff line number
Diff line change
@@ -1214,47 +1214,47 @@ Notice that changing the type of the metavariable from `Nat` to, for example, `S
1214
1214
2. [**Metavariables**] What would `instantiateMVars (Lean.mkAppN (Expr.const 'Nat.add []) #[mkNatLit 1, mkNatLit 2])` output?
1215
1215
3. [**Metavariables**] Fill in the missing lines in the following code.
1216
1216
1217
-
```
1218
-
#eval show MetaM Unit from do
1219
-
let oneExpr := Expr.app (Expr.const `Nat.succ []) (Expr.const ``Nat.zero [])
1220
-
let twoExpr := Expr.app (Expr.const `Nat.succ []) oneExpr
1221
-
1222
-
-- Create `mvar1` with type `Nat`
1223
-
-- let mvar1 ← ...
1224
-
-- Create `mvar2` with type `Nat`
1225
-
-- let mvar2 ← ...
1226
-
-- Create `mvar3` with type `Nat`
1227
-
-- let mvar3 ← ...
1228
-
1229
-
-- Assign `mvar1` to `2 + ?mvar2 + ?mvar3`
1230
-
-- ...
1231
-
1232
-
-- Assign `mvar3` to `1`
1233
-
-- ...
1234
-
1235
-
-- Instantiate `mvar1`, which should result in expression `2 + ?mvar2 + 1`
1236
-
...
1237
-
```
1217
+
```lean
1218
+
#eval show MetaM Unit from do
1219
+
let oneExpr := Expr.app (Expr.const `Nat.succ []) (Expr.const ``Nat.zero [])
1220
+
let twoExpr := Expr.app (Expr.const `Nat.succ []) oneExpr
1221
+
1222
+
-- Create `mvar1` with type `Nat`
1223
+
-- let mvar1 ← ...
1224
+
-- Create `mvar2` with type `Nat`
1225
+
-- let mvar2 ← ...
1226
+
-- Create `mvar3` with type `Nat`
1227
+
-- let mvar3 ← ...
1228
+
1229
+
-- Assign `mvar1` to `2 + ?mvar2 + ?mvar3`
1230
+
-- ...
1231
+
1232
+
-- Assign `mvar3` to `1`
1233
+
-- ...
1234
+
1235
+
-- Instantiate `mvar1`, which should result in expression `2 + ?mvar2 + 1`
1236
+
...
1237
+
```
1238
1238
4. [**Metavariables**] Consider the theorem `red`, and tactic `explore` below.
1239
-
a) What would be the `type` and `userName` of metavariable `mvarId`?
1240
-
b) What would be the `type`s and `userName`s of all local declarations in this metavariable's local context?
1239
+
**a)** What would be the `type` and `userName` of metavariable `mvarId`?
1240
+
**b)** What would be the `type`s and `userName`s of all local declarations in this metavariable's local context?
1241
1241
Print them all out.
1242
1242
1243
-
```
1244
-
elab "explore" : tactic => do
1245
-
let mvarId : MVarId ← Lean.Elab.Tactic.getMainGoal
1246
-
let metavarDecl : MetavarDecl ← mvarId.getDecl
1243
+
```lean
1244
+
elab "explore" : tactic => do
1245
+
let mvarId : MVarId ← Lean.Elab.Tactic.getMainGoal
1246
+
let metavarDecl : MetavarDecl ← mvarId.getDecl
1247
1247
1248
-
IO.println "Our metavariable"
1249
-
-- ...
1248
+
IO.println "Our metavariable"
1249
+
-- ...
1250
1250
1251
-
IO.println "All of its local declarations"
1252
-
-- ...
1251
+
IO.println "All of its local declarations"
1252
+
-- ...
1253
1253
1254
-
theorem red (hA : 1 = 1) (hB : 2 = 2) : 2 = 2 := by
1255
-
explore
1256
-
sorry
1257
-
```
1254
+
theorem red (hA : 1 = 1) (hB : 2 = 2) : 2 = 2 := by
1255
+
explore
1256
+
sorry
1257
+
```
1258
1258
5. [**Metavariables**] Write a tactic `solve` that proves the theorem `red`.
1259
1259
6. [**Computation**] What is the normal form of the following expressions:
1260
1260
**a)** `fun x => x` of type `Bool → Bool`
@@ -1270,36 +1270,36 @@ Notice that changing the type of the metavariable from `Nat` to, for example, `S
1270
1270
**f)** `2 + ?a =?= 2 + 1`
1271
1271
9. [**Computation**] Write down what you expect the following code to output.
1272
1272
1273
-
```
1274
-
@[reducible] def reducibleDef : Nat := 1 -- same as `abbrev`
1275
-
@[instance] def instanceDef : Nat := 2 -- same as `instance`
1276
-
def defaultDef : Nat := 3
1277
-
@[irreducible] def irreducibleDef : Nat := 4
1273
+
```lean
1274
+
@[reducible] def reducibleDef : Nat := 1 -- same as `abbrev`
1275
+
@[instance] def instanceDef : Nat := 2 -- same as `instance`
1276
+
def defaultDef : Nat := 3
1277
+
@[irreducible] def irreducibleDef : Nat := 4
1278
1278
1279
-
@[reducible] def sum := [reducibleDef, instanceDef, defaultDef, irreducibleDef]
1279
+
@[reducible] def sum := [reducibleDef, instanceDef, defaultDef, irreducibleDef]
1280
1280
1281
-
#eval show MetaM Unit from do
1282
-
let constantExpr := Expr.const `sum []
1281
+
#eval show MetaM Unit from do
1282
+
let constantExpr := Expr.const `sum []
1283
1283
1284
-
Meta.withTransparency Meta.TransparencyMode.reducible do
1285
-
let reducedExpr ← Meta.reduce constantExpr
1286
-
dbg_trace (← ppExpr reducedExpr) -- ...
1284
+
Meta.withTransparency Meta.TransparencyMode.reducible do
1285
+
let reducedExpr ← Meta.reduce constantExpr
1286
+
dbg_trace (← ppExpr reducedExpr) -- ...
1287
1287
1288
-
Meta.withTransparency Meta.TransparencyMode.instances do
1289
-
let reducedExpr ← Meta.reduce constantExpr
1290
-
dbg_trace (← ppExpr reducedExpr) -- ...
1288
+
Meta.withTransparency Meta.TransparencyMode.instances do
1289
+
let reducedExpr ← Meta.reduce constantExpr
1290
+
dbg_trace (← ppExpr reducedExpr) -- ...
1291
1291
1292
-
Meta.withTransparency Meta.TransparencyMode.default do
1293
-
let reducedExpr ← Meta.reduce constantExpr
1294
-
dbg_trace (← ppExpr reducedExpr) -- ...
1292
+
Meta.withTransparency Meta.TransparencyMode.default do
1293
+
let reducedExpr ← Meta.reduce constantExpr
1294
+
dbg_trace (← ppExpr reducedExpr) -- ...
1295
1295
1296
-
Meta.withTransparency Meta.TransparencyMode.all do
1297
-
let reducedExpr ← Meta.reduce constantExpr
1298
-
dbg_trace (← ppExpr reducedExpr) -- ...
1296
+
Meta.withTransparency Meta.TransparencyMode.all do
1297
+
let reducedExpr ← Meta.reduce constantExpr
1298
+
dbg_trace (← ppExpr reducedExpr) -- ...
1299
1299
1300
-
let reducedExpr ← Meta.reduce constantExpr
1301
-
dbg_trace (← ppExpr reducedExpr) -- ...
1302
-
```
1300
+
let reducedExpr ← Meta.reduce constantExpr
1301
+
dbg_trace (← ppExpr reducedExpr) -- ...
1302
+
```
1303
1303
10. [**Constructing Expressions**] Create expression `fun x, 1 + x` in two ways:
1304
1304
**a)** not idiomatically, with loose bound variables
1305
1305
**b)** idiomatically.
@@ -1312,20 +1312,20 @@ def defaultDef : Nat := 3
1312
1312
13. [**Constructing Expressions**] Create expression `fun (f : Nat → Nat), ∀ (n : Nat), f n = f (n + 1)` idiomatically.
1313
1313
14. [**Constructing Expressions**] What would you expect the output of the following code to be?
1314
1314
1315
-
```
1316
-
#eval show Lean.Elab.Term.TermElabM _ from do
1317
-
let stx : Syntax ← `(∀ (a : Prop) (b : Prop), a ∨ b → b → a ∧ a)
1318
-
let expr ← Elab.Term.elabTermAndSynthesize stx none
1315
+
```lean
1316
+
#eval show Lean.Elab.Term.TermElabM _ from do
1317
+
let stx : Syntax ← `(∀ (a : Prop) (b : Prop), a ∨ b → b → a ∧ a)
1318
+
let expr ← Elab.Term.elabTermAndSynthesize stx none
1319
1319
1320
-
let (_, _, conclusion) ← forallMetaTelescope expr
1321
-
dbg_trace conclusion -- ...
1320
+
let (_, _, conclusion) ← forallMetaTelescope expr
1321
+
dbg_trace conclusion -- ...
1322
1322
1323
-
let (_, _, conclusion) ← forallMetaBoundedTelescope expr 2
1324
-
dbg_trace conclusion -- ...
1323
+
let (_, _, conclusion) ← forallMetaBoundedTelescope expr 2
1324
+
dbg_trace conclusion -- ...
1325
1325
1326
-
let (_, _, conclusion) ← lambdaMetaTelescope expr
1327
-
dbg_trace conclusion -- ...
1328
-
```
1326
+
let (_, _, conclusion) ← lambdaMetaTelescope expr
1327
+
dbg_trace conclusion -- ...
1328
+
```
1329
1329
15. [**Backtracking**] Check that the expressions `?a + Int` and `"hi" + ?b` are definitionally equal with `isDefEq` (make sure to use the proper types or `Option.none` for the types of your metavariables!).
1330
1330
Use `saveState` and `restoreState` to revert metavariable assignments.
Copy file name to clipboardExpand all lines: lean/main/05_syntax.lean
+41-41Lines changed: 41 additions & 41 deletions
Original file line number
Diff line number
Diff line change
@@ -559,68 +559,68 @@ the bound variables, we refer the reader to the macro chapter.
559
559
560
560
1. Create an "urgent minus 💀" notation such that `5 * 8 💀 4` returns `20`, and `8 💀 6 💀 1` returns `3`.
561
561
562
-
**a)** Using `notation` command.
563
-
**b)** Using `infix` command.
564
-
**c)** Using `syntax` command.
562
+
**a)** Using `notation` command.
563
+
**b)** Using `infix` command.
564
+
**c)** Using `syntax` command.
565
565
566
-
Hint: multiplication in Lean 4 is defined as `infixl:70 " * " => HMul.hMul`.
566
+
Hint: multiplication in Lean 4 is defined as `infixl:70 " * " => HMul.hMul`.
567
567
568
568
2. Consider the following syntax categories: `term`, `command`, `tactic`; and 3 syntax rules given below. Make use of each of these newly defined syntaxes.
569
569
570
-
```
571
-
syntax "good morning" : term
572
-
syntax "hello" : command
573
-
syntax "yellow" : tactic
574
-
```
570
+
```
571
+
syntax "good morning" : term
572
+
syntax "hello" : command
573
+
syntax "yellow" : tactic
574
+
```
575
575
576
576
3. Create a `syntax` rule that would accept the following commands:
577
577
578
-
- `red red red 4`
579
-
- `blue 7`
580
-
- `blue blue blue blue blue 18`
578
+
- `red red red 4`
579
+
- `blue 7`
580
+
- `blue blue blue blue blue 18`
581
581
582
-
(So, either all `red`s followed by a number; or all `blue`s followed by a number; `red blue blue 5` - shouldn't work.)
582
+
(So, either all `red`s followed by a number; or all `blue`s followed by a number; `red blue blue 5` - shouldn't work.)
583
583
584
-
Use the following code template:
584
+
Use the following code template:
585
585
586
-
```
587
-
syntax (name := colors) ...
588
-
-- our "elaboration function" that infuses syntax with semantics
4. Mathlib has a `#help option` command that displays all options available in the current environment, and their descriptions. `#help option pp.r` will display all options starting with a "pp.r" substring.
593
593
594
-
Create a `syntax` rule that would accept the following commands:
594
+
Create a `syntax` rule that would accept the following commands:
595
595
596
-
- `#better_help option`
597
-
- `#better_help option pp.r`
598
-
- `#better_help option some.other.name`
596
+
- `#better_help option`
597
+
- `#better_help option pp.r`
598
+
- `#better_help option some.other.name`
599
599
600
-
Use the following template:
600
+
Use the following template:
601
601
602
-
```
603
-
syntax (name := help) ...
604
-
-- our "elaboration function" that infuses syntax with semantics
We want `nth_rewrite 5 [←add_zero a] at h` to print out `"rewrite location!"` if the user provided location, and `"rewrite target!"` if the user didn't provide location.
391
+
We want `nth_rewrite 5 [←add_zero a] at h` to print out `"rewrite location!"` if the user provided location, and `"rewrite target!"` if the user didn't provide location.
392
392
393
-
Please add these semantics:
393
+
Please add these semantics:
394
394
395
-
**a)** using `syntax` + `@[tactic nthRewrite] def elabNthRewrite : Lean.Elab.Tactic.Tactic`.
396
-
**b)** using `syntax` + `elab_rules`.
397
-
**c)** using `elab`.
395
+
**a)** using `syntax` + `@[tactic nthRewrite] def elabNthRewrite : Lean.Elab.Tactic.Tactic`.
0 commit comments