libencoding.ml 10.2 KB
Newer Older
1 2
(**************************************************************************)
(*                                                                        *)
MARCHE Claude's avatar
MARCHE Claude committed
3
(*  Copyright (C) 2010-2012                                               *)
4 5 6
(*    François Bobot                                                      *)
(*    Jean-Christophe Filliâtre                                           *)
(*    Claude Marché                                                       *)
MARCHE Claude's avatar
MARCHE Claude committed
7
(*    Guillaume Melquiond                                                 *)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
(*    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.                  *)
(*                                                                        *)
(**************************************************************************)

open Util
open Ident
open Ty
open Term
open Decl
26 27
open Theory

28
let debug = Debug.register_info_flag "encoding"
29
  ~desc:"About the encoding of polymorphism."
30 31 32

(* meta to tag the protected types *)
let meta_kept = register_meta "encoding : kept" [MTty]
33
  ~desc:"Specify a type to keep during the encoding of polymorphism."
34

35 36 37 38 39 40 41
(* sort symbol of the "universal" type *)
let ts_base = create_tysymbol (id_fresh "uni") [] None

(* "universal" sort *)
let ty_base = ty_app ts_base []

(* ts_base declaration *)
42
let d_ts_base = create_ty_decl ts_base
43

44 45 46 47 48 49 50
(* sort symbol of (polymorphic) types *)
let ts_type = create_tysymbol (id_fresh "ty") [] None

(* sort of (polymorphic) types *)
let ty_type = ty_app ts_type []

(* ts_type declaration *)
51
let d_ts_type = create_ty_decl ts_type
52 53 54 55 56 57

(* function symbol mapping ty_type^n to ty_type *)
let ls_of_ts = Wts.memoize 63 (fun ts ->
  let args = List.map (const ty_type) ts.ts_args in
  create_fsymbol (id_clone ts.ts_name) args ty_type)

58 59 60 61 62 63 64
(* function symbol selecting ty_type from ty_type^n *)
let ls_selects_of_ts = Wts.memoize 63 (fun ts ->
  let create_select _ =
    let preid = id_fresh ("select_"^ts.ts_name.id_string) in
    create_fsymbol preid [ty_type] ty_type in
  List.rev_map create_select ts.ts_args)

65 66
let ls_int_of_ty = create_fsymbol (id_fresh "int_of_ty") [ty_type] ty_int

67 68 69 70
(** definition of the previous selecting functions *)
let ls_selects_def_of_ts acc ts =
  let ls = ls_of_ts ts in
  let vars = List.rev_map
71
    (fun _ -> create_vsymbol (id_fresh "x") ty_type) ts.ts_args
72 73
  in
  let tvars = List.map t_var vars in
74 75 76
  (** type to int *)
  let id = string_of_int (id_hash ts.ts_name) in
  let acc =
77
    let t = fs_app ls tvars ty_type in
78 79
    let f = t_equ (fs_app ls_int_of_ty [t] ty_int) (t_int_const id) in
    let f = t_forall_close vars [[t]] f in
80 81 82 83 84
    let prsymbol = create_prsymbol (id_clone ts.ts_name) in
    create_prop_decl Paxiom prsymbol f :: acc
  in
  (** select *)
  let ls_selects = ls_selects_of_ts ts in
85 86
  let fmlas = List.rev_map2
    (fun ls_select value ->
87 88
      let t = fs_app ls tvars ty_type in
      let t = fs_app ls_select [t] ty_type in
89 90
      let f = t_equ t value in
      let f = t_forall_close vars [[t]] f in
91 92 93 94 95 96 97 98
      f)
    ls_selects tvars in
  let create_props ls_select fmla =
    let prsymbol = create_prsymbol (id_clone ls_select.ls_name) in
    create_prop_decl Paxiom prsymbol fmla in
  let props =
    List.fold_left2 (fun acc x y -> create_props x y::acc)
      acc ls_selects fmlas in
99
  let add acc fs = create_param_decl fs :: acc in
100 101
  List.fold_left add props ls_selects

102 103 104
(* convert a type to a term of type ty_type *)
let rec term_of_ty tvmap ty = match ty.ty_node with
  | Tyvar tv ->
105
      Mtv.find tv tvmap
106
  | Tyapp (ts,tl) ->
107
      fs_app (ls_of_ts ts) (List.map (term_of_ty tvmap) tl) ty_type
