simplify_recursive_definition.ml 5.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
(**************************************************************************)
(*                                                                        *)
(*  Copyright (C) 2010-                                                   *)
(*    Francois Bobot                                                      *)
(*    Jean-Christophe Filliatre                                           *)
(*    Johannes Kanig                                                      *)
(*    Andrei Paskevich                                                    *)
(*                                                                        *)
(*  This software is free software; you can redistribute it and/or        *)
(*  modify it under the terms of the GNU Library General Public           *)
(*  License version 2.1, with the special exception on linking            *)
(*  described in file LICENSE.                                            *)
(*                                                                        *)
(*  This software is distributed in the hope that it will be useful,      *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *)
(*                                                                        *)
(**************************************************************************)

20
open Util
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
open Ident
open Ty
open Term
open Theory


type seen =
  | SNot
  | SOnce
  | SBack

let rec find h e = 
  try
    let r = Hid.find h e in
    if r == e then e 
    else 
      let r = find h r in
      Hid.replace h e r;
      r
  with Not_found -> e

let union h e1 e2 = Hid.replace h (find h e1) (find h e2)

let connexe (m:Sid.t Mid.t)  = 
  let uf = Hid.create 32 in
  let visited = Hid.create 32 in
  Mid.iter (fun e _ -> Hid.replace visited e SNot) m;
  let rec visit i last = 
    match Hid.find visited i with
      | SNot -> 
          Hid.replace visited i SOnce;
          let s = Mid.find i m in
          let last = i::last in
          Sid.iter (fun x -> visit x last) s;
          Hid.replace visited i SBack
      | SOnce ->  
          (try 
             List.iter (fun e -> if e==i then raise Exit else union uf i e) last
           with Exit -> ())
      | SBack -> ()
  in
  Mid.iter (fun e _ -> visit e []) m;
  let ce = Hid.create 32 in
  Mid.iter (fun e es -> 
              let r = find uf e in
              let rl,rs,rb = try
                Hid.find ce r
              with Not_found -> [],Sid.empty, ref false in
              Hid.replace ce r (e::rl,Sid.union rs es,rb)) m;
  let rec visit (l,s,b) acc =
    if !b then acc
    else
      begin
        b := true;
        let acc = Sid.fold (fun e acc -> 
                              try 
                                visit (Hid.find ce e) acc
                              with Not_found -> acc) s acc in
        l::acc
      end
  in
Francois Bobot's avatar
Francois Bobot committed
82
  List.rev (Hid.fold (fun _ -> visit) ce [])
83 84 85 86 87 88 89 90



let elt d = 
  match d.d_node with
    | Dlogic l -> 
        let mem = Hid.create 16 in
        List.iter (function
91
                     | Lfunction  (fs,_) as a -> Hid.add mem fs.ls_name a
92
                     | Lpredicate (ps,_) as a -> Hid.add mem ps.ls_name a) l;
Francois Bobot's avatar
Francois Bobot committed
93
        let tyoccurences acc _ = acc in
94 95
        let loccurences acc ls = 
          if Hid.mem mem ls.ls_name then Sid.add ls.ls_name acc else acc in
96 97 98 99 100
        let m = List.fold_left 
          (fun acc a -> match a with 
             | Lfunction (fs,l) -> 
                let s = match l with
                  | None -> Sid.empty
101
                  | Some fd -> 
102
                      let fd = fs_defn_axiom fd in
103 104
                      f_s_fold tyoccurences loccurences Sid.empty fd in
                Mid.add fs.ls_name s acc
105 106 107
             | Lpredicate (ps,l) -> 
                let s = match l with
                  | None -> Sid.empty
108
                  | Some fd -> 
109
                      let fd = ps_defn_axiom fd in
110 111
                      f_s_fold tyoccurences loccurences Sid.empty fd in
                Mid.add ps.ls_name s acc) Mid.empty l in
112
          let l = connexe m in
113
          List.map (fun e -> create_logic_decl (List.map (Hid.find mem) e)) l
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    | Dtype l -> 
        let mem = Hid.create 16 in
        List.iter (fun ((ts,_) as a) -> Hid.add mem ts.ts_name a) l;
        let tyoccurences acc t = 
          match t.ty_node with
            | Tyapp (ts,_) when Hid.mem mem ts.ts_name ->
                Sid.add ts.ts_name acc
            | _ -> acc in
        let m = List.fold_left 
          (fun acc (ts,def) -> 
             let s = match def with
               | Tabstract -> 
                   begin match ts.ts_def with
                     | None -> Sid.empty
                     | Some ty -> ty_fold tyoccurences Sid.empty ty
                   end
               | Talgebraic l -> 
                   List.fold_left 
132 133
                     (fun acc {ls_args = tyl; ls_value = ty} ->
                        let ty = of_option ty in
134 135 136 137 138
                        List.fold_left 
                          (fun acc ty-> ty_fold tyoccurences acc ty) acc (ty::tyl)
                     ) Sid.empty l in
             Mid.add ts.ts_name s acc) Mid.empty l in
        let l = connexe m in
139 140 141
        List.map (fun e -> create_ty_decl (List.map (Hid.find mem) e)) l
    | Dind _ -> [d] (* TODO *)
    | Dprop _ | Dclone _ | Duse _ -> [d]
142

143
let t env = Transform.elt (fun _ -> elt) env