grew_grs.ml 11.8 KB
Newer Older
bguillaum's avatar
bguillaum committed
1 2 3 4 5 6 7 8 9 10
(**********************************************************************************)
(*    Libcaml-grew - a Graph Rewriting library dedicated to NLP applications      *)
(*                                                                                *)
(*    Copyright 2011-2013 Inria, Université de Lorraine                           *)
(*                                                                                *)
(*    Webpage: http://grew.loria.fr                                               *)
(*    License: CeCILL (see LICENSE folder or "http://www.cecill.info")            *)
(*    Authors: see AUTHORS file                                                   *)
(**********************************************************************************)

pj2m's avatar
pj2m committed
11 12 13
open Printf
open Log

14
open Grew_fs
bguillaum's avatar
bguillaum committed
15
open Grew_base
16
open Grew_types
bguillaum's avatar
bguillaum committed
17
open Grew_ast
18
open Grew_edge
bguillaum's avatar
bguillaum committed
19 20 21
open Grew_command
open Grew_graph
open Grew_rule
bguillaum's avatar
bguillaum committed
22
open Grew_parser
pj2m's avatar
pj2m committed
23

24
(* ==================================================================================================== *)
pj2m's avatar
pj2m committed
25 26
module Rewrite_history = struct
  type t = {
27 28 29 30 31
    instance: Instance.t;
    module_name: string;
    good_nf: t list;
    bad_nf: Instance.t list;
  }
pj2m's avatar
pj2m committed
32

33
  let rec is_empty t =
bguillaum's avatar
bguillaum committed
34
    (t.instance.Instance.rules = []) && List.for_all is_empty t.good_nf
bguillaum's avatar
bguillaum committed
35 36

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

41
  let save_nfs ?filter ?main_feat ~dot base_name t =
42
    let rec loop file_name rules t =
43
      match (t.good_nf, t.bad_nf) with
44
        | [],[] when dot -> Instance.save_dot_png ?filter ?main_feat file_name t.instance; [rules, file_name]
bguillaum's avatar
bguillaum committed
45
        | [],[] -> ignore (Instance.save_dep_png ?filter ?main_feat file_name t.instance); [rules, file_name]
46 47
        | [],_ -> []
        | l, _ ->
48
          List_.foldi_left
49
            (fun i acc son ->
50
              (* Instance.save_dep_png ?main_feat (sprintf "%s_%d" file_name i) son.instance; *)
51 52 53 54
              let nfs = loop
                (sprintf "%s_%d" file_name i)
                (rules @ [t.module_name, son.instance.Instance.rules])
                son in
55 56 57 58 59
              nfs @ acc
            )
            [] l
    in loop base_name [] t

60 61 62 63 64 65 66
  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
67 68 69 70 71 72 73
  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

74 75 76 77 78 79 80 81
  (* 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
82

bguillaum's avatar
bguillaum committed
83 84 85 86 87 88 89 90 91
  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
bguillaum's avatar
bguillaum committed
92 93 94
        let (afn,apos) = G_graph.get_annot_info alt_1.instance.Instance.graph
        and (bfn,bpos) = G_graph.get_annot_info alt_2.instance.Instance.graph in
        (base_name,i,(afn,apos),(bfn,bpos),(hpa,hpb))
bguillaum's avatar
bguillaum committed
95 96 97
      | _ -> Error.run "Not two alternatives in an annotation rewriting in %s" base_name
      ) t.good_nf

98
  let save_det_conll ?header base t =
bguillaum's avatar
bguillaum committed
99 100
    let rec loop t =
      match (t.good_nf, t.bad_nf) with
101 102 103 104 105 106 107
        | ([],[]) ->
          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
108 109 110
        | _ -> Error.run "Not a single rewriting"
    in loop t

bguillaum's avatar
bguillaum committed
111 112 113 114 115
  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
116
          Some (G_graph.to_dep graph)
bguillaum's avatar
bguillaum committed
117
        | [one], [] -> loop one
bguillaum's avatar
bguillaum committed
118
        | _ -> None
bguillaum's avatar
bguillaum committed
119
    in loop t
bguillaum's avatar
bguillaum committed
120 121 122 123 124 125 126 127 128 129 130 131 132

  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
133
end (* module Rewrite_history *)
pj2m's avatar
pj2m committed
134

135
(* ==================================================================================================== *)
pj2m's avatar
pj2m committed
136 137
module Modul = struct
  type t = {
138
    name: string;
139
    local_labels: (string * string list) array;
140
    suffixes: string list;
141 142 143 144 145
    rules: Rule.t list;
    filters: Rule.t list;
    confluent: bool;
    loc: Loc.t;
  }
pj2m's avatar
pj2m committed
146

147 148 149 150
  let check t =
    (* check for duplicate rules *)
    let rec loop already_defined = function
      | [] -> ()
151 152
      | 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)
153 154
      | r::tail -> loop ((Rule.get_name r) :: already_defined) tail in
    loop [] t.rules
pj2m's avatar
pj2m committed
155

156
  let build ast_module =
157 158
    let locals = Array.of_list ast_module.Ast.local_labels in
    Array.sort compare locals;
159 160
    let suffixes = ast_module.Ast.suffixes in
    let rules_or_filters = List.map (Rule.build ~locals suffixes ast_module.Ast.mod_dir) ast_module.Ast.rules in
161 162
    let (filters, rules) = List.partition Rule.is_filter rules_or_filters in
    let modul =
163
      {
164 165
        name = ast_module.Ast.module_id;
        local_labels = locals;
166 167 168
        suffixes;
        rules;
        filters;
169 170 171
        confluent = ast_module.Ast.confluent;
        loc = ast_module.Ast.mod_loc;
      } in