108

109
(* rewrite a closed formula modulo its free typevars *)
François Bobot's avatar
François Bobot committed
110
let type_close tvs fn f =
111
  let get_vs tv = create_vsymbol (id_clone tv.tv_name) ty_type in
112 113 114
  let tvm = Mtv.mapi (fun v () -> get_vs v) tvs in
  let vl = Mtv.values tvm in
  let tvm = Mtv.map t_var tvm in
115
  t_forall_close_simp vl [] (fn tvm f)
116

117
let t_type_close fn f =
118
  let tvs = t_ty_freevars Stv.empty f in
François Bobot's avatar
François Bobot committed
119 120
  type_close tvs fn f

121
(* convert a type declaration to a list of lsymbol declarations *)
122
let lsdecl_of_ts ts = create_param_decl (ls_of_ts ts)
123 124

(* convert a type declaration to a list of lsymbol declarations *)
125 126 127
let lsdecl_of_ts_select ts =
  let defs = ls_selects_def_of_ts [] ts in
  create_param_decl (ls_of_ts ts) :: defs
128 129

(* convert a constant to a functional symbol of type ty_base *)
130
let ls_of_const =
131
  let ht = Hterm.create 63 in
132
  fun t -> match t.t_node with
133 134
    | Tconst _ ->
        begin try Hterm.find ht t with Not_found ->
135
          let s = "const_" ^ Pp.string_of_wnl Pretty.print_term t in
136
          let ls = create_fsymbol (id_fresh s) [] ty_base in
137 138
          Hterm.add ht t ls;
          ls
139 140 141
        end
    | _ -> assert false

142 143
(* unprotected and unprotecting idents *)

Andrei Paskevich's avatar
Andrei Paskevich committed
144 145
let unprotected_label = Ident.create_label "encoding : unprotected"
let unprotecting_label = Ident.create_label "encoding : unprotecting"
146

147 148
let id_unprotected n = id_fresh ~label:(Slab.singleton unprotected_label) n
let id_unprotecting n = id_fresh ~label:(Slab.singleton unprotecting_label) n
149

150 151
let is_protected_id id = not (Slab.mem unprotected_label id.id_label)
let is_protecting_id id = not (Slab.mem unprotecting_label id.id_label)
152 153 154 155 156 157 158

let is_protected_vs kept vs =
  is_protected_id vs.vs_name && Sty.mem vs.vs_ty kept

let is_protected_ls kept ls =
  is_protected_id ls.ls_name && Sty.mem (of_option ls.ls_value) kept

159 160
(* monomorphise modulo the set of kept types * and an lsymbol map *)

161
let vs_monomorph kept vs =
162
  if is_protected_vs kept vs then vs else
163
  create_vsymbol (id_clone vs.vs_name) ty_base
164

165 166
let rec t_monomorph kept lsmap consts vmap t =
  let t_mono = t_monomorph kept lsmap consts in
167 168 169
  t_label_copy t (match t.t_node with
    | Tvar v ->
        Mvs.find v vmap
170
    | Tconst _ when Sty.mem (t_type t) kept ->
171
        t
172
    | Tconst _ ->
173
        let ls = ls_of_const t in
174
        consts := Sls.add ls !consts;
175
        fs_app ls [] ty_base
176 177 178 179 180
    | Tapp (ps,[t1;t2]) when ls_equal ps ps_equ ->
        t_equ (t_mono vmap t1) (t_mono vmap t2)
    | Tapp (ls,tl) ->
        let ls = lsmap ls in
        t_app ls (List.map (t_mono vmap) tl) ls.ls_value
181
    | Tif (f,t1,t2) ->
182
        t_if (t_mono vmap f) (t_mono vmap t1) (t_mono vmap t2)
183 184
    | Tlet (t1,b) ->
        let u,t2,close = t_open_bound_cb b in
185
        let v = vs_monomorph kept u in
186 187 188 189 190
        let t2 = t_mono (Mvs.add u (t_var v) vmap) t2 in
        t_let (t_mono vmap t1) (close v t2)
    | Tcase _ ->
        Printer.unsupportedTerm t "no match expressions at this point"
    | Teps b ->
191
        let u,f,close = t_open_bound_cb b in
192
        let v = vs_monomorph kept u in
193
        let f = t_mono (Mvs.add u (t_var v) vmap) f in
194
        t_eps (close v f)
195 196
    | Tquant (q,b) ->
        let ul,tl,f1,close = t_open_quant_cb b in
197
        let vl = List.map (vs_monomorph kept) ul in
198 199
        let add acc u v = Mvs.add u (t_var v) acc in
        let vmap = List.fold_left2 add vmap ul vl in
200 201
        let tl = tr_map (t_mono vmap) tl in
        t_quant q (close vl tl (t_mono vmap f1))
202
    | Tbinop (op,f1,f2) ->
203
        t_binary op (t_mono vmap f1) (t_mono vmap f2)
204
    | Tnot f1 ->
205
        t_not (t_mono vmap f1)
206
    | Ttrue | Tfalse ->
207
        t)
