]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/contribs/lambdadelta/bin/roles/rolesEngine.ml
update in binaries for λδ
[helm.git] / matita / matita / contribs / lambdadelta / bin / roles / rolesEngine.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic
3     ||A||  Library of Mathematics, developed at the Computer Science
4     ||T||  Department, University of Bologna, Italy.
5     ||I||
6     ||T||  HELM is free software; you can redistribute it and/or
7     ||A||  modify it under the terms of the GNU General Public License
8     \   /  version 2 or (at your option) any later version.
9      \ /   This software is distributed as is, NO WARRANTY.
10       V_______________________________________________________________ *)
11
12 module EG = RolesGlobal
13 module EI = RolesInput
14 module EO = RolesOutput
15 module EU = RolesUtils
16 module ET = RolesTypes
17
18 let st = EU.new_status
19
20 let new_stage v =
21   if st.ET.w = [] then st.ET.s <- v
22   else EU.raise_error ET.EWaiting
23
24 let toggle_entry = function
25   | [0]       -> st.ET.r <- EU.list_toggle_all st.ET.r
26   | [0;m]     -> st.ET.r <- EU.list_toggle m st.ET.r
27   | [0;m;1]   ->
28     let r = EU.list_nth m st.ET.r in
29     r.ET.o <- EU.list_toggle_all r.ET.o
30   | [0;m;1;n] ->
31     let r = EU.list_nth m st.ET.r in
32     r.ET.o <- EU.list_toggle n r.ET.o
33   | [0;m;2]   ->
34     let r = EU.list_nth m st.ET.r in
35     r.ET.n <- EU.list_toggle_all r.ET.n
36   | [0;m;2;n] ->
37     let r = EU.list_nth m st.ET.r in
38     r.ET.n <- EU.list_toggle n r.ET.n
39   | [1]        -> st.ET.t <- EU.list_toggle_all st.ET.t
40   | [1;m]      -> st.ET.t <- EU.list_toggle m st.ET.t
41   | [2]        -> st.ET.w <- EU.list_toggle_all st.ET.w
42   | [2;m]      -> st.ET.w <- EU.list_toggle m st.ET.w
43   | _          -> EU.raise_error ET.ENoEntry
44
45 let add_role () =
46   let ts,os = EU.list_split st.ET.t in
47   let ws,ns = EU.list_split st.ET.w in
48   if os = [] && ns = [] then () else
49   begin match EU.list_select None st.ET.r with
50   | None   ->
51     let r = {ET.v = st.ET.s; ET.o = os; ET.n = ns} in
52     st.ET.r <- EU.roles_union [false, r] st.ET.r
53   | Some r ->
54     if r.ET.v <> st.ET.s then EU.raise_error ET.EWrongVersion else
55     r.ET.o <- EU.objs_union os r.ET.o;
56     r.ET.n <- EU.names_union ns r.ET.n;
57   end;
58   st.ET.t <- ts; st.ET.w <- ws
59
60 let add_tops v =
61   if EU.exists_role_deleted st.ET.s st.ET.r || st.ET.t <> []
62   then EU.raise_error ET.ETops else
63   let ds, ts = EU.get_tops v st.ET.r in
64   if ds <> [] then begin
65     let r = {ET.v = st.ET.s; ET.o = ds; ET.n = []} in
66     st.ET.r <- EU.roles_union [false, r] st.ET.r
67   end;
68   if ts <> [] then st.ET.t <- ts
69
70 let rec add_matching () =
71   match EU.match_names 0 0 st.ET.t st.ET.w with
72   | None          -> ()
73   | Some  (ti,wi) ->
74     toggle_entry [1;ti];
75     toggle_entry [2;wi];
76     add_role ();
77     add_matching ()
78
79 let read_waiting fname =
80   if st.ET.s = [] then EU.raise_error ET.ENoStage else
81   let ich = Scanf.Scanning.open_in fname in
82   let ws = EI.read_rev_names ich [] in
83   Scanf.Scanning.close_in ich;
84   let map ws w = EU.names_union ws [w] in
85   st.ET.w <- List.fold_left map st.ET.w ws
86
87 let read_status () =
88   if st.ET.s <> [] then EU.raise_error (ET.EStage st.ET.s) else
89   let fname = Filename.concat !EG.cwd "roles.osn" in
90   let ich = open_in fname in
91   let tmp = EI.read_status ich in
92   close_in ich;
93   st.ET.r <- tmp.ET.r;
94   st.ET.s <- tmp.ET.s;
95   st.ET.t <- tmp.ET.t;
96   st.ET.w <- tmp.ET.w
97
98 let write_status () =
99   let fname = Filename.concat !EG.cwd "roles.osn" in
100   let och = open_out fname in
101   EO.out_status och st;
102   close_out och
103
104 let print_status () =
105   EO.out_status stdout st
106
107 let visit_status
108   before_r each_r before after after_r stage
109   before_t each_t after_t before_w each_w after_w =
110   let visit_tw _ _ = () in
111   let visit_r p r =
112     before (EU.string_of_pointer (List.rev p));
113     EU.list_visit before_t each_t visit_tw after_t EU.string_of_obj (1::p) r.ET.o;
114     EU.list_visit before_w each_w visit_tw after_w EU.string_of_name (2::p) r.ET.n;
115     after ()
116   in
117   EU.list_visit before_r each_r visit_r after_r EU.string_of_role [0] st.ET.r;
118   stage (EU.string_of_version st.ET.s);
119   EU.list_visit before_t each_t visit_tw after_t EU.string_of_obj [1] st.ET.t;
120   EU.list_visit before_w each_w visit_tw after_w EU.string_of_name [2] st.ET.w