typing.ml 27 KB
Newer Older
1 2
(**************************************************************************)
(*                                                                        *)
Jean-Christophe Filliâtre's avatar
headers  
Jean-Christophe Filliâtre committed
3 4 5 6 7
(*  Copyright (C) 2010-                                                   *)
(*    Francois Bobot                                                      *)
(*    Jean-Christophe Filliatre                                           *)
(*    Johannes Kanig                                                      *)
(*    Andrei Paskevich                                                    *)
8 9 10 11 12 13 14 15 16 17 18
(*                                                                        *)
(*  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.                  *)
(*                                                                        *)
(**************************************************************************)
19

20 21
open Util
open Format
22
open Pp
23
open Ident
24
open Ty
25
open Term
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
26
open Ptree
27
open Theory
28

29 30 31
(** errors *)

type error = 
32
  | Message of string
33
  | ClashType of string
34
  | DuplicateTypeVar of string
35
  | UnboundType of string
36
  | TypeArity of qualid * int * int
37
  | Clash of string
38 39
  | PredicateExpected
  | TermExpected
40
  | UnboundSymbol of string
41 42
  | TermExpectedType of (formatter -> unit) * (formatter -> unit) 
      (* actual / expected *)
43
  | BadNumberOfArguments of Ident.ident * int * int 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
44
  | ClashTheory of string
45
  | ClashNamespace of string
46
  | UnboundTheory of string
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
47
  | AlreadyTheory of string
48
  | UnboundNamespace of string
49 50 51 52
  | UnboundFile of string
  | UnboundDir of string
  | AmbiguousPath of string * string
  | NotInLoadpath of string
53 54
  | CyclicTypeDef
  | UnboundTypeVar of string
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
55
  | AnyMessage of string
56 57 58 59 60 61 62

exception Error of error

let error ?loc e = match loc with
  | None -> raise (Error e)
  | Some loc -> raise (Loc.Located (loc, Error e))

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
63 64 65 66 67 68 69 70 71 72 73
let errorm ?loc f =
  let buf = Buffer.create 512 in
  let fmt = Format.formatter_of_buffer buf in
  Format.kfprintf 
    (fun _ -> 
       Format.pp_print_flush fmt ();
       let s = Buffer.contents buf in
       Buffer.clear buf;
       error ?loc (AnyMessage s))
    fmt f

74
let rec print_qualid fmt = function
75
  | Qident s -> fprintf fmt "%s" s.id
76
  | Qdot (m, s) -> fprintf fmt "%a.%s" print_qualid m s.id
77

78
let report fmt = function
79 80
  | Message s ->
      fprintf fmt "%s" s
81 82
  | ClashType s ->
      fprintf fmt "clash with previous type %s" s
83
  | DuplicateTypeVar s ->
84
      fprintf fmt "duplicate type parameter %s" s
85
  | UnboundType s ->
86
       fprintf fmt "Unbound type '%s'" s
87
  | TypeArity (id, a, n) ->
88
      fprintf fmt "@[The type %a expects %d argument(s),@ " print_qualid id a;
89 90
      fprintf fmt "but is applied to %d argument(s)@]" n
  | Clash id ->
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
91
      fprintf fmt "Clash with previous symbol %s" id
92 93 94 95
  | PredicateExpected ->
      fprintf fmt "syntax error: predicate expected"
  | TermExpected ->
      fprintf fmt "syntax error: term expected"
96
  | UnboundSymbol s ->
97
       fprintf fmt "Unbound symbol '%s'" s
98
  | BadNumberOfArguments (s, n, m) ->
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
99
      fprintf fmt "@[Symbol `%s' is applied to %d terms,@ " s.id_short n;
100 101 102 103
      fprintf fmt "but is expecting %d arguments@]" m
  | TermExpectedType (ty1, ty2) ->
      fprintf fmt "@[This term has type "; ty1 fmt; 
      fprintf fmt "@ but is expected to have type@ "; ty2 fmt; fprintf fmt "@]"
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
104 105
  | ClashTheory s ->
      fprintf fmt "clash with previous theory %s" s
106 107
  | ClashNamespace s ->
      fprintf fmt "clash with previous namespace %s" s
108 109
  | UnboundTheory s ->
      fprintf fmt "unbound theory %s" s
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
110 111
  | AlreadyTheory s ->
      fprintf fmt "already using a theory with name %s" s
112 113
  | UnboundNamespace s ->
      fprintf fmt "unbound namespace %s" s
