forked from leanprover-community/mathlib4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoding.lean
More file actions
244 lines (193 loc) · 8.14 KB
/
Encoding.lean
File metadata and controls
244 lines (193 loc) · 8.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/-
Copyright (c) 2020 Pim Spelier, Daan van Gent. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Pim Spelier, Daan van Gent
-/
module
public import Mathlib.Data.Fintype.Basic
public import Mathlib.Data.Num.Lemmas
public import Mathlib.Data.Option.Basic
public import Mathlib.SetTheory.Cardinal.Basic
public import Mathlib.Tactic.DeriveFintype
/-!
# Encodings
This file contains the definition of a (finite) encoding, a map from a type to
strings in an alphabet, used in defining computability by Turing machines.
It also contains several examples:
## Examples
- `finEncodingNatBool` : a binary encoding of `ℕ` in a simple alphabet.
- `finEncodingNatΓ'` : a binary encoding of `ℕ` in the alphabet used for TM's.
- `unaryFinEncodingNat` : a unary encoding of `ℕ`
- `finEncodingBoolBool` : an encoding of `Bool`.
- `finEncodingList` : an encoding of `List α` in the alphabet `α`.
- `finEncodingPair` : an encoding of `α × β` from encodings of `α` and `β`.
-/
@[expose] public section
universe u v
open Cardinal
namespace Computability
/-- An encoding of a type in a certain alphabet, together with a decoding. -/
structure Encoding (α : Type u) where
/-- The alphabet of the encoding -/
Γ : Type v
/-- The encoding function -/
encode : α → List Γ
/-- The decoding function -/
decode : List Γ → Option α
/-- Decoding and encoding are inverses of each other. -/
decode_encode : ∀ x, decode (encode x) = some x
attribute [simp] Encoding.decode_encode
theorem Encoding.encode_injective {α : Type u} (e : Encoding α) : Function.Injective e.encode := by
refine fun _ _ h => Option.some_injective _ ?_
rw [← e.decode_encode, ← e.decode_encode, h]
/-- An `Encoding` plus a guarantee of finiteness of the alphabet. -/
structure FinEncoding (α : Type u) extends Encoding.{u, 0} α where
/-- The alphabet of the encoding is finite -/
ΓFin : Fintype Γ
instance Γ.fintype {α : Type u} (e : FinEncoding α) : Fintype e.toEncoding.Γ :=
e.ΓFin
/-- A standard Turing machine alphabet, consisting of blank,bit0,bit1,bra,ket,comma. -/
inductive Γ'
| blank
| bit (b : Bool)
| bra
| ket
| comma
deriving DecidableEq, Fintype
instance inhabitedΓ' : Inhabited Γ' :=
⟨Γ'.blank⟩
/-- The natural inclusion of `Bool` in `Γ'`. -/
def inclusionBoolΓ' : Bool → Γ' :=
Γ'.bit
/-- An arbitrary section of the natural inclusion of `Bool` in `Γ'`. -/
def sectionΓ'Bool : Γ' → Bool
| Γ'.bit b => b
| _ => Inhabited.default
@[simp]
theorem sectionΓ'Bool_inclusionBoolΓ' {b} : sectionΓ'Bool (inclusionBoolΓ' b) = b := by
cases b <;> rfl
theorem inclusionBoolΓ'_injective : Function.Injective inclusionBoolΓ' :=
Function.HasLeftInverse.injective ⟨_, (fun _ => sectionΓ'Bool_inclusionBoolΓ')⟩
/-- An encoding function of the positive binary numbers in `Bool`. -/
def encodePosNum : PosNum → List Bool
| PosNum.one => [true]
| PosNum.bit0 n => false :: encodePosNum n
| PosNum.bit1 n => true :: encodePosNum n
/-- An encoding function of the binary numbers in `Bool`. -/
def encodeNum : Num → List Bool
| Num.zero => []
| Num.pos n => encodePosNum n
/-- An encoding function of `ℕ` in `Bool`. -/
def encodeNat (n : ℕ) : List Bool :=
encodeNum n
/-- A decoding function from `List Bool` to the positive binary numbers. -/
def decodePosNum : List Bool → PosNum
| false :: l => PosNum.bit0 (decodePosNum l)
| true :: l => ite (l = []) PosNum.one (PosNum.bit1 (decodePosNum l))
| _ => PosNum.one
/-- A decoding function from `List Bool` to the binary numbers. -/
def decodeNum : List Bool → Num := fun l => ite (l = []) Num.zero <| decodePosNum l
/-- A decoding function from `List Bool` to `ℕ`. -/
def decodeNat : List Bool → Nat := fun l => decodeNum l
theorem encodePosNum_nonempty (n : PosNum) : encodePosNum n ≠ [] :=
PosNum.casesOn n (List.cons_ne_nil _ _) (fun _m => List.cons_ne_nil _ _) fun _m =>
List.cons_ne_nil _ _
@[simp] theorem decode_encodePosNum (n) : decodePosNum (encodePosNum n) = n := by
induction n with unfold encodePosNum decodePosNum
| one => rfl
| bit1 m hm =>
rw [hm]
exact if_neg (encodePosNum_nonempty m)
| bit0 m hm => exact congr_arg PosNum.bit0 hm
@[simp] theorem decode_encodeNum (n) : decodeNum (encodeNum n) = n := by
obtain - | n := n <;> unfold encodeNum decodeNum
· rfl
rw [decode_encodePosNum n]
rw [PosNum.cast_to_num]
exact if_neg (encodePosNum_nonempty n)
@[simp] theorem decode_encodeNat (n) : decodeNat (encodeNat n) = n := by
conv_rhs => rw [← Num.to_of_nat n]
exact congr_arg ((↑) : Num → ℕ) (decode_encodeNum n)
/-- A binary `Encoding` of `ℕ` in `Bool`. -/
def encodingNatBool : Encoding ℕ where
Γ := Bool
encode := encodeNat
decode n := some (decodeNat n)
decode_encode n := congr_arg _ (decode_encodeNat n)
/-- A binary encoding of `ℕ` in `Bool`, as a `FinEncoding`. -/
def finEncodingNatBool : FinEncoding ℕ :=
⟨encodingNatBool, Bool.fintype⟩
/-- A binary `Encoding` of `ℕ` in `Γ'`. -/
def encodingNatΓ' : Encoding ℕ where
Γ := Γ'
encode x := List.map inclusionBoolΓ' (encodeNat x)
decode x := some (decodeNat (List.map sectionΓ'Bool x))
decode_encode x := congr_arg _ <| by simp [Function.comp_def]
/-- A binary `FinEncoding` of `ℕ` in `Γ'`. -/
def finEncodingNatΓ' : FinEncoding ℕ :=
⟨encodingNatΓ', inferInstanceAs (Fintype Γ')⟩
/-- A unary encoding function of `ℕ` in `Bool`. -/
def unaryEncodeNat : Nat → List Bool
| 0 => []
| n + 1 => true :: unaryEncodeNat n
/-- A unary decoding function from `List Bool` to `ℕ`. -/
def unaryDecodeNat : List Bool → Nat :=
List.length
@[simp] theorem unary_decode_encode_nat : ∀ n, unaryDecodeNat (unaryEncodeNat n) = n := fun n =>
Nat.rec rfl (fun (_m : ℕ) hm => (congr_arg Nat.succ hm.symm).symm) n
/-- A unary `FinEncoding` of `ℕ` in `Bool`. -/
def unaryFinEncodingNat : FinEncoding ℕ where
Γ := Bool
encode := unaryEncodeNat
decode n := some (unaryDecodeNat n)
decode_encode n := congr_arg _ (unary_decode_encode_nat n)
ΓFin := Bool.fintype
/-- An encoding function of `Bool` in `Bool`. -/
def encodeBool : Bool → List Bool := pure
/-- A decoding function from `List Bool` to `Bool`. -/
def decodeBool : List Bool → Bool
| b :: _ => b
| _ => Inhabited.default
@[simp] theorem decode_encodeBool (b : Bool) : decodeBool (encodeBool b) = b := rfl
/-- A `FinEncoding` of `Bool` in `Bool`. -/
def finEncodingBoolBool : FinEncoding Bool where
Γ := Bool
encode := encodeBool
decode x := some (decodeBool x)
decode_encode x := congr_arg _ (decode_encodeBool x)
ΓFin := Bool.fintype
instance inhabitedFinEncoding : Inhabited (FinEncoding Bool) :=
⟨finEncodingBoolBool⟩
instance inhabitedEncoding : Inhabited (Encoding Bool) :=
⟨finEncodingBoolBool.toEncoding⟩
theorem Encoding.card_le_card_list {α : Type u} (e : Encoding.{u, v} α) :
Cardinal.lift.{v} #α ≤ Cardinal.lift.{u} #(List e.Γ) :=
Cardinal.lift_mk_le'.2 ⟨⟨e.encode, e.encode_injective⟩⟩
theorem Encoding.card_le_aleph0 {α : Type u} (e : Encoding.{u, v} α) [Countable e.Γ] :
#α ≤ ℵ₀ :=
haveI : Countable α := e.encode_injective.countable
Cardinal.mk_le_aleph0
theorem FinEncoding.card_le_aleph0 {α : Type u} (e : FinEncoding α) : #α ≤ ℵ₀ :=
e.toEncoding.card_le_aleph0
/-- A `FinEncoding` of a `List α` in (finite) alphabet `α`, encoded directly. -/
def finEncodingList (α : Type) [Fintype α] : FinEncoding (List α) where
Γ := α
encode := id
decode := Option.some
decode_encode _ := rfl
ΓFin := inferInstance
/--
Given `FinEncoding` of `α` and `β`,
constructs a `FinEncoding` of `α × β` by concatenating the encodings,
mapping the symbols from the first encoding with `Sum.inl`
and those from the second with `Sum.inr`.
-/
def finEncodingPair {α β : Type*} (ea : FinEncoding α) (eb : FinEncoding β) :
FinEncoding (α × β) where
Γ := ea.Γ ⊕ eb.Γ
encode x := (ea.encode x.1).map .inl ++ (eb.encode x.2).map .inr
decode x := Option.map₂ Prod.mk (ea.decode (x.filterMap Sum.getLeft?))
(eb.decode (x.filterMap Sum.getRight?))
decode_encode x := by simp
ΓFin := inferInstance
end Computability