Skip to content
This repository was archived by the owner on Jan 5, 2025. It is now read-only.

Commit 06b4aa0

Browse files
feat: terms and structural recursion (#207)
Closes #55
1 parent f60c485 commit 06b4aa0

31 files changed

+3266
-372
lines changed

.vale/styles/config/ignore/terms.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
APIs
12
Abelian
23
Kleisli
34
Packrat
4-
accessor
5-
accessors
65
antiquotation
76
antiquotation's
87
antiquotations
@@ -26,6 +25,9 @@ delaborated
2625
delaborates
2726
delaborating
2827
delaboration
28+
destructure
29+
destructured
30+
destructures
2931
desugar
3032
desugar
3133
desugared
@@ -34,6 +36,8 @@ desugaring
3436
desugaring
3537
desugarings
3638
desugars
39+
discriminant
40+
discriminant's
3741
disjointness
3842
disjunct
3943
disjuncts
@@ -66,6 +70,8 @@ letterlike
6670
lookup
6771
lookups
6872
macro_rules
73+
matcher
74+
matchers
6975
memoization
7076
metaprogram
7177
metaprogramming
@@ -142,3 +148,4 @@ unexpander
142148
unexpanders
143149
uninstantiated
144150
unparenthesized
151+
walkthrough

Manual/BasicTypes.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ tag := "char-runtime"
131131
%%%
132132

133133

134-
In monomorphic contexts, characters are represented as 32-bit immediate values. In other words, a field of a constructor or structure of type `Char` does not require indirection to access. In polymorphic contexts, characters are boxed.
134+
In monomorphic contexts, characters are represented as 32-bit immediate values. In other words, a field of a constructor or structure of type {lean}`Char` does not require indirection to access. In polymorphic contexts, characters are boxed.
135135

136136

137137
## API Reference

Manual/Elaboration.lean

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,16 @@ In practice, apparent non-termination is indistinguishable from sufficiently slo
201201
These metatheoretic properties are a result of having impredicativity, quotient types that compute, definitional proof irrelevance, and propositional extensionality; these features are immensely valuable both to support ordinary mathematical practice and to enable automation.
202202

203203
# Elaboration Results
204+
%%%
205+
tag := "elaboration-results"
206+
%%%
204207

205208
Lean's core type theory does not include pattern matching or recursive definitions.
206209
Instead, it provides low-level {tech}[recursors] that can be used to implement both case distinction and primitive recursion.
207-
Thus, the elaborator must translate definitions that use pattern matching and recursion into definitions that use recursors.
210+
Thus, the elaborator must translate definitions that use pattern matching and recursion into definitions that use recursors.{margin}[More details on the elaboration of recursive definitions is available in the {ref "recursive-definitions"}[dedicated section] on the topic.]
208211
This translation is additionally a proof that the function terminates for all potential arguments, because all functions that can be translated to recursors also terminate.
209212

210-
The translation to recursors happens in two phases: during term elaboration, uses of pattern matching are replaced by appeals to auxiliary functions that implement the particular case distinction that occurs in the code.
213+
The translation to recursors happens in two phases: during term elaboration, uses of pattern matching are replaced by appeals to {deftech}_auxiliary matching functions_ that implement the particular case distinction that occurs in the code.
211214
These auxiliary functions are themselves defined using recursors, though they do not make use of the recursors' ability to actually implement recursive behavior.{margin}[They use the `casesOn` construction that is described in the {ref "recursor-elaboration-helpers"}[section on recursors and elaboration].]
212215
The term elaborator thus returns core-language terms in which pattern matching has been replaced with the use of special functions that implement case distinction, but these terms may still contain recursive occurrences of the function being defined.
213216
To see auxiliary pattern matching functions in Lean's output, set the option {option}`pp.match` to {lean}`false`.
@@ -251,11 +254,12 @@ This split is for three reasons:
251254
* The compiler can compile {ref "partial-unsafe"}[`partial` functions] that the kernel treats as opaque constants for the purposes of reasoning.
252255
* The compiler can also compile {ref "partial-unsafe"}[`unsafe` functions] that bypass the kernel entirely.
253256
* Translation to recursors does not necessarily preserve the cost model expected by programmers, in particular laziness vs strictness, but compiled code must have predictable performance.
257+
254258
The compiler stores an intermediate representation in an environment extension.
255259

256260
For straightforwardly structurally recursive functions, the translation will use the type's recursor.
257261
These functions tend to be relatively efficient when run in the kernel, their defining equations hold definitionally, and they are easy to understand.
258-
Functions that use other patterns of recursion that cannot be captured by the type's recursor are translated using {deftech}[well-founded recursion], which is structural recursion on a proof that some measure decreases at each recursive call.
262+
Functions that use other patterns of recursion that cannot be captured by the type's recursor are translated using {deftech}[well-founded recursion], which is structural recursion on a proof that some {deftech}_measure_ decreases at each recursive call.
259263
Lean can automatically derive many of these cases, but some require manual proofs.
260264
Well-founded recursion is more flexible, but the resulting functions are often slower to execute in the kernel due to the proof terms that show that a measure decreases, and their defining equations may hold only propositionally.
261265
To provide a uniform interface to functions defined via structural and well-founded recursion and to check its own correctness, the elaborator proves equational lemmas that relate the function to its original definition.
@@ -377,6 +381,26 @@ A C file is produced for each Lean module; these are then compiled to native cod
377381
If the `precompileModules` option is set in the build configuration, then this native code can be dynamically loaded and invoked by Lean; otherwise, an interpreter is used.
378382
For most workloads, the overhead of compilation is larger than the time saved by avoiding the interpreter, but some workloads can be sped up dramatically by pre-compiling tactics, language extensions, or other extensions to Lean.
379383

384+
## Memory Allocation and Reference Counting
385+
386+
:::planned 208
387+
388+
The most important topics related to Lean's reference-counting-based allocator:
389+
390+
* Overview of {deftech key:="reference count"}_reference counting_
391+
392+
* Compact regions
393+
394+
* When are counts incremented and decremented?
395+
396+
* Tools for debugging uniqueness issues
397+
398+
* When should C code increment or decrement reference counts?
399+
400+
* What is the meaning of the borrow annotation (`@&`)?
401+
402+
:::
403+
380404

381405
# Initialization
382406

Manual/Language.lean

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@ import Manual.Meta
1010
import Manual.Language.Classes
1111
import Manual.Language.Files
1212
import Manual.Language.Types
13+
import Manual.Language.RecursiveDefs
1314

1415
import Lean.Parser.Command
1516

16-
open Manual hiding «example»
17+
open Manual
1718
open Verso.Genre
1819
open Verso.Genre.Manual
19-
open Lean.Parser.Command (declModifiers «example»)
20+
21+
22+
open Lean.Elab.Tactic.GuardMsgs.WhitespaceMode
2023

2124
set_option pp.rawOnError true
25+
set_option maxRecDepth 3000
2226

2327
set_option linter.unusedVariables false
2428

@@ -178,6 +182,7 @@ $_
178182

179183
Describe signatures, including the following topics:
180184
* Explicit, implicit, instance-implicit, and strict implicit parameter binders
185+
* {deftech key := "optional parameter"}[Optional] and {deftech}[automatic parameters]
181186
* {deftech}_Automatic implicit_ parameters
182187
* Argument names and by-name syntax
183188
* Which parts can be omitted where? Why?
@@ -193,6 +198,18 @@ The {deftech}[_header_] of a definition or declaration specifies the signature o
193198
* Mention interaction with autoimplicits
194199
:::
195200

201+
## Namespaces
202+
%%%
203+
tag := "namespaces"
204+
%%%
205+
206+
:::planned 210
207+
208+
Describe {deftech}[namespaces], aliases, and the semantics of `export` and `open`.
209+
Which language features are controlled by the currently open namespaces?
210+
211+
:::
212+
196213
## Section Scopes
197214
%%%
198215
tag := "scopes"
@@ -207,7 +224,7 @@ Section scopes track the following:
207224
* The {deftech}_current namespace_
208225
* The {deftech key:="open namespace"}_open namespaces_
209226
* The values of all {deftech}_options_
210-
* Variable and universe declarations
227+
* {deftech}[Section variable] and universe declarations
211228

212229
This section will describe this mechanism.
213230

@@ -236,48 +253,11 @@ scoped
236253
Describe {deftech}_axioms_ in detail
237254
:::
238255

239-
# Recursive Definitions
240-
241-
## Structural Recursion
242-
::: planned 55
243-
This section will describe the specification of the translation to recursors.
244-
:::
245-
246-
### Mutual Structural Recursion
247-
248-
::: planned 56
249-
This section will describe the specification of the translation to recursors.
250-
:::
251-
252-
## Well-Founded Recursion
253-
%%%
254-
tag := "well-founded-recursion"
255-
%%%
256+
{include 0 Manual.Language.RecursiveDefs}
256257

257-
::: planned 57
258-
This section will describe the translation of {deftech}[well-founded recursion].
259-
:::
260258

261-
## Controlling Reduction
262259

263-
:::planned 58
264-
This section will describe {deftech}_reducibility_: {deftech}[reducible], {deftech}[semi-reducible], and {deftech}[irreducible] definitions.
265-
:::
266260

267-
## Partial and Unsafe Recursive Definitions
268-
%%%
269-
tag := "partial-unsafe"
270-
%%%
271-
272-
:::planned 59
273-
This section will describe `partial` and `unsafe` definitions:
274-
275-
276-
* Interaction with the kernel and elaborator
277-
* What guarantees are there, and what aren't there?
278-
* How to bridge from unsafe to safe code?
279-
280-
:::
281261

282262
# Attributes
283263
%%%

Manual/Language/Classes/BasicClasses.lean

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ Many Lean type classes exist in order to allow built-in notations such as additi
3737
{docstring LE}
3838

3939
# Decidability
40+
%%%
41+
tag := "decidable-propositions"
42+
%%%
4043

4144
A proposition is {deftech}_decidable_ if it can be checked algorithmically.{index}[decidable]{index subterm:="decidable"}[proposition]
4245
The Law of the Excluded Middle means that every proposition is true or false, but it provides no way to check which of the two cases holds, which can often be useful.

0 commit comments

Comments
 (0)