208

209
let d_monomorph kept lsmap d =
210
  let consts = ref Sls.empty in
211 212
  let kept = Sty.add ty_base kept in
  let t_mono = t_monomorph kept lsmap consts in
213
  let dl = match d.d_node with
214 215 216 217 218 219 220 221 222
    | Dtype { ts_def = Some _ } -> []
    | Dtype ts when not (Sty.exists (ty_s_any (ts_equal ts)) kept) -> []
    | Dtype ts ->
        [create_ty_decl ts]
    | Ddata _ ->
        Printer.unsupportedDecl d "no algebraic types at this point"
    | Dparam ls ->
        let ls = if ls_equal ls ps_equ then ls else lsmap ls in
        [create_param_decl ls]
223 224
    | Dlogic ldl ->
        let conv (ls,ld) =
225 226 227 228 229 230
          let ls = if ls_equal ls ps_equ then ls else lsmap ls in
          let ul,e,close = open_ls_defn_cb ld in
          let vl = List.map (vs_monomorph kept) ul in
          let add acc u v = Mvs.add u (t_var v) acc in
          let vmap = List.fold_left2 add Mvs.empty ul vl in
          close ls vl (t_mono vmap e)
231 232
        in
        [create_logic_decl (List.map conv ldl)]
233
    | Dind (s, idl) ->
234
        let iconv (pr,f) = pr, t_mono Mvs.empty f in
235
        let conv (ls,il) = lsmap ls, List.map iconv il in
236
        [create_ind_decl s (List.map conv idl)]
237
    | Dprop (k,pr,f) ->
238
        [create_prop_decl k pr (t_mono Mvs.empty f)]
239
  in
240
  let add ls acc = create_param_decl ls :: acc in
241
  Sls.fold add !consts dl
242

243 244 245 246 247 248 249 250 251
(* replace type variables in a goal with fresh type constants *)
let monomorphise_goal = Trans.goal (fun pr f ->
  let stv = t_ty_freevars Stv.empty f in
  if Stv.is_empty stv then [create_prop_decl Pgoal pr f] else
  let mty,ltv = Stv.fold (fun tv (mty,ltv) ->
    let ts = create_tysymbol (id_clone tv.tv_name) [] None in
    Mtv.add tv (ty_app ts []) mty, ts::ltv) stv (Mtv.empty,[]) in
  let f = t_ty_subst mty Mvs.empty f in
  List.fold_left
252
    (fun acc ts -> create_ty_decl ts :: acc)
253 254 255
    [create_prop_decl Pgoal pr f] ltv)

(* close by subtype the set of types tagged by meta_kept *)
256
let close_kept =
257 258 259
  Trans.on_tagged_ty meta_kept (fun kept ->
    let rec add acc ty = ty_fold add (Sty.add ty acc) ty in
    let kept' = Sty.fold (Util.flip add) kept kept in
260 261 262
    if kept == kept' then Trans.identity
    else
      let kept' = Sty.diff kept' kept in
263
      let fold ty acc = create_meta meta_kept [MAty ty] :: acc in
264
      Trans.add_tdecls (Sty.fold fold kept' []))
265

266
(* reconstruct a definition of an lsymbol or make a defining axiom *)
267 268 269 270 271 272
let defn_or_axiom ls f =
  match Decl.ls_defn_of_axiom f with
    | Some ld -> [create_logic_decl [ld]]
    | None ->
        let nm = ls.ls_name.id_string ^ "_def" in
        let pr = create_prsymbol (id_derive nm ls.ls_name) in
273
        [create_param_decl ls; create_prop_decl Paxiom pr f]