-
Notifications
You must be signed in to change notification settings - Fork 9
/
topsort.v
230 lines (201 loc) · 6.37 KB
/
topsort.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
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
(******************************************************************************)
(* PipeCheck: Specifying and Verifying Microarchitectural *)
(* Enforcement of Memory Consistency Models *)
(* *)
(* Copyright (c) 2014 Daniel Lustig, Princeton University *)
(* All rights reserved. *)
(* *)
(* This library is free software; you can redistribute it and/or *)
(* modify it under the terms of the GNU Lesser General Public *)
(* License as published by the Free Software Foundation; either *)
(* version 2.1 of the License, or (at your option) any later version. *)
(* *)
(* This library is distributed in the hope that it will be useful, *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *)
(* Lesser General Public License for more details. *)
(* *)
(* You should have received a copy of the GNU Lesser General Public *)
(* License along with this library; if not, write to the Free Software *)
(* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 *)
(* USA *)
(******************************************************************************)
Require Import List.
Import ListNotations.
Require Import util2.
Require Import adjacency.
Require Import Ascii.
Require Import String.
Inductive TopsortResult : Type :=
| TotalOrdering : list nat -> TopsortResult
| CycleFound : list nat -> TopsortResult.
(** * [EdgesWithConnections]: Given an adjacency list, return a list of bools
such that the nth element is true if there is at least one edge connected
to node n. *)
(** The destination of each edge is connected *)
Fixpoint EdgesWithConnections''
(l : list nat)
(b : list bool)
: list bool :=
match l with
| h::t =>
replaceNth (EdgesWithConnections'' t b) h true false
| [] => b
end.
(** Include the source and dest of each edge as connected *)
Fixpoint EdgesWithConnections'
(g : list (list nat))
(b : list bool)
(n : nat)
: list bool :=
match g with
| h::t =>
let b' :=
match h with
| _::_ =>
(* outgoing edge from this node - edge is connected *)
replaceNth b n true false
| [] =>
(* no outgoing edge from this node *)
b
end in
EdgesWithConnections' t (EdgesWithConnections'' h b') (S n)
| [] => b
end.
Fixpoint EdgesWithConnections
(g : list (list nat))
: list bool :=
EdgesWithConnections' g [] 0.
(** * Topological Sort *)
Inductive Marking : Type :=
Unmarked | MarkedTemp | Marked.
(** ** Debugging code *)
Open Scope string_scope.
Fixpoint natListToString
(l : list nat)
: string :=
match l with
| h::t =>
append (append (stringOfNat h) " ") (natListToString t)
| [] => ""
end.
Fixpoint markingListToString
(l : list Marking)
: string :=
match l with
| Unmarked::t => append ("U") (markingListToString t)
| MarkedTemp::t => append ("T") (markingListToString t)
| Marked::t => append ("M") (markingListToString t)
| [] => ""
end.
Fixpoint topsortStateToString
(omr : (list Marking * TopsortResult))
: string :=
let (lm, r) := omr in
append
(match r with
| TotalOrdering r =>
append (append "TotalOrdering " (natListToString r)) ":"
| CycleFound r =>
append (append "CycleFound " (natListToString r)) ":"
end)
(append (markingListToString lm) newline).
Fixpoint AdjacencyListToString
(l : list (list nat))
(n : nat)
: string :=
match l with
| h::t =>
fold_left append [
stringOfNat n; ": ("; natListToString h; ")"; newline;
AdjacencyListToString t (S n)
] ""
| [] => ""
end.
(** ** Topological sort *)
Fixpoint topsortVisit
(unroll : nat)
(g : list (list nat))
(omr : (list Marking * TopsortResult))
(n : nat)
: list Marking * TopsortResult :=
match omr with
| (m, TotalOrdering r) =>
match (unroll, nth n m Unmarked) with
| (S unroll', Unmarked) =>
let m' := replaceNth m n MarkedTemp Unmarked in
let adj_of_n := nth n g [] in
match fold_left (topsortVisit unroll' g) adj_of_n
(m', TotalOrdering r) with
| (m'', TotalOrdering r'') =>
(replaceNth m'' n Marked Unmarked, TotalOrdering ([n] ++ r''))
| x => x
end
| (S unroll', Marked) => omr
| (0, _) => (m, CycleFound [1])
| _ =>
(m, CycleFound (FindPathAdj g n n))
end
| x => x
end.
Fixpoint range (j : nat) : list nat :=
match j with
| 0 => []
| S j' => j' :: range j'
end.
Close Scope string_scope.
Definition topsortAdj'
(g : list (list nat))
: TopsortResult :=
(* let g := PrintfHook g
(append (append "Graph: " newline) (AdjacencyListToString g 0)) in *)
let l := fold_left plus (map (List.length (A:=_)) g) (List.length g) in
match fold_left (topsortVisit l g) (range l) (([], TotalOrdering [])) with
| (_, r) => r
end.
Fixpoint KeepIfIn
(l : list nat)
(b : list bool)
: list nat :=
match l with
| h::t =>
if nth h b false
then h::KeepIfIn t b
else KeepIfIn t b
| [] => []
end.
Definition topsortAdj
(g : list (list nat))
: TopsortResult :=
let c := EdgesWithConnections g in
match topsortAdj' g with
| TotalOrdering o => TotalOrdering (KeepIfIn o c)
| x => x
end.
Definition topsort
(g : list (nat * nat))
: TopsortResult :=
topsortAdj (AdjacencyListFromEdges g).
Module TopsortExample.
Example e1:
EdgesWithConnections [[12; 8; 4]; []; []; []; [12; 8]; []; []; []; [12]] =
[true; false; false; false; true; false; false; false; true; false;
false; false; true].
Proof.
auto.
Qed.
Example e2: topsortAdj [[3]; []; [0; 3]; [1]] = TotalOrdering [2; 0; 3; 1].
Proof.
auto.
Qed.
Example e3:
topsort [(0, 3); (2, 0); (2, 3); (3, 1)] = TotalOrdering [2; 0; 3; 1].
Proof.
auto.
Qed.
Example e4:
topsort [(0, 1); (1, 0)] = CycleFound [1; 0; 1].
Proof.
cbv. auto.
Qed.
End TopsortExample.