114 115 116 117 118 119 120 121
  | UnboundFile s ->
      fprintf fmt "no such file %s" s
  | UnboundDir s ->
      fprintf fmt "no such directory %s" s
  | AmbiguousPath (f1, f2) ->
      fprintf fmt "@[ambiguous path:@ both `%s'@ and `%s'@ match@]" f1 f2
  | NotInLoadpath f ->
      fprintf fmt "cannot find `%s' in loadpath" f
122 123 124 125
  | CyclicTypeDef ->
      fprintf fmt "cyclic type definition"
  | UnboundTypeVar s ->
      fprintf fmt "unbound type variable '%s" s
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
126 127
  | AnyMessage s ->
      fprintf fmt "%s" s
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
128

129 130
(** Environments *)

131
module S = Set.Make(String)
132 133 134 135
module M = Map.Make(String)

type env = {
  loadpath : string list;
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
136
  theories : theory M.t; (* local theories *)
137 138 139 140 141 142
}

let create lp = {
  loadpath = lp;
  theories = M.empty;
}
143 144

(** typing using destructive type variables 
145

146 147 148 149 150 151
    parsed trees        intermediate trees       typed trees
      (Ptree)               (D below)               (Term)
   -----------------------------------------------------------
     ppure_type  ---dty--->   dty       ---ty--->    ty
      lexpr      --dterm-->   dterm     --term-->    term
      lexpr      --dfmla-->   dfmla     --fmla-->    fmla
152

153 154 155 156 157 158 159 160 161 162 163 164 165
*)

(** types with destructive type variables *)

type dty =
  | Tyvar of type_var
  | Tyapp of tysymbol * dty list
      
and type_var = { 
  tag : int; 
  user : bool;
  tvsymbol : tvsymbol;
  mutable type_val : dty option;
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
166
  type_var_loc : loc option;
167
}
168

169 170 171 172
let rec print_dty fmt = function
  | Tyvar { type_val = Some t } ->
      print_dty fmt t
  | Tyvar { type_val = None; tvsymbol = v } ->
173
      fprintf fmt "'%s" v.id_short
174
  | Tyapp (s, []) ->
175
      fprintf fmt "%s" s.ts_name.id_short
176
  | Tyapp (s, [t]) -> 
177
      fprintf fmt "%a %s" print_dty t s.ts_name.id_short
178
  | Tyapp (s, l) -> 
179 180
      fprintf fmt "(%a) %s" 
	(print_list comma print_dty) l s.ts_name.id_short
181

182 183
let create_type_var =
  let t = ref 0 in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
184
  fun ?loc ~user tv -> 
185
    incr t; 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
186 187
    { tag = !t; user = user; tvsymbol = tv; type_val = None;
      type_var_loc = loc }
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

let rec occurs v = function
  | Tyvar { type_val = Some t } -> occurs v t
  | Tyvar { tag = t; type_val = None } -> v.tag = t
  | Tyapp (_, l) -> List.exists (occurs v) l

(* destructive type unification *)
let rec unify t1 t2 = match t1, t2 with
  | Tyvar { type_val = Some t1 }, _ ->
      unify t1 t2
  | _, Tyvar { type_val = Some t2 } ->
      unify t1 t2
  | Tyvar v1, Tyvar v2 when v1.tag = v2.tag ->
      true
	(* instantiable variables *)
  | Tyvar ({user=false} as v), t
  | t, Tyvar ({user=false} as v) ->
      not (occurs v t) && (v.type_val <- Some t; true)
	(* recursive types *)
  | Tyapp (s1, l1), Tyapp (s2, l2) ->
      s1 == s2 && List.length l1 = List.length l2 &&
	  List.for_all2 unify l1 l2
  | Tyapp _, _ | _, Tyapp _ ->
      false
	(* other cases *)
  | Tyvar {user=true; tag=t1}, Tyvar {user=true; tag=t2} -> 
      t1 = t2

216
(** Destructive typing environment, that is
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
217
    environment + local variables.
218
    It is only local to this module and created with [create_denv] below. *)
219 220

type denv = { 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
221
  th : theory_uc; (* the theory under construction *)
222 223
  utyvars : (string, type_var) Hashtbl.t; (* user type variables *)
  dvars : dty M.t; (* local variables, to be bound later *)
224 225
}

226 227
let create_denv th = { 
  th = th; 
228 229 230
  utyvars = Hashtbl.create 17; 
  dvars = M.empty;
}
231

232 233 234 235
let find_user_type_var x env =
  try
    Hashtbl.find env.utyvars x
  with Not_found ->
