src/Pure/sign.ML
author wenzelm
Tue Nov 30 11:04:07 1993 +0100 (1993-11-30)
changeset 171 ab0f93a291b5
parent 169 1b2765146aab
child 197 7c7179e687b2
permissions -rw-r--r--
*** empty log message ***
wenzelm@19
     1
(*  Title:      Pure/sign.ML
clasohm@0
     2
    ID:         $Id$
wenzelm@19
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1992  University of Cambridge
clasohm@0
     5
wenzelm@143
     6
The abstract types "sg" (signatures) and "cterm" / "ctyp" (certified terms /
wenzelm@143
     7
typs under a signature).
clasohm@0
     8
*)
clasohm@0
     9
wenzelm@19
    10
signature SIGN =
clasohm@0
    11
sig
clasohm@0
    12
  structure Type: TYPE
clasohm@0
    13
  structure Symtab: SYMTAB
clasohm@0
    14
  structure Syntax: SYNTAX
wenzelm@143
    15
  sharing Symtab = Type.Symtab
clasohm@0
    16
  type sg
clasohm@0
    17
  type cterm
clasohm@0
    18
  type ctyp
clasohm@0
    19
  val cfun: (term -> term) -> (cterm -> cterm)
clasohm@0
    20
  val cterm_of: sg -> term -> cterm
clasohm@0
    21
  val ctyp_of: sg -> typ -> ctyp
clasohm@0
    22
  val extend: sg -> string ->
wenzelm@19
    23
        (class * class list) list * class list *
wenzelm@19
    24
        (string list * int) list *
wenzelm@19
    25
        (string list * (sort list * class)) list *
wenzelm@19
    26
        (string list * string)list * Syntax.sext option -> sg
clasohm@0
    27
  val merge: sg * sg -> sg
clasohm@0
    28
  val pure: sg
clasohm@0
    29
  val read_cterm: sg -> string * typ -> cterm
clasohm@0
    30
  val read_ctyp: sg -> string -> ctyp
clasohm@0
    31
  val read_insts: sg -> (indexname -> typ option) * (indexname -> sort option)
wenzelm@19
    32
                  -> (indexname -> typ option) * (indexname -> sort option)
wenzelm@19
    33
                  -> (string*string)list
wenzelm@19
    34
                  -> (indexname*ctyp)list * (cterm*cterm)list
clasohm@0
    35
  val read_typ: sg * (indexname -> sort option) -> string -> typ
clasohm@0
    36
  val rep_cterm: cterm -> {T: typ, t: term, sign: sg, maxidx: int}
clasohm@0
    37
  val rep_ctyp: ctyp -> {T: typ, sign: sg}
clasohm@0
    38
  val rep_sg: sg -> {tsig: Type.type_sig,
wenzelm@19
    39
                     const_tab: typ Symtab.table,
wenzelm@19
    40
                     syn: Syntax.syntax,
wenzelm@19
    41
                     stamps: string ref list}
clasohm@0
    42
  val string_of_cterm: cterm -> string
clasohm@0
    43
  val string_of_ctyp: ctyp -> string
clasohm@0
    44
  val pprint_cterm: cterm -> pprint_args -> unit
clasohm@0
    45
  val pprint_ctyp: ctyp -> pprint_args -> unit
clasohm@0
    46
  val string_of_term: sg -> term -> string
clasohm@0
    47
  val string_of_typ: sg -> typ -> string
clasohm@0
    48
  val pprint_term: sg -> term -> pprint_args -> unit
clasohm@0
    49
  val pprint_typ: sg -> typ -> pprint_args -> unit
clasohm@0
    50
  val term_of: cterm -> term
clasohm@0
    51
  val typ_of: ctyp -> typ
clasohm@0
    52
  val pretty_term: sg -> term -> Syntax.Pretty.T
clasohm@0
    53
end;
clasohm@0
    54
wenzelm@143
    55
functor SignFun(structure Type: TYPE and Syntax: SYNTAX): SIGN =
wenzelm@143
    56
struct
clasohm@0
    57
