characteristic.ml 57.3 KB
Newer Older
charguer's avatar
init  
charguer committed
1 2 3 4 5 6 7 8 9 10 11
open Misc
open Asttypes
open Types
open Typedtree
open Mytools
open Longident
open Print_tast
open Print_type
open Formula
open Coq
open Path
charguer's avatar
charguer committed
12
open Renaming
charguer's avatar
init  
charguer committed
13 14 15 16 17
open Printf

(*#########################################################################*)
(* ** Switch for generating formulae for purely-functional programs *)

charguer's avatar
credits  
charguer committed
18 19 20
let use_credits = ref false


charguer's avatar
init  
charguer committed
21 22 23 24 25 26 27 28 29
(*#########################################################################*)
(* ** Error messages *)

exception Not_in_normal_form of string

let not_in_normal_form s =
   raise (Not_in_normal_form s)


charguer's avatar
charguer committed
30 31 32 33 34 35
(*#########################################################################*)
(* ** List of external modules that need to be required *)

let external_modules = ref []

let external_modules_add name =
36 37 38
   if not (List.mem name !external_modules) 
     then external_modules := name::!external_modules
   (* TODO: use a structure more efficient than lists *)
charguer's avatar
charguer committed
39 40

let external_modules_get_coqtop () = 
charguer's avatar
charguer committed
41
   List.map (fun name -> Coqtop_require [name]) !external_modules
charguer's avatar
charguer committed
42 43 44 45 46

let external_modules_reset () = 
   external_modules := []


charguer's avatar
charguer committed
47

charguer's avatar
init  
charguer committed
48 49 50
(*#########################################################################*)
(* ** Lifting of paths *)

charguer's avatar
charguer committed
51 52 53
(* Take a module name and add "_ml" suffix to it;
   Moreover, insert a "Require" directive in case the module 
   corresponds to a file (i.e. a compilation unit. *)
charguer's avatar
charguer committed
54

charguer's avatar
charguer committed
55
let lift_module_name id =
charguer's avatar
init  
charguer committed
56
   let name = Ident.name id in
charguer's avatar
charguer committed
57
   let coqname = module_name name in
charguer's avatar
charguer committed
58 59
   if Ident.persistent id then external_modules_add coqname;
   coqname
charguer's avatar
charguer committed
60

charguer's avatar
charguer committed
61
   (* -- old:
charguer's avatar
charguer committed
62
     if Ident.persistent id  
charguer's avatar
charguer committed
63
      then (let result = name ^ "_ml" in external_modules_add result; result)
charguer's avatar
init  
charguer committed
64
      else "ML" ^ name
charguer's avatar
charguer committed
65 66
   *)
   (* -- old: if name = "OkaStream" then "CFPrim" else  *)
charguer's avatar
init  
charguer committed
67

charguer's avatar
charguer committed
68 69 70 71
(* Function for adding "_ml" to the modules in a path, 
   including the last constant which is assumed to a module name *)
   (* TODO: rename this function *)

charguer's avatar
init  
charguer committed
72
let rec lift_full_path = function
charguer's avatar
charguer committed
73
  | Pident id -> Pident (Ident.create (lift_module_name id))
charguer's avatar
charguer committed
74
  | Pdot(p, s, pos) -> Pdot(lift_full_path p, (module_name s), pos) 
charguer's avatar
init  
charguer committed
75 76
  | Papply(p1, p2) -> assert false 

charguer's avatar
charguer committed
77 78 79
(* Function for adding "_ml" to the modules in a path, 
   but not changing the last constant in the path *)

charguer's avatar
init  
charguer committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
let lift_path = function
  | Pident id -> Pident id
  | Pdot(p, s, pos) -> Pdot(lift_full_path p, s, pos) 
  | Papply(p1, p2) -> assert false 

(** Translates a path into a string. A module called "Test" 
    becomes "Test_ml" if it refers to a file, and it becomes
    "MLTest" if it refers to a functor argument. 
    --todo: call them all "Test_ml"? *)

let lift_full_path_name p =
  Path.name (lift_full_path p)

(** Translates a path into a string --todo: why not full? *)

let lift_path_name p =
  Path.name (lift_path p)


(*#########################################################################*)
(* ** Lifting of types *)

(** Computes the free variables of a [btyp] *)

let rec fv_btyp ?(through_arrow = true) t =
   let aux = fv_btyp in
   match t with
   | Btyp_val -> []
   | Btyp_arrow (t1,t2) -> if through_arrow then aux t1 @ aux t2 else []
   | Btyp_constr (id,ts) -> list_concat_map aux ts
   | Btyp_tuple ts -> list_concat_map aux ts
   | Btyp_var (b,s) -> [s]
   | Btyp_poly (ss,t) -> unsupported "poly-types"
   | Btyp_alias (t,s) -> s :: aux t 

(** Translates a [btyp] into a Coq type *)

let rec lift_btyp t =
   let aux = lift_btyp in
   match t with
   | Btyp_val -> 
      val_type
   | Btyp_arrow (t1,t2) -> 
      val_type
charguer's avatar
charguer committed
124 125
   | Btyp_constr (id,[t]) when Path.name id = "array" -> 
      (* || Path.name id = "Pervasives.array" *)
charguer's avatar
init  
charguer committed
126 127
      loc_type
   | Btyp_constr (id,ts) -> 
charguer's avatar
charguer committed
128
      coq_apps (Coq_var (type_constr_name (lift_path_name id))) (List.map aux ts)
charguer's avatar
init  
charguer committed
129 130 131 132 133 134 135 136 137 138 139 140 141
   | Btyp_tuple ts -> 
      coq_prod (List.map aux ts)
   | Btyp_var (b,s) -> 
      if b then unsupported "non-generalizable free type variables (of the form '_a); please add a type annotation";
      Coq_var s
   | Btyp_poly (ss,t) -> 
      unsupported "poly-types"
   | Btyp_alias (t1,s) -> 
      let occ = fv_btyp ~through_arrow:false t1 in
      if List.mem s occ 
        then unsupported ("found a recursive type that is not erased by an arrow:" ^ (print_out_type t));
      aux t1 

charguer's avatar
charguer committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
(* TEMPORARILY DEPRECATED

   | Btyp_constr (id,[t]) when Path.name id = "ref" || Path.name id = "Pervasives.ref" ->
      loc_type

   | Btyp_constr (id,[t]) when Path.name id = "heap" || Path.name id = "Heap.heap" ->
      loc_type

   | Btyp_constr (id,[t]) when Path.same id Predef.path_lazy_t || Path.name id = "Lazy.t" -> 
      aux t  
   | Btyp_constr (id,[t]) when Path.name id = "Stream.stream" || Path.name id = "stream" -> 
      Coq_app (Coq_var "list", aux t)
   | Btyp_constr (id,[t]) when Path.name id = "Stream.stream_cell" || Path.name id = "stream_cell" -> 
      Coq_app (Coq_var "list", aux t)
*)
(* REMARK: les Lazy provenant des patterns ne sont pas identique Predef.path_lazy_t *)


charguer's avatar
init  
charguer committed
160 161 162 163 164 165 166 167
(** Translates a Caml type into a Coq type *)

let lift_typ_exp ty =
  lift_btyp (btyp_of_typ_exp ty)  

(** Translates a Caml type scheme into a Coq type *)

let lift_typ_sch ty =
charguer's avatar
charguer committed
168 169
   let t = btyp_of_typ_sch_simple ty in
   let fv = fv_btyp ~through_arrow:false t in
charguer's avatar
init  
charguer committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
   fv, lift_btyp t

(** Translates the type of a Caml expression into a Coq type *)

let coq_typ e =
   lift_typ_exp (e.exp_type)

(** Translates the type of a Caml pattern into a Coq type *)

let coq_typ_pat p =
   lift_typ_exp (p.pat_type)

(** Decompose "A.B.s" as ("A.B","s") *)

let rec path_decompose = function
    Pident id -> ("", Ident.name id)
  | Pdot(p, s, pos) -> 
      let (f,r) = path_decompose p in
      (f ^ r ^ ".", s)
  | Papply(p1, p2) -> unsupported "application in paths"


(** Extracts a record path_name / path from a type *)

let get_record_decomposed_name_for_exp e = 
   let b = btyp_of_typ_exp (e.exp_type) in   
   match b with 
   | Btyp_constr (p,_) -> path_decompose (lift_path p)
   | _ -> failwith "illegal argument for get_record_decomposed_name_for_exp"



(*#########################################################################*)
(* ** Type arity functions *)

(** Get the number of type arguments of a (polymorphic) free variable *)

let typ_arity_var env x =
   match x with
   | Path.Pident id -> 
      begin try Ident.find_same id env
      with Not_found -> 0 end
   | _ -> 0

(** Get the number of type arguments of a (polymorphic) data constructor *)

let typ_arity_constr c =
   match (c.cstr_res).desc with
   | Tconstr (_,ts,_) -> List.length ts
   | _ -> failwith "typ_arity_constr: result type of constructor is not a type constructor"

(** Translate a Caml data constructor into a Coq expression *)

let coq_of_constructor p c =
   let x = string_of_path p in
   match find_builtin_constructor x with
   | None -> coq_app_var_wilds x (typ_arity_constr c) 
charguer's avatar
encours  
charguer committed
227
   | Some (y,arity) -> coq_app_var_wilds y arity
charguer's avatar
init  
charguer committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311


(*#########################################################################*)
(* ** Lifting of patterns *)

(** Compute the free variables of a pattern *)

let rec pattern_variables p : typed_vars = (* ignores aliases *)
   let aux = pattern_variables in
   match p.pat_desc with
   | Tpat_any -> not_in_normal_form "wildcard patterns remain after normalization"
   | Tpat_var s -> [Ident.name s, coq_typ_pat p]
   | Tpat_alias (p, s) -> aux p
   | Tpat_constant c -> []
   | Tpat_tuple l -> list_concat_map aux l
   | Tpat_construct (p, c, ps) -> list_concat_map aux ps
   | Tpat_lazy p1 -> aux p1
   | Tpat_variant (_,_,_) -> unsupported "variant patterns"
   | Tpat_record _ -> unsupported "record patterns"
   | Tpat_array pats -> unsupported "array patterns"
   | Tpat_or (_,p1,p2) -> unsupported "or patterns"

(** Translate a Caml pattern into a Coq expression, and
    ignores the aliases found in the pattern *)

let rec lift_pat ?(through_aliases=true) p : coq = 
   let aux = lift_pat ~through_aliases:through_aliases in
   match p.pat_desc with
   | Tpat_var s -> 
      Coq_var (Ident.name s)
   | Tpat_constant (Const_int n) -> 
      Coq_int n
   | Tpat_tuple l -> 
      Coq_tuple (List.map aux l)
   | Tpat_construct (p, c, ps) -> 
      coq_apps (coq_of_constructor p c) (List.map aux ps)
   | Tpat_alias (p, ak) -> 
      begin match ak with
      | TPat_alias id -> 
          if through_aliases then aux p else Coq_var (Ident.name id)
      | TPat_constraint _ -> aux p
      | TPat_type pp -> aux p 
      end
   | Tpat_lazy p1 ->
      aux p1
   | Tpat_record _ -> unsupported "record patterns" (* todo! *)
   | Tpat_array pats -> unsupported "array patterns" (* todo! *)
   | Tpat_constant _ -> unsupported "only integer constant are supported"
   | Tpat_any -> not_in_normal_form "wildcard patterns remain after normalization"
   | Tpat_variant (_,_,_) -> unsupported "variant patterns"
   | Tpat_or (_,p1,p2) -> unsupported "or patterns in depth"

(** Extracts the aliases from a Caml pattern, in the form of an
    association list mapping variables to Coq expressions *)

let pattern_aliases p : (typed_var*coq) list = 
   let rec aux p =
      match p.pat_desc with
      | Tpat_var s -> []
      | Tpat_constant (Const_int n) -> []
      | Tpat_tuple l -> list_concat_map aux l
      | Tpat_construct (p, c, ps) -> list_concat_map aux ps
      | Tpat_alias (p1, ak) -> 
          begin match ak with
          | TPat_alias id ->
             ((Ident.name id, coq_typ_pat p), lift_pat ~through_aliases:false p1) :: (aux p1)
          | TPat_constraint _ -> aux p1
          | TPat_type pp -> aux p1
         end
      | Tpat_lazy p1 ->  aux p1
      | Tpat_record _ -> unsupported "record patterns" (* todo! *)
      | Tpat_array pats -> unsupported "array patterns" (* todo! *)
      | Tpat_constant _ -> unsupported "only integer constant are supported"
      | Tpat_any -> not_in_normal_form "wildcard patterns remain after normalization"
      | Tpat_variant (_,_,_) -> unsupported "variant patterns"
      | Tpat_or (_,p1,p2) -> unsupported "or patterns"   
      in
   List.rev (aux p)


(*#########################################################################*)
(* ** Helper functions for primitive functions *)

let register_cf x =
charguer's avatar
charguer committed
312
   Coqtop_register ("database_cf", x, cf_axiom_name x)
charguer's avatar
init  
charguer committed
313 314 315 316

let register_spec x v =
   Coqtop_register ("database_spec", x, v)

charguer's avatar
charguer committed
317 318

(* TODO: rewrite this function by using a normalization functiont that returns p *)
charguer's avatar
init  
charguer committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
let rec prefix_for_label typ = 
  match typ.desc with  
  | Tconstr (p, _, _) -> lift_path_name p 
  | Tlink t -> prefix_for_label t
  | _ -> failwith "string_of_label: type of a record should be a Tconstr or Tlink"
  (*
  | Tvar -> failwith "x1"
  | Tarrow _ -> failwith "x2"
  | Ttuple  _ -> failwith "x3"
  | Tconstr _ -> failwith "x4"
  | Tobject  _ -> failwith "x5"
  | Tfield _ -> failwith "x6"
  | Tnil _ -> failwith "x7"
  | Tsubst  _ -> failwith "x9"
  | Tvariant  _ -> failwith "x10"
  | Tunivar -> failwith "x11"
  | Tpoly  _ -> failwith "x12"
  *)

charguer's avatar
charguer committed
338
(* DEPRECATED 
charguer's avatar
init  
charguer committed
339 340 341 342 343
let string_of_label_with prefix lbl =
  prefix ^ "_" ^ lbl.lbl_name

let string_of_label typ lbl =
  string_of_label_with (prefix_for_label typ) lbl
charguer's avatar
charguer committed
344
*)
charguer's avatar
init  
charguer committed
345 346 347 348 349 350 351 352 353 354 355

let simplify_apply_args oargs =
  List.map (function (lab, Some e, Required) -> e | _ -> unsupported "optional arguments") oargs 

let exp_find_inlined_primitive e oargs =
   let args = simplify_apply_args oargs in
    match e.exp_desc, args with 
    | Texp_ident (f,d), [n; {exp_desc = Texp_constant (Const_int m)}] 
        when m > 0 && let x = Path.name f in x = "Pervasives.mod" || x = "Pervasives./" -> 
        find_inlined_primitive (Path.name f) primitive_special
    | Texp_ident (f,d), _ -> 
charguer's avatar
charguer committed
356 357 358 359
        let r = find_inlined_primitive (Path.name f) (List.length args) in
        (* debug: Printf.printf "exp_find_inlined_primitive: %s %d\n"  (Path.name f)  (List.length args);
        if r = None then Printf.printf "failed\n"; *)
        r
charguer's avatar
init  
charguer committed
360 361 362 363 364 365 366 367 368 369 370
    | _ -> None

let exp_is_inlined_primitive e oargs =
   exp_find_inlined_primitive e oargs <> None

let exp_get_inlined_primitive e oargs =
   match exp_find_inlined_primitive e oargs with
   | Some x -> x
   | _ -> failwith "get_inlined_primitive: not an inlined primitive"


charguer's avatar
charguer committed
371

charguer's avatar
init  
charguer committed
372 373 374 375 376 377 378 379 380
(*#########################################################################*)
(* ** Lifting of values *)

(** Translate a Caml identifier into a Coq identifier, possibly 
    applied to wildcards for taking type applications into account *)

let lift_exp_path env p =
   match find_primitive (Path.name p) with
   | None -> 
381
      let x = lift_path_name (protect_infix_path p) in
charguer's avatar
init  
charguer committed
382
      coq_app_var_wilds x (typ_arity_var env p)
charguer's avatar
charguer committed
383 384
   | Some y -> 
      Coq_var y 
charguer's avatar
init  
charguer committed
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411

(** Translate a Caml value into a Coq value. Fails if the Coq 
    expression provided is not a value. *)

let rec lift_val env e = 
   let aux = lift_val env in
   match e.exp_desc with
   | Texp_ident (p,d) -> 
     lift_exp_path env p 
   | Texp_open (p, _) -> 
     assert false
   | Texp_constant (Const_int n) ->
      Coq_int n
   | Texp_constant _ -> 
      unsupported "only integer constant are supported"
   | Texp_tuple el -> 
      Coq_tuple (List.map aux el)
   | Texp_construct (p, c, es) ->
      coq_apps (coq_of_constructor p c) (List.map aux es)
   | Texp_record (l, opt_init_expr) ->  
       if opt_init_expr <> None then unsupported "record-with expression"; (* todo *)
       if List.length l < 1 then failwith "record should have at least one field";
       let labels = ((fun (p,li,ei) -> li) (List.hd l)).lbl_all in
       let args = Array.make (Array.length labels) (Coq_var "dummy") in
       let register_arg lbl v =
          Array.iteri (fun i lbli -> if lbl.lbl_name = lbli.lbl_name then args.(i) <- v) labels in
       List.iter (fun (p,lbl,v) -> register_arg lbl (aux v)) l;
charguer's avatar
charguer committed
412
       let constr = record_constructor_name_from_type (prefix_for_label (e.exp_type)) in
charguer's avatar
init  
charguer committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
       let typ_args = 
          match btyp_of_typ_exp e.exp_type with
          | Btyp_constr (id,ts) -> List.map lift_btyp ts
          | _ -> failwith "record should have a type-constructor as type"
          in
       coq_apps (coq_var_at constr) (typ_args @ Array.to_list args)
   | Texp_apply (funct, oargs) when exp_is_inlined_primitive funct oargs ->
      let f = exp_get_inlined_primitive funct oargs in
      let args = simplify_apply_args oargs in
      coq_apps (Coq_var f) (List.map aux args)
   | Texp_lazy e -> 
      aux e
   | Texp_array [] -> 
      Coq_var "array_empty"
   | Texp_constraint (e,_,_) -> 
      aux e

   (* --uncomment for debugging 
   | Texp_function _ -> not_in_normal_form "function"
   | Texp_apply _ -> not_in_normal_form "apply"
   | Texp_assertfalse -> not_in_normal_form "assert false"
   | Texp_try(body, pat_expr_list) -> not_in_normal_form "try expression"
   | Texp_variant(l, arg) ->  not_in_normal_form "variant expression"
   | Texp_setfield(arg, p, lbl, newval) -> not_in_normal_form "set-field expression"
   | Texp_array expr_list -> not_in_normal_form "array expressions"
   | Texp_ifthenelse(cond, ifso, None) -> not_in_normal_form "if-then-without-else expressions"
   | Texp_sequence(expr1, expr2) -> not_in_normal_form "sequence expressions"
   | Texp_while(cond, body) -> not_in_normal_form "while expressions"
   | Texp_for(param, low, high, dir, body) -> not_in_normal_form "for expressions"
   | Texp_when(cond, body) -> not_in_normal_form "when expressions"
   | Texp_send(_ , _, _) -> not_in_normal_form "send expressions"
   | Texp_new (cl, _) -> not_in_normal_form "new expressions"
   | Texp_instvar(path_self, path) -> not_in_normal_form "inst-var expressions"
   | Texp_setinstvar(path_self, path, expr) -> not_in_normal_form "set-inst-var expressions"
   | Texp_override(path_self, modifs) -> not_in_normal_form "override expressions"
   | Texp_letmodule(id, modl, body) -> not_in_normal_form "let-module expressions"
   | Texp_assert (cond) -> not_in_normal_form "assert expressions"
   | Texp_object (_, _) -> not_in_normal_form "object expressions"
   | Texp_poly _  -> not_in_normal_form "object expressions"
   | Texp_newtype _  -> not_in_normal_form "object expressions"
   | Texp_pack _  -> not_in_normal_form "object expressions"
   | Texp_let _ -> not_in_normal_form "let"
   | Texp_match _ -> not_in_normal_form "match"
   | Texp_field _ -> not_in_normal_form "field"
   *)
   | _ -> not_in_normal_form ("in liftval: " ^ Print_tast.string_of_expression false e)

   (* --todo: could be a value in a purely-functional setting
   | Texp_field (e, lbl) ->
       let typ = e.exp_type in
       Coq_app (Coq_var (string_of_label typ lbl), aux e) *)


(*#########################################################################*)
(* ** Helper functions for producing label names *)

charguer's avatar
charguer committed
469 470
(* FOR FUTURE USE *)

charguer's avatar
init  
charguer committed
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
let counter_local_label = 
   ref 0
let get_next_local_label () = 
   incr counter_local_label;
   "_m" ^ (string_of_int !counter_local_label)
let reset_local_labels() = 
   counter_local_label := 0

let used_labels : (var list) ref = 
   ref []
let reset_used_labels () =
   used_labels := []
let add_used_label x =
   if not (List.mem x !used_labels)
      then used_labels := x :: !used_labels

let cfg_extract_labels () =
   let labs = List.rev !used_labels in
   let cft = [ Cftop_coqs (list_mapi (fun i x -> Coqtop_label (x,i+1)) labs) ] in
   reset_used_labels();
   cft


(*#########################################################################*)
(* ** Helper functions for names *)

(** Takes a pattern that is expected to be reduced simply to an identifier, 
    and returns this identifier *)

let rec pattern_ident p =
   match p.pat_desc with
   | Tpat_var s -> s
   | Tpat_alias (p1,_) -> pattern_ident p1
   | _ -> failwith ("pattern_ident: the pattern is not a name: " ^ (Print_tast.string_of_pattern false p))

(** Takes a pattern that is expected to be reduced simply to an identifier, 
    and returns the name of this identifier *)

let pattern_name p =
   Ident.name (pattern_ident p)

charguer's avatar
charguer committed
512 513 514
(** Takes a function name and encodes its name in case of an infix operator *)

let pattern_name_protect_infix p =
charguer's avatar
charguer committed
515
   protect_infix (pattern_name p)
charguer's avatar
charguer committed
516 517


charguer's avatar
init  
charguer committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
(** An alternative version of function extract_label_names, for obtaining record labels *)

let rec extract_label_names_simple env ty =
  let ty = Ctype.repr ty in
  match ty.desc with
  | Tconstr (path, _, _) ->
      let td = Env.find_type path env in
      begin match td.type_kind with
      | Type_record (fields, _) ->
          List.map (fun (name, _, _) -> name) fields
      | Type_abstract when td.type_manifest <> None ->
          failwith "unsupported building of a record with abstract type"
      | _ -> assert false
      end
  | _ -> assert false


(*#########################################################################*)
(* ** Characteristic formulae for expressions *)

(** Translate a Caml expression into its Coq characteristic formula *)

let rec cfg_exp env e =
   let aux = cfg_exp env in
   let lift e = lift_val env e in
   let ret e = Cf_ret (lift e) in
   let not_normal () =
      not_in_normal_form (Print_tast.string_of_expression false e) in
   match e.exp_desc with
   | Texp_ident (x,d) -> ret e
   | Texp_open (p, {exp_desc = Texp_ident _}) -> assert false
   | Texp_constant cst -> ret e
   | Texp_tuple el -> ret e
   | Texp_construct(p, cstr, args) -> ret e
   (* TODO: only in purely function setting:   | Texp_record (lbl_expr_list, opt_init_expr) -> ret e*)

   | Texp_record (lbl_expr_list, opt_init_expr) ->
      if opt_init_expr <> None then unsupported "record-with"; (* TODO *)
      let (pathfront,pathend) = get_record_decomposed_name_for_exp e in
charguer's avatar
charguer committed
557
      let func = Coq_var (pathfront ^ (record_make_name pathend)) in (* tood: move the underscore *)
charguer's avatar
init  
charguer committed
558 559 560 561 562 563 564 565
      let named_args = List.map (fun (p,li,ei) -> (li.lbl_name,ei)) lbl_expr_list in
      (* deprecated sorting: let args = List.map snd (list_ksort str_cmp named_args) in *)
      let fields_names = extract_label_names_simple e.exp_env e.exp_type in
      let args = 
         try List.map (fun name -> List.assoc name named_args) fields_names
         with Not_found -> failwith "some fields are missing in a record construction" 
         in
      let tprod = coq_prod (List.map coq_typ args) in
charguer's avatar
charguer committed
566
      Cf_app ([tprod], loc_type, func, [Coq_tuple (List.map lift args)]) 
charguer's avatar
init  
charguer committed
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589

   | Texp_apply (funct, oargs) when exp_is_inlined_primitive funct oargs -> ret e

   | Texp_function (_, pat_expr_list, partial) -> assert false; (*not_normal ()   todo:better message*)
       

   | Texp_let(rf, fvs, pat_expr_list, body) ->
      
      let is_let_fun = 
         match (snd (List.hd pat_expr_list)).exp_desc with
         | Texp_function (_,_,_) -> true
         | Texp_constraint ({exp_desc = Texp_function (_,_,_)},_,_) -> true (* todo: generalize *)
         | _ -> false in

      (* binding of functions, possibly mutually-recursive *)
      if is_let_fun then begin
        let env' = match rf with 
           | Nonrecursive -> env
           | Recursive -> env
              (* --todo: add better support for local polymorphic recursion
              List.fold_left (fun (pat,bod) acc -> Ident.add (pattern_ident pat) 0 acc) env pat_expr_list *)
           | Default -> unsupported "Default recursion mode"
           in
charguer's avatar
charguer committed
590
        let ncs = List.map (fun (pat,bod) -> (pattern_name_protect_infix pat, cfg_func env' fvs pat bod)) pat_expr_list in
charguer's avatar
init  
charguer committed
591 592 593 594 595 596 597 598 599
        let cf_body = cfg_exp env' body in 
        add_used_label (fst (List.hd ncs));
        Cf_letfunc (ncs, cf_body)
        (* --todo: check what happens with recursive types *)

      (* let-binding of a single value *)
      end else begin 
        if (List.length pat_expr_list <> 1) then not_normal();
        let (pat,bod) = List.hd pat_expr_list in
charguer's avatar
charguer committed
600
        let x = pattern_name_protect_infix pat in
charguer's avatar
init  
charguer committed
601 602 603 604 605
        let fvs_typ, typ = lift_typ_sch pat.pat_type in
        let fvs = List.map name_of_type fvs in
        let fvs_strict = list_intersect fvs fvs_typ in
        let fvs_others = list_minus fvs fvs_strict in
            
charguer's avatar
charguer committed
606
        (* deprecated: pure-mode let-binding
charguer's avatar
init  
charguer committed
607 608 609 610 611 612 613
        if !pure_mode then begin 
       
           let cf1 = cfg_exp env bod in
           let env' = Ident.add (pattern_ident pat) (List.length fvs_strict) env in
           let cf2 = cfg_exp env' body in
           add_used_label x;
           Cf_letpure (x, fvs_strict, fvs_others, typ, cf1, cf2)
charguer's avatar
charguer committed
614
         end else *)
charguer's avatar
init  
charguer committed
615 616

        (* value let-binding *)
charguer's avatar
charguer committed
617
        if Typecore.is_nonexpansive bod then begin 
charguer's avatar
init  
charguer committed
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651

           let v = 
             try lift_val env bod  
             with Not_in_normal_form s -> 
                raise (Not_in_normal_form (s ^ " (only value can satisfy the value restriction)"))
             in
           let env' = Ident.add (pattern_ident pat) (List.length fvs_strict) env in
           let cf = cfg_exp env' body in
           add_used_label x;
           Cf_letval (x, fvs_strict, fvs_others, typ, v, cf)

        (* term let-binding *)
        end else begin
            
           if fvs_strict <> [] || fvs_others <> [] 
               then not_in_normal_form ("(unsatisfied value restriction) "
                                        ^ (Print_tast.string_of_expression false e));
           let cf1 = cfg_exp env bod in
           let env' = Ident.add (pattern_ident pat) (List.length fvs_strict) env in
           let cf2 = cfg_exp env' body in
           add_used_label x;
           Cf_let ((x,typ), cf1, cf2)

        end
      end

   | Texp_ifthenelse (cond, ifso, Some ifnot) ->
      (* old: Cf_caseif (aux cond, aux ifso, aux ifnot) *)
      Cf_caseif (lift cond, aux ifso, aux ifnot) 

   | Texp_apply (funct, oargs) ->
      let args = simplify_apply_args oargs in
      let tr = coq_typ e in
      let ts = List.map coq_typ args in
charguer's avatar
charguer committed
652
      Cf_app (ts, tr, lift funct, List.map lift args) 
charguer's avatar
init  
charguer committed
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673

   | Texp_match (arg, pat_expr_list, partial) ->
      let tested = lift arg in
      let conclu = match partial with Partial -> Cf_fail | Total -> Cf_done in
      let cfg_case (pat,body) acc =
         let whenopt, cfbody =  
            match body.exp_desc with 
            | Texp_when (econd, ebody) ->
                let w = 
                   try lift_val env econd  
                   with Not_in_normal_form s -> 
                      raise (Not_in_normal_form (s ^ " (Only total expressions are allowed in when clauses)"))
                   in
                Some w, aux ebody
            | _ -> None, aux body
            in
         Cf_case (tested, pattern_variables pat, lift_pat pat, whenopt, pattern_aliases pat, cfbody, acc) in
      let label = get_next_local_label() in
      add_used_label label;
      Cf_match (label, List.length pat_expr_list, List.fold_right cfg_case pat_expr_list conclu)

charguer's avatar
charguer committed
674
   | Texp_assert e -> 
charguer's avatar
init  
charguer committed
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
      Cf_assert (aux e)

   | Texp_assertfalse -> 
      Cf_fail

   | Texp_lazy e -> 
      aux e

   | Texp_sequence(expr1, expr2) -> 
      Cf_seq (aux expr1, aux expr2)

   | Texp_while(cond, body) -> 
      Cf_while (aux cond, aux body)

   | Texp_for(param, low, high, dir, body) -> 
      begin match dir with 
        | Upto -> Cf_for (Ident.name param, lift low, lift high, aux body)
        | Downto -> unsupported "for-downto expressions" (* later *)
      end

   | Texp_array expr_list -> unsupported "array expressions" (* later *)

   | Texp_field (arg, p, lbl) -> 
      let tr = coq_typ e in 
      let ts = coq_typ arg in (* todo: check it is always 'loc' *)
charguer's avatar
charguer committed
700
      let func = Coq_var (record_field_get_name lbl.lbl_name) in
charguer's avatar
charguer committed
701
      Cf_app ([ts], tr, func, [lift arg]) 
charguer's avatar
init  
charguer committed
702 703 704 705

   | Texp_setfield(arg, p, lbl, newval) -> 
      let ts1 = coq_typ arg in (* todo: check it is always 'loc' *)
      let ts2 = coq_typ newval in 
charguer's avatar
charguer committed
706
      let func = Coq_var (record_field_set_name lbl.lbl_name) in
charguer's avatar
charguer committed
707
      Cf_app ([ts1;ts2], coq_unit, func, [lift arg; lift newval]) 
charguer's avatar
init  
charguer committed
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735

   | Texp_try(body, pat_expr_list) -> unsupported "try expression"
   | Texp_variant(l, arg) ->  unsupported "variant expression"
   | Texp_ifthenelse(cond, ifso, None) -> unsupported "if-then-without-else expressions should have been normalized"
   | Texp_when(cond, body) -> unsupported "when expressions outside of pattern matching"
   | Texp_send(_, _, _) -> unsupported "send expressions"
   | Texp_new (cl, _) -> unsupported "new expressions"
   | Texp_instvar(path_self, path) -> unsupported "inst-var expressions"
   | Texp_setinstvar(path_self, path, expr) -> unsupported "set-inst-var expressions"
   | Texp_override(path_self, modifs) -> unsupported "override expressions"
   | Texp_letmodule(id, modl, body) -> unsupported "let-module expressions"
   | Texp_object _ -> unsupported "object expressions"
   | Texp_poly (_,_) -> unsupported "poly"
   | Texp_newtype (_,_) -> unsupported "newtype"
   | Texp_pack _ -> unsupported "pack"
   | Texp_open (_,_) -> unsupported "open in term"
   | Texp_constraint (e,_,_) -> aux e

and cfg_func env fvs pat bod =
   let rec get_typed_args acc e =
      match e.exp_desc with
      | Texp_function (_,[p1,e1],partial) 
      | Texp_constraint ({exp_desc = Texp_function (_,[p1,e1],partial)},_,_) ->
         if partial <> Total 
            then not_in_normal_form (Print_tast.string_of_expression false e);
         get_typed_args ((pattern_name p1, coq_typ_pat p1)::acc) e1
      | _ -> List.rev acc, e
      in  
charguer's avatar
charguer committed
736
   let f = pattern_name_protect_infix pat in
charguer's avatar
init  
charguer committed
737 738 739
   let targs, body = get_typed_args [] bod in
   let typ = coq_typ body in
   let cf_body = cfg_exp env body in
charguer's avatar
credits  
charguer committed
740
   let cf_body = if !use_credits then Cf_pay cf_body else cf_body in
charguer's avatar
init  
charguer committed
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
   let fvs = List.map name_of_type fvs in
   Cf_body (f, fvs, targs, typ, cf_body) 
   (* --todo: check if the set of type variables quantified is not too
      conservative. Indeed, some type variables may no longer occur. *)


(*#########################################################################*)
(* ** Characteristic formulae for modules *)

(** Helper functions to find out the kind of a type declaration *)

let is_algebraic (name,dec) =
   match dec.typ_type.type_kind with Type_variant _ -> true | _ -> false 

let is_type_abbrev (name,dec) =
   match dec.typ_type.type_kind with Type_abstract -> true | _ -> false 

let is_type_record (name,dec) =
   match dec.typ_type.type_kind with Type_record _ -> true | _ -> false 

(** Generate the top-level Coq declarations associated with 
    a top-level declaration from a module. *)

let rec cfg_structure_item s : cftops = 
  match s.str_desc with
  | Tstr_value(rf, fvs, pat_expr_list) ->
      reset_local_labels();

      (* --todo: improve code sharing between local bindings and top-level bindings *)
      let is_let_fun (pat,exp) = 
         match exp.exp_desc with 
         | Texp_function (_,_,_) -> true
         | Texp_constraint({exp_desc = Texp_function (_,_,_)},_,_) -> true
         | _ -> false in

      if List.for_all is_let_fun pat_expr_list then begin
        let env' = match rf with 
           | Nonrecursive -> Ident.empty
           | Recursive -> Ident.empty
               (* --todo: better support for polymorphic recursion
              List.fold_left (fun (pat,bod) acc -> Ident.add (pattern_ident pat) 0 acc) env pat_expr_list *)
           | Default -> unsupported "Default recursion mode"
           in
charguer's avatar
charguer committed
784
        let ncs = List.map (fun (pat,bod) -> (pattern_name_protect_infix pat, cfg_func env' fvs pat bod)) pat_expr_list in
charguer's avatar
init  
charguer committed
785 786 787 788 789 790 791
          (List.map (fun (name,_) -> Cftop_val (name, val_type)) ncs)
        @ (List.map (fun (name,cf_body) -> Cftop_fun_cf (name, cf_body)) ncs)
        @ [Cftop_coqs (List.map (fun (name,_) -> register_cf name) ncs)]
   
      (* let-binding of a single value *)
      end else if (List.length pat_expr_list = 1) then begin (* later: check it is not a function *)
        let (pat,bod) = List.hd pat_expr_list in
charguer's avatar
charguer committed
792
        let x = pattern_name_protect_infix pat in
charguer's avatar
charguer committed
793 794
        (* DEPRECATED if (hack_recognize_okasaki_lazy x) then [] else *)
        begin
charguer's avatar
init  
charguer committed
795 796 797 798 799
        let fvs_typ, typ = lift_typ_sch pat.pat_type in
        let fvs = List.map name_of_type fvs in
        let fvs_strict = list_intersect fvs fvs_typ in
        let fvs_others = list_minus fvs fvs_strict in
        
charguer's avatar
charguer committed
800
        (* deprecated: pure-mode let-binding 
charguer's avatar
init  
charguer committed
801 802 803 804 805 806 807 808 809 810 811 812
        if !pure_mode then begin 

           let cf_body = cfg_exp (Ident.empty) bod in
           let implicits = 
              match fvs_strict with
              | [] -> []
              | _ ->  [ Coqtop_implicit (x, List.map (fun t -> (t,Coqi_maximal)) fvs_strict) ]
              in
           [ Cftop_val (x, coq_forall_types fvs_strict typ);
             Cftop_coqs implicits;
             Cftop_pure_cf (x, fvs_strict, fvs_others, cf_body); 
             Cftop_coqs [register_cf x]; ] 
charguer's avatar
charguer committed
813
        end else*)
charguer's avatar
init  
charguer committed
814 815

        (* value let-binding *)
charguer's avatar
charguer committed
816
        if Typecore.is_nonexpansive bod then begin 
charguer's avatar
init  
charguer committed
817 818 819 820 821 822

           let v = 
             try lift_val (Ident.empty) bod  
             with Not_in_normal_form s -> 
                raise (Not_in_normal_form (s ^ " (only value can satisfy the value restriction)"))
            in
charguer's avatar
charguer committed
823
           let v_typed = coq_annot v typ in
charguer's avatar
init  
charguer committed
824 825 826 827 828 829 830
           let implicits = 
              match fvs_strict with
              | [] -> []
              | _ ->  [ Coqtop_implicit (x, List.map (fun t -> (t,Coqi_maximal)) fvs_strict) ]
              in
           [ Cftop_val (x, coq_forall_types fvs_strict typ);
             Cftop_coqs implicits;
charguer's avatar
charguer committed
831
             Cftop_val_cf (x, fvs_strict, fvs_others, v_typed); 
charguer's avatar
init  
charguer committed
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
             Cftop_coqs [register_cf x]; ] 

        (* term let-binding -- later *)
        end else begin
            
           failwith "unsupported top-level binding of terms that are not values";

           (* if fvs_strict <> [] || fvs_others <> [] 
               then not_in_normal_form ("(unsatisfied value restriction) "
                                        ^ (Print_tast.string_of_expression false e));
           let cf1 = cfg_exp env bod in
           let env' = Ident.add (pattern_ident pat) (List.length fvs_strict) env in
           let cf2 = cfg_exp env' body in
           add_used_label x;
           Cf_let ((x,typ), cf1, cf2) *)

        end

      end (* for skip_cf *)

     end else
        unsupported ("mutually-recursive values that are not all functions");


  | Tstr_type(decls) -> [ Cftop_coqs (cfg_type_decls decls) ]

  | Tstr_module(id, modl) -> cfg_module id modl 

  | Tstr_modtype(id, decl) -> cfg_modtype id decl

  | Tstr_open path -> 
charguer's avatar
charguer committed
863
      (* TEMPORARILY DEPRECATED if is_primitive_module (Path.name path) then [] else *)
charguer's avatar
charguer committed
864
      [ Cftop_coqs [ Coqtop_require_import [ lift_full_path_name path ] ] ]
charguer's avatar
init  
charguer committed
865 866

  | Tstr_primitive(id, descr) ->
charguer's avatar
charguer committed
867
      let id = Ident.name id in
charguer's avatar
charguer committed
868 869 870 871
      let fvs, typ = lift_typ_sch descr.val_desc.ctyp_type in
      let typ = coq_fun_types fvs typ in
      [ Cftop_val (id, typ) ]

charguer's avatar
init  
charguer committed
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
  | Tstr_exception(id, decl) ->
      [] (* unsupported "exceptions" *)
  | Tstr_exn_rebind(id, path) -> 
      [] (* unsupported "exceptions" *)

  | Tstr_recmodule bindings -> unsupported "recursive modules"
  | Tstr_class _ -> unsupported "objects"
  | Tstr_class_type _ -> unsupported "objects"
  | Tstr_include (m,ids) -> unsupported "module-include"
  | Tstr_eval expr -> unsupported "top level eval expression (let _)"

(** Generate the top-level Coq declarations associated with 
    a type abbreviation. *)

and cfg_type_abbrev (name,dec) =
charguer's avatar
charguer committed
887
   let x = type_constr_name (Ident.name name) in
charguer's avatar
init  
charguer committed
888 889 890 891 892 893 894 895 896 897 898 899 900 901
   let args = List.map name_of_type dec.typ_type.type_params in
   let sort = coq_impl_types (List.length args) in
   let coqs = match dec.typ_type.type_manifest with
      | None -> [Coqtop_param (x, sort)]
      | Some t -> [Coqtop_def ((x, sort), coq_fun_types args (lift_typ_exp t));
                   Coqtop_hint_unfold ([x],"typeclass_instances") ] in
   coqs, [] 

(** Generate the top-level Coq declarations associated with 
    a record definition. *)

and cfg_type_record (name,dec) =
   let x = Ident.name name in
   let name_of_field lbl = 
charguer's avatar
charguer committed
902
      record_field_name lbl in
charguer's avatar
init  
charguer committed
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
   let fields = match dec.typ_type.type_kind with Type_record (l,_) -> l | _ -> assert false in
   (* let fields_base_names = List.map (fun (lbl,_,_) -> lbl) fields in *)
   let declared_params = List.map name_of_type dec.typ_type.type_params in
   let branches, branches_params = List.split (List.map (fun (lbl, mut, typ) -> 
      let btyp = btyp_of_typ_exp typ in 
      ((name_of_field lbl, lift_btyp btyp), fv_btyp ~through_arrow:false btyp)) fields) in
          (* todo: use a function to factorize above work *)

   (* deprecated sorting: let branches = list_ksort str_cmp branches in *)
   let fields_names, fields_types = List.split branches in
   (* let remaining_params = List.concat branches_params in *)
   (* todo: assert remaining_params included in declared_params *)
   (* TODO: enlever le polymorphisme inutile : list_intersect remaining_params declared_params *) 
   let params = declared_params in 
   let top = { 
charguer's avatar
charguer committed
918 919
      coqind_name = record_structure_name x;
      coqind_constructor_name = record_constructor_name x;
charguer's avatar
init  
charguer committed
920 921 922 923 924 925 926 927
      coqind_targs = coq_types params;
      coqind_ret = Coq_type;
      coqind_branches = branches } in
   let implicit_decl =
      match params with
      | [] -> []
      | _ -> List.map (fun field -> Coqtop_implicit (field, List.map (fun p -> p, Coqi_maximal) params)) fields_names 
      in
charguer's avatar
charguer committed
928
   let type_abbrev = Coqtop_def ((type_constr_name x, Coq_wild), coq_fun_types params loc_type) in
charguer's avatar
init  
charguer committed
929 930 931
   [ type_abbrev ],
   [ Coqtop_record top ] 
   @ (implicit_decl)
charguer's avatar
charguer committed
932 933
   @ [ Coqtop_hint_constructors ([record_structure_name x], "typeclass_instances") ]
   @ record_functions x (record_constructor_name x) (record_repr_name x) params fields_names fields_types
charguer's avatar
init  
charguer committed
934 935 936 937
  (*  todo: move le "_of" *)

(** Auxiliary function to generate stuff for records *)

charguer's avatar
fix  
charguer committed
938
and record_functions name record_constr repr_name params fields_names fields_types =
charguer's avatar
init  
charguer committed
939 940 941 942 943
   let nth i l = List.nth l i in
   let n = List.length fields_names in
   let indices = list_nat n in
   let for_indices f = List.map f indices in

charguer's avatar
fix  
charguer committed
944
   let new_name = record_make_name name in
charguer's avatar
charguer committed
945 946
   let get_names = for_indices (fun i -> record_field_get_name (nth i fields_names)) in
   let set_names = for_indices (fun i -> record_field_set_name (nth i fields_names)) in
charguer's avatar
init  
charguer committed
947 948 949 950 951
   let new_decl = Coqtop_param (new_name, val_type) in
   let get_set_decl i =
      [ Coqtop_param (nth i get_names, val_type); 
        Coqtop_param (nth i set_names, val_type) ] in
   
charguer's avatar
charguer committed
952
   let logicals = List.map str_capitalize_1 fields_names (* for_indices (fun i -> sprintf "A%d" (i+1)) *) in
charguer's avatar
init  
charguer committed
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
   let reprs = for_indices (fun i -> sprintf "_T%d" (i+1)) in
   let abstracts = for_indices (fun i -> sprintf "_X%d" (i+1)) in
   let concretes = for_indices (fun i -> sprintf "x%d" (i+1)) in
   let loc = "l" in

   let vparams = coq_vars params in
   let vlogicals = coq_vars logicals in
   let vreprs = coq_vars reprs in
   let vabstracts = coq_vars abstracts in
   let vconcretes = coq_vars concretes in
   let vloc = coq_var "l" in

   let tparams = coq_types params in
   let tlogicals = coq_types logicals in
   let treprs = List.map (fun i -> nth i reprs, htype (nth i vlogicals) (nth i fields_types)) indices in
   let tabstracts = List.map (fun i -> nth i abstracts, nth i vlogicals) indices in
   let tconcretes = List.map (fun i -> nth i concretes, nth i fields_types) indices in
   let tloc = (loc, loc_type) in

   let repr_args = tparams @ tlogicals @ treprs @ tabstracts @ [tloc] in
   let hcore = heap_is_single vloc (coq_apps (coq_var_at record_constr) (vparams @ vconcretes)) in
   let helems_items = for_indices (fun i -> hdata (nth i vconcretes) (Coq_app (nth i vreprs, nth i vabstracts))) in
   let helems = heap_stars helems_items in
   let repr_body = heap_star hcore helems in
   let repr_def = coqtop_def_untyped repr_name (coq_funs repr_args (heap_existss tconcretes repr_body)) in

   let repr_folded = hdata vloc (coq_apps (coq_var_at repr_name) (vparams @ vlogicals @ vreprs @ vabstracts)) in
   let repr_unfolded = hdata vloc (coq_apps (coq_var_at repr_name) (vparams @ fields_types @ (list_make n id_repr) @ vconcretes)) in
   let repr_elems = helems in
   let repr_convert_body = coq_eq repr_folded (heap_existss tconcretes (heap_star repr_unfolded repr_elems)) in
   let repr_focus_body = heap_impl repr_folded (heap_existss tconcretes (heap_star repr_unfolded repr_elems)) in
   let repr_unfocus_body = heap_impl (heap_star repr_unfolded repr_elems) repr_folded in
   let repr_convert_quantif = [tloc] @ tparams @ tlogicals @ treprs @ tabstracts in
   let repr_focus_quantif = repr_convert_quantif in
   let repr_unfocus_quantif = [tloc] @ tparams @ tconcretes @ tlogicals @ treprs @ tabstracts in
charguer's avatar
fix  
charguer committed
988 989 990
   let convert_name = record_convert_name name in
   let focus_name = record_unfocus_name name in
   let unfocus_name = record_focus_name name in
charguer's avatar
init  
charguer committed
991 992 993 994 995 996 997
   let repr_convert = Coqtop_param (convert_name, coq_foralls repr_convert_quantif repr_convert_body) in
   let repr_focus = Coqtop_param (focus_name, coq_foralls repr_focus_quantif repr_focus_body) in
   let repr_unfocus = Coqtop_param (unfocus_name, coq_foralls repr_unfocus_quantif repr_unfocus_body) in 
   let repr_convert_focus_unfocus = [ repr_convert; repr_focus; repr_unfocus;
      coqtop_noimplicit convert_name; coqtop_noimplicit focus_name; coqtop_noimplicit unfocus_name ] in
   let field_convert_focus_unfocus i = 
      let field_name = nth i fields_names in
charguer's avatar
fix  
charguer committed
998 999 1000
      let field_convert_name = record_convert_field_name field_name in
      let field_focus_name = record_focus_field_name field_name in
      let field_unfocus_name = record_unfocus_field_name field_name in
charguer's avatar
init  
charguer committed
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
      let tconcretei = nth i tconcretes in
      let helemi = nth i helems_items in 
      let field_folded = hdata vloc (coq_apps (coq_var_at repr_name) (vparams @ vlogicals @ vreprs @ vabstracts)) in
      let field_unfolded = hdata vloc (coq_apps (coq_var_at repr_name) (vparams @ (list_replace_nth i fields_types vlogicals) @ (list_replace i id_repr vreprs) @ (list_replace_nth i vconcretes vabstracts))) in
      let field_convert_body = coq_eq field_folded (heap_exists_one tconcretei (heap_star field_unfolded helemi)) in
      let field_focus_body = heap_impl field_folded (heap_exists_one tconcretei (heap_star field_unfolded helemi)) in
      let field_unfocus_body = heap_impl (heap_star field_unfolded helemi) field_folded in
      let field_convert_quantif = [tloc] @ tparams @ tlogicals @ treprs @ tabstracts in
      let field_focus_quantif = field_convert_quantif in
      let field_unfocus_quantif = [tloc] @ tparams @ [ tconcretei ] @ tlogicals @ treprs @ tabstracts in
      let field_convert = Coqtop_param (field_convert_name, coq_foralls field_convert_quantif field_convert_body) in
      let field_focus = Coqtop_param (field_focus_name, coq_foralls field_focus_quantif field_focus_body) in
      let field_unfocus = Coqtop_param (field_unfocus_name, coq_foralls field_unfocus_quantif field_unfocus_body) in 
      [ field_convert; field_focus; field_unfocus;
        coqtop_noimplicit field_convert_name; coqtop_noimplicit field_focus_name; coqtop_noimplicit field_unfocus_name ] 
      in
   let fields_convert_focus_unfocus = List.concat (List.map (fun i -> field_convert_focus_unfocus i) indices) in

   let new_spec =
      let new_name_spec = new_name ^ "_spec" in
      let r = "R" in
      let vr = coq_var r in
      let tr = (r, formula_type_of loc_type) in
      let x = "_X" in
      let tx = (x, coq_prod fields_types) in
      let data_targs = vparams @ fields_types @ (for_indices (fun _ -> id_repr)) in
      let post = coq_funs [(loc, loc_type)] (hdata vloc (coq_apps (coq_var_at repr_name) (data_targs @ vabstracts))) in
      let body = coq_funs [tx; tr] (Coq_lettuple (coq_vars abstracts, Coq_var x, coq_apps vr [heap_empty; post])) in
      let spec = coq_foralls tparams (coq_apps (Coq_var "spec_1") [body; coq_var new_name]) in
      [ Coqtop_param (new_name_spec, spec); 
        register_spec new_name new_name_spec; ]
      in
   let get_set_spec i = 
      let get_name = nth i get_names in
      let set_name = nth i set_names in
      let get_name_spec = get_name ^ "_spec" in
      let set_name_spec = set_name ^ "_spec" in
      let r = "R" in
      let vr = coq_var r in
      let trget = (r, formula_type_of (nth i fields_types)) in
      let trset = (r, formula_type_of coq_unit) in
      let x' = sprintf "_X%d'" i in
      let vx' = coq_var x' in
      let tx' = (x', nth i fields_types) in
      let selected_tlogicals = list_remove i tlogicals in
      let replaced_vlogicals = list_replace i (nth i fields_types) vlogicals in
      let replaced_vreprs = list_replace i id_repr vreprs in
      let selected_treprs = list_remove i treprs in
      let replaced_tabstracts = list_replace i (nth i abstracts, nth i fields_types) tabstracts in
      let update_vabstracts = list_replace i vx' vabstracts in
      let data_targs = vparams @ replaced_vlogicals @ replaced_vreprs in
      let data_initial = hdata vloc (coq_apps (coq_var_at repr_name) (data_targs @ vabstracts)) in
      let data_updated = hdata vloc (coq_apps (coq_var_at repr_name) (data_targs @ update_vabstracts)) in
      let post_get = coq_funs [("x", Coq_wild)] (heap_star (heap_pred (coq_eq (Coq_var "x") (nth i vabstracts))) data_initial) in
      let post_set = post_unit data_updated in
      let body_quantif = replaced_tabstracts @ selected_treprs in
      let body_get = coq_funs [tloc; trget] (coq_foralls body_quantif (coq_apps vr [data_initial; post_get])) in
      let body_set = coq_funs [tloc; tx'; trset] (coq_foralls body_quantif (coq_apps vr [data_initial; post_set])) in
      let spec_get = coq_foralls (tparams @ selected_tlogicals) (coq_apps (Coq_var "spec_1") [body_get; coq_var get_name]) in
      let spec_set = coq_foralls (tparams @ selected_tlogicals) (coq_apps (Coq_var "spec_2") [body_set; coq_var set_name]) in
      [ Coqtop_param (get_name_spec, spec_get); 
        register_spec get_name get_name_spec; 
        Coqtop_param (set_name_spec, spec_set);
        register_spec set_name set_name_spec; ]
      in

   let get_spec_focus i = 
      let get_name = nth i get_names in
      let get_name_spec = get_name ^ "_spec_focus" in
      let r = "R" in
      let vr = coq_var r in
      let trget = (r, formula_type_of (nth i fields_types)) in
      let replaced_vlogicals = list_replace i (nth i fields_types) vlogicals in
      let replaced_vreprs = list_replace i id_repr vreprs in
      let replaced_vabstracts = list_replace i (Coq_var "x") vabstracts in
      let data_initial = hdata vloc (coq_apps (coq_var_at repr_name) (vparams @ vlogicals @ vreprs @ vabstracts)) in
      let data_focused = hdata (Coq_var "x") (Coq_app (nth i vreprs, nth i vabstracts)) in
      let data_final = hdata vloc (coq_apps (coq_var_at repr_name) (vparams @ replaced_vlogicals @ replaced_vreprs @ replaced_vabstracts)) in
      let post_get = coq_funs [("x", Coq_wild)] (heap_star data_focused data_final) in
      let body_quantif = tabstracts @ treprs in
      let body_get = coq_funs [tloc; trget] (coq_foralls body_quantif (coq_apps vr [data_initial; post_get])) in
      let spec_get = coq_foralls (tparams @ tlogicals) (coq_apps (Coq_var "spec_1") [body_get; coq_var get_name]) in
      [ Coqtop_param (get_name_spec, spec_get); 
        coqtop_register "database_spec_focus" get_name get_name_spec; ]
      in

   let set_spec_unfocus i = 
      let set_name = nth i set_names in
      let set_name_spec = set_name ^ "_spec_unfocus" in
      let r = "R" in
      let vr = coq_var r in
      let trset = (r, formula_type_of coq_unit) in
      let x_concrete = "x0" in
      let tx_concrete = (x_concrete, nth i fields_types) in
      let vlogical0 = Coq_var "_A0" in
      let tlogical0 = ("_A0", Coq_type) in
      let vabstract0 = Coq_var "_X0" in
      let tabstract0 = ("_X0", vlogical0) in
      let vrepr0 = Coq_var "_T0" in
      let trepr0 = ("_T0", htype vlogical0 (nth i fields_types)) in
      let updated_vlogicals = list_replace i vlogical0 vlogicals in
      let updated_vreprs = list_replace i vrepr0 vreprs in
      let update_vabstracts = list_replace i vabstract0 vabstracts in
      let data_initial = hdata vloc (coq_apps (coq_var_at repr_name) (vparams @ vlogicals @ vreprs @ vabstracts)) in
      let data_updated = hdata vloc (coq_apps (coq_var_at repr_name) (vparams @ updated_vlogicals @ updated_vreprs @ update_vabstracts)) in
      let data_focused = hdata (Coq_var x_concrete) (Coq_app (vrepr0, vabstract0)) in
      let post_set = post_unit data_updated in
      let body_quantif = [tabstract0] @ tabstracts @ [trepr0] @ treprs in
      let body_set = coq_funs [tloc; tx_concrete; trset] (coq_foralls body_quantif (coq_apps vr [(heap_star data_initial data_focused); post_set])) in
      let spec_set = coq_foralls (tparams @ [tlogical0] @ tlogicals) (coq_apps (Coq_var "spec_2") [body_set; coq_var set_name]) in
      [ Coqtop_param (set_name_spec, spec_set);
        coqtop_register "database_spec_unfocus" set_name set_name_spec; ]
      in

     [ new_decl ]
   @ (List.concat (List.map get_set_decl indices))
charguer's avatar
charguer committed
1117
   @ [ repr_def ]
charguer's avatar
init  
charguer committed
1118 1119
   @ repr_convert_focus_unfocus
   @ fields_convert_focus_unfocus
charguer's avatar
fix  
charguer committed
1120 1121
   (* TODO: revive *)
   (*
charguer's avatar
init  
charguer committed
1122 1123 1124
   @ new_spec
   @ (List.concat (List.map get_set_spec indices))
   @ (List.concat (List.map get_spec_focus indices))
charguer's avatar
fix  
charguer committed
1125 1126
   @ (List.concat (List.map set_spec_unfocus indices)) *)
  
charguer's avatar
init  
charguer committed
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139


(** Generate the top-level Coq declarations associated with 
    a algebraic data type definition. *)

and cfg_algebraic decls =
   (* -- todo: data constructor type arity may be reduced when types are erased *)
   (* -- todo: Caml types often clash with Caml program variables, since in Coq
         they get put in the same namespace *)
    let trans_ind (name,dec) =
      let x = Ident.name name in
      let branches = match dec.typ_type.type_kind with Type_variant l -> l | _ -> assert false in
      let params = List.map name_of_type dec.typ_type.type_params in
charguer's avatar
charguer committed
1140
      let ret_typ = coq_apps (Coq_var (type_constr_name x)) (coq_vars params) in
charguer's avatar
init  
charguer committed
1141 1142 1143 1144
      let get_typed_constructor (c,ts) =
         (c, coq_impls (List.map lift_typ_exp ts) ret_typ) in
      let coqind_decl = 
         if List.length decls = 1 then
charguer's avatar
charguer committed
1145
            {  coqind_name = type_constr_name x;
charguer's avatar
charguer committed
1146
               coqind_constructor_name = record_constructor_name x;
charguer's avatar
init  
charguer committed
1147 1148 1149 1150
               coqind_targs = coq_types params;
               coqind_ret = Coq_type;
               coqind_branches = List.map get_typed_constructor branches; } 
          else
charguer's avatar
charguer committed
1151
            {  coqind_name = type_constr_name x;
charguer's avatar
charguer committed
1152
               coqind_constructor_name = record_constructor_name x;
charguer's avatar
init  
charguer committed
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
               coqind_targs = [];
               coqind_ret = coq_impl_types (List.length params); 
               coqind_branches = List.map 
                  (fun tc -> let (c,t) = get_typed_constructor tc in
                             (c, coq_foralls (coq_types params) t)
                     ) branches; } 
          in
      let implicit_decl =
         match params with
         | [] -> []
         | _ -> List.map (fun (cname,_) -> Coqtop_implicit (cname, List.map (fun p -> p,Coqi_maximal) params)) branches 
         in
      (coqind_decl, implicit_decl)
      in
   let inds,maxiss = List.split (List.map trans_ind decls) in
     [ Coqtop_ind inds ] 
   @ (List.concat maxiss)
   @ [ Coqtop_hint_constructors (List.map (fun i -> i.coqind_name) inds, "typeclass_instances") ],
   []

(** Generate the top-level Coq declarations associated with 
    a type definition. *)

and cfg_type_decls (decls : (Ident.t * Typedtree.type_declaration) list) =
   let rec aux decls =   
       if List.length decls = 1 && is_type_abbrev (List.hd decls)  
          then cfg_type_abbrev (List.hd decls)
       else if List.length decls = 1 && is_type_record (List.hd decls)  
          then cfg_type_record (List.hd decls)
       else if (List.for_all is_algebraic decls)  
          then cfg_algebraic decls
       else 
(* /todo/ very experimental support: simply break circularity; might stack overflow! *)
          let (a,b) = List.split (List.map aux (List.map (fun x -> [x]) decls)) in
          (List.concat a, List.concat b)
          (* unsupported "type definitions must be single abbreviations or mutually-recursive inductive definitions (mixing both is not supported in Coq)" *)
      in
   let (a,b) = aux decls in 
   a @ b

(** Generate the top-level Coq declarations associated with 
    the content of a module. *)

and cfg_structure s =
   reset_used_labels();
   let body = list_concat_map (fun si -> reset_names(); cfg_structure_item si) s.str_items in
charguer's avatar
charguer committed
1199 1200
   (* cfg_extract_labels() @ *)
   body
charguer's avatar
init  
charguer committed
1201 1202 1203 1204 1205

(** Generate the top-level Coq declarations associated with 
    a Caml signature definition. *)

and cfg_modtype id mt =
charguer's avatar
charguer committed
1206
   let id = lift_module_name id in
charguer's avatar
init  
charguer committed
1207 1208 1209 1210 1211 1212
   let rec aux (bindings:mod_bindings) mt =
      match mt.mty_desc with
      | Tmty_ident p -> Coqtop_module_type (id, bindings, Mod_def_inline [lift_full_path_name p]), None
      | Tmty_signature s -> Coqtop_module_type (id, bindings, Mod_def_declare), Some (cfg_signature s)
      | Tmty_functor (x,mtx,mtr) -> 
          begin match mtx.mty_desc with
charguer's avatar
charguer committed
1213
          | Tmty_ident p -> aux (bindings @ [([lift_module_name x], Mod_typ_var (lift_full_path_name p))]) mtr (* TODO: use List.rev *)
charguer's avatar
init  
charguer committed
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
          | _ -> unsupported "functor with on-the-fly signature for its argument"
          end
      | Tmty_with _ -> unsupported "module sig with"
      | Tmty_typeof _ -> unsupported "type of module"
      in
   let mod_typ_dec, sign_opt = aux [] mt in
   match sign_opt with
   | None -> [ Cftop_coqs [ mod_typ_dec ] ]
   | Some sign -> [ Cftop_coqs ( [mod_typ_dec] @ sign @ [Coqtop_end id] ) ]

(** Generate the top-level Coq declarations associated with 
    a top-level declaration from a signature. *)

and cfg_signature_item s =
  match s.sig_desc with

  | Tsig_value (id,vd) -> 
     if vd.val_val.val_kind <> Val_reg then unsupported "value in signature which is not a regular value";
     let fv, typ = lift_typ_sch vd.val_val.val_type in
     let x = Ident.name id in
     let implicit_decl =
         match fv with
         | [] -> []
         | _ -> [ Coqtop_implicit (x, List.map (fun p -> p, Coqi_maximal) fv) ]
         in
     [Coqtop_param (x, coq_forall_types fv typ)] @ implicit_decl

  | Tsig_type decls -> 
     cfg_type_decls decls 
(* deprecated
     List.iter (fun (id,decl) -> printf "%s\n" (Ident.name id)) decls;  print_newline();
     assert false
*)
      (* -- old
 (Ident.t * type_declaration) list
      if rs <> Trec_not then unsupported "recursive type definitions in signatures"; 
      begin match td.type_kind with
      | Type_abstract -> cfg_type_abbrev (id,td)
      | Type_variant _ -> cfg_algebraic [id,td]
      | Type_record _ -> unsupported "record types"
      end
      *)

  | Tsig_module (id,mt) ->
charguer's avatar
charguer committed
1258
      let x = lift_module_name id in
charguer's avatar
init  
charguer committed
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
      let mt' =
         match mt.mty_desc with
         | Tmty_ident p -> Mod_typ_var (lift_full_path_name p)
         | Tmty_signature s -> 
            (*
            Printf.printf "%d\n" (List.length s);
             begin match List.hd s with
              | Tsig_value (id,vd) -> unsupported "u" 
              | Tsig_type (id, td, rs) -> unsupported "x" 
              | Tsig_module (id,mt,rs) ->   unsupported "y" 
              | Tsig_modtype (id,decl) ->   unsupported "z"  
              | Tsig_exception _ -> unsupported "exceptions"
              | Tsig_class _ -> unsupported "objects"
              | Tsig_cltype _ -> unsupported "objects"
              end;
             *) 
            unsupported "module constraint is not just a name (4) -- todo: should be supported"  
         | _ -> unsupported "module constraint is not just a name (2)"  
         in
      [Coqtop_declare_module (x, mt')] 

  | Tsig_modtype (id,decl) -> 
      unsupported "module types declared in module signatures"
      (*
      begin match decl with 
      | Tmodtype_abstract -> unsupported "abstract module types"
      | Tmodtype_manifest mt -> cfg_modtype id mt
      end
      *)
  | Tsig_exception _ -> unsupported "exceptions"
  | Tsig_class _ -> unsupported "objects"
  | Tsig_class_type _ -> unsupported "objects"
  | Tsig_recmodule _ -> unsupported "recursive module signature"
  | Tsig_open _ -> (* todo ? *) []
  | Tsig_include mty -> 
      begin match mty.mty_desc with
      | Tmty_ident p ->  [Coqtop_module_type_include (lift_full_path_name p)] 
      | Tmty_signature s -> 
         unsupported "sig include is not just a name"  
      | _ -> unsupported "sig include is not just a name"  
      end
 

(** Generate the top-level Coq declarations associated with 
    a signature. Handles mutually-recursive type definitions
    for algebraic data types. *)

and cfg_signature s = 
   list_concat_map cfg_signature_item s.sig_items

(** Generate the top-level Coq declarations associated with 
    a Caml module. *)

and cfg_module id m =
charguer's avatar
charguer committed
1313
   let id = lift_module_name id in
charguer's avatar
init  
charguer committed
1314 1315 1316 1317 1318 1319 1320
   let rec aux bindings cast m =
      let return def =
         Coqtop_module (id, bindings, cast, def) in
      match m.mod_desc with
      | Tmod_ident p -> return (Mod_def_inline [lift_full_path_name p]), None
      | Tmod_structure str -> return Mod_def_declare, Some (cfg_structure str)
      | Tmod_functor (x, mt, m1) -> 
charguer's avatar
charguer committed
1321
          let x = lift_module_name x in
charguer's avatar
init  
charguer committed
1322 1323 1324 1325
          if cast <> Mod_cast_free then unsupported "cast before arguments in module declaration";
          begin match mt.mty_desc with
          | Tmty_ident p -> aux (([x], Mod_typ_var (lift_full_path_name p))::bindings) cast m1
          | _ -> 
charguer's avatar
charguer committed
1326
             (* hack for Dijkstra   Printf.printf "-->%s %s\n" (lift_module_name x) id; Pident (Ident.create ("PqueueSig") *)
charguer's avatar
init  
charguer committed
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
             if id = "MLDijkstra" && x = "MLPqueue" 
             then 
               aux (([x], Mod_typ_with_mod (Mod_typ_var "MLPqueueSig", "MLElement", "MLNextNode"))::bindings) cast m1
             else unsupported "functor with on-the-fly signature for its argument"
            
          end
      | Tmod_apply (m1, m2, coercion) ->
          let rec get_apps acc m0 =
             match m0.mod_desc with
             | Tmod_ident p -> lift_full_path_name p :: List.rev acc
             | Tmod_apply (m1, m2, coercion) -> 
                 begin match m2.mod_desc with
                 | Tmod_ident p -> get_apps (lift_full_path_name p :: acc) m1
                 | _ -> unsupported "module application can only be made between module paths"
                 end
             | _ -> unsupported "module application can only be made between module paths"
             in
          return (Mod_def_inline (get_apps [] m)), None
      | Tmod_constraint(m1, _, mtd, coercion) ->
          begin match mtd with
          | Tmodtype_implicit -> unsupported "implicit module type constraint"
          | Tmodtype_explicit mt ->
              begin match mt.mty_desc with
              | Tmty_ident p -> aux bindings (Mod_cast_super (Mod_typ_var (lift_full_path_name p))) m1
              | Tmty_signature s -> 
                unsupported "module constraint is not just a name (3) -- todo: should be supported"  
               (*Printf.printf "%d\n" (List.length s);
                begin match List.hd s with
                 | Tsig_value (id,vd) -> unsupported "u" 
                 | Tsig_type (id, td, rs) -> unsupported "x" 
                 | Tsig_module (id,mt,rs) ->   unsupported "y" 
                 | Tsig_modtype (id,decl) ->   unsupported "z"  
                 | Tsig_exception _ -> unsupported "exceptions"
                 | Tsig_class _ -> unsupported "objects"
                 | Tsig_cltype _ -> unsupported "objects"
                 end
                  *)
              | Tmty_with _ -> unsupported "module sig with"
              | Tmty_typeof _ -> unsupported "type of module"
              | _ -> unsupported "module constraint is not just a name (1)"  
              end
           end
      | Tmod_unpack _ -> unsupported "unpack"
      in
   let mod_dec, str_opt = aux [] Mod_cast_free m in
   match str_opt with
   | None -> [ Cftop_coqs [ mod_dec ] ]
   | Some str -> [ Cftop_coqs [ mod_dec ] ] @ str @ [ Cftop_coqs [ Coqtop_end id ] ]

(** Generate the top-level Coq declarations associated with 
    a Caml file. *)

let cfg_file str =
charguer's avatar
charguer committed
1380 1381
   [ Cftop_coqs ([ 
      Coqtop_set_implicit_args; 
1382
      Coqtop_require [ "Coq.ZArith.BinInt"; "LibLogic"; "LibRelation"; "LibInt"; "Shared"; "CFHeaps"; "CFApp" ];  
charguer's avatar
charguer committed
1383
      Coqtop_require_import ["CFHeader"];
charguer's avatar
charguer committed
1384
      Coqtop_require ["CFPrint"];
charguer's avatar
charguer committed
1385
      Coqtop_custom "Open Scope list_scope.";
charguer's avatar
charguer committed
1386 1387
      Coqtop_custom "Local Notation \"'int'\" := (Coq.ZArith.BinInt.Z).";
      Coqtop_custom "Delimit Scope Z_scope with Z." 
charguer's avatar
charguer committed
1388
      ]
charguer's avatar
charguer committed
1389
      @ (external_modules_get_coqtop())) ]
charguer's avatar
init  
charguer committed
1390 1391
   @ cfg_structure str

charguer's avatar
charguer committed
1392 1393 1394 1395 1396

      (*deprecated: (if !pure_mode then "FuncPrim" else "CFHeader") *)


      (* TODO: prevent "let int = 3" to work *)