grew_grs.ml 10.7 KB
Newer Older
pj2m's avatar
pj2m committed
1 2 3
open Printf
open Log

4
open Grew_fs
5
open Libgrew_utils
bguillaum's avatar
bguillaum committed
6
open Grew_ast
7
open Grew_edge
bguillaum's avatar
bguillaum committed
8 9 10
open Grew_command
open Grew_graph
open Grew_rule
bguillaum's avatar
bguillaum committed
11
open Grew_parser
pj2m's avatar
pj2m committed
12

13
(* ==================================================================================================== *)
pj2m's avatar
pj2m committed
14 15
module Rewrite_history = struct
  type t = {
16 17 18 19 20
    instance: Instance.t;
    module_name: string;
    good_nf: t list;
    bad_nf: Instance.t list;
  }
pj2m's avatar
pj2m committed
21

22
  let rec is_empty t =
bguillaum's avatar
bguillaum committed
23
    (t.instance.Instance.rules = []) && List.for_all is_empty t.good_nf
bguillaum's avatar
bguillaum committed
24 25

  let rec num_sol = function
26 27
    | { good_nf = []; bad_nf = [] } -> 1
    | { good_nf = [] } -> 0 (* dead branch *)
bguillaum's avatar
bguillaum committed
28
    | { good_nf = l} -> List.fold_left (fun acc t -> acc + (num_sol t)) 0 l
29

30
  let save_nfs ?filter ?main_feat ~dot base_name t =
31
    let rec loop file_name rules t =
32
      match (t.good_nf, t.bad_nf) with
33
        | [],[] when dot -> Instance.save_dot_png ?filter ?main_feat file_name t.instance; [rules, file_name]
bguillaum's avatar
bguillaum committed
34
        | [],[] -> ignore (Instance.save_dep_png ?filter ?main_feat file_name t.instance); [rules, file_name]
35 36
        | [],_ -> []
        | l, _ ->
37
          List_.foldi_left
38
            (fun i acc son ->
39
              (* Instance.save_dep_png ?main_feat (sprintf "%s_%d" file_name i) son.instance; *)
40 41 42 43
              let nfs = loop
                (sprintf "%s_%d" file_name i)
                (rules @ [t.module_name, son.instance.Instance.rules])
                son in
44 45 46 47 48
              nfs @ acc
            )
            [] l
    in loop base_name [] t

49 50 51 52 53 54 55
  let save_gr base t =
    let rec loop file_name t =
      match (t.good_nf, t.bad_nf) with
        | [],[] -> File.write (Instance.to_gr t.instance) (file_name^".gr")
        | l, _ -> List_.iteri (fun i son -> loop (sprintf "%s_%d" file_name i) son) l
    in loop base t

bguillaum's avatar
bguillaum committed
56 57 58 59 60 61 62
  let save_conll base t =
    let rec loop file_name t =
      match (t.good_nf, t.bad_nf) with
        | [],[] -> File.write (Instance.to_conll t.instance) (file_name^".conll")
        | l, _ -> List_.iteri (fun i son -> loop (sprintf "%s_%d" file_name i) son) l
    in loop base t

63 64 65 66 67 68 69 70
  (* suppose that all modules are confluent and produced exacly one normal form *)
  let save_det_gr base t =
    let rec loop t =
      match (t.good_nf, t.bad_nf) with
        | [],[] -> File.write (Instance.to_gr t.instance) (base^".gr")
        | [one], [] -> loop one
        | _ -> Error.run "Not a single rewriting"
    in loop t
bguillaum's avatar
bguillaum committed
71

bguillaum's avatar
bguillaum committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  let save_annot out_dir base_name t =
    List_.mapi
      (fun i alts ->
        match alts.good_nf with
      | [alt_1; alt_2] ->
        let a = sprintf "%s_%d_A" base_name i in
        let b = sprintf "%s_%d_B" base_name i in
        let hpa = Instance.save_dep_svg (Filename.concat out_dir a) alt_1.instance in
        let hpb = Instance.save_dep_svg (Filename.concat out_dir b) alt_2.instance in
        let (afn,afv,apos) = G_graph.get_annot_info alt_1.instance.Instance.graph
        and (bfn,bfv,bpos) = G_graph.get_annot_info alt_2.instance.Instance.graph in
        (base_name,i,(afn,afv,apos),(bfn,bfv,bpos),(hpa,hpb))
      | _ -> Error.run "Not two alternatives in an annotation rewriting in %s" base_name
      ) t.good_nf

87
  let save_det_conll ?header base t =
bguillaum's avatar
bguillaum committed
88 89
    let rec loop t =
      match (t.good_nf, t.bad_nf) with
90 91 92 93 94 95 96
        | ([],[]) ->
          let output =
            match header with
              | Some h -> sprintf "%% %s\n%s" h (Instance.to_conll t.instance)
              | None -> Instance.to_conll t.instance in
          File.write output (base^".conll")
        | ([one], []) -> loop one
bguillaum's avatar
bguillaum committed
97 98 99
        | _ -> Error.run "Not a single rewriting"
    in loop t

