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

update Lean version and replace std to batteries #150

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 12 additions & 10 deletions lake-manifest.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
{"version": 7,
{"version": "1.1.0",
"packagesDir": ".lake/packages",
"packages":
[{"url": "https://github.com/Seasawher/mdgen",
[{"url": "https://github.com/leanprover-community/batteries",
"type": "git",
"subDir": null,
"rev": "2c641fe54d48a5d4adff3a78dbc4a50c5f76b0c0",
"name": "mdgen",
"scope": "leanprover-community",
"rev": "9e583efcea920afa13ee2a53069821a2297a94c0",
"name": "batteries",
"manifestFile": "lake-manifest.json",
"inputRev": "v1.3.0",
"inputRev": "main",
"inherited": false,
"configFile": "lakefile.lean"},
{"url": "https://github.com/leanprover/std4",
"configFile": "lakefile.toml"},
{"url": "https://github.com/Seasawher/mdgen",
"type": "git",
"subDir": null,
"rev": "32983874c1b897d78f20d620fe92fc8fd3f06c3a",
"name": "std",
"scope": "",
"rev": "2c641fe54d48a5d4adff3a78dbc4a50c5f76b0c0",
"name": "mdgen",
"manifestFile": "lake-manifest.json",
"inputRev": "v4.7.0",
"inputRev": "v1.3.0",
"inherited": false,
"configFile": "lakefile.lean"}],
"name": "«lean4-metaprogramming-book»",
Expand Down
2 changes: 1 addition & 1 deletion lakefile.lean
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ lean_lib «lean4-metaprogramming-book» where
require mdgen from git
"https://github.com/Seasawher/mdgen" @ "v1.3.0"

require std from git "https://github.com/leanprover/std4" @ "v4.7.0"
require "leanprover-community" / "batteries" @ git "main"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we use the corresponding tag -- i.e. https://github.com/leanprover-community/batteries/releases/tag/v4.15.0-rc1 -- so this doesn't break when Batteries updates its Lean version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Batteries updates will not break the code in this book until a lake update is manually performed.

This is not a problem, as any updates to Batteries should eventually be followed.


def runCmd (cmd : String) (args : Array String) : ScriptM Bool := do
let out ← IO.Process.output {
Expand Down
2 changes: 1 addition & 1 deletion lean-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
leanprover/lean4:v4.7.0
leanprover/lean4:v4.15.0-rc1
15 changes: 11 additions & 4 deletions lean/main/01_intro.lean
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,17 @@ elab "#assertType " termStx:term " : " typeStx:term : command =>

/-- info: success -/
#guard_msgs in --#
#assertType 5 : Nat

/-- error: failure -/
#guard_msgs in --#
#assertType 5 : Nat

/--
error: type mismatch
[]
has type
List ?m.3211 : Type ?u.3210
but is expected to have type
Nat : Type
-/
#guard_msgs (error) in --#
#assertType [] : Nat

/-! We started by using `elab` to define a `command` syntax. When parsed
Expand Down
10 changes: 5 additions & 5 deletions lean/main/05_syntax.lean
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ Let's define a simple theory of sets to test it:
-- -> a simple predicate on the type of its elements
def Set (α : Type u) := α → Prop

def Set.mem (x : α) (X : Set α) : Prop := X x
def Set.mem (X : Set α) (x : α) : Prop := X x

-- Integrate into the already existing typeclass for membership notation
instance : Membership α (Set α) where
Expand Down Expand Up @@ -619,14 +619,14 @@ the bound variables, we refer the reader to the macro chapter.
Use the following template:

```lean
import Std.Classes.SetNotation
import Std.Util.ExtendedBinder
import Batteries.Classes.SetNotation
import Batteries.Util.ExtendedBinder
syntax (name := bigsumin) ...
-- our "elaboration function" that infuses syntax with semantics
@[term_elab bigsumin] def elabSum : TermElab := λ stx tp => return mkNatLit 666
```

Hint: use the `Std.ExtendedBinder.extBinder` parser.
Hint: you need Std4 installed in your Lean project for these imports to work.
Hint: use the `Batteries.ExtendedBinder.extBinder` parser.
Hint: you need Batteries installed in your Lean project for these imports to work.

-/
2 changes: 1 addition & 1 deletion lean/main/06_macros.lean
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ As promised in the syntax chapter here is Binders 2.0. We'll start by
reintroducing our theory of sets:
-/
def Set (α : Type u) := α → Prop
def Set.mem (x : α) (X : Set α) : Prop := X x
def Set.mem (X : Set α) (x : α) : Prop := X x

-- Integrate into the already existing typeclass for membership notation
instance : Membership α (Set α) where
Expand Down
10 changes: 5 additions & 5 deletions lean/main/07_elaboration.lean
Original file line number Diff line number Diff line change
Expand Up @@ -274,21 +274,21 @@ Adding new term elaborators works basically the same way as adding new
command elaborators so we'll only take a very brief look:
-/

syntax (name := myterm1) "myterm 1" : term
syntax (name := myterm1) "myterm_1" : term

def mytermValues := [1, 2]

@[term_elab myterm1]
def myTerm1Impl : TermElab := fun stx type? =>
def myTerm1Impl : TermElab := fun stx type? => do
mkAppM ``List.get! #[.const ``mytermValues [], mkNatLit 0] -- `MetaM` code

#eval myterm 1 -- 1
#eval myterm_1 -- 1

-- Also works with `elab`
elab "myterm 2" : term => do
elab "myterm_2" : term => do
mkAppM ``List.get! #[.const ``mytermValues [], mkNatLit 1] -- `MetaM` code

#eval myterm 2 -- 2
#eval myterm_2 -- 2

/-!
### Mini project
Expand Down
2 changes: 1 addition & 1 deletion lean/main/09_tactics.lean
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ elab "custom_assump_2" : tactic =>
then return Option.some declExpr
else return Option.none
match option_matching_expr with
| some e => Lean.Elab.Tactic.closeMainGoal e
| some e => Lean.Elab.Tactic.closeMainGoal `custom_assump_2 e
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm only scanning the PR so apologies for not trying to figure this out myself, but can you say something about what's happening here with the extra argument?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Lean.Elab.Tactic.closeMainGoal argument appears to have been increased. It now accepts the tactic name for use in error messages.

| none =>
Lean.Meta.throwTacticEx `custom_assump_2 goal
(m!"unable to find matching hypothesis of type ({goalType})")
Expand Down
8 changes: 4 additions & 4 deletions lean/solutions/05_syntax.lean
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Lean
import Lean.Parser.Syntax
import Std.Util.ExtendedBinder
import Batteries.Util.ExtendedBinder

open Lean Elab Command Term

Expand Down Expand Up @@ -28,7 +28,7 @@ open a

/- ### 2. -/

syntax "good morning" : term
syntax "good" "morning" : term
syntax "hello" : command
syntax "yellow" : tactic

Expand Down Expand Up @@ -70,8 +70,8 @@ def elabHelp : CommandElab := fun stx => Lean.logInfo "success!"

/- ### 5. -/

-- Note: std4 has to be in dependencies of your project for this to work.
syntax (name := bigsumin) "∑ " Std.ExtendedBinder.extBinder "in " term "," term : term
-- Note: Batteries has to be in dependencies of your project for this to work.
syntax (name := bigsumin) "∑ " Batteries.ExtendedBinder.extBinder "in " term "," term : term

@[term_elab bigsumin]
def elabSum : TermElab := fun stx tp =>
Expand Down
20 changes: 10 additions & 10 deletions lean/solutions/09_tactics.lean
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ elab "step_1" : tactic => do
let Expr.app (Expr.app (Expr.const `Iff _) a) b := goalType | throwError "Goal type is not of the form `a ↔ b`"

-- 1. Create new `_`s with appropriate types.
let mvarId1 ← mkFreshExprMVar (Expr.forallE `xxx a b .default) (userName := "red")
let mvarId2 ← mkFreshExprMVar (Expr.forallE `yyy b a .default) (userName := "blue")
let mvarId1 ← mkFreshExprMVar (Expr.forallE `xxx a b .default) (userName := `red)
let mvarId2 ← mkFreshExprMVar (Expr.forallE `yyy b a .default) (userName := `blue)

-- 2. Assign the main goal to the expression `Iff.intro _ _`.
mvarId.assign (mkAppN (Expr.const `Iff.intro []) #[a, b, mvarId1, mvarId2])
Expand Down Expand Up @@ -60,8 +60,8 @@ elab "step_3" : tactic => do
let Expr.app (Expr.app (Expr.const `And _) q) p := goalType | throwError "Goal type is not of the form `And q p`"

-- 1. Create new `_`s with appropriate types.
let mvarId1 ← mkFreshExprMVarAt mainDecl.lctx mainDecl.localInstances q (userName := "red1")
let mvarId2 ← mkFreshExprMVarAt mainDecl.lctx mainDecl.localInstances p (userName := "red2")
let mvarId1 ← mkFreshExprMVarAt mainDecl.lctx mainDecl.localInstances q (userName := `red1)
let mvarId2 ← mkFreshExprMVarAt mainDecl.lctx mainDecl.localInstances p (userName := `red2)

-- 2. Assign the main goal to the expression `And.intro _ _`.
mvarId.assign (← mkAppM `And.intro #[mvarId1, mvarId2])
Expand Down Expand Up @@ -109,8 +109,8 @@ elab "forker_a" : tactic => do
liftMetaTactic fun mvarId => do
let (Expr.app (Expr.app (Expr.const `And _) p) q) ← mvarId.getType | Lean.Meta.throwTacticEx `forker mvarId ("Goal is not of the form P ∧ Q")

let mvarIdP ← mkFreshExprMVar p (userName := "red")
let mvarIdQ ← mkFreshExprMVar q (userName := "blue")
let mvarIdP ← mkFreshExprMVar p (userName := `red)
let mvarIdQ ← mkFreshExprMVar q (userName := `blue)

let proofTerm := mkAppN (Expr.const `And.intro []) #[p, q, mvarIdP, mvarIdQ]
mvarId.assign proofTerm
Expand All @@ -124,8 +124,8 @@ elab "forker_b" : tactic => do
let (Expr.app (Expr.app (Expr.const `And _) p) q) := goalType | Lean.Meta.throwTacticEx `forker mvarId ("Goal is not of the form P ∧ Q")

mvarId.withContext do
let mvarIdP ← mkFreshExprMVar p (userName := "red")
let mvarIdQ ← mkFreshExprMVar q (userName := "blue")
let mvarIdP ← mkFreshExprMVar p (userName := `red)
let mvarIdQ ← mkFreshExprMVar q (userName := `blue)

let proofTerm := mkAppN (Expr.const `And.intro []) #[p, q, mvarIdP, mvarIdQ]
mvarId.assign proofTerm
Expand All @@ -139,8 +139,8 @@ elab "forker_c" : tactic => do
let (Expr.app (Expr.app (Expr.const `And _) p) q) := goalType | Lean.Meta.throwTacticEx `forker mvarId ("Goal is not of the form P ∧ Q")

mvarId.withContext do
let mvarIdP ← mkFreshExprMVar p (userName := "red")
let mvarIdQ ← mkFreshExprMVar q (userName := "blue")
let mvarIdP ← mkFreshExprMVar p (userName := `red)
let mvarIdQ ← mkFreshExprMVar q (userName := `blue)

let proofTerm := mkAppN (Expr.const `And.intro []) #[p, q, mvarIdP, mvarIdQ]
mvarId.assign proofTerm
Expand Down
Loading