src/Pure/sign.ML
author wenzelm
Thu Nov 25 11:37:51 1993 +0100 (1993-11-25)
changeset 143 f8152ca36cd5
parent 19 929ad32d63fc
child 155 f58571828c68
permissions -rw-r--r--
Sign.extend: Syntax.extend now called with read_ty;
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
clasohm@0
   104
(** The Extend operation **)
clasohm@0
   105
clasohm@0
   106
(*Reset TVar indices to zero, renaming to preserve distinctness*)
wenzelm@19
   107
fun zero_tvar_indices tsig T =
clasohm@0
   108
  let val inxSs = typ_tvars T;
clasohm@0
   109
      val nms' = variantlist(map (#1 o #1) inxSs,[]);
clasohm@0
   110
      val tye = map (fn ((v,S),a) => (v, TVar((a,0),S))) (inxSs ~~ nms')
clasohm@0
   111
  in typ_subst_TVars tye T end
clasohm@0
   112
wenzelm@143
   113
clasohm@0
   114
(*Check that all types mentioned in the list of declarations are valid.
clasohm@0
   115
  If errors found then raise exception.
clasohm@0
   116
  Zero type var indices because type inference requires it.
clasohm@0
   117
*)
clasohm@0
   118
fun read_consts(tsig,syn) =
clasohm@0
   119
let val showtyp = Syntax.string_of_typ syn;
clasohm@0
   120
    fun read [] = []
clasohm@0
   121
      | read((cs,s)::pairs) =
wenzelm@19
   122
        let val t = Syntax.read syn Syntax.typeT s handle ERROR =>
wenzelm@19
   123
                    error("The error above occurred in type " ^ quote s);
wenzelm@19
   124
            val S = Type.defaultS tsig;
wenzelm@19
   125
            val T = Type.varifyT(Syntax.typ_of_term (K S) t)
wenzelm@19
   126
            val T0 = zero_tvar_indices tsig T;
wenzelm@19
   127
        in (case Type.type_errors (tsig,showtyp) (T0,[]) of
wenzelm@19
   128
                [] => (cs,T0) :: read pairs
wenzelm@19
   129
            | errs => error (cat_lines
wenzelm@19
   130
           (("Error in type of constants " ^ space_implode " " cs) :: errs)))
wenzelm@19
   131
        end
clasohm@0
   132
in read end;
clasohm@0
   133
clasohm@0
   134
clasohm@0
   135
(*Extend a signature: may add classes, types and constants.
clasohm@0
   136
  Replaces syntax with "syn".
clasohm@0
   137
  The "ref" in stamps ensures that no two signatures are identical --
wenzelm@143
   138
  it is impossible to forge a signature. *)
wenzelm@143
   139
wenzelm@143
   140
fun extend (Sg {tsig, const_tab, syn, stamps}) signame
wenzelm@143
   141
  (newclasses, newdefault, otypes, newtypes, const_decs, osext) =
wenzelm@143
   142
  let
wenzelm@143
   143
    (* FIXME abbr *)
wenzelm@143
   144
    val tsig' = Type.extend (tsig, newclasses, newdefault, otypes, newtypes);
wenzelm@143
   145
wenzelm@143
   146
    (* FIXME expand_typ, check typ *)
wenzelm@143
   147
    val read_ty = Syntax.read_typ syn (K (Type.defaultS tsig'));
wenzelm@143
   148
wenzelm@143
   149
    val roots = filter (Type.logical_type tsig') (distinct (flat (map #1 newtypes)));
clasohm@0
   150
    val xconsts = map #1 newclasses @ flat (map #1 otypes) @ flat (map #1 const_decs);
clasohm@0
   151
    val syn' =
wenzelm@143
   152
      (case osext of
wenzelm@143
   153
        Some sext => Syntax.extend syn read_ty (roots, xconsts, sext)
wenzelm@143
   154
      | None =>
wenzelm@143
   155
          if null roots andalso null xconsts then syn
wenzelm@143
   156
          else Syntax.extend syn read_ty (roots, xconsts, Syntax.empty_sext));
wenzelm@143
   157
    val sconsts =
wenzelm@143
   158
      (case osext of
wenzelm@143
   159
        Some sext => Syntax.constants sext
wenzelm@143
   160
      | None => []);
wenzelm@143
   161
    val const_decs' = read_consts (tsig', syn') (sconsts @ const_decs);
wenzelm@143
   162
  in
wenzelm@143
   163
    Sg {
wenzelm@143
   164
      tsig = tsig',
wenzelm@143
   165
      const_tab = Symtab.st_of_declist (const_decs', const_tab)
wenzelm@143
   166
        handle Symtab.DUPLICATE a => error ("Constant " ^ quote a ^ " declared twice"),
wenzelm@143
   167
      syn = syn',
wenzelm@143
   168
      stamps = ref signame :: stamps}
wenzelm@143
   169
  end;
clasohm@0
   170
clasohm@0
   171
clasohm@0
   172
(* The empty signature *)
wenzelm@143
   173
wenzelm@143
   174
val sg0 = Sg {tsig = Type.tsig0,
wenzelm@143
   175
  const_tab = Symtab.null, syn = Syntax.type_syn, stamps= []};
clasohm@0
   176
wenzelm@143
   177
wenzelm@143
   178
(* The pure signature *)
wenzelm@143
   179
wenzelm@143
   180
val pure = extend sg0 "Pure"
clasohm@0
   181
([("logic", [])],
clasohm@0
   182
 ["logic"],
wenzelm@143
   183
 [(["fun"], 2),
wenzelm@143
   184
  (["prop"], 0),
wenzelm@143
   185
  (Syntax.syntax_types, 0)],
clasohm@0
   186
 [(["fun"],  ([["logic"], ["logic"]], "logic")),
clasohm@0
   187
  (["prop"], ([], "logic"))],
clasohm@0
   188
 [(["*NORMALIZED*"], "'a::{} => 'a"),
wenzelm@143
   189
  ([Syntax.constrainC], "'a::logic => 'a")],  (* MMW FIXME replace logic by {} (?) *)
wenzelm@143
   190
 Some Syntax.pure_sext);
wenzelm@143
   191
clasohm@0
   192
clasohm@0
   193
clasohm@0
   194
(** The Merge operation **)
clasohm@0
   195
clasohm@0
   196
(*Update table with (a,x) providing any existing asgt to "a" equals x. *)
clasohm@0
   197
fun update_eq ((a,x),tab) =
clasohm@0
   198
    case Symtab.lookup(tab,a) of
wenzelm@19
   199
        None => Symtab.update((a,x), tab)
wenzelm@19
   200
      | Some y => if x=y then tab
wenzelm@19
   201
            else  raise TERM ("Incompatible types for constant: "^a, []);
clasohm@0
   202
clasohm@0
   203
(*Combine tables, updating tab2 by tab1 and checking.*)
wenzelm@19
   204
fun merge_tabs (tab1,tab2) =
clasohm@0
   205
    Symtab.balance (foldr update_eq (Symtab.alist_of tab1, tab2));
clasohm@0
   206
clasohm@0
   207
(*Combine tables, overwriting tab2 with tab1.*)
wenzelm@19
   208
fun smash_tabs (tab1,tab2) =
clasohm@0
   209
    Symtab.balance (foldr Symtab.update (Symtab.alist_of tab1, tab2));
clasohm@0
   210
clasohm@0
   211
(*Combine stamps, checking that theory names are disjoint. *)
wenzelm@19
   212
fun merge_stamps (stamps1,stamps2) =
clasohm@0
   213
  let val stamps = stamps1 union stamps2 in
clasohm@0
   214
  case findrep (map ! stamps) of
clasohm@0
   215
     a::_ => error ("Attempt to merge different versions of theory: " ^ a)
clasohm@0
   216
   | [] => stamps
clasohm@0
   217
  end;
clasohm@0
   218
clasohm@0
   219
(*Merge two signatures.  Forms unions of tables.  Prefers sign1. *)
clasohm@0
   220
fun merge (sign1 as Sg{tsig=tsig1,const_tab=ctab1,stamps=stamps1,syn=syn1},
wenzelm@19
   221
           sign2 as Sg{tsig=tsig2,const_tab=ctab2,stamps=stamps2,syn=syn2}) =
clasohm@0
   222
    if stamps2 subset stamps1 then sign1
clasohm@0
   223
    else if stamps1 subset stamps2 then sign2
clasohm@0
   224
    else  (*neither is union already;  must form union*)
wenzelm@19
   225
           Sg{tsig= Type.merge(tsig1,tsig2),
wenzelm@19
   226
              const_tab= merge_tabs (ctab1, ctab2),
wenzelm@19
   227
              stamps= merge_stamps (stamps1,stamps2),
wenzelm@19
   228
              syn = Syntax.merge(syn1,syn2)};
clasohm@0
   229
clasohm@0
   230
clasohm@0
   231
(**** CERTIFIED TYPES ****)
clasohm@0
   232
clasohm@0
   233
clasohm@0
   234
(*Certified typs under a signature*)
clasohm@0
   235
datatype ctyp = Ctyp of {sign: sg,  T: typ};
clasohm@0
   236
clasohm@0
   237
fun rep_ctyp(Ctyp ctyp) = ctyp;
clasohm@0
   238
clasohm@0
   239
fun typ_of (Ctyp{sign,T}) = T;
clasohm@0
   240
clasohm@0
   241
fun ctyp_of (sign as Sg{tsig,...}) T =
wenzelm@19
   242
        case Type.type_errors (tsig,string_of_typ sign) (T,[]) of
wenzelm@19
   243
          [] => Ctyp{sign= sign,T= T}
wenzelm@19
   244
        | errs =>  error (cat_lines ("Error in type:" :: errs));
clasohm@0
   245
clasohm@0
   246
(*The only use is a horrible hack in the simplifier!*)
clasohm@0
   247
fun read_typ(Sg{tsig,syn,...}, defS) s =
clasohm@0
   248
    let val term = Syntax.read syn Syntax.typeT s;
wenzelm@19
   249
        val S0 = Type.defaultS tsig;
wenzelm@19
   250
        fun defS0 s = case defS s of Some S => S | None => S0;
clasohm@0
   251
    in Syntax.typ_of_term defS0 term end;
clasohm@0
   252
clasohm@0
   253
fun read_ctyp sign = ctyp_of sign o read_typ(sign, K None);
clasohm@0
   254
clasohm@0
   255
fun string_of_ctyp (Ctyp{sign,T}) = string_of_typ sign T;
clasohm@0
   256
clasohm@0
   257
fun pprint_ctyp (Ctyp{sign,T}) = pprint_typ sign T;
clasohm@0
   258
clasohm@0
   259
clasohm@0
   260
(**** CERTIFIED TERMS ****)
clasohm@0
   261
clasohm@0
   262
(*Certified terms under a signature, with checked typ and maxidx of Vars*)
clasohm@0
   263
datatype cterm = Cterm of {sign: sg,  t: term,  T: typ,  maxidx: int};
clasohm@0
   264
clasohm@0
   265
fun rep_cterm (Cterm args) = args;
clasohm@0
   266
clasohm@0
   267
(*Return the underlying term*)
clasohm@0
   268
fun term_of (Cterm{sign,t,T,maxidx}) = t;
clasohm@0
   269
clasohm@0
   270
(** pretty printing of terms **)
clasohm@0
   271
clasohm@0
   272
fun pretty_term (Sg{tsig,syn,...}) = Syntax.pretty_term syn;
clasohm@0
   273
clasohm@0
   274
fun string_of_term sign t = Pretty.string_of (pretty_term sign t);
clasohm@0
   275
clasohm@0
   276
fun pprint_term sign = Pretty.pprint o Pretty.quote o (pretty_term sign);
clasohm@0
   277
clasohm@0
   278
fun string_of_cterm (Cterm{sign,t,...}) = string_of_term sign t;
clasohm@0
   279
clasohm@0
   280
fun pprint_cterm (Cterm{sign,t,...}) = pprint_term sign t;
clasohm@0
   281
clasohm@0
   282
(*Create a cterm by checking a "raw" term with respect to a signature*)
clasohm@0
   283
fun cterm_of sign t =
clasohm@0
   284
  case  term_errors sign (t,[])  of
clasohm@0
   285
      [] => Cterm{sign=sign, t=t, T= type_of t, maxidx= maxidx_of_term t}
clasohm@0
   286
    | errs => raise TERM(cat_lines("Term not in signature"::errs), [t]);
clasohm@0
   287
clasohm@0
   288
fun cfun f = fn Cterm{sign,t,...} => cterm_of sign (f t);
clasohm@0
   289
clasohm@0
   290
(*Lexing, parsing, polymorphic typechecking of a term.*)
clasohm@0
   291
fun read_def_cterm (sign as Sg{tsig, const_tab, syn,...}, types, sorts)
wenzelm@19
   292
                   (a,T) =
clasohm@0
   293
  let val showtyp = string_of_typ sign
clasohm@0
   294
      and showterm = string_of_term sign
clasohm@0
   295
      fun termerr [] = ""
wenzelm@19
   296
        | termerr [t] = "\nInvolving this term:\n" ^ showterm t ^ "\n"
wenzelm@19
   297
        | termerr ts = "\nInvolving these terms:\n" ^
wenzelm@19
   298
                       cat_lines (map showterm ts)
clasohm@0
   299
      val t = Syntax.read syn T a;
clasohm@0
   300
      val (t',tye) = Type.infer_types (tsig, const_tab, types,
wenzelm@19
   301
                                       sorts, showtyp, T, t)
wenzelm@19
   302
                  handle TYPE (msg, Ts, ts) =>
wenzelm@19
   303
          error ("Type checking error: " ^ msg ^ "\n" ^
wenzelm@19
   304
                  cat_lines (map showtyp Ts) ^ termerr ts)
clasohm@0
   305
  in (cterm_of sign t', tye)
clasohm@0
   306
  end
clasohm@0
   307
  handle TERM (msg, _) => error ("Error: " ^  msg);
clasohm@0
   308
clasohm@0
   309
clasohm@0
   310
fun read_cterm sign = #1 o (read_def_cterm (sign, K None, K None));
clasohm@0
   311
clasohm@0
   312
(** reading of instantiations **)
clasohm@0
   313
clasohm@0
   314
fun indexname cs = case Syntax.scan_varname cs of (v,[]) => v
wenzelm@19
   315
        | _ => error("Lexical error in variable name " ^ quote (implode cs));
clasohm@0
   316
clasohm@0
   317
fun absent ixn =
clasohm@0
   318
  error("No such variable in term: " ^ Syntax.string_of_vname ixn);
clasohm@0
   319
clasohm@0
   320
fun inst_failure ixn =
clasohm@0
   321
  error("Instantiation of " ^ Syntax.string_of_vname ixn ^ " fails");
clasohm@0
   322
clasohm@0
   323
fun read_insts (sign as Sg{tsig,...}) (rtypes,rsorts) (types,sorts) insts =
clasohm@0
   324
let fun split([],tvs,vs) = (tvs,vs)
clasohm@0
   325
      | split((sv,st)::l,tvs,vs) = (case explode sv of
wenzelm@19
   326
                  "'"::cs => split(l,(indexname cs,st)::tvs,vs)
wenzelm@19
   327
                | cs => split(l,tvs,(indexname cs,st)::vs));
clasohm@0
   328
    val (tvs,vs) = split(insts,[],[]);
clasohm@0
   329
    fun readT((a,i),st) =
wenzelm@19
   330
        let val ixn = ("'" ^ a,i);
wenzelm@19
   331
            val S = case rsorts ixn of Some S => S | None => absent ixn;
wenzelm@19
   332
            val T = read_typ (sign,sorts) st;
wenzelm@19
   333
        in if Type.typ_instance(tsig,T,TVar(ixn,S)) then (ixn,T)
wenzelm@19
   334
           else inst_failure ixn
wenzelm@19
   335
        end
clasohm@0
   336
    val tye = map readT tvs;
clasohm@0
   337
    fun add_cterm ((cts,tye), (ixn,st)) =
wenzelm@19
   338
        let val T = case rtypes ixn of
wenzelm@19
   339
                      Some T => typ_subst_TVars tye T
wenzelm@19
   340
                    | None => absent ixn;
wenzelm@19
   341
            val (ct,tye2) = read_def_cterm (sign,types,sorts) (st,T);
wenzelm@19
   342
            val cv = cterm_of sign (Var(ixn,typ_subst_TVars tye2 T))
wenzelm@19
   343
        in ((cv,ct)::cts,tye2 @ tye) end
clasohm@0
   344
    val (cterms,tye') = foldl add_cterm (([],tye), vs);
clasohm@0
   345
in (map (fn (ixn,T) => (ixn,ctyp_of sign T)) tye', cterms) end;
clasohm@0
   346
clasohm@0
   347
end;
wenzelm@143
   348