bguillaum's avatar
bguillaum committed
100 101 102 103 104
  let det_dep_string t =
    let rec loop t =
      match (t.good_nf, t.bad_nf) with
        | [],[] ->
          let graph = t.instance.Instance.graph in
bguillaum's avatar
bguillaum committed
105
          Some (G_graph.to_dep graph)
bguillaum's avatar
bguillaum committed
106
        | [one], [] -> loop one
bguillaum's avatar
bguillaum committed
107
        | _ -> None
bguillaum's avatar
bguillaum committed
108
    in loop t
bguillaum's avatar
bguillaum committed
109 110 111 112 113 114 115 116 117 118 119 120 121

  let conll_dep_string ?(keep_empty_rh=false) t =
    if (not keep_empty_rh) && is_empty t
    then None
    else
      let rec loop t =
        match (t.good_nf, t.bad_nf) with
          | [],[] ->
            let graph = t.instance.Instance.graph in
            Some (G_graph.to_conll graph)
          | [one], [] -> loop one
          | _ -> None
      in loop t
122
end (* module Rewrite_history *)
pj2m's avatar
pj2m committed
123

124
(* ==================================================================================================== *)
pj2m's avatar
pj2m committed
125 126
module Modul = struct
  type t = {
127
    name: string;
128
    local_labels: (string * string list) array;
129 130 131 132 133
    rules: Rule.t list;
    filters: Rule.t list;
    confluent: bool;
    loc: Loc.t;
  }
pj2m's avatar
pj2m committed
134

135 136 137 138
  let check t =
    (* check for duplicate rules *)
    let rec loop already_defined = function
      | [] -> ()
139 140
      | r::_ when List.mem (Rule.get_name r) already_defined ->
        Error.build ~loc:(Rule.get_loc r) "Rule '%s' is defined twice in the same module" (Rule.get_name r)
141 142
      | r::tail -> loop ((Rule.get_name r) :: already_defined) tail in
    loop [] t.rules
pj2m's avatar
pj2m committed
143

144
  let build ast_module =
145 146
    let locals = Array.of_list ast_module.Ast.local_labels in
    Array.sort compare locals;
147
    let rules_or_filters = List.map (Rule.build ~locals ast_module.Ast.mod_dir) ast_module.Ast.rules in
148 149
    let (filters, rules) = List.partition Rule.is_filter rules_or_filters in
    let modul =
150
      {
151 152 153 154 155 156 157
        name = ast_module.Ast.module_id;
        local_labels = locals;
        rules = rules;
        filters = filters;
        confluent = ast_module.Ast.confluent;
        loc = ast_module.Ast.mod_loc;
      } in
158
    check modul; modul
159
end (* module Modul *)
160

161
(* ==================================================================================================== *)
162 163
module Sequence = struct
  type t = {
164 165 166 167
    name: string;
    def: string list;
    loc: Loc.t;
  }
168 169 170

  let check module_list t =
    List.iter
171
      (fun module_name ->
172
        if not (List.exists (fun modul -> modul.Modul.name = module_name) module_list)
bguillaum's avatar
Typos  
bguillaum committed
173
        then Error.build ~loc:t.loc "sequence \"%s\" refers to the unknown module \"%s\"."
174
          t.name module_name
175 176
      ) t.def

177 178
  let build module_list ast_sequence =
    let sequence =
179
      {
180 181 182 183
        name = ast_sequence.Ast.seq_name;
        def = ast_sequence.Ast.seq_mod;
        loc = ast_sequence.Ast.seq_loc;
      } in
184
    check module_list sequence; sequence
185
end (* module Sequence *)
pj2m's avatar
pj2m committed
186

187
(* ==================================================================================================== *)
pj2m's avatar
pj2m committed
188
module Grs = struct
189

pj2m's avatar
pj2m committed
190
  type t = {
191 192
    labels: Label.t list;        (* the list of global edge labels *)
    modules: Modul.t list;       (* the ordered list of modules used from rewriting *)
193
    sequences: Sequence.t list;
bguillaum's avatar
bguillaum committed
194 195
    filename: string;
    ast: Ast.grs;
196 197
  }

bguillaum's avatar
bguillaum committed
198
  let get_modules t = t.modules
bguillaum's avatar
bguillaum committed
199 200
  let get_ast t = t.ast
  let get_filename t = t.filename
bguillaum's avatar
bguillaum committed
201

202 203
  let sequence_names t = List.map (fun s -> s.Sequence.name) t.sequences

bguillaum's avatar
bguillaum committed
204
  let empty = {labels=[]; modules=[]; sequences=[]; ast=Ast.empty_grs; filename=""; }
pj2m's avatar
pj2m committed
205

206 207 208 209
  let check t =
    (* check for duplicate modules *)
    let rec loop already_defined = function
      | [] -> ()
210 211
      | m::_ when List.mem m.Modul.name already_defined ->
        Error.build ~loc:m.Modul.loc "Module '%s' is defined twice" m.Modul.name