clasohm@0
    58
structure Type = Type;
clasohm@0
    59
structure Symtab = Type.Symtab;
clasohm@0
    60
structure Syntax = Syntax;
clasohm@0
    61
structure Pretty = Syntax.Pretty
clasohm@0
    62
wenzelm@143
    63
wenzelm@143
    64
(* Signatures of theories. *)
wenzelm@143
    65
wenzelm@19
    66
datatype sg =
wenzelm@143
    67
  Sg of {
wenzelm@143
    68
    tsig: Type.type_sig,            (*order-sorted signature of types*)
wenzelm@143
    69
    const_tab: typ Symtab.table,    (*types of constants*)
wenzelm@143
    70
    syn: Syntax.syntax,             (*syntax for parsing and printing*)
wenzelm@143
    71
    stamps: string ref list};       (*unique theory indentifier*)
clasohm@0
    72
clasohm@0
    73
clasohm@0
    74
fun rep_sg (Sg args) = args;
clasohm@0
    75
clasohm@0
    76
fun string_of_typ(Sg{tsig,syn,...}) = Syntax.string_of_typ syn;
clasohm@0
    77
clasohm@0
    78
fun pprint_typ(Sg{syn,...}) = Pretty.pprint o Pretty.quote o (Syntax.pretty_typ syn);
clasohm@0
    79
clasohm@0
    80
(*Is constant present in table with more generic type?*)
clasohm@0
    81
fun valid_const tsig ctab (a,T) = case Symtab.lookup(ctab, a) of
wenzelm@19
    82
        Some U => Type.typ_instance(tsig,T,U) | _ => false;
clasohm@0
    83
clasohm@0
    84
clasohm@0
    85
(*Check a term for errors.  Are all constants and types valid in signature?
clasohm@0
    86
  Does not check that term is well-typed!*)
wenzelm@19
    87
fun term_errors (sign as Sg{tsig,const_tab,...}) =
clasohm@0
    88
let val showtyp = string_of_typ sign;
clasohm@0
    89
    fun terrs (Const (a,T), errs) =
wenzelm@19
    90
        if valid_const tsig const_tab (a,T)
wenzelm@19
    91
        then Type.type_errors (tsig,showtyp) (T,errs)
wenzelm@19
    92
        else ("Illegal type for constant: " ^ a ^ ": " ^ showtyp T) :: errs
clasohm@0
    93
      | terrs (Free (_,T), errs) = Type.type_errors (tsig,showtyp) (T,errs)
clasohm@0
    94
      | terrs (Var  ((a,i),T), errs) =
wenzelm@19
    95
        if  i>=0  then  Type.type_errors (tsig,showtyp) (T,errs)
wenzelm@19
    96
        else  ("Negative index for Var: " ^ a) :: errs
clasohm@0
    97
      | terrs (Bound _, errs) = errs (*loose bvars detected by type_of*)
wenzelm@19
    98
      | terrs (Abs (_,T,t), errs) =
wenzelm@19
    99
            Type.type_errors(tsig,showtyp)(T,terrs(t,errs))
clasohm@0
   100
      | terrs (f$t, errs) = terrs(f, terrs (t,errs))
clasohm@0
   101
in  terrs  end;
clasohm@0
   102
clasohm@0
   103
wenzelm@169
   104
clasohm@0
   105
(** The Extend operation **)
clasohm@0
   106
wenzelm@169
   107
(* Extend a signature: may add classes, types and constants. The "ref" in
wenzelm@169
   108
   stamps ensures that no two signatures are identical -- it is impossible to
wenzelm@169
   109
   forge a signature. *)
wenzelm@169
   110
wenzelm@169
   111
fun extend (Sg {tsig, const_tab, syn, stamps}) name
wenzelm@169
   112
  (classes, default, types, arities, const_decs, opt_sext) =
wenzelm@169
   113
  let
wenzelm@169
   114
    fun err_in_typ s = error ("The error(s) above occurred in type " ^ quote s);
wenzelm@169
   115
wenzelm@169
   116
    fun read_typ tsg sy s =
