-
Notifications
You must be signed in to change notification settings - Fork 1
/
fsm_spec.cljc
310 lines (260 loc) · 10.3 KB
/
fsm_spec.cljc
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
(ns com.yetanalytics.persephone.pattern.fsm-spec
(:require [clojure.spec.alpha :as s]
[clojure.spec.gen.alpha :as sgen]
[clojure.set :as cset]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; FSM Property Predicates
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn count-states [fsm-coll]
(reduce (fn [cnt {:keys [states]}] (+ cnt (count states)))
0
fsm-coll))
(defn has-accept-states?
"Does the FSM have at least one accept state?"
[{:keys [accepts] :as _fsm}]
(< 0 (count accepts)))
(defn valid-start-state?
"Is the start state in the state set?"
[{:keys [states start] :as _fsm}]
(contains? states start))
(defn valid-accept-states?
"Are all the accept states in the state set?"
[{:keys [states accepts] :as _fsm}]
(cset/superset? states accepts))
(defn valid-transition-src-states?
"Are all source states in the transition table in the state set?"
[{:keys [states transitions] :as _fsm}]
(let [trans-srcs (reduce-kv (fn [acc src _] (conj acc src))
#{}
transitions)]
(cset/subset? trans-srcs states)))
(defn- valid-transition-dest-states?
[collect-dest-fn {:keys [states transitions] :as _fsm}]
(letfn [(is-valid-trans-dests?
[trans]
(cset/superset?
states
(reduce-kv (fn [acc _ dests] (collect-dest-fn acc dests))
#{}
trans)))]
(nil? (some (complement is-valid-trans-dests?) (vals transitions)))))
(defn valid-transition-dest-states-nfa?
"Are all dest states in the transition table in the NFA's state set?"
[nfa]
(valid-transition-dest-states? (fn [acc dests] (cset/union acc dests)) nfa))
(defn valid-transition-dest-states-dfa?
"Are all dest states in the transition table in the DFA's state set?"
[dfa]
(valid-transition-dest-states? (fn [acc dest] (conj acc dest)) dfa))
(defn- valid-transition-symbols?
"Are all the symbols in the transition table in the alphabet?"
[conj-epsilon? {:keys [symbols transitions] :as _fsm}]
(let [symbols
(if conj-epsilon?
(conj symbols [:epsilon nil])
symbols)
trans-symbols
(reduce-kv (fn [acc _ trans]
(cset/union acc
(reduce-kv (fn [acc sym _] (conj acc sym))
#{}
trans)))
#{}
transitions)]
(cset/superset? symbols trans-symbols)))
(defn valid-transition-symbols-nfa?
[nfa]
(valid-transition-symbols? true nfa))
(defn valid-transition-symbols-dfa?
[dfa]
(valid-transition-symbols? false dfa))
;; For NFAs produced via Thompson's Construction
;; There are several constraints such NFAs have to follow (which are listed on
;; the Wikipedia page), but this is the main structural spec.
(defn one-accept-state?
[{:keys [accepts] :as _nfa}]
(= 1 (count accepts)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Common specs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Common specs and generators
#_{:clj-kondo/ignore [:unresolved-var]}
(defn- pred-gen []
(sgen/fmap (fn [x] (fn [y] (= x y)))
(sgen/one-of [(sgen/int) (sgen/char) (sgen/boolean)])))
;; TODO: Add back keyword symbols?
;; (Remember that :epsilon was a keyword)
(s/def ::symbol-id string?)
(s/def ::symbol-pred (s/with-gen fn? pred-gen))
(s/def ::symbols (s/every-kv ::symbol-id
::symbol-pred
:min-count 1))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Common generators
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- sample-one [coll]
(-> coll seq rand-nth))
(defn- sample-one-to-set [coll]
#{(sample-one coll)})
(defn- sample-to-set [prob coll]
(set (random-sample prob coll)))
(defn- fsm-overrider
[accept-fn trans-fn {:keys [symbols states] :as fsm}]
(-> fsm
(assoc :start
(sample-one states))
(assoc :accepts
(accept-fn states))
(assoc :transitions
(reduce
(fn [acc src]
(let [symbs (random-sample 0.33 (keys symbols))
trans (reduce
(fn [acc sym]
(assoc acc sym (trans-fn states)))
{}
symbs)]
(assoc acc src trans)))
{}
states))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; NFA Specs and Generators
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(s/def :nfa/type (s/with-gen #(= :nfa %) #(sgen/return :nfa)))
(s/def :nfa/state nat-int?)
(s/def :nfa/states (s/coll-of :nfa/state :kind set? :min-count 1 :gen-max 10))
(s/def :nfa/start :nfa/state)
(s/def :nfa/accepts (s/coll-of :nfa/state :kind set?))
(s/def :nfa/transitions (s/map-of :nfa/state
(s/every-kv
(s/with-gen
(s/or :symbol ::symbol-id
:epsilon #(= :epsilon %))
#(s/gen ::symbol-id))
:nfa/states)))
(s/def :nfa/nfa-basics (s/keys :req-un [::symbols
:nfa/type
:nfa/states]))
(s/def :nfa/nfa (s/keys :req-un [::symbols
:nfa/type
:nfa/states
:nfa/start
:nfa/accepts
:nfa/transitions]))
(def nfa-common-spec (s/and :nfa/nfa
valid-start-state?
valid-accept-states?
valid-transition-src-states?
valid-transition-dest-states-nfa?
valid-transition-symbols-nfa?))
(def nfa-spec
(s/with-gen
nfa-common-spec
(fn [] (sgen/fmap (partial
fsm-overrider
(partial sample-to-set 0.33)
(partial sample-to-set 0.25))
(s/gen :nfa/nfa-basics)))))
(def thompsons-nfa-spec
(s/with-gen
(s/and nfa-common-spec
one-accept-state?)
(fn [] (sgen/fmap (partial
fsm-overrider
(partial sample-one-to-set)
(partial sample-to-set 0.25))
(s/gen :nfa/nfa-basics)))))
;; Public NFA spec
(s/def ::nfa nfa-spec)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; DFA Specs and Generators
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(s/def :dfa/type (s/with-gen #(= :dfa %) #(sgen/return :dfa)))
(s/def :int-dfa/state nat-int?)
(s/def :int-dfa/states (s/coll-of :int-dfa/state
:kind set?
:min-count 1
:gen-max 10))
(s/def :int-dfa/start :int-dfa/state)
(s/def :int-dfa/accepts (s/coll-of :int-dfa/state :kind set?))
(s/def :int-dfa/transitions (s/map-of :int-dfa/state
(s/map-of ::symbol-id :int-dfa/state)))
(s/def :int-dfa/dfa-basics
(s/keys :req-un [::symbols
:dfa/type
:int-dfa/states]))
(s/def :int-dfa/dfa
(s/keys :req-un [::symbols
:dfa/type
:int-dfa/states
:int-dfa/start
:int-dfa/accepts
:int-dfa/transitions]))
(s/def :set-dfa/state (s/every nat-int?
:kind set?
:gen-max 5))
(s/def :set-dfa/states (s/coll-of :set-dfa/state
:kind set?
:min-count 1
:gen-max 10))
(s/def :set-dfa/start :set-dfa/state)
(s/def :set-dfa/accepts (s/coll-of :set-dfa/state :kind set?))
(s/def :set-dfa/transitions (s/map-of :set-dfa/state
(s/map-of ::symbol-id :set-dfa/state)))
(s/def :set-dfa/dfa-basics
(s/keys :req-un [::symbols
:dfa/type
:set-dfa/states]))
(s/def :set-dfa/dfa
(s/keys :req-un [::symbols
:dfa/type
:set-dfa/states
:set-dfa/start
:set-dfa/accepts
:set-dfa/transitions]))
(def dfa-common-spec (s/and valid-start-state?
valid-accept-states?
valid-transition-src-states?
valid-transition-dest-states-dfa?
valid-transition-symbols-dfa?))
(def dfa-gen-fmap
(partial fsm-overrider
(partial sample-to-set 0.33)
(partial sample-one)))
(def dfa-spec
(s/with-gen
(s/and :int-dfa/dfa
dfa-common-spec)
(fn [] (sgen/fmap
dfa-gen-fmap
(s/gen :int-dfa/dfa-basics)))))
(def set-dfa-spec
(s/with-gen
(s/and :set-dfa/dfa
dfa-common-spec)
(fn [] (sgen/fmap
dfa-gen-fmap
(s/gen :set-dfa/dfa-basics)))))
;; Public DFA spec
(s/def ::dfa dfa-spec)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; State Info Specs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(s/def ::accepted? boolean?)
(s/def ::visited (s/coll-of ::symbol-id))
(def state-info-spec
(s/every (s/keys :req-un [(or :nfa/state
:int-dfa/state)
::accepted?]
:opt-un [::visited])
:kind set?))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; FSM Metadata Specs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(s/def :meta/states
(s/every-kv (s/or :nfa :nfa/state :dfa :int-dfa/state) any?))
;; TODO: Remove in next break ver
(def ^:deprecated fsm-metadata-spec
(s/keys :opt-un [:meta/states]))
(s/def ::nfa-meta
(s/keys :opt-un [:meta/states]))