212 213 214 215 216 217
      | m::tail -> loop (m.Modul.name :: already_defined) tail in
    loop [] t.modules;

    (* check for duplicate sequences *)
    let rec loop already_defined = function
      | [] -> ()
218 219
      | s::_ when List.mem s.Sequence.name already_defined ->
        Error.build ~loc:s.Sequence.loc "Sequence '%s' is defined twice" s.Sequence.name
220 221 222
      | s::tail -> loop (s.Sequence.name :: already_defined) tail in
    loop [] t.sequences

bguillaum's avatar
bguillaum committed
223 224 225 226 227
  let build filename =
    let ast = Grew_parser.grs_of_file filename in
    Label.init ast.Ast.labels;
    Domain.init ast.Ast.domain;
    let modules = List.map Modul.build ast.Ast.modules in
228
    let grs = {
bguillaum's avatar
bguillaum committed
229 230 231
      labels = List.map (fun (l,_) -> Label.from_string l) ast.Ast.labels;
      sequences = List.map (Sequence.build modules) ast.Ast.sequences;
      modules; ast; filename;
232 233
    } in
    check grs; grs
pj2m's avatar
pj2m committed
234

bguillaum's avatar
bguillaum committed
235
  let modules_of_sequence grs sequence =
236
    let module_names =
237
      try
238 239
        let seq = List.find (fun s -> s.Sequence.name = sequence) grs.sequences in
        seq.Sequence.def
bguillaum's avatar
bguillaum committed
240 241
      with Not_found -> [sequence] in (* a module name can be used as a singleton sequence *)

242 243 244
    List.map
      (fun name ->
        try List.find (fun m -> m.Modul.name=name) grs.modules
bguillaum's avatar
bguillaum committed
245 246 247 248
        with Not_found -> Log.fcritical "No sequence or module named '%s'" name
      )
      module_names

249
  let rewrite grs sequence instance =
bguillaum's avatar
bguillaum committed
250
    Timeout.start ();
bguillaum's avatar
bguillaum committed
251
    let modules_to_apply = modules_of_sequence grs sequence in
252

pj2m's avatar
pj2m committed
253
    let rec loop instance = function
254 255 256 257 258 259 260 261 262
      | [] -> (* no more modules to apply *)
        {Rewrite_history.instance = instance; module_name = ""; good_nf = []; bad_nf = []; }
      | next::tail ->
        let (good_set, bad_set) =
          Rule.normalize
            next.Modul.name
            ~confluent: next.Modul.confluent
            next.Modul.rules
            next.Modul.filters
263
            (Instance.flatten instance) in
264 265 266 267 268 269 270 271
        let good_list = Instance_set.elements good_set
        and bad_list = Instance_set.elements bad_set in
        {
          Rewrite_history.instance = instance;
          module_name = next.Modul.name;
          good_nf = List.map (fun i -> loop i tail) good_list;
          bad_nf = bad_list;
        } in
bguillaum's avatar
bguillaum committed
272
    loop instance modules_to_apply
273

bguillaum's avatar
bguillaum committed
274 275
  let build_rew_display grs sequence instance =
    let modules_to_apply = modules_of_sequence grs sequence in
pj2m's avatar
pj2m committed
276 277 278

    let rec loop instance = function
      | [] -> Grew_types.Leaf instance.Instance.graph
279
      | next :: tail ->
280 281 282 283 284 285
        let (good_set, bad_set) =
          Rule.normalize
            next.Modul.name
            ~confluent: next.Modul.confluent
            next.Modul.rules
            next.Modul.filters
286
            (Instance.flatten instance) in
287
        let inst_list = Instance_set.elements good_set
288
              (* and bad_list = Instance_set.elements bad_set *) in
pj2m's avatar
pj2m committed
289

290
        match inst_list with
291
          | [{Instance.big_step = None}] ->
292
            Grew_types.Local_normal_form (instance.Instance.graph, next.Modul.name, loop instance tail)
293
          | _ -> Grew_types.Node
294 295 296 297 298 299 300 301 302 303
            (
              instance.Instance.graph,
              next.Modul.name,
              List.map
                (fun inst ->
                  match inst.Instance.big_step with
                    | None -> Error.bug "Cannot have no big_steps and more than one reducts at the same time"
                    | Some bs -> (bs, loop inst tail)
                ) inst_list
            )
pj2m's avatar
pj2m committed
304
    in loop instance modules_to_apply
bguillaum's avatar
bguillaum committed
305

bguillaum's avatar
bguillaum committed
306 307 308
  let rule_iter fct grs =
    List.iter
      (fun modul ->
bguillaum's avatar
bguillaum committed
309 310 311 312 313 314 315
        List.iter (fun rule -> fct modul.Modul.name rule) modul.Modul.rules
      ) grs.modules

  let filter_iter fct grs =
    List.iter
      (fun modul ->
        List.iter (fun filter -> fct modul.Modul.name filter) modul.Modul.filters
bguillaum's avatar
bguillaum committed
316
      ) grs.modules
317
end (* module Grs *)