-
Notifications
You must be signed in to change notification settings - Fork 1
/
interactive.ml
330 lines (307 loc) · 11.6 KB
/
interactive.ml
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
exception Change_shape of string
module Term = ANSITerminal
module type InteractiveType =
sig
type t
val empty : t
val simple_tests : unit -> unit
val interactive : unit -> unit
val process_command : t -> Parser.command -> t
val bench : string -> unit
end
module Make (Node:Node.NodeType) =
(struct
module Cat = Cat.Make (Node)
module Graph = Graph.Make (Node)
module Hom = Homomorphism.Make (Node)
module Model = Model.Make (Node)
module EB = Basis.Make (Node)
open Lib.Util
let draw_line u v g =
let g =
List.fold_left
(fun g u ->
Graph.add_node u g
) g [u;v] in
Graph.add_edge u v g
let rec draw line_list g =
match line_list with
[] -> g
| (u,v)::tl ->
let g' = draw_line u v g in
draw tl g'
let graph_of_library name =
try
let edges = Lib.StringMap.find name Node.library in
draw edges Graph.empty
with
Graph.Incoherent -> failwith (name^" is not a coherent graph!")
let (=>) = Cat.(=>)
let (|>) = Cat.(|>)
let (=~=>) g h = Cat.flatten (Cat.extension_class (g => h))
let simple_tests () =
let dsquare = graph_of_library "dsquare"
in
let square = graph_of_library "square"
in
let one = graph_of_library "one" in
let o2_to_o8 = one =~=> square in
let o2_to_w = one =~=> dsquare in
List.iter (fun o2_o8 ->
List.iter (fun o2_w ->
let inf_to_mp,mp_to_base,mp_to_w,_ = List.hd (Cat.share o2_o8 o2_w) in
Printf.printf "%s \n %s \n %s \n" (Cat.string_of_span (o2_o8,o2_w))
(Cat.string_of_arrows inf_to_mp) (Cat.string_of_span (mp_to_base,mp_to_w))
) o2_to_w
) o2_to_o8
type t = {model : Model.t ;
show_positive : bool ;
max_step : int option ;
min_sharing : int ;
self_adjust : int option ;
tree_shape : bool ;
eb : (EB.t * EB.t) option ;
sparse : bool ;
rule : (string * string) option
}
let empty =
{model = Model.empty ;
show_positive = true ;
eb = None; rule = None ;
max_step = None ;
min_sharing = 1 ;
self_adjust = None ;
tree_shape = false ;
sparse = false}
let string_of_env env =
Printf.sprintf
"show_positive: %b\nmax_step: %s\nmin_sharing: %d\nself_adjust: %s\ntree_shape: %b\nsparse: %b\n"
env.show_positive
(match env.max_step with None -> "None" | Some i -> string_of_int i)
env.min_sharing (match env.self_adjust with None -> "None" | Some i -> string_of_int i) env.tree_shape env.sparse
let output env =
if db() then flush stdout ;
match env.eb with
None -> ()
| Some (pb,nb) ->
let eb = if env.show_positive then pb else nb
in
let d = open_out "web_eb.dot" in
Printf.fprintf d "%s\n%s"
(EB.to_dot true env.model.Model.dict eb)
(EB.to_dot_content eb) ;
close_out d
let build_base ?obs_name env =
let params = {EB.max_step = env.max_step ; EB.min_sharing = env.min_sharing ; EB.tree_shape = env.tree_shape ; EB.sparse = env.sparse}
in
let () = if db() then Printexc.record_backtrace true in
match env.rule with
None -> env
| Some (l,r) ->
let lg =
try graph_of_library l
with Not_found ->
Model.get_obs (Lib.Dict.to_id l env.model.Model.dict) env.model
in
let rg = try graph_of_library r
with
Not_found ->
Model.get_obs (Lib.Dict.to_id r env.model.Model.dict) env.model
in
print_endline "Generating witnesses..." ;
let nw,pw =
match obs_name with
None -> Model.witnesses_of_rule (lg,rg) env.model
| Some o ->
let obs_id = (Lib.Dict.to_id o env.model.Model.dict)
in
Model.witnesses_of_rule ~obs:obs_id (lg,rg) env.model
in
let n = List.length pw in
Term.printf [] "%d witnesses found for observable %s!\n" n ( (function Some o -> o | None -> "") obs_name) ;
let eb_pos,eb_neg =
match env.eb with
None ->
let get_seed = function
(id_obs,tile)::_ ->
Cat.left_of_tile tile
| [] -> Graph.empty
in
(EB.empty (get_seed pw) , EB.empty (get_seed nw))
| Some basis -> basis
in
Term.printf [Term.Bold; Term.blue] "Building positive extension base...\n" ;
let min_sharing f = match env.self_adjust with
None -> env.min_sharing
| Some i -> max env.min_sharing ((Cat.size f) / i) in
let _,(pos_ext_base,opt) =
try
List.fold_left
(fun (cpt,(ext_base,opt)) (id_obs,tile) ->
Term.printf [Term.yellow] "%d/%d" cpt n; flush stdout ;
Term.move_bol () ;
match Cat.upper_bound tile with
None -> failwith "no witness"
| Some (to_w,from_o) ->
if db() then
begin
Term.printf [Term.red] "------------------------\n" ;
Printf.printf "Inserting witness of observable '%s': %s\n"
(Lib.Dict.to_name id_obs env.model.Model.dict)
(Cat.string_of_cospan (to_w,from_o)) ; flush stdout
end;
(cpt+1,EB.insert {params with EB.min_sharing = (min_sharing to_w)} to_w from_o id_obs ext_base)
) (1,(eb_pos,None)) pw
with EB.Invariant_failure (str,ext_base) -> print_endline (red str) ; (0,(ext_base,None))
in
print_newline () ;
let neg_ext_base,opt =
try
List.fold_left
(fun (ext_base,opt) (id_obs,tile) ->
match Cat.upper_bound tile with
None -> failwith "no witness"
| Some (to_w,from_o) ->
if db() then
Printf.printf "Inserting witness of observable '%s': %s\n"
(Lib.Dict.to_name id_obs env.model.Model.dict)
(Cat.string_of_cospan (to_w,from_o)) ; flush stdout ;
EB.insert params to_w from_o id_obs ext_base
) (eb_neg,None) nw
with EB.Invariant_failure (str,ext_base) -> print_endline (red str) ; (ext_base,None)
in
{env with eb = Some (pos_ext_base,neg_ext_base)}
let rec process_command env = function
| Parser.Help -> log "set [sharing <int> | adjust <int> | sparse | treelike | complete | step <int>]" ; env
| Parser.BaseShape t ->
begin
match t with
Parser.Complete -> {env with tree_shape = false ; sparse = false}
| Parser.Tree -> {env with tree_shape = true ; sparse = false}
| Parser.Sparse -> {env with tree_shape = false ; sparse = true}
end
| Parser.Sharing t ->
begin
match t with
Parser.MinShare i -> {env with min_sharing = i}
| Parser.Adjust i -> {env with self_adjust = Some i}
end
| Parser.MaxStep m -> {env with max_step = if m>=0 then Some m else None}
| Parser.Mode s ->
log "Changing mode. Current model has been erased.";
raise (Change_shape s)
| Parser.Debug ->
begin
debug_mode () ;
env
end
| Parser.Safe ->
begin
safe_mode () ;
env
end
| Parser.List ->
log ("Observables:\n") ;
log (String.concat "\n" (proj_left (Model.list env.model))) ;
log ("Rules:\n") ;
log (String.concat "\n" (proj_right (Model.list env.model))) ;
log ("Parameters:\n") ;
log ((string_of_env env)^"\n") ;
env
| Parser.Build (l,r) ->
log (Printf.sprintf "Generating extension basis for rule %s -> %s" l r);
let env = {env with rule = Some (l,r) ; eb = None} in
let env = build_base env in
output env ;
env
| Parser.Add v ->
if Lib.StringMap.mem v Node.library then
let graph = graph_of_library v in
let model = Model.add_obs v graph env.model in
match env.eb with
None -> {env with model = model}
| Some _ ->
let env = build_base ~obs_name:v {env with model = model}
in output env ; env
else
begin
log "Unrecognized shape" ;
env
end
| Parser.Output positive -> let env = {env with show_positive = positive} in output env ; env
| Parser.Add_named (lst,v) ->
let edges = Node.tn lst in
let graph = draw edges Graph.empty in
let env = {env with model = Model.add_obs v graph env.model} in
let env = build_base ~obs_name:v env in
output env ;
env
| Parser.Exit -> log "exiting" ; exit 0
| Parser.Reset -> empty
| Parser.Blank -> env
| Parser.Shell (inst,args) ->
let pid = Unix.fork () in
if pid = 0 then Unix.execvp inst args
else
let _ = Unix.wait () in
env
| Parser.Load file ->
let run_line acc lineno line = match Parser.parse line with
| Result.Ok command -> (match command with
| Parser.Mode _ ->
log (Printf.sprintf "Ignoring mode command at line %d" lineno);
Some acc
| _ ->
Some (process_command acc command))
| Result.Error _ ->
log (Printf.sprintf "Parse error at line %d" lineno);
None
in
each_line file run_line env
let bench file =
let ic = open_in file in
let rec exec env =
let line = input_line ic in
match Parser.parse line with
Result.Ok command ->
exec (process_command env command)
| Result.Error s -> print_endline s ; close_in ic ; exit (-1)
in
try
exec empty
with
End_of_file -> close_in ic ; exit 0
let interactive debug =
let rec session env =
let _ = Unix.waitpid [Unix.WNOHANG] in
let prompt () =
let db_str = if db() then "db" else "" in
let safe_str = if safe() then "!" else "" in
let shr_str = if env.min_sharing = 1 then "" else Printf.sprintf "[%d]" env.min_sharing in
Printf.sprintf "(%s)%s%s%s> " Node.info db_str safe_str shr_str
in
(match (LNoise.linenoise (prompt ())) with
| None ->
log "Attempting to save session history";
ignore (LNoise.history_save histfile);
exit 0
| Some line ->
ignore (LNoise.history_add line);
(match Parser.parse line with
| Result.Ok command ->
if db() then session (process_command env command)
else
(try
session (process_command env command)
with
Change_shape _ as exn -> raise exn
| exn ->
if db() then raise exn else log (Printexc.to_string exn) ; session env)
| Result.Error s -> log ("Parse error "^s); session env);
)
in
session empty
end:InteractiveType)
module SPrompt = Make (Node.SimpleNode)
module KPrompt = Make (PortNode.KappaNode)
module DPrompt = Make (PortNode.DegreeNode)