-
Notifications
You must be signed in to change notification settings - Fork 0
/
turing.v
167 lines (137 loc) · 3.76 KB
/
turing.v
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
Module Turing.
Require Import List.
Require Import EqNat.
Inductive Direction :=
Left | Right.
(* A single step in each evaluation of a machine. *)
Inductive Step (State : Set) :=
| StepDo : nat -> State -> Direction -> Step State
| StepHalt : Step State.
(* a total mapping from State*Symbol to a Step. *)
Definition Stepper (State : Set) :=
State -> nat -> Step State.
(* Our infinitely long tape, and its read head.
There are symbols to the left and symbols to the right.
It also has a default symbol that the whole thing is initialised to. *)
Inductive Tape : Set :=
TTape : nat -> list nat -> list nat -> Tape.
(* Get the value at the tapehead *)
Definition get (t : Tape) :=
match t with
| TTape default nil _ => default
| TTape _ (x::_) _ => x
end.
(* Replace value at tapehead, producing new tape *)
Definition write (t : Tape) (s : nat) :=
match t with
| TTape d nil rs => TTape d (s::nil) rs
| TTape d (_::ls) rs => TTape d (s::ls) rs
end.
(* Move tapehead to left *)
Definition move_left (t : Tape) :=
match t with
| TTape default nil rs => TTape default nil (default :: rs)
| TTape default (l::ls) rs => TTape default ls (l::rs)
end.
Definition move_right (t : Tape) :=
match t with
| TTape default ls nil => TTape default (default :: ls) nil
| TTape default ls (r::rs) => TTape default (r::ls) rs
end.
Definition move (t : Tape) (d : Direction) :=
match d with
| Left => move_left t
| Right => move_right t
end.
Definition empty (default : nat) :=
TTape default nil nil.
(* What do we know about our infinite tape? *)
(* How do we say that two tapes are equivalent?
Chop any defaults off the ends and then compare? *)
Definition chopList (d : nat) (l : list nat) :=
let fix go l' :=
match l' with
| nil => nil
| x::xs =>
match beq_nat x d with
| true => go xs
| false => x::xs
end
end
in rev (go (rev l)).
Definition chop (t : Tape) :=
match t with
| TTape d ls rs => TTape d (chopList d ls) (chopList d rs)
end.
Definition equiv (l r : Tape) :=
chop l = chop r.
Notation "l ~= r" := (equiv l r) (at level 70).
Theorem chopList_one (d : nat) :
chopList d (d::nil) = nil.
Proof.
intros. unfold chopList. simpl.
rewrite <- beq_nat_refl.
reflexivity.
Qed.
(* left.right = id *)
Theorem tape_lr_id (t : Tape) :
move_left (move_right t) ~= t.
Proof.
intros. unfold equiv.
destruct t; destruct l0; simpl.
rewrite chopList_one. reflexivity.
reflexivity.
Qed.
Theorem tape_rl_id (t : Tape) :
move_right (move_left t) ~= t.
Proof.
intros. unfold equiv.
destruct t; destruct l; simpl.
rewrite chopList_one. reflexivity.
reflexivity.
Qed.
(* write.write = write *)
Theorem tape_ww (t : Tape) (s1 s2 : nat) :
write (write t s2) s1 ~= write t s1.
Proof.
intros. unfold equiv.
destruct t; destruct l; auto.
Qed.
(* The whole thing, with current state, tape and a stepper function *)
Inductive Machine (State : Set) : Set :=
MMachine : State
-> Tape
-> Stepper State
-> Machine State.
Definition step {State : Set}
(m : Machine State) :=
match m with
| MMachine state tape stepper =>
match stepper state (get tape) with
| StepDo sy st' dir =>
Some (MMachine _ st' (move (write tape sy) dir) stepper)
| StepHalt => None
end
end.
(* fill *)
Module Fill.
Inductive FState := FLeft | FRight.
Definition stepper : Stepper FState :=
fun st => fun sym =>
match st with
| FLeft =>
match sym with
| 0 => StepDo _ 0 FRight Right
| 1 => StepDo _ 1 FLeft Left
| _ => StepHalt _
end
| FRight =>
match sym with
| 0 => StepDo _ 1 FLeft Left
| 1 => StepDo _ 1 FRight Right
| _ => StepHalt _
end
end.
Definition machine := MMachine _ FRight (empty 0) stepper.
Eval compute in step machine.
End Fill.