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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
open Base
type atomic_proposition = Signal.t [@@deriving sexp_of]
let name s =
"bool("
^ (match List.hd (Signal.names s) with
| Some s -> s
| None -> "_" ^ Signal.Uid.to_string (Signal.uid s))
^ ")"
;;
let atomic_proposition_must_be_1_bit ap =
if Signal.width ap <> 1
then raise_s [%message "atomic propositions must be 1 bit" (ap : Signal.t)]
;;
module CTL = struct
type state =
| True
| P of atomic_proposition
| And of state * state
| Not of state
| E of path
| A of path
and path =
| X of state
| U of state * state
| F of state
| G of state
[@@deriving sexp_of]
let t = True
let p ap =
atomic_proposition_must_be_1_bit ap;
P ap
;;
let ( &: ) a b = And (a, b)
let ( ~: ) a = Not a
let e p = E p
let a p = A p
let x p = X p
let rec ax ?(n = 1) s = if n = 0 then s else a (x (ax ~n:(n - 1) s))
let rec ex ?(n = 1) s = if n = 0 then s else e (x (ex ~n:(n - 1) s))
let u a b = U (a, b)
let au x y = a @@ u x y
let eu x y = e @@ u x y
let f s = F s
let af s = a @@ f s
let ef s = e @@ f s
let g s = G s
let ag s = a @@ g s
let eg s = e @@ g s
let rec to_string ?(name = name) p =
let to_string = to_string ~name in
match p with
| True -> "TRUE"
| P ap -> name ap
| And (a, b) -> "(" ^ to_string a ^ " & " ^ to_string b ^ ")"
| Not s -> "(!" ^ to_string s ^ ")"
| E (G p) -> "(EG " ^ to_string p ^ ")"
| E (F p) -> "(EF " ^ to_string p ^ ")"
| E (X p) -> "(EX " ^ to_string p ^ ")"
| E (U (a, b)) -> "(E [" ^ to_string a ^ " U " ^ to_string b ^ "])"
| A (G p) -> "(AG " ^ to_string p ^ ")"
| A (F p) -> "(AF " ^ to_string p ^ ")"
| A (X p) -> "(AX " ^ to_string p ^ ")"
| A (U (a, b)) -> "(A [" ^ to_string a ^ " U " ^ to_string b ^ "])"
;;
let rec atomic_propositions = function
| True -> []
| P ap -> [ ap ]
| And (a, b) -> atomic_propositions a @ atomic_propositions b
| Not s -> atomic_propositions s
| E (G p) -> atomic_propositions p
| E (F p) -> atomic_propositions p
| E (X p) -> atomic_propositions p
| E (U (a, b)) -> atomic_propositions a @ atomic_propositions b
| A (G p) -> atomic_propositions p
| A (F p) -> atomic_propositions p
| A (X p) -> atomic_propositions p
| A (U (a, b)) -> atomic_propositions a @ atomic_propositions b
;;
let map_atomic_propositions p ~f =
let rec g = function
| True -> True
| P ap -> P (f ap)
| And (a, b) -> And (g a, g b)
| Not s -> Not (g s)
| E (G p) -> E (G (g p))
| E (F p) -> E (F (g p))
| E (X p) -> E (X (g p))
| E (U (a, b)) -> E (U (g a, g b))
| A (G p) -> A (G (g p))
| A (F p) -> A (F (g p))
| A (X p) -> A (X (g p))
| A (U (a, b)) -> A (U (g a, g b))
in
g p
;;
end
module LTL = struct
type path =
| True
| P of atomic_proposition
| Pn of atomic_proposition
| And of path * path
| Or of path * path
| Not of path
| X of path
| U of path * path
| R of path * path
| F of path
| G of path
[@@deriving sexp_of]
let vdd = True
let gnd = Not True
let p ap =
atomic_proposition_must_be_1_bit ap;
P ap
;;
let ( &: ) a b = And (a, b)
let ( |: ) a b = Or (a, b)
let ( ~: ) a = Not a
let ( ^: ) a b = a &: ~:b |: (~:a &: b)
let ( ==: ) a b = ~:(a ^: b)
let ( <>: ) a b = a ^: b
let ( ==>: ) a b = ~:a |: b
let rec x ?(n = 1) s = if n = 0 then s else X (x ~n:(n - 1) s)
let u a b = U (a, b)
let r a b = R (a, b)
let f p = F p
let g p = G p
let w p q = u p q |: g p
let rec to_string ?(name = name) p =
let to_string = to_string ~name in
match p with
| U (True, b) -> "(F " ^ to_string b ^ ")"
| Not (U (True, Not p)) -> "(G " ^ to_string p ^ ")"
| True -> "TRUE"
| P ap -> name ap
| Pn ap -> "(!" ^ to_string (P ap) ^ ")"
| And (a, b) -> "(" ^ to_string a ^ " & " ^ to_string b ^ ")"
| Or (a, b) -> "(" ^ to_string a ^ " | " ^ to_string b ^ ")"
| Not a -> "(!" ^ to_string a ^ ")"
| X p -> "(X " ^ to_string p ^ ")"
| U (a, b) -> "(" ^ to_string a ^ " U " ^ to_string b ^ ")"
| R (a, b) -> "(" ^ to_string a ^ " V " ^ to_string b ^ ")"
| F p -> "(F " ^ to_string p ^ ")"
| G p -> "(G " ^ to_string p ^ ")"
;;
let rec atomic_propositions = function
| True -> []
| P ap -> [ ap ]
| Pn ap -> [ ap ]
| And (a, b) -> atomic_propositions a @ atomic_propositions b
| Or (a, b) -> atomic_propositions a @ atomic_propositions b
| Not a -> atomic_propositions a
| X p -> atomic_propositions p
| U (a, b) -> atomic_propositions a @ atomic_propositions b
| R (a, b) -> atomic_propositions a @ atomic_propositions b
| F p -> atomic_propositions p
| G p -> atomic_propositions p
;;
let map_atomic_propositions p ~f =
let rec g = function
| True -> True
| P ap -> P (f ap)
| Pn ap -> Pn (f ap)
| And (a, b) -> And (g a, g b)
| Or (a, b) -> Or (g a, g b)
| Not a -> Not (g a)
| X p -> X (g p)
| U (a, b) -> U (g a, g b)
| R (a, b) -> R (g a, g b)
| F p -> F (g p)
| G p -> G (g p)
in
g p
;;
let rec depth = function
| True -> 0
| P _ -> 0
| Pn _ -> 0
| And (a, b) -> max (depth a) (depth b)
| Or (a, b) -> max (depth a) (depth b)
| Not a -> depth a
| X p -> 1 + depth p
| U (a, b) -> max (depth a) (depth b)
| R (a, b) -> max (depth a) (depth b)
| F p -> depth p
| G p -> depth p
;;
let rec nnf x =
match x with
| True | P _ -> x
| Pn _ -> x
| And (a, b) -> And (nnf a, nnf b)
| Or (a, b) -> Or (nnf a, nnf b)
| X a -> X (nnf a)
| U (a, b) -> U (nnf a, nnf b)
| R (a, b) -> R (nnf a, nnf b)
| F a -> F (nnf a)
| G a -> G (nnf a)
| Not True -> x
| Not (P x) -> Pn x
| Not (Pn x) -> P x
| Not (And (a, b)) -> nnf ~:a |: nnf ~:b
| Not (Or (a, b)) -> nnf ~:a &: nnf ~:b
| Not (Not a) -> nnf a
| Not (X p) -> X (nnf (Not p))
| Not (U (a, b)) -> R (nnf ~:a, nnf ~:b)
| Not (R (a, b)) -> U (nnf ~:a, nnf ~:b)
| Not (F a) -> G (nnf ~:a)
| Not (G a) -> F (nnf ~:a)
;;
let limit_depth k x =
let rec f i x =
match x with
| X p -> if i < k then f (i + 1) p else Not True
| True | P _ | Pn _ -> x
| And (a, b) -> And (f i a, f i b)
| Or (a, b) -> Or (f i a, f i b)
| U (a, b) -> U (f i a, f i b)
| R (a, b) -> R (f i a, f i b)
| F a -> F (f i a)
| G a -> G (f i a)
| Not a -> Not (f i a)
in
f 0 x
;;
end