236
    (* TODO: shouldn't we localize this ident? *)
237 238
    let v = create_tvsymbol (id_fresh x) in
    let v = create_type_var ~user:true v in
239 240 241
    Hashtbl.add env.utyvars x v;
    v
 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
242 243
(* Specialize *)

244
module Htv = Hid
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
245

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
246
let find_type_var ~loc env tv =
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
247 248 249
  try
    Htv.find env tv
  with Not_found ->
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
250
    let v = create_type_var ~loc ~user:false tv in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
251 252 253
    Htv.add env tv v;
    v
 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
254
let rec specialize ~loc env t = match t.ty_node with
255
  | Ty.Tyvar tv -> 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
256
      Tyvar (find_type_var ~loc env tv)
257
  | Ty.Tyapp (s, tl) ->
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
258
      Tyapp (s, List.map (specialize ~loc env) tl)
259

260 261 262
(** generic find function using a path *)

let find_local_namespace {id=x; id_loc=loc} ns = 
263
  try Mnm.find x ns.ns_ns
264 265 266 267 268 269 270 271 272 273 274 275
  with Not_found -> error ~loc (UnboundNamespace x)

let rec find_namespace q ns = match q with
  | Qident t -> find_local_namespace t ns
  | Qdot (q, t) -> let ns = find_namespace q ns in find_local_namespace t ns

let rec find f q ns = match q with
  | Qident x -> f x ns
  | Qdot (m, x) -> let ns = find_namespace m ns in f x ns

(** specific find functions using a path *)

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
276
let find_local_tysymbol {id=x; id_loc=loc} ns = 
277
  try Mnm.find x ns.ns_ts
278 279
  with Not_found -> error ~loc (UnboundType x)

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
280 281 282
let find_tysymbol_ns p ns =
  find find_local_tysymbol p ns

283
let find_tysymbol p th = 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
284
  find_tysymbol_ns p (get_namespace th)
285

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
286
let specialize_tysymbol ~loc x env =
287
  let s = find_tysymbol x env.th in
288
  let env = Htv.create 17 in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
289
  s, List.map (find_type_var ~loc env) s.ts_args
290
	
291 292
let find_lsymbol {id=x; id_loc=loc} ns = 
  try Mnm.find x ns.ns_ls
293 294
  with Not_found -> error ~loc (UnboundSymbol x)

295 296
let find_lsymbol_ns p ns = 
  find find_lsymbol p ns
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
297

298 299
let find_lsymbol p th = 
  find_lsymbol_ns p (get_namespace th)
300

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
301
let specialize_fsymbol ~loc s =
302 303 304 305
  let tl, t = match s.ls_value with
    | Some t -> s.ls_args, t
    | _ -> assert false (* FIXME: is it right? *)
  in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
306
  let env = Htv.create 17 in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
307
  s, List.map (specialize ~loc env) tl, specialize ?loc env t
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
308

309 310
let find_lsymbol {id=x; id_loc=loc} ns = 
  try Mnm.find x ns.ns_ls
311 312
  with Not_found -> error ~loc (UnboundSymbol x)

313 314
let find_lsymbol_ns p ns =
  find find_lsymbol p ns
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
315

316 317
let find_lsymbol p th =
  find_lsymbol_ns p (get_namespace th)
318

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
319
let specialize_psymbol ~loc s =
320 321 322 323
  let tl = match s.ls_value with
    | None -> s.ls_args
    | _ -> assert false (* FIXME: is it right? *)
  in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
324
  let env = Htv.create 17 in
325
  s, List.map (specialize ~loc env) tl
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
326

327 328
(** Typing types *)

329
let rec qloc = function
330
  | Qident x -> x.id_loc
331
  | Qdot (m, x) -> Loc.join (qloc m) x.id_loc
332 333

(* parsed types -> intermediate types *)
334 335 336 337 338

let rec type_inst s ty = match ty.ty_node with
  | Ty.Tyvar n -> Mid.find n s
  | Ty.Tyapp (ts, tyl) -> Tyapp (ts, List.map (type_inst s) tyl)

339
let rec dty env = function
340
  | PPTtyvar {id=x} -> 
341
      Tyvar (find_user_type_var x env)
342
  | PPTtyapp (p, x) ->
343
      let loc = qloc x in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
344
      let ts, tv = specialize_tysymbol ~loc x env in
345 346 347
      let np = List.length p in
      let a = List.length tv in
      if np <> a then error ~loc (TypeArity (x, a, np));
348 349 350 351 352 353 354 355
      let tyl = List.map (dty env) p in
      match ts.ts_def with
	| None -> 
	    Tyapp (ts, tyl)
	| Some ty -> 
	    let add m v t = Mid.add v t m in
            let s = List.fold_left2 add Mid.empty ts.ts_args tyl in
	    type_inst s ty
356 357

(* intermediate types -> types *)
358
let rec ty_of_dty = function
359
  | Tyvar { type_val = Some t } ->
360
      ty_of_dty t
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
361 362
  | Tyvar { type_val = None; user = false; type_var_loc = loc } ->
      errorm ?loc "undefined type variable"
363 364 365
  | Tyvar { tvsymbol = tv } ->
      Ty.ty_var tv
  | Tyapp (s, tl) ->
366
      Ty.ty_app s (List.map ty_of_dty tl)
367

368
(** Typing terms and formulas *)
369

370 371
type uquant = string list * dty

372 373 374
type dterm = { dt_node : dterm_node; dt_ty : dty }

and dterm_node =
375
  | Tvar of string
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
376
  | Tconst of constant
377
  | Tapp of lsymbol * dterm list
378 379
  | Tlet of dterm * string * dterm
(*   | Tcase of dterm * tbranch list *)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
380
  | Tnamed of string * dterm
381 382 383
  | Teps of string * dfmla

and dfmla = 
384
  | Fapp of lsymbol * dterm list
385
  | Fquant of quant * uquant list * dtrigger list list * dfmla
386 387 388 389 390
  | Fbinop of binop * dfmla * dfmla
  | Fnot of dfmla
  | Ftrue
  | Ffalse
  | Fif of dfmla * dfmla * dfmla
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
391
  | Flet of dterm * string * dfmla
392
(*   | Fcase of dterm * (pattern * dfmla) list *)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
393
  | Fnamed of string * dfmla
394

395 396 397 398
and dtrigger =
  | TRterm of dterm
  | TRfmla of dfmla

399 400 401 402 403 404
let binop = function
  | PPand -> Fand
  | PPor -> For
  | PPimplies -> Fimplies
  | PPiff -> Fiff

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
405 406 407 408 409
let rec trigger_not_a_term_exn = function
  | Error (TermExpected | UnboundSymbol _) -> true
  | Loc.Located (_, exn) -> trigger_not_a_term_exn exn
  | _ -> false

410 411 412 413 414
let rec dterm env t = 
  let n, ty = dterm_node t.pp_loc env t.pp_desc in
  { dt_node = n; dt_ty = ty }

and dterm_node loc env = function
415 416 417 418 419 420
  | PPvar (Qident {id=x}) when M.mem x env.dvars ->
      (* local variable *)
      let ty = M.find x env.dvars in
      Tvar x, ty
  | PPvar x -> 
      (* 0-arity symbol (constant) *)
421
      let s = find_lsymbol x env.th in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
422
      let s, tyl, ty = specialize_fsymbol ~loc s in
423
      let n = List.length tyl in
424
      if n > 0 then error ~loc (BadNumberOfArguments (s.ls_name, 0, n));
425
      Tapp (s, []), ty
426
  | PPapp (x, tl) ->
427
      let s = find_lsymbol x env.th in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
428
      let s, tyl, ty = specialize_fsymbol ~loc s in
429
      let tl = dtype_args s.ls_name loc env tyl tl in
430
      Tapp (s, tl), ty
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
431
  | PPconst (ConstInt _ as c) ->
432
      Tconst c, Tyapp (Ty.ts_int, [])
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
433
  | PPconst (ConstReal _ as c) ->
434
      Tconst c, Tyapp (Ty.ts_real, [])
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
435 436 437 438 439 440
  | PPlet ({id=x}, e1, e2) ->
      let e1 = dterm env e1 in
      let ty = e1.dt_ty in
      let env = { env with dvars = M.add x ty env.dvars } in
      let e2 = dterm env e2 in
      Tlet (e1, x, e2), e2.dt_ty
441 442
  | PPmatch _ ->
      assert false (* TODO *)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
443 444 445
  | PPnamed (x, e1) ->
      let e1 = dterm env e1 in
      Tnamed (x, e1), e1.dt_ty
446
  | PPquant _ | PPif _ 
447 448
  | PPprefix _ | PPinfix _ | PPfalse | PPtrue ->
      error ~loc TermExpected
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
449

450 451 452 453 454 455 456 457 458 459 460
and dfmla env e = match e.pp_desc with
  | PPtrue ->
      Ftrue
  | PPfalse ->
      Ffalse
  | PPprefix (PPnot, a) ->
      Fnot (dfmla env a)
  | PPinfix (a, (PPand | PPor | PPimplies | PPiff as op), b) ->
      Fbinop (binop op, dfmla env a, dfmla env b)
  | PPif (a, b, c) ->
      Fif (dfmla env a, dfmla env b, dfmla env c)  
461 462 463 464 465 466 467 468 469
  | PPquant (q, uqu, trl, a) ->
      let uquant env (idl,ty) =
        let ty = dty env ty in
        let env, idl = 
          map_fold_left
            (fun env {id=x} -> { env with dvars = M.add x ty env.dvars }, x)
            env idl
        in
        env, (idl,ty)
470
      in
471
      let env, uqu = map_fold_left uquant env uqu in
472 473 474
      let trigger e = (* FIXME? *)
	try 
	  TRterm (dterm env e) 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
475 476
	with exn when trigger_not_a_term_exn exn ->
	  TRfmla (dfmla env e) 
477 478
      in
      let trl = List.map (List.map trigger) trl in
479 480 481 482 483
      let q = match q with 
        | PPforall -> Fforall 
        | PPexists -> Fexists
      in
      Fquant (q, uqu, trl, dfmla env a)
484
  | PPapp (x, tl) ->
485
      let s = find_lsymbol x env.th in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
486
      let s, tyl = specialize_psymbol ~loc:e.pp_loc s in
487
      let tl = dtype_args s.ls_name e.pp_loc env tyl tl in
488
      Fapp (s, tl)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
489 490 491 492 493 494
  | PPlet ({id=x}, e1, e2) ->
      let e1 = dterm env e1 in
      let ty = e1.dt_ty in
      let env = { env with dvars = M.add x ty env.dvars } in
      let e2 = dfmla env e2 in
      Flet (e1, x, e2)
495 496
  | PPmatch _ ->
      assert false (* TODO *)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
497 498 499
  | PPnamed (x, f1) ->
      let f1 = dfmla env f1 in
      Fnamed (x, f1)
500 501
  | PPvar _ -> 
      assert false (* TODO *)
502
  | PPconst _ ->
503
      error ~loc:e.pp_loc PredicateExpected
504

505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
and dtype_args s loc env el tl =
  let n = List.length el and m = List.length tl in
  if n <> m then error ~loc (BadNumberOfArguments (s, m, n));
  let rec check_arg = function
    | [], [] -> 
	[]
    | a :: al, t :: bl ->
	let loc = t.pp_loc in
	let t = dterm env t in
	if not (unify a t.dt_ty) then
	  error ~loc (TermExpectedType ((fun f -> print_dty f t.dt_ty),
					(fun f -> print_dty f a)));
	t :: check_arg (al, bl)
    | _ ->
	assert false
  in
  check_arg (el, tl)

let rec term env t = match t.dt_node with
  | Tvar x ->
      assert (M.mem x env);
      t_var (M.find x env)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
527
  | Tconst c ->
528
      t_const c (ty_of_dty t.dt_ty)
529
  | Tapp (s, tl) ->
530
      t_app s (List.map (term env) tl) (ty_of_dty t.dt_ty)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
531 532 533 534 535 536 537
  | Tlet (e1, x, e2) ->
      let ty = ty_of_dty e1.dt_ty in
      let e1 = term env e1 in 
      let v = create_vsymbol (id_fresh x) ty in
      let env = M.add x v env in
      let e2 = term env e2 in
      t_let v e1 e2
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
538 539 540
  | Tnamed (x, e1) ->
      let e1 = term env e1 in
      t_label_add x e1
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
541 542
  | Teps _ ->
      assert false (*TODO*)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
543

544 545 546 547 548 549 550 551 552 553 554
and fmla env = function
  | Ftrue ->
      f_true
  | Ffalse ->
      f_false 
  | Fnot f ->
      f_not (fmla env f)
  | Fbinop (op, f1, f2) ->
      f_binary op (fmla env f1) (fmla env f2)
  | Fif (f1, f2, f3) ->
      f_if (fmla env f1) (fmla env f2) (fmla env f3)
555
  | Fquant (q, uqu, trl, f1) ->
556
      (* TODO: shouldn't we localize this ident? *)
557 558 559 560 561 562
      let uquant env (xl,ty) =
        let ty = ty_of_dty ty in
        map_fold_left
          (fun env x -> 
             let v = create_vsymbol (id_fresh x) ty in M.add x v env, v) 
          env xl 
563
      in
564 565 566
      let env, vl = map_fold_left uquant env uqu in
      (* TODO: triggers *)
      f_quant q (List.concat vl) [] (fmla env f1)
567 568
  | Fapp (s, tl) ->
      f_app s (List.map (term env) tl)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
569
  | Flet (e1, x, f2) ->
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
570 571 572 573
      let ty = ty_of_dty e1.dt_ty in
      let e1 = term env e1 in 
      let v = create_vsymbol (id_fresh x) ty in
      let env = M.add x v env in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
574 575 576
      let f2 = fmla env f2 in
      f_let v e1 f2
  | Fnamed (x, f1) ->
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
577
      let f1 = fmla env f1 in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
578
      f_label_add x f1
579

580
(** Typing declarations, that is building environments. *)
581 582 583

open Ptree

584
let add_types loc dl th =
585
  let ns = get_namespace th in
586 587 588 589
  let def = 
    List.fold_left 
      (fun def d -> 
	 let id = d.td_ident in
590 591
	 if M.mem id.id def || Mnm.mem id.id ns.ns_ts then 
	   error ~loc:id.id_loc (ClashType id.id);
592 593
	 M.add id.id d def) 
      M.empty dl 
594
  in
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
  let tysymbols = Hashtbl.create 17 in
  let rec visit x = 
    try
      match Hashtbl.find tysymbols x with
	| None -> error CyclicTypeDef
	| Some ts -> ts
    with Not_found ->
      Hashtbl.add tysymbols x None;
      let d = M.find x def in
      let id = d.td_ident.id in
      let vars = Hashtbl.create 17 in
      let vl = 
	List.map 
	  (fun v -> 
	     if Hashtbl.mem vars v.id then 
	       error ~loc:v.id_loc (DuplicateTypeVar v.id);
611
	     let i = create_tvsymbol (id_user v.id v.id_loc) in
612 613 614 615
	     Hashtbl.add vars v.id i;
	     i)
	  d.td_params 
      in
616
      let id = id_user id d.td_ident.id_loc in
617 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
      let ts = match d.td_def with
	| TDalias ty -> 
	    let rec apply = function
	      | PPTtyvar v -> 
		  begin 
		    try ty_var (Hashtbl.find vars v.id)
		    with Not_found -> error ~loc:v.id_loc (UnboundTypeVar v.id)
		  end
	      | PPTtyapp (tyl, q) ->
		  let ts = match q with
		    | Qident x when M.mem x.id def ->
			visit x.id
		    | Qident _ | Qdot _ ->
			find_tysymbol q th
		  in
		  try
		    ty_app ts (List.map apply tyl)
		  with Ty.BadTypeArity ->
		    error ~loc:(qloc q) (TypeArity (q, List.length ts.ts_args,
						    List.length tyl))
	    in
	    create_tysymbol id vl (Some (apply ty))
	| TDabstract | TDalgebraic _ -> 
	    create_tysymbol id vl None
      in
      Hashtbl.add tysymbols x (Some ts);
      ts
  in
645 646 647 648 649 650
  let th' = 
    let tsl = 
      M.fold (fun x _ tsl -> let ts = visit x in (ts, Tabstract) :: tsl) def []
    in
    add_decl th (create_type tsl)
  in
651
  let decl d = 
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
    let ts, th' = match Hashtbl.find tysymbols d.td_ident.id with
      | None -> 
	  assert false
      | Some ts -> 
	  let th' = create_denv th' in
	  let vars = th'.utyvars in
	  List.iter 
	    (fun v -> 
	       Hashtbl.add vars v.id_short (create_type_var ~user:true v)) 
	    ts.ts_args;
	  ts, th'
    in
    let d = match d.td_def with
      | TDabstract | TDalias _ -> 
	  Tabstract
      | TDalgebraic cl -> 
	  let ty = ty_app ts (List.map ty_var ts.ts_args) in
	  let constructor (loc, id, pl) = 
	    let param (_, t) = ty_of_dty (dty th' t) in
	    let tyl = List.map param pl in
672
	    create_fconstr (id_user id.id id.id_loc) tyl ty
673 674 675 676
	  in
	  Talgebraic (List.map constructor cl)
    in
    ts, d
677 678
  in
  let dl = List.map decl dl in
679
  add_decl th (create_type dl)
680

Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
681 682 683 684
let env_of_vsymbol_list vl =
  List.fold_left (fun env v -> M.add v.vs_name.id_short v env) M.empty vl

let add_logics loc dl th =
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
685
  let ns = get_namespace th in
Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
686 687
  let fsymbols = Hashtbl.create 17 in
  let psymbols = Hashtbl.create 17 in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
688
  let denvs = Hashtbl.create 17 in
Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
689 690 691
  (* 1. create all symbols and make an environment with these symbols *)
  let create_symbol th d = 
    let id = d.ld_ident.id in
692
    if Hashtbl.mem denvs id || Mnm.mem id ns.ns_ls then error ~loc (Clash id);
693
    let v = id_user id loc in
Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
694
    let denv = create_denv th in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
695
    Hashtbl.add denvs id denv;
696
    let type_ty (_,t) = ty_of_dty (dty denv t) in
Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
697 698 699 700 701
    let pl = List.map type_ty d.ld_params in
    match d.ld_type with
      | None -> (* predicate *)
	  let ps = create_psymbol v pl in
	  Hashtbl.add psymbols id ps;
702
	  add_decl th (create_logic [Lpredicate (ps, None)])
Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
703 704
      | Some t -> (* function *)
	  let t = type_ty (None, t) in
705
	  let fs = create_fsymbol v pl t in
Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
706
	  Hashtbl.add fsymbols id fs;
707
	  add_decl th (create_logic [Lfunction (fs, None)])
Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
708 709 710 711 712 713 714 715 716
  in
  let th' = List.fold_left create_symbol th dl in
  (* 2. then type-check all definitions *)
  let type_decl d = 
    let id = d.ld_ident.id in
    let dadd_var denv (x, ty) = match x with
      | None -> denv
      | Some id -> { denv with dvars = M.add id.id (dty denv ty) denv.dvars }
    in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
717 718 719
    let denv = Hashtbl.find denvs id in
    let denv = { denv with th = th' } in
    let denv = List.fold_left dadd_var denv d.ld_params in
Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
720 721
    let create_var (x, _) ty = 
      let id = match x with 
722 723
	| None -> id_fresh "%x"
	| Some id -> id_user id.id id.id_loc
Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
724 725 726
      in
      create_vsymbol id ty
    in
727
    let mk_vlist = List.map2 create_var d.ld_params in
Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
728 729 730
    match d.ld_type with
    | None -> (* predicate *)
	let ps = Hashtbl.find psymbols id in
731 732
        let defn = match d.ld_def with
	  | None -> None
Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
733 734
	  | Some f -> 
	      let f = dfmla denv f in
735 736 737 738
              let vl = match ps.ls_value with
                | None -> mk_vlist ps.ls_args
                | _ -> assert false
              in
Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
739
	      let env = env_of_vsymbol_list vl in
740 741 742
              Some (make_ps_defn ps vl (fmla env f))
        in
        Lpredicate (ps, defn)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
743
    | Some ty -> (* function *)
Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
744
	let fs = Hashtbl.find fsymbols id in
745 746
        let defn = match d.ld_def with
	  | None -> None
Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
747
	  | Some t -> 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
748
	      let loc = t.pp_loc in
Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
749
	      let t = dterm denv t in
750 751 752 753
              let vl = match fs.ls_value with
                | Some _ -> mk_vlist fs.ls_args
                | _ -> assert false
              in
Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
754
	      let env = env_of_vsymbol_list vl in
755 756 757 758 759 760
              try Some (make_fs_defn fs vl (term env t))
	      with _ -> error ~loc (TermExpectedType 
                ((fun f -> print_dty f t.dt_ty),
                 (fun f -> print_dty f (dty denv ty))))
        in
        Lfunction (fs, defn)
Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
761 762
  in
  let dl = List.map type_decl dl in
763
  add_decl th (create_logic dl)
764

765

766 767 768
let term env t =
  let denv = create_denv env in
  let t = dterm denv t in
769
  term M.empty t
770

Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
771 772 773 774 775
let fmla env f =
  let denv = create_denv env in
  let f = dfmla denv f in
  fmla M.empty f

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
776 777 778
let add_prop k loc s f th =
  let f = fmla th f in
  try
779
    add_decl th (create_prop k (id_user s.id loc) f)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
780 781
  with ClashSymbol _ ->
    error ~loc (Clash s.id)
782

783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
let find_in_loadpath env f =
  let rec find c lp = match lp, c with
    | [], None -> 
	error ~loc:f.id_loc (NotInLoadpath f.id)
    | [], Some file ->
	file
    | dir :: lp, _ ->
	let file = Filename.concat dir f.id in
	if Sys.file_exists file then begin match c with
	  | None -> find (Some file) lp
	  | Some file' -> error ~loc:f.id_loc (AmbiguousPath (file, file'))
	end else
	  find c lp
  in
  find None env.loadpath

let locate_file env q = 
  let rec locate_dir = function
    | Qident d ->
	find_in_loadpath env d
    | Qdot (q, d) -> 
	let dir = locate_dir q in
	let dir = Filename.concat dir d.id in
	if not (Sys.file_exists dir) then error ~loc:d.id_loc (UnboundDir dir);
	dir
  in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
809 810 811 812 813 814 815 816 817
  match q with
    | Qident f -> 
	find_in_loadpath env { f with id = f.id ^ ".why" }
    | Qdot (p, f) -> 
	let dir = locate_dir p in 
	let file = Filename.concat dir f.id ^ ".why" in
	if not (Sys.file_exists file) then 
	  error ~loc:(qloc q) (UnboundFile file);
	file
818

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
819
let loaded_theories = Hashtbl.create 17 (* file -> string -> Theory.t *)
820 821

(* parse file and store all theories into parsed_theories *)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
822 823 824 825 826 827 828
let load_file file =
  let c = open_in file in
  let lb = Lexing.from_channel c in
  Loc.set_file file lb;
  let tl = Lexer.parse_logic_file lb in 
  close_in c;
  tl
829

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
830
let prop_kind = function
831 832 833
  | Kaxiom -> Paxiom
  | Kgoal -> Pgoal
  | Klemma -> Plemma
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
834

835
let rec find_theory env q = match q with
836
  | Qident id -> 
837
      (* local theory *)
838 839 840 841 842
      begin 
	try M.find id.id env.theories 
	with Not_found -> error ~loc:id.id_loc (UnboundTheory id.id) 
      end
  | Qdot (f, id) ->
843 844
      (* theory in file f *)
      let file = locate_file env f in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
845 846 847 848 849 850 851 852 853 854 855 856
      if not (Hashtbl.mem loaded_theories file) then begin
	let tl = load_file file in
	let h = Hashtbl.create 17 in
	Hashtbl.add loaded_theories file h;
	let env = { env with theories = M.empty } in
	let add env pt =
	  let t, env = add_theory env pt in
	  Hashtbl.add h t.th_name.id_short t;
	  env
	in
	ignore (List.fold_left add env tl);
      end;
857
      let h = Hashtbl.find loaded_theories file in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
858 859
      try Hashtbl.find h id.id
      with Not_found -> error ~loc:id.id_loc (UnboundTheory id.id)
860 861

and type_theory env id pt =
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
862
  (* TODO: use long name *)
863
  let n = id_user id.id id.id_loc in
864
  let th = create_theory n in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
865
  let th = add_decls env th pt.pt_decl in
866 867 868
  close_theory th

and add_decls env th = List.fold_left (add_decl env) th
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
869

870
and add_decl env th = function
871 872
  | TypeDecl (loc, dl) ->
      add_types loc dl th
Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
873 874
  | Logic (loc, dl) ->
      add_logics loc dl th
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
875 876
  | Prop (loc, k, s, f) ->
      add_prop (prop_kind k) loc s f th
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
877
  | UseClone (loc, use, subst) ->
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
878
      let t = find_theory env use.use_theory in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
879 880 881 882 883 884 885
      let use_or_clone th = match subst with
	| None -> 
	    use_export th t
	| Some s -> 
	    let add_ts m (p, q) = 
	      Mts.add (find_tysymbol_ns p t.th_export) (find_tysymbol q th) m
	    in
886 887
	    let add_ls m (p, q) = 
	      Mls.add (find_lsymbol_ns p t.th_export) (find_lsymbol q th) m
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
888 889 890
	    in
	    let s = 
	      { inst_ts = List.fold_left add_ts Mts.empty s.ts_subst;
891
                inst_ls = List.fold_left add_ls Mls.empty s.ls_subst; }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
892 893 894
	    in
	    clone_export th t s
      in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
895
      let n = match use.use_as with 
896 897
	| None -> t.th_name.id_short
	| Some x -> x.id
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
898
      in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
899
      begin try match use.use_imp_exp with
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
900 901 902
	| Nothing ->
	    (* use T = namespace T use_export T end *)
	    let th = open_namespace th in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
903
	    let th = use_or_clone th in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
904 905 906 907
	    close_namespace th n ~import:false
	| Import ->
	    (* use import T = namespace T use_export T end import T *)
	    let th = open_namespace th in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
908
	    let th = use_or_clone th in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
909 910
	    close_namespace th n ~import:true
	| Export ->
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
911
	    use_or_clone th 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
912 913
      with Theory.ClashSymbol s ->
	error ~loc (Clash s)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
914
      end
915 916
  | Namespace (_, {id=id; id_loc=loc}, dl) ->
      let ns = get_namespace th in
917
      if Mnm.mem id ns.ns_ns then error ~loc (ClashNamespace id);
918
      let th = open_namespace th in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
919
      let th = add_decls env th dl in
920
      close_namespace th id ~import:false
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
921
  | Inductive_def _ ->
922
      assert false (*TODO*)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
923

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
924 925 926
and add_theory env pt =
  let id = pt.pt_name in
  if M.mem id.id env.theories then error ~loc:pt.pt_loc (ClashTheory id.id);
927
  let t = type_theory env id pt in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
928 929 930 931 932
  t, { env with theories = M.add id.id t env.theories }

let add_theories =
  List.fold_left
    (fun env pt -> let _, env = add_theory env pt in env) 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
933

934
let list_theory env = 
935
  List.rev (M.fold (fun _ v acc -> v::acc) env.theories [])
936

937 938
(*
Local Variables: 
Jean-Christophe Filliâtre's avatar
logic  
Jean-Christophe Filliâtre committed
939
compile-command: "make -C ../.. test"
940 941
End: 
*)