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

4
open Grew_fs
bguillaum's avatar
bguillaum committed
5 6
open Grew_utils
open Grew_ast
7
open Grew_edge
bguillaum's avatar
bguillaum committed
8 9 10
open Grew_command
open Grew_graph
open Grew_rule
pj2m's avatar
pj2m committed
11 12 13 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

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

50

51 52 53 54 55 56 57
  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
58 59 60 61 62 63 64
  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

65 66 67 68 69 70 71 72
  (* 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
73 74 75 76 77 78 79 80 81

  let save_det_conll base t =
    let rec loop t =
      match (t.good_nf, t.bad_nf) with
        | [],[] -> File.write (Instance.to_conll t.instance) (base^".conll")
        | [one], [] -> loop one
        | _ -> Error.run "Not a single rewriting"
    in loop t

bguillaum's avatar
bguillaum committed
82 83 84 85 86 87 88 89 90
  let det_dep_string t =
    let rec loop t =
      match (t.good_nf, t.bad_nf) with
        | [],[] ->
          let graph = t.instance.Instance.graph in
          G_graph.to_dep graph
        | [one], [] -> loop one
        | _ -> Error.run "Not a single rewriting"
    in loop t
pj2m's avatar
pj2m committed
91 92 93 94 95 96 97
end




module Modul = struct
  type t = {
98
    name: string;
99
    local_labels: (string * string list) array;
100 101 102 103 104
    rules: Rule.t list;
    filters: Rule.t list;
    confluent: bool;
    loc: Loc.t;
  }
pj2m's avatar
pj2m committed
105

106 107 108 109
  let check t =
    (* check for duplicate rules *)
    let rec loop already_defined = function
      | [] -> ()
110 111
      | 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)
112 113
      | r::tail -> loop ((Rule.get_name r) :: already_defined) tail in
    loop [] t.rules
pj2m's avatar
pj2m committed
114

115
  let build ast_module =
116 117
    let locals = Array.of_list ast_module.Ast.local_labels in
    Array.sort compare locals;
118
    let rules_or_filters = List.map (Rule.build ~locals ast_module.Ast.mod_dir) ast_module.Ast.rules in
119 120
    let (filters, rules) = List.partition Rule.is_filter rules_or_filters in
    let modul =
121
      {
122 123 124 125 126 127 128
        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
129 130 131 132 133
    check modul; modul
end

module Sequence = struct
  type t = {
134 135 136 137
    name: string;
    def: string list;
    loc: Loc.t;
  }
138 139 140

  let check module_list t =
    List.iter
141
      (fun module_name ->
142 143
        if not (List.exists (fun modul -> modul.Modul.name = module_name) module_list)
        then Error.build ~loc:t.loc "sequence \"%s\", refers to the unknown module \"%s\"."
144
          t.name module_name
145 146
      ) t.def

147 148
  let build module_list ast_sequence =
    let sequence =
149
      {
150 151 152 153
        name = ast_sequence.Ast.seq_name;
        def = ast_sequence.Ast.seq_mod;
        loc = ast_sequence.Ast.seq_loc;
      } in
154
    check module_list sequence; sequence
pj2m's avatar
pj2m committed
155 156 157
end

module Grs = struct
158

pj2m's avatar
pj2m committed
159
  type t = {
160 161 162 163 164
    labels: Label.t list;    (* the list of global edge labels *)
    modules: Modul.t list;          (* the ordered list of modules used from rewriting *)
    sequences: Sequence.t list;
  }

bguillaum's avatar
bguillaum committed
165 166
  let get_modules t = t.modules

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

pj2m's avatar
pj2m committed
169 170
  let empty = {labels=[]; modules=[]; sequences=[];}

171 172 173 174
  let check t =
    (* check for duplicate modules *)
    let rec loop already_defined = function
      | [] -> ()
175 176
      | m::_ when List.mem m.Modul.name already_defined ->
        Error.build ~loc:m.Modul.loc "Module '%s' is defined twice" m.Modul.name
177 178 179 180 181 182
      | m::tail -> loop (m.Modul.name :: already_defined) tail in
    loop [] t.modules;

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

pj2m's avatar
pj2m committed
188
  let build ast_grs =
189
    Label.init ast_grs.Ast.labels;
190 191
    Domain.init ast_grs.Ast.domain;
    let modules = List.map Modul.build ast_grs.Ast.modules in
192 193 194 195 196 197
    let grs = {
      labels = List.map (fun (l,_) -> Label.from_string l) ast_grs.Ast.labels;
      modules = modules;
      sequences = List.map (Sequence.build modules) ast_grs.Ast.sequences;
    } in
    check grs; grs
pj2m's avatar
pj2m committed
198

bguillaum's avatar
bguillaum committed
199
  let modules_of_sequence grs sequence =
200
    let module_names =
201
      try
202 203
        let seq = List.find (fun s -> s.Sequence.name = sequence) grs.sequences in
        seq.Sequence.def
bguillaum's avatar
bguillaum committed
204 205
      with Not_found -> [sequence] in (* a module name can be used as a singleton sequence *)

206 207 208
    List.map
      (fun name ->
        try List.find (fun m -> m.Modul.name=name) grs.modules
bguillaum's avatar
bguillaum committed
209 210 211 212
        with Not_found -> Log.fcritical "No sequence or module named '%s'" name
      )
      module_names

213
  let rewrite grs sequence instance =
bguillaum's avatar
bguillaum committed
214
    Timeout.start ();
bguillaum's avatar
bguillaum committed
215
    let modules_to_apply = modules_of_sequence grs sequence in
216

pj2m's avatar
pj2m committed
217
    let rec loop instance = function
218 219 220 221 222 223 224 225 226
      | [] -> (* 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
227
            (Instance.flatten instance) in
228 229 230 231 232 233 234 235
        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
236
    loop instance modules_to_apply
237

bguillaum's avatar
bguillaum committed
238 239
  let build_rew_display grs sequence instance =
    let modules_to_apply = modules_of_sequence grs sequence in
pj2m's avatar
pj2m committed
240 241 242

    let rec loop instance = function
      | [] -> Grew_types.Leaf instance.Instance.graph
243
      | next :: tail ->
244 245 246 247 248 249
        let (good_set, bad_set) =
          Rule.normalize
            next.Modul.name
            ~confluent: next.Modul.confluent
            next.Modul.rules
            next.Modul.filters
250
            (Instance.flatten instance) in
251
        let inst_list = Instance_set.elements good_set
252
              (* and bad_list = Instance_set.elements bad_set *) in
pj2m's avatar
pj2m committed
253

254
        match inst_list with
255
          | [{Instance.big_step = None}] ->
256
            Grew_types.Local_normal_form (instance.Instance.graph, next.Modul.name, loop instance tail)
257
          | _ -> Grew_types.Node
258 259 260 261 262 263 264 265 266 267
            (
              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
268
    in loop instance modules_to_apply
bguillaum's avatar
bguillaum committed
269

bguillaum's avatar
bguillaum committed
270 271 272
  let rule_iter fct grs =
    List.iter
      (fun modul ->
bguillaum's avatar
bguillaum committed
273 274 275 276 277 278 279
        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
280
      ) grs.modules
281
end