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