wenzelm@169
   117
      Syntax.read_typ sy (K (Type.defaultS tsg)) s handle ERROR => err_in_typ s;
wenzelm@169
   118
wenzelm@169
   119
    fun check_typ tsg sy ty =
wenzelm@169
   120
      (case Type.type_errors (tsg, Syntax.string_of_typ sy) (ty, []) of
wenzelm@169
   121
        [] => ty
wenzelm@169
   122
      | errs => (prs (cat_lines errs); err_in_typ (Syntax.string_of_typ sy ty)));
wenzelm@169
   123
wenzelm@169
   124
    (*reset TVar indices to zero, renaming to preserve distinctness*)
wenzelm@169
   125
    fun zero_tvar_indices T =
wenzelm@169
   126
      let
wenzelm@169
   127
        val inxSs = typ_tvars T;
wenzelm@169
   128
        val nms' = variantlist (map (#1 o #1) inxSs, []);
wenzelm@169
   129
        val tye = map (fn ((v, S), a) => (v, TVar ((a, 0), S))) (inxSs ~~ nms');
wenzelm@169
   130
      in typ_subst_TVars tye T end;
wenzelm@169
   131
wenzelm@169
   132
    (*read and check the type mentioned in a const declaration; zero type var
wenzelm@169
   133
      indices because type inference requires it*)
wenzelm@169
   134
wenzelm@169
   135
    fun read_consts tsg sy (cs, s) =
wenzelm@169
   136
      let val ty = zero_tvar_indices (Type.varifyT (read_typ tsg sy s));
wenzelm@169
   137
      in
wenzelm@169
   138
        (case Type.type_errors (tsg, Syntax.string_of_typ sy) (ty, []) of
wenzelm@169
   139
          [] => (cs, ty)
wenzelm@169
   140
        | errs => error (cat_lines (("Error in type of constants " ^
wenzelm@169
   141
            space_implode " " (map quote cs)) :: errs)))
wenzelm@169
   142
      end;
clasohm@0
   143
wenzelm@143
   144
wenzelm@143
   145
    (* FIXME abbr *)
nipkow@155
   146
    val tsig' = Type.extend (tsig, classes, default, types, arities);
wenzelm@143
   147
wenzelm@169
   148
    (* FIXME *)
wenzelm@169
   149
    fun expand_typ _ ty = ty;
wenzelm@143
   150
wenzelm@169
   151
    val read_ty =
wenzelm@169
   152
      (expand_typ tsig') o (check_typ tsig' syn) o (read_typ tsig' syn);
wenzelm@169
   153
    val log_types = Type.logical_types tsig';
nipkow@155
   154
    val xconsts = map #1 classes @ flat (map #1 types) @ flat (map #1 const_decs);
wenzelm@169
   155
    val sext = case opt_sext of Some sx => sx | None => Syntax.empty_sext;
wenzelm@169
   156
wenzelm@169
   157
    val syn' = Syntax.extend syn read_ty (log_types, xconsts, sext);
wenzelm@169
   158
wenzelm@169
   159
    val const_decs' =
wenzelm@169
   160
      map (read_consts tsig' syn') (Syntax.constants sext @ const_decs);
wenzelm@143
   161
  in
wenzelm@143
   162
    Sg {
wenzelm@143
   163
      tsig = tsig',
wenzelm@143
   164
      const_tab = Symtab.st_of_declist (const_decs', const_tab)
wenzelm@143
   165
        handle Symtab.DUPLICATE a => error ("Constant " ^ quote a ^ " declared twice"),
wenzelm@143
   166
      syn = syn',
wenzelm@169
   167
      stamps = ref name :: stamps}
wenzelm@143
   168
  end;
clasohm@0
   169
clasohm@0
   170
clasohm@0
   171
(* The empty signature *)
wenzelm@143
   172
wenzelm@169
   173
val sg0 = Sg {tsig = Type.tsig0, const_tab = Symtab.null,
wenzelm@169
   174
  syn = Syntax.type_syn, stamps = []};
clasohm@0
   175
wenzelm@143
   176
wenzelm@143
   177
(* The pure signature *)
wenzelm@143
   178
wenzelm@143
   179
val pure = extend sg0 "Pure"
clasohm@0
   180
([("logic", [])],
clasohm@0
   181
 ["logic"],
wenzelm@143
   182
 [(["fun"], 2),
wenzelm@143
   183
  (["prop"], 0),
wenzelm@143
   184
  (Syntax.syntax_types, 0)],
clasohm@0
   185
 [(["fun"],  ([["logic"], ["logic"]], "logic")),
clasohm@0
   186
  (["prop"], ([], "logic"))],
wenzelm@171
   187
 [([Syntax.constrainC], "'a::logic => 'a")],
wenzelm@143
   188
 Some Syntax.pure_sext);
wenzelm@143
   189
clasohm@0
   190
clasohm@0
   191
clasohm@0
   192
(** The Merge operation **)
clasohm@0
   193
clasohm@0
   194
(*Update table with (a,x) providing any existing asgt to "a" equals x. *)
clasohm@0
   195
fun update_eq ((a,x),tab) =
clasohm@0
   196
    case Symtab.lookup(tab,a) of
wenzelm@19
   197
        None => Symtab.update((a,x), tab)
wenzelm@19
   198
      | Some y => if x=y then tab
wenzelm@19
   199
            else  raise TERM ("Incompatible types for constant: "^a, []);
clasohm@0
   200
clasohm@0
   201
(*Combine tables, updating tab2 by tab1 and checking.*)
wenzelm@19
   202
fun merge_tabs (tab1,tab2) =
clasohm@0
   203
    Symtab.balance (foldr update_eq (Symtab.alist_of tab1, tab2));
clasohm@0
   204
clasohm@0
   205
(*Combine tables, overwriting tab2 with tab1.*)
wenzelm@19
   206
fun smash_tabs (tab1,tab2) =
clasohm@0
   207
    Symtab.balance (foldr Symtab.update (Symtab.alist_of tab1, tab2));
clasohm@0
   208
clasohm@0
   209
(*Combine stamps, checking that theory names are disjoint. *)
wenzelm@19
   210
fun merge_stamps (stamps1,stamps2) =
clasohm@0
   211
  let val stamps = stamps1 union stamps2 in
clasohm@0
   212
  case findrep (map ! stamps) of
clasohm@0
   213
     a::_ => error ("Attempt to merge different versions of theory: " ^ a)
clasohm@0
   214
   | [] => stamps
clasohm@0
   215
  end;
clasohm@0
   216
clasohm@0
   217
(*Merge two signatures.  Forms unions of tables.  Prefers sign1. *)
wenzelm@169
   218
fun merge
wenzelm@169
   219
  (sign1 as Sg {tsig = tsig1, const_tab = ctab1, stamps = stamps1, syn = syn1},
wenzelm@169
   220
   sign2 as Sg {tsig = tsig2, const_tab = ctab2, stamps = stamps2, syn = syn2}) =
clasohm@0
   221
    if stamps2 subset stamps1 then sign1
clasohm@0
   222
    else if stamps1 subset stamps2 then sign2
wenzelm@169
   223
    else (*neither is union already; must form union*)
wenzelm@169
   224
      let val tsig = Type.merge (tsig1, tsig2);
wenzelm@169
   225
      in
wenzelm@169
   226
        Sg {tsig = tsig, const_tab = merge_tabs (ctab1, ctab2),
wenzelm@169
   227
          stamps = merge_stamps (stamps1, stamps2),
wenzelm@169
   228
          syn = Syntax.merge (Type.logical_types tsig) syn1 syn2}
wenzelm@169
   229
      end;
wenzelm@169
   230
clasohm@0
   231
clasohm@0
   232
clasohm@0
   233
(**** CERTIFIED TYPES ****)
clasohm@0
   234
clasohm@0
   235
clasohm@0
   236
(*Certified typs under a signature*)
clasohm@0
   237
datatype ctyp = Ctyp of {sign: sg,  T: typ};
clasohm@0
   238
clasohm@0
   239
fun rep_ctyp(Ctyp ctyp) = ctyp;
clasohm@0
   240
clasohm@0
   241
fun typ_of (Ctyp{sign,T}) = T;
clasohm@0
   242
clasohm@0
   243
fun ctyp_of (sign as Sg{tsig,...}) T =
wenzelm@19
   244
        case Type.type_errors (tsig,string_of_typ sign) (T,[]) of
wenzelm@19
   245
          [] => Ctyp{sign= sign,T= T}
wenzelm@19
   246
        | errs =>  error (cat_lines ("Error in type:" :: errs));
clasohm@0
   247
clasohm@0
   248
(*The only use is a horrible hack in the simplifier!*)
clasohm@0
   249
fun read_typ(Sg{tsig,syn,...}, defS) s =
clasohm@0
   250
    let val term = Syntax.read syn Syntax.typeT s;
wenzelm@19
   251
        val S0 = Type.defaultS tsig;
wenzelm@19
   252
        fun defS0 s = case defS s of Some S => S | None => S0;
clasohm@0
   253
    in Syntax.typ_of_term defS0 term end;
clasohm@0
   254
clasohm@0
   255
fun read_ctyp sign = ctyp_of sign o read_typ(sign, K None);
clasohm@0
   256
clasohm@0
   257
fun string_of_ctyp (Ctyp{sign,T}) = string_of_typ sign T;
clasohm@0
   258
clasohm@0
   259
fun pprint_ctyp (Ctyp{sign,T}) = pprint_typ sign T;
clasohm@0
   260
clasohm@0
   261
clasohm@0
   262
(**** CERTIFIED TERMS ****)
clasohm@0
   263
clasohm@0
   264
(*Certified terms under a signature, with checked typ and maxidx of Vars*)
clasohm@0
   265
datatype cterm = Cterm of {sign: sg,  t: term,  T: typ,  maxidx: int};
clasohm@0
   266
clasohm@0
   267
fun rep_cterm (Cterm args) = args;
clasohm@0
   268
clasohm@0
   269
(*Return the underlying term*)
clasohm@0
   270
fun term_of (Cterm{sign,t,T,maxidx}) = t;
clasohm@0
   271
clasohm@0
   272
(** pretty printing of terms **)
clasohm@0
   273
clasohm@0
   274
fun pretty_term (Sg{tsig,syn,...}) = Syntax.pretty_term syn;
clasohm@0
   275
clasohm@0
   276
fun string_of_term sign t = Pretty.string_of (pretty_term sign t);
clasohm@0
   277
clasohm@0
   278
fun pprint_term sign = Pretty.pprint o Pretty.quote o (pretty_term sign);
clasohm@0
   279
clasohm@0
   280
fun string_of_cterm (Cterm{sign,t,...}) = string_of_term sign t;
clasohm@0
   281
clasohm@0
   282
fun pprint_cterm (Cterm{sign,t,...}) = pprint_term sign t;
clasohm@0
   283
clasohm@0
   284
(*Create a cterm by checking a "raw" term with respect to a signature*)
clasohm@0
   285
fun cterm_of sign t =
clasohm@0
   286
  case  term_errors sign (t,[])  of
clasohm@0
   287
      [] => Cterm{sign=sign, t=t, T= type_of t, maxidx= maxidx_of_term t}
clasohm@0
   288
    | errs => raise TERM(cat_lines("Term not in signature"::errs), [t]);
clasohm@0
   289
clasohm@0
   290
fun cfun f = fn Cterm{sign,t,...} => cterm_of sign (f t);
clasohm@0
   291
clasohm@0
   292
(*Lexing, parsing, polymorphic typechecking of a term.*)
clasohm@0
   293
fun read_def_cterm (sign as Sg{tsig, const_tab, syn,...}, types, sorts)
wenzelm@19
   294
                   (a,T) =
clasohm@0
   295
  let val showtyp = string_of_typ sign
clasohm@0
   296
      and showterm = string_of_term sign
clasohm@0
   297
      fun termerr [] = ""
wenzelm@19
   298
        | termerr [t] = "\nInvolving this term:\n" ^ showterm t ^ "\n"
wenzelm@19
   299
        | termerr ts = "\nInvolving these terms:\n" ^
wenzelm@19
   300
                       cat_lines (map showterm ts)
clasohm@0
   301
      val t = Syntax.read syn T a;
clasohm@0
   302
      val (t',tye) = Type.infer_types (tsig, const_tab, types,
wenzelm@19
   303
                                       sorts, showtyp, T, t)
wenzelm@19
   304
                  handle TYPE (msg, Ts, ts) =>
wenzelm@19
   305
          error ("Type checking error: " ^ msg ^ "\n" ^
wenzelm@19
   306
                  cat_lines (map showtyp Ts) ^ termerr ts)
clasohm@0
   307
  in (cterm_of sign t', tye)
clasohm@0
   308
  end
clasohm@0
   309
  handle TERM (msg, _) => error ("Error: " ^  msg);
clasohm@0
   310
clasohm@0
   311
clasohm@0
   312
fun read_cterm sign = #1 o (read_def_cterm (sign, K None, K None));
clasohm@0
   313
clasohm@0
   314
(** reading of instantiations **)
clasohm@0
   315
clasohm@0
   316
fun indexname cs = case Syntax.scan_varname cs of (v,[]) => v
wenzelm@19
   317
        | _ => error("Lexical error in variable name " ^ quote (implode cs));
clasohm@0
   318
clasohm@0
   319
fun absent ixn =
clasohm@0
   320
  error("No such variable in term: " ^ Syntax.string_of_vname ixn);
clasohm@0
   321
clasohm@0
   322
fun inst_failure ixn =
clasohm@0
   323
  error("Instantiation of " ^ Syntax.string_of_vname ixn ^ " fails");
clasohm@0
   324
clasohm@0
   325
fun read_insts (sign as Sg{tsig,...}) (rtypes,rsorts) (types,sorts) insts =
clasohm@0
   326
let fun split([],tvs,vs) = (tvs,vs)
clasohm@0
   327
      | split((sv,st)::l,tvs,vs) = (case explode sv of
wenzelm@19
   328
                  "'"::cs => split(l,(indexname cs,st)::tvs,vs)
wenzelm@19
   329
                | cs => split(l,tvs,(indexname cs,st)::vs));
clasohm@0
   330
    val (tvs,vs) = split(insts,[],[]);
clasohm@0
   331
    fun readT((a,i),st) =
wenzelm@19
   332
        let val ixn = ("'" ^ a,i);
wenzelm@19
   333
            val S = case rsorts ixn of Some S => S | None => absent ixn;
wenzelm@19
   334
            val T = read_typ (sign,sorts) st;
wenzelm@19
   335
        in if Type.typ_instance(tsig,T,TVar(ixn,S)) then (ixn,T)
wenzelm@19
   336
           else inst_failure ixn
wenzelm@19
   337
        end
clasohm@0
   338
    val tye = map readT tvs;
clasohm@0
   339
    fun add_cterm ((cts,tye), (ixn,st)) =
wenzelm@19
   340
        let val T = case rtypes ixn of
wenzelm@19
   341
                      Some T => typ_subst_TVars tye T
wenzelm@19
   342
                    | None => absent ixn;
wenzelm@19
   343
            val (ct,tye2) = read_def_cterm (sign,types,sorts) (st,T);
wenzelm@19
   344
            val cv = cterm_of sign (Var(ixn,typ_subst_TVars tye2 T))
wenzelm@19
   345
        in ((cv,ct)::cts,tye2 @ tye) end
clasohm@0
   346
    val (cterms,tye') = foldl add_cterm (([],tye), vs);
clasohm@0
   347
in (map (fn (ixn,T) => (ixn,ctyp_of sign T)) tye', cterms) end;
clasohm@0
   348
clasohm@0
   349
end;
wenzelm@143
   350