172
    check modul; modul
173
end (* module Modul *)
174

175
(* ==================================================================================================== *)
176 177
module Sequence = struct
  type t = {
178 179 180 181
    name: string;
    def: string list;
    loc: Loc.t;
  }
182 183 184

  let check module_list t =
    List.iter
185
      (fun module_name ->
186
        if not (List.exists (fun modul -> modul.Modul.name = module_name) module_list)
bguillaum's avatar
Typos  
bguillaum committed
187
        then Error.build ~loc:t.loc "sequence \"%s\" refers to the unknown module \"%s\"."
188
          t.name module_name
189 190
      ) t.def

191 192
  let build module_list ast_sequence =
    let sequence =
193
      {
194 195 196 197
        name = ast_sequence.Ast.seq_name;
        def = ast_sequence.Ast.seq_mod;
        loc = ast_sequence.Ast.seq_loc;
      } in
198
    check module_list sequence; sequence
199
end (* module Sequence *)
pj2m's avatar
pj2m committed
200

201
(* ==================================================================================================== *)
pj2m's avatar
pj2m committed
202
module Grs = struct
203

pj2m's avatar
pj2m committed
204
  type t = {
205 206
    labels: Label.t list;        (* the list of global edge labels *)
    modules: Modul.t list;       (* the ordered list of modules used from rewriting *)
207
    sequences: Sequence.t list;
bguillaum's avatar
bguillaum committed
208 209
    filename: string;
    ast: Ast.grs;
210 211
  }

bguillaum's avatar
bguillaum committed
212
  let get_modules t = t.modules
bguillaum's avatar
bguillaum committed
213 214
  let get_ast t = t.ast
  let get_filename t = t.filename
bguillaum's avatar
bguillaum committed
215

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

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

220 221 222 223
  let check t =
    (* check for duplicate modules *)
    let rec loop already_defined = function
      | [] -> ()
224 225
      | m::_ when List.mem m.Modul.name already_defined ->
        Error.build ~loc:m.Modul.loc "Module '%s' is defined twice" m.Modul.name
226 227 228 229 230 231
      | m::tail -> loop (m.Modul.name :: already_defined) tail in
    loop [] t.modules;

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

bguillaum's avatar
bguillaum committed
237 238 239 240 241
  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
242
    let grs = {
bguillaum's avatar
bguillaum committed
243 244 245
      labels = List.map (fun (l,_) -> Label.from_string l) ast.Ast.labels;
      sequences = List.map (Sequence.build modules) ast.Ast.sequences;
      modules; ast; filename;
246 247
    } in
    check grs; grs
pj2m's avatar
pj2m committed
248

bguillaum's avatar
bguillaum committed
249
  let modules_of_sequence grs sequence =
250
    let module_names =
251
      try
252 253
        let seq = List.find (fun s -> s.Sequence.name = sequence) grs.sequences in
        seq.Sequence.def
bguillaum's avatar
bguillaum committed
254 255
      with Not_found -> [sequence] in (* a module name can be used as a singleton sequence *)

256 257 258
    List.map
      (fun name ->
        try List.find (fun m -> m.Modul.name=name) grs.modules
bguillaum's avatar
bguillaum committed
259 260 261 262
        with Not_found -> Log.fcritical "No sequence or module named '%s'" name
      )
      module_names

263
  let rewrite grs sequence instance =
bguillaum's avatar
bguillaum committed
264
    Timeout.start ();
bguillaum's avatar
bguillaum committed
265
    let modules_to_apply = modules_of_sequence grs sequence in
266

267 268 269
    let rec loop instance module_list =
      let instance = {instance with Instance.graph = G_graph.normalize instance.Instance.graph} in
      match module_list with
270 271 272 273 274 275 276 277 278
      | [] -> (* 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
279
            (Instance.flatten instance) in
280 281 282 283 284 285 286 287
        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
288
    loop instance modules_to_apply
289

bguillaum's avatar
bguillaum committed
290 291
  let build_rew_display grs sequence instance =
    let modules_to_apply = modules_of_sequence grs sequence in
pj2m's avatar
pj2m committed
292

293 294 295
    let rec loop instance module_list =
      let instance = {instance with Instance.graph = G_graph.normalize instance.Instance.graph} in
      match module_list with
bguillaum's avatar
bguillaum committed
296
      | [] -> Libgrew_types.Leaf instance.Instance.graph
297
      | next :: tail ->
298 299 300 301 302 303
        let (good_set, bad_set) =
          Rule.normalize
            next.Modul.name
            ~confluent: next.Modul.confluent
            next.Modul.rules
            next.Modul.filters
304
            (Instance.flatten instance) in
305
        let inst_list = Instance_set.elements good_set
306
              (* and bad_list = Instance_set.elements bad_set *) in
pj2m's avatar
pj2m committed
307

308
        match inst_list with
309
          | [{Instance.big_step = None}] ->
bguillaum's avatar
bguillaum committed
310 311
            Libgrew_types.Local_normal_form (instance.Instance.graph, next.Modul.name, loop instance tail)
          | _ -> Libgrew_types.Node
312 313 314 315 316 317 318 319 320 321
            (
              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
322
    in loop instance modules_to_apply
bguillaum's avatar
bguillaum committed
323

bguillaum's avatar
bguillaum committed
324 325 326
  let rule_iter fct grs =
    List.iter
      (fun modul ->
bguillaum's avatar
bguillaum committed
327 328 329 330 331 332 333
        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
334
      ) grs.modules
335
end (* module Grs *)