-
Notifications
You must be signed in to change notification settings - Fork 9
/
interleavings.v
96 lines (82 loc) · 3.67 KB
/
interleavings.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
(******************************************************************************)
(* 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.
(** * Interleavings
Return a list of all possible sequential interleavings of the input lists *)
Fixpoint Interleavings''' {A : Type}
(l' l : list (list A))
: list (A * list (list A)) :=
match l with
| h::t =>
match h with
| hh::ht =>
(hh, l' ++ ht :: t) :: Interleavings''' (l' ++ [h]) t
| [] => Interleavings''' l' t
end
| [] => []
end.
Definition Interleavings'' {A : Type}
(a : A)
(l : list (list A))
: list (list A) :=
match l with
| [] => [[a]]
| _ => map (fun x => a::x) l
end.
Fixpoint Interleavings' {A : Type}
(l : list (list A))
(n : nat)
: list (list A) :=
match n with
| S n' =>
let f p := Interleavings'' (fst p) (Interleavings' (snd p) n') in
fold_left (app (A:=_)) (map f (Interleavings''' [] l)) []
| 0 => []
end.
Definition Interleavings {A : Type}
(l : list (list A))
: list (list A) :=
let n := fold_left plus (map (length (A:=_)) l) 0 in
Interleavings' l n.
Module InterleavingsExample.
Example e1: Interleavings''' [] [[1; 2]; [3; 4]]
= [(1, [[2]; [3; 4]]); (3, [[1; 2]; [4]])].
Proof. auto. Qed.
Example e2: Interleavings''' [] [[1; 2]; [3; 4]; []]
= [(1, [[2]; [3; 4]; []]); (3, [[1; 2]; [4]; []])].
Proof. auto. Qed.
Example e3: Interleavings [[1; 2]; [3; 4]]
= [[1; 2; 3; 4]; [1; 3; 2; 4]; [1; 3; 4; 2]; [3; 1; 2; 4]; [3; 1; 4; 2];
[3; 4; 1; 2]].
Proof. auto. Qed.
Example e4: Interleavings [[0; 1; 2]; [3; 4; 5]]
= [[0; 1; 2; 3; 4; 5]; [0; 1; 3; 2; 4; 5]; [0; 1; 3; 4; 2; 5];
[0; 1; 3; 4; 5; 2]; [0; 3; 1; 2; 4; 5]; [0; 3; 1; 4; 2; 5];
[0; 3; 1; 4; 5; 2]; [0; 3; 4; 1; 2; 5]; [0; 3; 4; 1; 5; 2];
[0; 3; 4; 5; 1; 2]; [3; 0; 1; 2; 4; 5]; [3; 0; 1; 4; 2; 5];
[3; 0; 1; 4; 5; 2]; [3; 0; 4; 1; 2; 5]; [3; 0; 4; 1; 5; 2];
[3; 0; 4; 5; 1; 2]; [3; 4; 0; 1; 2; 5]; [3; 4; 0; 1; 5; 2];
[3; 4; 0; 5; 1; 2]; [3; 4; 5; 0; 1; 2]].
Proof. auto. Qed.
End InterleavingsExample.