-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBook.lean
More file actions
164 lines (117 loc) · 5.14 KB
/
Book.lean
File metadata and controls
164 lines (117 loc) · 5.14 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
/-
Copyright (c) Philip Wadler. All rights reserved.
Released under Creative Commons CC-BY License as described in file LICENSE.
Author: Philip Wadler
-/
import VersoManual
import Book.Meta.Lean
import Book.Papers
import Book.Nat
import Book.Naturals
/-!
Book: top-level file for plfl
-/
-- This gets access to most of the manual genre (which is also useful for textbooks)
open Verso.Genre Manual
-- This gets access to Lean code that's in code blocks, elaborated in the same process and
-- environment as Verso
open Verso.Genre.Manual.InlineLean
open Book
set_option pp.rawOnError true
#doc (Manual) "Programming Language Foundations in Lean" =>
%%%
authors := ["Philip Wadler"]
%%%
{index}[intro]
This is my inspiring introduction.
{include 1 Book.Naturals}
# Lean Code
The tools in this section come from the Verso namespace `Verso.Genre.Manual.InlineLean`.
The {lean}`lean` code block allows Lean code to be included in the text.
It is elaborated in the context of the text's elaboration, but in a separate section scope (so the set of variables and opened namespaces can be controlled).
```lean
inductive NatList where
| nil
| cons : Nat → NatList → NatList
```
Use the {lean}`leanSection` directive to create a Lean section that delimits scope changes.
The {lean}`lean` role allows Lean terms to be included as inline elements in paragraphs.
Use {lean}`name` to refer to a name that can't be easily elaborated as a term, e.g. due to implicit parameters.
## Saved Lean Code
The tools in this section come from the Verso namespace `Book` in the module `Book.Meta.Lean`.
The {lean}`savedLean` code block is just like the {lean}`lean` block, except it additionally saves the contents to a file when the book is built.
The code is saved to the output directory, in the subdirectory `example-code` (by default, this is `_out/example-code`), with its filename being that of the file in which it is edited.
Use {lean}`savedImport` to save code for the file header.
```savedComment
Here's some commentary for the file
```
```savedLean
def x : Nat := 15
```
When named, the code block's output is saved.
It can be both checked and included in the document using {lean}`leanOutput`:
```savedLean (name := xVal)
#eval x
```
```leanOutput xVal
15
```
Expected error messages must be indicated explicitly:
```lean (error := true) (name := yVal)
#eval y
```
```leanOutput yVal
unknown identifier 'y'
```
{include 1 Book.Nat}
# Notes
Use {lean}`margin` to create a marginal note.{margin}[Marginal notes should be used like footnotes.]
# Citations
Cite works using {lean}`citet`, {lean}`citep`, or {lean}`citehere`.
They take a name of a citable reference value as a parameter.
References should be defined as values, typically in one module that is imported (similar to the role of a `.bib` file in LaTeX).
Textual citations, as with {citet someThesis}[], look like this.
Parenthetical {citep someArXiv}[] looks like this.
Use {lean}`citehere` to literally include the cite rather than making a margin note, e.g. {citehere somePaper}[].
Literally-included cites are mostly useful when performing citation inside a margin note.
# Section References
%%%
tag := "sec-ref"
%%%
Sections with tags can be cross-referenced.
They additionally gain permalink indicators that can be used to link to them even if the document is reorganized.
Tags are added in section metadata, e.g.
````
%%%
tag := "my-tag"
%%%
````
They can be linked to using {lean}`ref`.
Here's one to {ref "sec-ref"}[this section].
# Viewing the Output
Verso's HTML doesn't presently work correctly when opened directly in a browser, so it should be served via a server.{margin}[This is due to security restrictions on retrieved files: some of the code hovers are deduplicated to a JSON file that's fetched on demand.]
Python has a simple web server module that's useful for this.
In the output directory, run:
```
python3 -m http.server 8000 --directory .
```
The port and root can be customized by modifying the appropriate parameters.
One downside of this simple server is that it sets cache headers optimistically.
If incorrect hovers are appearing locally, then try disabling caching in your browser's development tools.
# Using an Index
{index}[index]
The index should contain an entry for “lorem ipsum”.
{index}[lorem ipsum] foo
{index subterm:="of lorem"}[ipsum]
{index subterm:="per se"}[ipsum]
{index}[ipsum]
Lorem ipsum dolor {index}[dolor] sit amet, consectetur adipiscing elit, sed {index}[sed] do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris {index}[laboris] {see "lorem ipsum"}[laboris] {seeAlso "dolor"}[laboris] nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
This is done using the `{index}[term]` syntax. Sub-terms {index subterm:="sub-term"}[entry] can be added using the `subterm` parameter to `index`.
Multiple index {index}[index] targets for a term also work.
{ref "index"}[Index link]
# Index
%%%
number := false
tag := "index"
%%%
{theIndex}