-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNSA.fs
190 lines (143 loc) · 6.21 KB
/
NSA.fs
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
(*
Copyright (C) 2022-2024 Raven Beutner
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*)
/// Module for nondetermintic safety automata, i.e., automata that accept all words, as long as there exists some run
module FsOmegaLib.NSA
open System
open System.IO
open SAT
open AutomatonSkeleton
open AbstractAutomaton
exception private NotWellFormedException of string
type NSA<'T, 'L when 'T : comparison and 'L : comparison> =
{
Skeleton : NondeterministicAutomatonSkeleton<'T, 'L>
InitialStates : Set<'T>
}
member this.States = this.Skeleton.States
member this.Edges = this.Skeleton.Edges
member this.APs = this.Skeleton.APs
interface AbstractAutomaton<'T, 'L> with
member this.Skeleton =
NondeterministicAutomatonSkeleton.toAlternatingAutomatonSkeleton this.Skeleton
member this.FindError() =
try
match NondeterministicAutomatonSkeleton.findError this.Skeleton with
| Some err -> raise <| NotWellFormedException err
| None -> ()
this.InitialStates
|> Seq.iter (fun x ->
if this.Skeleton.States.Contains x |> not then
raise
<| NotWellFormedException $"State $A{x} is initial but not contained in the set of states"
)
None
with NotWellFormedException msg ->
Some msg
member this.ToHoaString (stateStringer : 'T -> string) (alphStringer : 'L -> string) =
let s = new StringWriter()
s.WriteLine("HOA: v1")
s.WriteLine("States: " + string (this.States.Count))
for n in this.InitialStates do
s.WriteLine("Start: " + stateStringer (n))
s.WriteLine(
"AP: "
+ string (this.APs.Length)
+ " "
+ List.fold (fun s x -> s + " \"" + alphStringer (x) + "\"") "" this.APs
)
s.WriteLine("acc-name: all")
s.WriteLine("Acceptance: 0 t")
s.WriteLine "--BODY--"
for n in this.States do
let edges = this.Edges.[n]
s.WriteLine("State: " + stateStringer (n))
for (g, n') in edges do
s.WriteLine("[" + DNF.print g + "] " + stateStringer (n'))
s.WriteLine "--END--"
s.ToString()
module NSA =
let actuallyUsedAPs (nsa : NSA<'T, 'L>) =
NondeterministicAutomatonSkeleton.actuallyUsedAPs nsa.Skeleton
let convertStatesToInt (nsa : NSA<'T, 'L>) =
let idDict = nsa.Skeleton.States |> Seq.mapi (fun i x -> x, i) |> Map.ofSeq
{
NSA.Skeleton =
{
AutomatonSkeleton.States = nsa.Skeleton.States |> Set.map (fun x -> idDict.[x])
APs = nsa.Skeleton.APs
Edges =
nsa.Skeleton.Edges
|> Map.toSeq
|> Seq.map (fun (k, v) -> idDict.[k], v |> List.map (fun (g, s) -> g, idDict.[s]))
|> Map.ofSeq
}
InitialStates = nsa.InitialStates |> Set.map (fun x -> idDict.[x])
}
let mapAPs (f : 'L -> 'U) (nsa : NSA<'T, 'L>) =
{
Skeleton = NondeterministicAutomatonSkeleton.mapAPs f nsa.Skeleton
InitialStates = nsa.InitialStates
}
let trueAutomaton () : NSA<int, 'L> =
{
NSA.Skeleton =
{
States = set ([ 0 ])
APs = []
Edges = [ 0, [ DNF.trueDNF, 0 ] ] |> Map.ofList
}
InitialStates = set ([ 0 ])
}
let falseAutomaton () : NSA<int, 'L> =
{
NSA.Skeleton =
{
States = set ([ 0 ])
APs = []
Edges = [ 0, List.empty ] |> Map.ofList
}
InitialStates = set ([ 0 ])
}
let toHoaString (stateStringer : 'T -> string) (alphStringer : 'L -> string) (nsa : NSA<'T, 'L>) =
(nsa :> AbstractAutomaton<'T, 'L>).ToHoaString stateStringer alphStringer
let findError (nsa : NSA<'T, 'L>) =
(nsa :> AbstractAutomaton<'T, 'L>).FindError()
let bringToSameAPs (autList : list<NSA<'T, 'L>>) =
autList
|> List.map (fun x -> x.Skeleton)
|> NondeterministicAutomatonSkeleton.bringSkeletonsToSameAps
|> List.mapi (fun i x -> { autList.[i] with Skeleton = x })
let bringPairToSameAPs (nsa1 : NSA<'T, 'L>) (nsa2 : NSA<'T, 'L>) =
let sk1, sk2 =
NondeterministicAutomatonSkeleton.bringSkeletonPairToSameAps nsa1.Skeleton nsa2.Skeleton
{ nsa1 with Skeleton = sk1 }, { nsa2 with Skeleton = sk2 }
let addAPs (aps : list<'L>) (nsa : NSA<'T, 'L>) =
{ nsa with
Skeleton = NondeterministicAutomatonSkeleton.addAPsToSkeleton aps nsa.Skeleton
}
let fixAPs (aps : list<'L>) (nsa : NSA<'T, 'L>) =
{ nsa with
Skeleton = NondeterministicAutomatonSkeleton.fixAPsToSkeleton aps nsa.Skeleton
}
let projectToTargetAPs (newAPs : list<'L>) (nsa : NSA<int, 'L>) =
{ nsa with
Skeleton = NondeterministicAutomatonSkeleton.projectToTargetAPs newAPs nsa.Skeleton
}
let computeBisimulationQuotient (nsa : NSA<int, 'L>) =
let skeleton, m =
NondeterministicAutomatonSkeleton.computeBisimulationQuotient (fun _ -> 0) nsa.Skeleton
{
NSA.Skeleton = skeleton
InitialStates = nsa.InitialStates |> Set.map (fun x -> m.[x])
}