Types and their string representation
Lax709149.Types · concepts/Lax709149/Types.lean · lax-709149
No public endorsements yet.
Loading review…
Sign in with ORCIDNatural Language Statement
Definition
The types of Section C.5 of Transducers (Definition C.5.1) are the expressions built from the unit type , which has the unique element , by the product , the co-product and the list type . Every element of a type has a string representation over a fixed alphabet with eight letters
defined by induction on the type: the unique element of is , an element of is or , a pair is , and a list is . Through this representation, type-to-type functions can be computed by string-to-string transducers (Definition C.5.2).
Concept map
In the paper
- page 138 of the paper of lax-157538, Transducers
Lean source view on GitHub
| 1 | import Mathlib.Data.Fintype.Basic |
| 2 | import Mathlib.Data.Fintype.Sum |
| 3 | import Mathlib.Tactic.DeriveFintype |
| 4 | |
| 5 | /-! |
| 6 | --- |
| 7 | title: Types and their string representation |
| 8 | type: definition |
| 9 | --- |
| 10 | The *types* of Section C.5 of *Transducers* (Definition C.5.1) are the |
| 11 | expressions built from the unit type , which has the unique element |
| 12 | , by the product , the co-product and the list type |
| 13 | . Every element of a type has a *string representation* over a fixed |
| 14 | alphabet with eight letters |
| 15 | |
| 16 | defined by induction on the type: the unique element of is , an |
| 17 | element of is or , a pair is , and a list is |
| 18 | . Through this representation, type-to-type functions can |
| 19 | be computed by string-to-string transducers (Definition C.5.2). |
| 20 | |
| 21 | # Formalization notes |
| 22 | |
| 23 | `Ty.Elt` interprets a type as a Lean type — `Unit`, products, sums and lists — |
| 24 | and `Ty.repr` is the representation, a string over the eight-letter alphabet |
| 25 | `Sym8`, the same alphabet for all types. Lists are written with the separating |
| 26 | commas of the book and no trailing separator. |
| 27 | -/ |
| 28 | |
| 29 | namespace Lax709149.Types |
| 30 | |
| 31 | /-- A type: built from the unit type by product, co-product and list. -/ |
| 32 | inductive Ty : Type |
| 33 | | one : Ty |
| 34 | | prod : Ty → Ty → Ty |
| 35 | | sum : Ty → Ty → Ty |
| 36 | | list : Ty → Ty |
| 37 | deriving DecidableEq |
| 38 | |
| 39 | /-- The set of elements of a type. -/ |
| 40 | def Ty.Elt : Ty → Type |
| 41 | | .one => Unit |
| 42 | | .prod A B => A.Elt × B.Elt |
| 43 | | .sum A B => A.Elt ⊕ B.Elt |
| 44 | | .list A => List A.Elt |
| 45 | |
| 46 | /-- The alphabet with eight letters `(`, `)`, `[`, `]`, `,`, `1`, `L`, `R`. -/ |
| 47 | inductive Sym8 : Type |
| 48 | | lpar | rpar | lbrack | rbrack | comma | one | left | right |
| 49 | deriving DecidableEq |
| 50 | |
| 51 | instance : Fintype Sym8 := derive_fintype% _ |
| 52 | |
| 53 | /-- The entries of a list representation, separated by commas. -/ |
| 54 | def joinSep : List (List Sym8) → List Sym8 |
| 55 | | [] => [] |
| 56 | | [x] => x |
| 57 | | x :: xs => x ++ Sym8.comma :: joinSep xs |
| 58 | |
| 59 | /-- The string representation of an element: `1` for the unit, `L a` and `R b` for |
| 60 | the co-product, `(a,b)` for a pair, `[a₁,…,aₙ]` for a list. -/ |
| 61 | def Ty.repr : (t : Ty) → t.Elt → List Sym8 |
| 62 | | .one, _ => [Sym8.one] |
| 63 | | .prod A B, x => Sym8.lpar :: (A.repr x.1 ++ Sym8.comma :: (B.repr x.2 ++ [Sym8.rpar])) |
| 64 | | .sum A B, x => Sum.elim (fun a => Sym8.left :: A.repr a) (fun b => Sym8.right :: B.repr b) x |
| 65 | | .list A, l => Sym8.lbrack :: (joinSep (l.map A.repr) ++ [Sym8.rbrack]) |
| 66 | |
| 67 | end Lax709149.Types |
| 68 |
Formalization notes
interprets a type as a Lean type — , products, sums and lists — and is the representation, a string over the eight-letter alphabet , the same alphabet for all types. Lists are written with the separating commas of the book and no trailing separator.
Builds on
none
Discussion
Ask a question or add context. Endorsements and structured flags are kept in the review panel above.
0 comments