src/Pure/type.ML
author immler@in.tum.de
Thu, 26 Feb 2009 10:13:43 +0100
changeset 30151 629f3a92863e
parent 29275 9fa69e3858d6
child 30343 79f022df8527
permissions -rw-r--r--
removed global ref dfg_format
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
     1
(*  Title:      Pure/type.ML
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
     2
    Author:     Tobias Nipkow, Lawrence C Paulson, and Markus Wenzel
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     3
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
     4
Type signatures and certified types, special treatment of type vars,
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
     5
matching and unification of types, extend and merge type signatures.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     8
signature TYPE =
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
     9
sig
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    10
  (*type signatures and certified types*)
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    11
  datatype decl =
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    12
    LogicalType of int |
14989
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
    13
    Abbreviation of string list * typ * bool |
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    14
    Nonterminal
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    15
  type tsig
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    16
  val rep_tsig: tsig ->
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
    17
   {classes: NameSpace.T * Sorts.algebra,
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    18
    default: sort,
28017
4919bd124a58 type Properties.T;
wenzelm
parents: 27313
diff changeset
    19
    types: ((decl * Properties.T) * serial) NameSpace.table,
26641
cf67665296c2 tsig: removed unnecessary universal witness;
wenzelm
parents: 26517
diff changeset
    20
    log_types: string list}
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    21
  val empty_tsig: tsig
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    22
  val defaultS: tsig -> sort
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    23
  val logical_types: tsig -> string list
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    24
  val eq_sort: tsig -> sort * sort -> bool
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    25
  val subsort: tsig -> sort * sort -> bool
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    26
  val of_sort: tsig -> typ * sort -> bool
19464
d13309e30aba added inter_sort;
wenzelm
parents: 19305
diff changeset
    27
  val inter_sort: tsig -> sort * sort -> sort
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    28
  val cert_class: tsig -> class -> class
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    29
  val cert_sort: tsig -> sort -> sort
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    30
  val witness_sorts: tsig -> sort list -> sort list -> (typ * sort) list
24274
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
    31
  type mode
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
    32
  val mode_default: mode
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
    33
  val mode_syntax: mode
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
    34
  val mode_abbrev: mode
24484
013b98b57b86 maintain mode in context (get/set/restore_mode);
wenzelm
parents: 24274
diff changeset
    35
  val get_mode: Proof.context -> mode
013b98b57b86 maintain mode in context (get/set/restore_mode);
wenzelm
parents: 24274
diff changeset
    36
  val set_mode: mode -> Proof.context -> Proof.context
013b98b57b86 maintain mode in context (get/set/restore_mode);
wenzelm
parents: 24274
diff changeset
    37
  val restore_mode: Proof.context -> Proof.context -> Proof.context
24274
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
    38
  val cert_typ_mode: mode -> tsig -> typ -> typ
14993
802f3732a54e tuned certify_typ/term;
wenzelm
parents: 14989
diff changeset
    39
  val cert_typ: tsig -> typ -> typ
19464
d13309e30aba added inter_sort;
wenzelm
parents: 19305
diff changeset
    40
  val arity_number: tsig -> string -> int
d13309e30aba added inter_sort;
wenzelm
parents: 19305
diff changeset
    41
  val arity_sorts: Pretty.pp -> tsig -> string -> sort -> sort list
28017
4919bd124a58 type Properties.T;
wenzelm
parents: 27313
diff changeset
    42
  val the_tags: tsig -> string -> Properties.T
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    43
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    44
  (*special treatment of type vars*)
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    45
  val strip_sorts: typ -> typ
24982
f2f0722675b1 replaced Term.equiv_types by Type.similar_types;
wenzelm
parents: 24848
diff changeset
    46
  val similar_types: term * term -> bool
621
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
    47
  val no_tvars: typ -> typ
21116
be58cded79da fixed type signature of Type.varify
haftmann
parents: 20951
diff changeset
    48
  val varify: (string * sort) list -> term -> ((string * sort) * indexname) list * term
16289
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
    49
  val freeze_thaw_type: typ -> typ * (typ -> typ)
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
    50
  val freeze_type: typ -> typ
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
    51
  val freeze_thaw: term -> term * (term -> term)
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
    52
  val freeze: term -> term
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
    53
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    54
  (*matching and unification*)
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
    55
  exception TYPE_MATCH
25324
ed4ac5966c68 tuned signature;
wenzelm
parents: 24982
diff changeset
    56
  type tyenv = (sort * typ) Vartab.table
26327
fc8df36e2644 Type.lookup now curried; typ_of_sort
haftmann
parents: 25384
diff changeset
    57
  val lookup: tyenv -> indexname * sort -> typ option
16946
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
    58
  val typ_match: tsig -> typ * typ -> tyenv -> tyenv
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    59
  val typ_instance: tsig -> typ * typ -> bool
16946
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
    60
  val raw_match: typ * typ -> tyenv -> tyenv
19694
08894a78400b export raw_matches;
wenzelm
parents: 19642
diff changeset
    61
  val raw_matches: typ list * typ list -> tyenv -> tyenv
16946
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
    62
  val raw_instance: typ * typ -> bool
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
    63
  exception TUNIFY
16946
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
    64
  val unify: tsig -> typ * typ -> tyenv * int -> tyenv * int
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
    65
  val raw_unify: typ * typ -> tyenv -> tyenv
19696
26a268c299d8 export raw_unifys, could_unifys;
wenzelm
parents: 19694
diff changeset
    66
  val raw_unifys: typ list * typ list -> tyenv -> tyenv
17804
4deae4b33d97 added could_unify;
wenzelm
parents: 17756
diff changeset
    67
  val could_unify: typ * typ -> bool
19696
26a268c299d8 export raw_unifys, could_unifys;
wenzelm
parents: 19694
diff changeset
    68
  val could_unifys: typ list * typ list -> bool
16650
bd4f7149ba1e Moved eq_type from envir.ML to type.ML
berghofe
parents: 16444
diff changeset
    69
  val eq_type: tyenv -> typ * typ -> bool
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    70
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    71
  (*extend and merge type signatures*)
19515
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
    72
  val add_class: Pretty.pp -> NameSpace.naming -> bstring * class list -> tsig -> tsig
26669
c27efd6de25d simplified hide_XXX interfaces;
wenzelm
parents: 26641
diff changeset
    73
  val hide_class: bool -> string -> tsig -> tsig
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    74
  val set_defsort: sort -> tsig -> tsig
28017
4919bd124a58 type Properties.T;
wenzelm
parents: 27313
diff changeset
    75
  val add_type: NameSpace.naming -> Properties.T -> bstring * int -> tsig -> tsig
4919bd124a58 type Properties.T;
wenzelm
parents: 27313
diff changeset
    76
  val add_abbrev: NameSpace.naming -> Properties.T -> string * string list * typ -> tsig -> tsig
4919bd124a58 type Properties.T;
wenzelm
parents: 27313
diff changeset
    77
  val add_nonterminal: NameSpace.naming -> Properties.T -> string -> tsig -> tsig
26669
c27efd6de25d simplified hide_XXX interfaces;
wenzelm
parents: 26641
diff changeset
    78
  val hide_type: bool -> string -> tsig -> tsig
19515
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
    79
  val add_arity: Pretty.pp -> arity -> tsig -> tsig
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
    80
  val add_classrel: Pretty.pp -> class * class -> tsig -> tsig
14830
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
    81
  val merge_tsigs: Pretty.pp -> tsig * tsig -> tsig
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
    82
end;
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
    83
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
    84
structure Type: TYPE =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    85
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    86
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    87
(** type signatures and certified types **)
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
    88
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    89
(* type declarations *)
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    90
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    91
datatype decl =
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    92
  LogicalType of int |
14989
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
    93
  Abbreviation of string list * typ * bool |
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    94
  Nonterminal;
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    95
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    96
fun str_of_decl (LogicalType _) = "logical type constructor"
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    97
  | str_of_decl (Abbreviation _) = "type abbreviation"
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    98
  | str_of_decl Nonterminal = "syntactic type";
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
    99
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   100
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   101
(* type tsig *)
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   102
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   103
datatype tsig =
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   104
  TSig of {
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   105
    classes: NameSpace.T * Sorts.algebra,   (*order-sorted algebra of type classes*)
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   106
    default: sort,                          (*default sort on input*)
28017
4919bd124a58 type Properties.T;
wenzelm
parents: 27313
diff changeset
   107
    types: ((decl * Properties.T) * serial) NameSpace.table, (*declared types*)
26641
cf67665296c2 tsig: removed unnecessary universal witness;
wenzelm
parents: 26517
diff changeset
   108
    log_types: string list};                (*logical types sorted by number of arguments*)
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   109
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   110
fun rep_tsig (TSig comps) = comps;
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   111
26641
cf67665296c2 tsig: removed unnecessary universal witness;
wenzelm
parents: 26517
diff changeset
   112
fun make_tsig (classes, default, types, log_types) =
cf67665296c2 tsig: removed unnecessary universal witness;
wenzelm
parents: 26517
diff changeset
   113
  TSig {classes = classes, default = default, types = types, log_types = log_types};
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   114
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   115
fun build_tsig ((space, classes), default, types) =
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   116
  let
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   117
    val log_types =
27302
8d12ac6a3e1c type constructors now with markup
haftmann
parents: 27273
diff changeset
   118
      Symtab.fold (fn (c, ((LogicalType n, _), _)) => cons (c, n) | _ => I) (snd types) []
17756
d4a35f82fbb4 minor tweaks for Poplog/ML;
wenzelm
parents: 17496
diff changeset
   119
      |> Library.sort (Library.int_ord o pairself snd) |> map fst;
26641
cf67665296c2 tsig: removed unnecessary universal witness;
wenzelm
parents: 26517
diff changeset
   120
  in make_tsig ((space, classes), default, types, log_types) end;
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   121
26641
cf67665296c2 tsig: removed unnecessary universal witness;
wenzelm
parents: 26517
diff changeset
   122
fun map_tsig f (TSig {classes, default, types, log_types = _}) =
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   123
  build_tsig (f (classes, default, types));
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   124
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   125
val empty_tsig =
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   126
  build_tsig ((NameSpace.empty, Sorts.empty_algebra), [], NameSpace.empty_table);
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   127
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   128
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   129
(* classes and sorts *)
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   130
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   131
fun defaultS (TSig {default, ...}) = default;
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   132
fun logical_types (TSig {log_types, ...}) = log_types;
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   133
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   134
fun eq_sort (TSig {classes, ...}) = Sorts.sort_eq (#2 classes);
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   135
fun subsort (TSig {classes, ...}) = Sorts.sort_le (#2 classes);
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   136
fun of_sort (TSig {classes, ...}) = Sorts.of_sort (#2 classes);
19464
d13309e30aba added inter_sort;
wenzelm
parents: 19305
diff changeset
   137
fun inter_sort (TSig {classes, ...}) = Sorts.inter_sort (#2 classes);
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   138
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   139
fun cert_class (TSig {classes, ...}) = Sorts.certify_class (#2 classes);
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   140
fun cert_sort (TSig {classes, ...}) = Sorts.certify_sort (#2 classes);
19515
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   141
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   142
fun witness_sorts (tsig as TSig {classes, log_types, ...}) =
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   143
  Sorts.witness_sorts (#2 classes) log_types;
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   144
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   145
24274
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   146
(* certification mode *)
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   147
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   148
datatype mode = Mode of {normalize: bool, logical: bool};
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   149
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   150
val mode_default = Mode {normalize = true, logical = true};
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   151
val mode_syntax = Mode {normalize = true, logical = false};
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   152
val mode_abbrev = Mode {normalize = false, logical = false};
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   153
24484
013b98b57b86 maintain mode in context (get/set/restore_mode);
wenzelm
parents: 24274
diff changeset
   154
structure Mode = ProofDataFun
013b98b57b86 maintain mode in context (get/set/restore_mode);
wenzelm
parents: 24274
diff changeset
   155
(
013b98b57b86 maintain mode in context (get/set/restore_mode);
wenzelm
parents: 24274
diff changeset
   156
  type T = mode;
013b98b57b86 maintain mode in context (get/set/restore_mode);
wenzelm
parents: 24274
diff changeset
   157
  fun init _ = mode_default;
013b98b57b86 maintain mode in context (get/set/restore_mode);
wenzelm
parents: 24274
diff changeset
   158
);
013b98b57b86 maintain mode in context (get/set/restore_mode);
wenzelm
parents: 24274
diff changeset
   159
013b98b57b86 maintain mode in context (get/set/restore_mode);
wenzelm
parents: 24274
diff changeset
   160
val get_mode = Mode.get;
013b98b57b86 maintain mode in context (get/set/restore_mode);
wenzelm
parents: 24274
diff changeset
   161
fun set_mode mode = Mode.map (K mode);
013b98b57b86 maintain mode in context (get/set/restore_mode);
wenzelm
parents: 24274
diff changeset
   162
fun restore_mode ctxt = set_mode (get_mode ctxt);
013b98b57b86 maintain mode in context (get/set/restore_mode);
wenzelm
parents: 24274
diff changeset
   163
24274
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   164
27313
07754b7ba89d the_tags: explicit error message;
wenzelm
parents: 27302
diff changeset
   165
(* lookup types *)
07754b7ba89d the_tags: explicit error message;
wenzelm
parents: 27302
diff changeset
   166
07754b7ba89d the_tags: explicit error message;
wenzelm
parents: 27302
diff changeset
   167
fun undecl_type c = "Undeclared type constructor: " ^ quote c;
07754b7ba89d the_tags: explicit error message;
wenzelm
parents: 27302
diff changeset
   168
07754b7ba89d the_tags: explicit error message;
wenzelm
parents: 27302
diff changeset
   169
fun lookup_type (TSig {types, ...}) = Option.map fst o Symtab.lookup (snd types);
07754b7ba89d the_tags: explicit error message;
wenzelm
parents: 27302
diff changeset
   170
07754b7ba89d the_tags: explicit error message;
wenzelm
parents: 27302
diff changeset
   171
fun the_tags tsig c =
07754b7ba89d the_tags: explicit error message;
wenzelm
parents: 27302
diff changeset
   172
  (case lookup_type tsig c of
07754b7ba89d the_tags: explicit error message;
wenzelm
parents: 27302
diff changeset
   173
    SOME (_, tags) => tags
07754b7ba89d the_tags: explicit error message;
wenzelm
parents: 27302
diff changeset
   174
  | NONE => error (undecl_type c));
07754b7ba89d the_tags: explicit error message;
wenzelm
parents: 27302
diff changeset
   175
07754b7ba89d the_tags: explicit error message;
wenzelm
parents: 27302
diff changeset
   176
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   177
(* certified types *)
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   178
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   179
fun bad_nargs t = "Bad number of arguments for type constructor: " ^ quote t;
14830
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
   180
14998
wenzelm
parents: 14993
diff changeset
   181
local
wenzelm
parents: 14993
diff changeset
   182
wenzelm
parents: 14993
diff changeset
   183
fun inst_typ env (Type (c, Ts)) = Type (c, map (inst_typ env) Ts)
18957
8c3abd63bce3 TableFun: renamed xxx_multi to xxx_list;
wenzelm
parents: 18928
diff changeset
   184
  | inst_typ env (T as TFree (x, _)) = the_default T (AList.lookup (op =) env x)
14998
wenzelm
parents: 14993
diff changeset
   185
  | inst_typ _ T = T;
wenzelm
parents: 14993
diff changeset
   186
24274
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   187
in
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   188
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   189
fun cert_typ_mode (Mode {normalize, logical}) tsig ty =
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   190
  let
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   191
    fun err msg = raise TYPE (msg, [ty], []);
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   192
24274
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   193
    val check_logical =
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   194
      if logical then fn c => err ("Illegal occurrence of syntactic type: " ^ quote c)
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   195
      else fn _ => ();
14989
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   196
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   197
    fun cert (T as Type (c, Ts)) =
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   198
          let
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   199
            val Ts' = map cert Ts;
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   200
            fun nargs n = if length Ts <> n then err (bad_nargs c) else ();
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   201
          in
27302
8d12ac6a3e1c type constructors now with markup
haftmann
parents: 27273
diff changeset
   202
            (case lookup_type tsig c of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   203
              SOME (LogicalType n, _) => (nargs n; Type (c, Ts'))
24274
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   204
            | SOME (Abbreviation (vs, U, syn), _) =>
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   205
               (nargs (length vs);
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   206
                if syn then check_logical c else ();
14993
802f3732a54e tuned certify_typ/term;
wenzelm
parents: 14989
diff changeset
   207
                if normalize then inst_typ (vs ~~ Ts') U
14989
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   208
                else Type (c, Ts'))
24274
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   209
            | SOME (Nonterminal, _) => (nargs 0; check_logical c; T)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   210
            | NONE => err (undecl_type c))
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   211
          end
19515
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   212
      | cert (TFree (x, S)) = TFree (x, cert_sort tsig S)
14989
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   213
      | cert (TVar (xi as (_, i), S)) =
14993
802f3732a54e tuned certify_typ/term;
wenzelm
parents: 14989
diff changeset
   214
          if i < 0 then
802f3732a54e tuned certify_typ/term;
wenzelm
parents: 14989
diff changeset
   215
            err ("Malformed type variable: " ^ quote (Term.string_of_vname xi))
19515
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   216
          else TVar (xi, cert_sort tsig S);
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   217
14989
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   218
    val ty' = cert ty;
14993
802f3732a54e tuned certify_typ/term;
wenzelm
parents: 14989
diff changeset
   219
  in if ty = ty' then ty else ty' end;  (*avoid copying of already normal type*)
14830
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
   220
24274
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   221
val cert_typ = cert_typ_mode mode_default;
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   222
14998
wenzelm
parents: 14993
diff changeset
   223
end;
wenzelm
parents: 14993
diff changeset
   224
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   225
19464
d13309e30aba added inter_sort;
wenzelm
parents: 19305
diff changeset
   226
(* type arities *)
d13309e30aba added inter_sort;
wenzelm
parents: 19305
diff changeset
   227
27302
8d12ac6a3e1c type constructors now with markup
haftmann
parents: 27273
diff changeset
   228
fun arity_number tsig a =
8d12ac6a3e1c type constructors now with markup
haftmann
parents: 27273
diff changeset
   229
  (case lookup_type tsig a of
19464
d13309e30aba added inter_sort;
wenzelm
parents: 19305
diff changeset
   230
    SOME (LogicalType n, _) => n
d13309e30aba added inter_sort;
wenzelm
parents: 19305
diff changeset
   231
  | _ => error (undecl_type a));
d13309e30aba added inter_sort;
wenzelm
parents: 19305
diff changeset
   232
d13309e30aba added inter_sort;
wenzelm
parents: 19305
diff changeset
   233
fun arity_sorts _ tsig a [] = replicate (arity_number tsig a) []
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   234
  | arity_sorts pp (TSig {classes, ...}) a S = Sorts.mg_domain (#2 classes) a S
26641
cf67665296c2 tsig: removed unnecessary universal witness;
wenzelm
parents: 26517
diff changeset
   235
      handle Sorts.CLASS_ERROR err => error (Sorts.class_error pp err);
19464
d13309e30aba added inter_sort;
wenzelm
parents: 19305
diff changeset
   236
d13309e30aba added inter_sort;
wenzelm
parents: 19305
diff changeset
   237
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   238
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   239
(** special treatment of type vars **)
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   240
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   241
(* strip_sorts *)
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   242
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   243
fun strip_sorts (Type (a, Ts)) = Type (a, map strip_sorts Ts)
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   244
  | strip_sorts (TFree (x, _)) = TFree (x, [])
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   245
  | strip_sorts (TVar (xi, _)) = TVar (xi, []);
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   246
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   247
25384
7b9dfd4774a6 similar_types: uniform treatment of TFrees/TVars;
wenzelm
parents: 25324
diff changeset
   248
(* equivalence up to renaming of atomic types *)
24982
f2f0722675b1 replaced Term.equiv_types by Type.similar_types;
wenzelm
parents: 24848
diff changeset
   249
f2f0722675b1 replaced Term.equiv_types by Type.similar_types;
wenzelm
parents: 24848
diff changeset
   250
local
f2f0722675b1 replaced Term.equiv_types by Type.similar_types;
wenzelm
parents: 24848
diff changeset
   251
f2f0722675b1 replaced Term.equiv_types by Type.similar_types;
wenzelm
parents: 24848
diff changeset
   252
fun standard_types t =
f2f0722675b1 replaced Term.equiv_types by Type.similar_types;
wenzelm
parents: 24848
diff changeset
   253
  let
25384
7b9dfd4774a6 similar_types: uniform treatment of TFrees/TVars;
wenzelm
parents: 25324
diff changeset
   254
    val Ts = fold_types (fold_atyps (insert (op =))) t [];
7b9dfd4774a6 similar_types: uniform treatment of TFrees/TVars;
wenzelm
parents: 25324
diff changeset
   255
    val Ts' = map (fn a => TFree (a, [])) (Name.invents Name.context Name.aT (length Ts));
7b9dfd4774a6 similar_types: uniform treatment of TFrees/TVars;
wenzelm
parents: 25324
diff changeset
   256
  in map_types (map_atyps (perhaps (AList.lookup (op =) (Ts ~~ Ts')))) t end;
24982
f2f0722675b1 replaced Term.equiv_types by Type.similar_types;
wenzelm
parents: 24848
diff changeset
   257
f2f0722675b1 replaced Term.equiv_types by Type.similar_types;
wenzelm
parents: 24848
diff changeset
   258
in
f2f0722675b1 replaced Term.equiv_types by Type.similar_types;
wenzelm
parents: 24848
diff changeset
   259
f2f0722675b1 replaced Term.equiv_types by Type.similar_types;
wenzelm
parents: 24848
diff changeset
   260
val similar_types = op aconv o pairself (Term.map_types strip_sorts o standard_types);
f2f0722675b1 replaced Term.equiv_types by Type.similar_types;
wenzelm
parents: 24848
diff changeset
   261
f2f0722675b1 replaced Term.equiv_types by Type.similar_types;
wenzelm
parents: 24848
diff changeset
   262
end;
f2f0722675b1 replaced Term.equiv_types by Type.similar_types;
wenzelm
parents: 24848
diff changeset
   263
f2f0722675b1 replaced Term.equiv_types by Type.similar_types;
wenzelm
parents: 24848
diff changeset
   264
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   265
(* no_tvars *)
621
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
   266
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
   267
fun no_tvars T =
29275
9fa69e3858d6 moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents: 29269
diff changeset
   268
  (case Term.add_tvarsT T [] of [] => T
12501
36b2ac65e18d varify returns newly introduced variables;
wenzelm
parents: 12314
diff changeset
   269
  | vs => raise TYPE ("Illegal schematic type variable(s): " ^
29275
9fa69e3858d6 moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents: 29269
diff changeset
   270
      commas_quote (map (Term.string_of_vname o #1) (rev vs)), [T], []));
621
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
   271
7641
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   272
19806
f860b7a98445 renamed Type.(un)varifyT to Logic.(un)varifyT;
wenzelm
parents: 19696
diff changeset
   273
(* varify *)
621
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
   274
21116
be58cded79da fixed type signature of Type.varify
haftmann
parents: 20951
diff changeset
   275
fun varify fixed t =
621
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
   276
  let
19305
5c16895d548b avoid polymorphic equality;
wenzelm
parents: 19250
diff changeset
   277
    val fs = Term.fold_types (Term.fold_atyps
5c16895d548b avoid polymorphic equality;
wenzelm
parents: 19250
diff changeset
   278
      (fn TFree v => if member (op =) fixed v then I else insert (op =) v | _ => I)) t [];
29260
010b4dd637fe varify: regular name context;
wenzelm
parents: 28991
diff changeset
   279
    val used = Name.context
010b4dd637fe varify: regular name context;
wenzelm
parents: 28991
diff changeset
   280
      |> fold_types (fold_atyps (fn TVar ((a, _), _) => Name.declare a | _ => I)) t;
010b4dd637fe varify: regular name context;
wenzelm
parents: 28991
diff changeset
   281
    val fmap = fs ~~ map (rpair 0) (#1 (Name.variants (map fst fs) used));
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   282
    fun thaw (f as (a, S)) =
17184
3d80209e9a53 use AList operations;
wenzelm
parents: 16946
diff changeset
   283
      (case AList.lookup (op =) fmap f of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   284
        NONE => TFree f
16946
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   285
      | SOME xi => TVar (xi, S));
21116
be58cded79da fixed type signature of Type.varify
haftmann
parents: 20951
diff changeset
   286
  in (fmap, map_types (map_type_tfree thaw) t) end;
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   287
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   288
7641
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   289
(* freeze_thaw: freeze TVars in a term; return the "thaw" inverse *)
3411
163f8f4a42d7 Removal of freeze_vars and thaw_vars. New freeze_thaw
paulson
parents: 3175
diff changeset
   290
7641
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   291
local
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   292
16289
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   293
fun new_name (ix, (pairs, used)) =
20071
8f3e1ddb50e6 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 19806
diff changeset
   294
  let val v = Name.variant used (string_of_indexname ix)
16289
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   295
  in ((ix, v) :: pairs, v :: used) end;
621
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
   296
16289
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   297
fun freeze_one alist (ix, sort) =
17184
3d80209e9a53 use AList operations;
wenzelm
parents: 16946
diff changeset
   298
  TFree (the (AList.lookup (op =) alist ix), sort)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   299
    handle Option =>
3790
95a47d8bcd69 eliminated raise_term, raise_typ;
wenzelm
parents: 3411
diff changeset
   300
      raise TYPE ("Failure during freezing of ?" ^ string_of_indexname ix, [], []);
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   301
17184
3d80209e9a53 use AList operations;
wenzelm
parents: 16946
diff changeset
   302
fun thaw_one alist (a, sort) = TVar (the (AList.lookup (op =) alist a), sort)
16289
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   303
  handle Option => TFree (a, sort);
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   304
10495
284ee538991c export freeze_thaw_type;
wenzelm
parents: 9504
diff changeset
   305
in
284ee538991c export freeze_thaw_type;
wenzelm
parents: 9504
diff changeset
   306
284ee538991c export freeze_thaw_type;
wenzelm
parents: 9504
diff changeset
   307
(*this sort of code could replace unvarifyT*)
7641
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   308
fun freeze_thaw_type T =
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   309
  let
29275
9fa69e3858d6 moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents: 29269
diff changeset
   310
    val used = OldTerm.add_typ_tfree_names (T, [])
9fa69e3858d6 moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents: 29269
diff changeset
   311
    and tvars = map #1 (OldTerm.add_typ_tvars (T, []));
23178
07ba6b58b3d2 simplified/unified list fold;
wenzelm
parents: 21934
diff changeset
   312
    val (alist, _) = List.foldr new_name ([], used) tvars;
7641
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   313
  in (map_type_tvar (freeze_one alist) T, map_type_tfree (thaw_one (map swap alist))) end;
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   314
16289
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   315
val freeze_type = #1 o freeze_thaw_type;
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   316
3411
163f8f4a42d7 Removal of freeze_vars and thaw_vars. New freeze_thaw
paulson
parents: 3175
diff changeset
   317
fun freeze_thaw t =
7641
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   318
  let
29275
9fa69e3858d6 moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents: 29269
diff changeset
   319
    val used = OldTerm.it_term_types OldTerm.add_typ_tfree_names (t, [])
9fa69e3858d6 moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents: 29269
diff changeset
   320
    and tvars = map #1 (OldTerm.it_term_types OldTerm.add_typ_tvars (t, []));
23178
07ba6b58b3d2 simplified/unified list fold;
wenzelm
parents: 21934
diff changeset
   321
    val (alist, _) = List.foldr new_name ([], used) tvars;
7641
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   322
  in
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   323
    (case alist of
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   324
      [] => (t, fn x => x) (*nothing to do!*)
20548
8ef25fe585a8 renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents: 20071
diff changeset
   325
    | _ => (map_types (map_type_tvar (freeze_one alist)) t,
8ef25fe585a8 renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents: 20071
diff changeset
   326
      map_types (map_type_tfree (thaw_one (map swap alist)))))
7641
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   327
  end;
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   328
16289
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   329
val freeze = #1 o freeze_thaw;
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   330
7641
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   331
end;
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   332
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   333
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   334
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   335
(** matching and unification of types **)
8899
99266fe189a1 removed is_type_abbr;
wenzelm
parents: 8721
diff changeset
   336
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   337
type tyenv = (sort * typ) Vartab.table;
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   338
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   339
fun tvar_clash ixn S S' = raise TYPE ("Type variable " ^
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   340
  quote (Term.string_of_vname ixn) ^ " has two distinct sorts",
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   341
  [TVar (ixn, S), TVar (ixn, S')], []);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   342
26327
fc8df36e2644 Type.lookup now curried; typ_of_sort
haftmann
parents: 25384
diff changeset
   343
fun lookup tye (ixn, S) =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   344
  (case Vartab.lookup tye ixn of
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   345
    NONE => NONE
16289
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   346
  | SOME (S', T) => if S = S' then SOME T else tvar_clash ixn S S');
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   347
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   348
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   349
(* matching *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   350
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   351
exception TYPE_MATCH;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   352
16946
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   353
fun typ_match tsig =
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   354
  let
16340
fd027bb32896 renamed cert_typ_raw to cert_typ_abbrev;
wenzelm
parents: 16289
diff changeset
   355
    fun match (TVar (v, S), T) subs =
26327
fc8df36e2644 Type.lookup now curried; typ_of_sort
haftmann
parents: 25384
diff changeset
   356
          (case lookup subs (v, S) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   357
            NONE =>
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   358
              if of_sort tsig (T, S) then Vartab.update_new (v, (S, T)) subs
14993
802f3732a54e tuned certify_typ/term;
wenzelm
parents: 14989
diff changeset
   359
              else raise TYPE_MATCH
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   360
          | SOME U => if U = T then subs else raise TYPE_MATCH)
16340
fd027bb32896 renamed cert_typ_raw to cert_typ_abbrev;
wenzelm
parents: 16289
diff changeset
   361
      | match (Type (a, Ts), Type (b, Us)) subs =
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   362
          if a <> b then raise TYPE_MATCH
16885
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   363
          else matches (Ts, Us) subs
16340
fd027bb32896 renamed cert_typ_raw to cert_typ_abbrev;
wenzelm
parents: 16289
diff changeset
   364
      | match (TFree x, TFree y) subs =
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   365
          if x = y then subs else raise TYPE_MATCH
16885
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   366
      | match _ _ = raise TYPE_MATCH
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   367
    and matches (T :: Ts, U :: Us) subs = matches (Ts, Us) (match (T, U) subs)
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   368
      | matches _ subs = subs;
16946
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   369
  in match end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   370
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   371
fun typ_instance tsig (T, U) =
16946
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   372
  (typ_match tsig (U, T) Vartab.empty; true) handle TYPE_MATCH => false;
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   373
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   374
(*purely structural matching*)
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   375
fun raw_match (TVar (v, S), T) subs =
26327
fc8df36e2644 Type.lookup now curried; typ_of_sort
haftmann
parents: 25384
diff changeset
   376
      (case lookup subs (v, S) of
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   377
        NONE => Vartab.update_new (v, (S, T)) subs
16946
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   378
      | SOME U => if U = T then subs else raise TYPE_MATCH)
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   379
  | raw_match (Type (a, Ts), Type (b, Us)) subs =
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   380
      if a <> b then raise TYPE_MATCH
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   381
      else raw_matches (Ts, Us) subs
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   382
  | raw_match (TFree x, TFree y) subs =
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   383
      if x = y then subs else raise TYPE_MATCH
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   384
  | raw_match _ _ = raise TYPE_MATCH
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   385
and raw_matches (T :: Ts, U :: Us) subs = raw_matches (Ts, Us) (raw_match (T, U) subs)
19694
08894a78400b export raw_matches;
wenzelm
parents: 19642
diff changeset
   386
  | raw_matches ([], []) subs = subs
08894a78400b export raw_matches;
wenzelm
parents: 19642
diff changeset
   387
  | raw_matches _ _ = raise TYPE_MATCH;
16946
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   388
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   389
fun raw_instance (T, U) =
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   390
  (raw_match (U, T) Vartab.empty; true) handle TYPE_MATCH => false;
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   391
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   392
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   393
(* unification *)
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   394
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   395
exception TUNIFY;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   396
26641
cf67665296c2 tsig: removed unnecessary universal witness;
wenzelm
parents: 26517
diff changeset
   397
(*occurs check*)
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   398
fun occurs v tye =
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   399
  let
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   400
    fun occ (Type (_, Ts)) = exists occ Ts
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   401
      | occ (TFree _) = false
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   402
      | occ (TVar (w, S)) =
29269
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents: 29260
diff changeset
   403
          Term.eq_ix (v, w) orelse
26327
fc8df36e2644 Type.lookup now curried; typ_of_sort
haftmann
parents: 25384
diff changeset
   404
            (case lookup tye (w, S) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   405
              NONE => false
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   406
            | SOME U => occ U);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   407
  in occ end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   408
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   409
(*chase variable assignments; if devar returns a type var then it must be unassigned*)
16885
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   410
fun devar tye (T as TVar v) =
26327
fc8df36e2644 Type.lookup now curried; typ_of_sort
haftmann
parents: 25384
diff changeset
   411
      (case lookup tye v of
16885
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   412
        SOME U => devar tye U
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   413
      | NONE => T)
16885
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   414
  | devar tye T = T;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   415
17804
4deae4b33d97 added could_unify;
wenzelm
parents: 17756
diff changeset
   416
(*order-sorted unification*)
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   417
fun unify (tsig as TSig {classes = (_, classes), ...}) TU (tyenv, maxidx) =
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   418
  let
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   419
    val tyvar_count = ref maxidx;
24848
5dbbd33c3236 replaced literal 'a by Name.aT;
wenzelm
parents: 24484
diff changeset
   420
    fun gen_tyvar S = TVar ((Name.aT, inc tyvar_count), S);
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   421
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   422
    fun mg_domain a S = Sorts.mg_domain classes a S
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   423
      handle Sorts.CLASS_ERROR _ => raise TUNIFY;
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   424
16885
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   425
    fun meet (_, []) tye = tye
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   426
      | meet (TVar (xi, S'), S) tye =
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   427
          if Sorts.sort_le classes (S', S) then tye
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   428
          else Vartab.update_new
17221
6cd180204582 curried_lookup/update;
wenzelm
parents: 17184
diff changeset
   429
            (xi, (S', gen_tyvar (Sorts.inter_sort classes (S', S)))) tye
16885
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   430
      | meet (TFree (_, S'), S) tye =
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   431
          if Sorts.sort_le classes (S', S) then tye
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   432
          else raise TUNIFY
16885
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   433
      | meet (Type (a, Ts), S) tye = meets (Ts, mg_domain a S) tye
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   434
    and meets (T :: Ts, S :: Ss) tye = meets (Ts, Ss) (meet (devar tye T, S) tye)
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   435
      | meets _ tye = tye;
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   436
16885
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   437
    fun unif (ty1, ty2) tye =
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   438
      (case (devar tye ty1, devar tye ty2) of
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   439
        (T as TVar (v, S1), U as TVar (w, S2)) =>
29269
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents: 29260
diff changeset
   440
          if Term.eq_ix (v, w) then
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   441
            if S1 = S2 then tye else tvar_clash v S1 S2
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   442
          else if Sorts.sort_le classes (S1, S2) then
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   443
            Vartab.update_new (w, (S2, T)) tye
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   444
          else if Sorts.sort_le classes (S2, S1) then
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   445
            Vartab.update_new (v, (S1, U)) tye
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   446
          else
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   447
            let val S = gen_tyvar (Sorts.inter_sort classes (S1, S2)) in
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   448
              Vartab.update_new (v, (S1, S)) (Vartab.update_new (w, (S2, S)) tye)
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   449
            end
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   450
      | (TVar (v, S), T) =>
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   451
          if occurs v tye T then raise TUNIFY
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   452
          else meet (T, S) (Vartab.update_new (v, (S, T)) tye)
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   453
      | (T, TVar (v, S)) =>
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   454
          if occurs v tye T then raise TUNIFY
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   455
          else meet (T, S) (Vartab.update_new (v, (S, T)) tye)
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   456
      | (Type (a, Ts), Type (b, Us)) =>
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   457
          if a <> b then raise TUNIFY
16885
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   458
          else unifs (Ts, Us) tye
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   459
      | (T, U) => if T = U then tye else raise TUNIFY)
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   460
    and unifs (T :: Ts, U :: Us) tye = unifs (Ts, Us) (unif (T, U) tye)
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   461
      | unifs _ tye = tye;
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   462
  in (unif TU tyenv, ! tyvar_count) end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   463
16946
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   464
(*purely structural unification*)
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   465
fun raw_unify (ty1, ty2) tye =
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   466
  (case (devar tye ty1, devar tye ty2) of
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   467
    (T as TVar (v, S1), U as TVar (w, S2)) =>
29269
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents: 29260
diff changeset
   468
      if Term.eq_ix (v, w) then
16946
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   469
        if S1 = S2 then tye else tvar_clash v S1 S2
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   470
      else Vartab.update_new (w, (S2, T)) tye
16946
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   471
  | (TVar (v, S), T) =>
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   472
      if occurs v tye T then raise TUNIFY
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   473
      else Vartab.update_new (v, (S, T)) tye
16946
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   474
  | (T, TVar (v, S)) =>
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   475
      if occurs v tye T then raise TUNIFY
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   476
      else Vartab.update_new (v, (S, T)) tye
16946
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   477
  | (Type (a, Ts), Type (b, Us)) =>
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   478
      if a <> b then raise TUNIFY
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   479
      else raw_unifys (Ts, Us) tye
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   480
  | (T, U) => if T = U then tye else raise TUNIFY)
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   481
and raw_unifys (T :: Ts, U :: Us) tye = raw_unifys (Ts, Us) (raw_unify (T, U) tye)
19696
26a268c299d8 export raw_unifys, could_unifys;
wenzelm
parents: 19694
diff changeset
   482
  | raw_unifys ([], []) tye = tye
26a268c299d8 export raw_unifys, could_unifys;
wenzelm
parents: 19694
diff changeset
   483
  | raw_unifys _ _ = raise TUNIFY;
16946
7f9a7fe413f3 typ_match, unify: canonical argument order;
wenzelm
parents: 16885
diff changeset
   484
17804
4deae4b33d97 added could_unify;
wenzelm
parents: 17756
diff changeset
   485
(*fast unification filter*)
4deae4b33d97 added could_unify;
wenzelm
parents: 17756
diff changeset
   486
fun could_unify (Type (a, Ts), Type (b, Us)) = a = b andalso could_unifys (Ts, Us)
4deae4b33d97 added could_unify;
wenzelm
parents: 17756
diff changeset
   487
  | could_unify (TFree (a, _), TFree (b, _)) = a = b
4deae4b33d97 added could_unify;
wenzelm
parents: 17756
diff changeset
   488
  | could_unify (TVar _, _) = true
4deae4b33d97 added could_unify;
wenzelm
parents: 17756
diff changeset
   489
  | could_unify (_, TVar _) = true
4deae4b33d97 added could_unify;
wenzelm
parents: 17756
diff changeset
   490
  | could_unify _ = false
4deae4b33d97 added could_unify;
wenzelm
parents: 17756
diff changeset
   491
and could_unifys (T :: Ts, U :: Us) = could_unify (T, U) andalso could_unifys (Ts, Us)
19696
26a268c299d8 export raw_unifys, could_unifys;
wenzelm
parents: 19694
diff changeset
   492
  | could_unifys ([], []) = true
26a268c299d8 export raw_unifys, could_unifys;
wenzelm
parents: 19694
diff changeset
   493
  | could_unifys _ = false;
450
382b5368ec21 added raw_unify;
wenzelm
parents: 422
diff changeset
   494
17804
4deae4b33d97 added could_unify;
wenzelm
parents: 17756
diff changeset
   495
4deae4b33d97 added could_unify;
wenzelm
parents: 17756
diff changeset
   496
(*equality with respect to a type environment*)
16650
bd4f7149ba1e Moved eq_type from envir.ML to type.ML
berghofe
parents: 16444
diff changeset
   497
fun eq_type tye (T, T') =
16885
cabcd33cde18 tuned match, unify;
wenzelm
parents: 16650
diff changeset
   498
  (case (devar tye T, devar tye T') of
16650
bd4f7149ba1e Moved eq_type from envir.ML to type.ML
berghofe
parents: 16444
diff changeset
   499
     (Type (s, Ts), Type (s', Ts')) =>
bd4f7149ba1e Moved eq_type from envir.ML to type.ML
berghofe
parents: 16444
diff changeset
   500
       s = s' andalso ListPair.all (eq_type tye) (Ts, Ts')
bd4f7149ba1e Moved eq_type from envir.ML to type.ML
berghofe
parents: 16444
diff changeset
   501
   | (U, U') => U = U');
bd4f7149ba1e Moved eq_type from envir.ML to type.ML
berghofe
parents: 16444
diff changeset
   502
450
382b5368ec21 added raw_unify;
wenzelm
parents: 422
diff changeset
   503
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   504
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   505
(** extend and merge type signatures **)
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   506
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   507
(* classes *)
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   508
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   509
fun add_class pp naming (c, cs) tsig =
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   510
  tsig |> map_tsig (fn ((space, classes), default, types) =>
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   511
    let
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   512
      val cs' = map (cert_class tsig) cs
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   513
        handle TYPE (msg, _, _) => error msg;
28991
694227dd3e8c dropped NameSpace.declare_base
haftmann
parents: 28965
diff changeset
   514
      val (c', space') = space |> NameSpace.declare naming (Binding.name c);
19515
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   515
      val classes' = classes |> Sorts.add_class pp (c', cs');
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   516
    in ((space', classes'), default, types) end);
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   517
26669
c27efd6de25d simplified hide_XXX interfaces;
wenzelm
parents: 26641
diff changeset
   518
fun hide_class fully c = map_tsig (fn ((space, classes), default, types) =>
c27efd6de25d simplified hide_XXX interfaces;
wenzelm
parents: 26641
diff changeset
   519
  ((NameSpace.hide fully c space, classes), default, types));
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   520
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   521
19515
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   522
(* arities *)
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   523
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   524
fun add_arity pp (t, Ss, S) tsig = tsig |> map_tsig (fn ((space, classes), default, types) =>
19515
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   525
  let
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   526
    val _ =
27302
8d12ac6a3e1c type constructors now with markup
haftmann
parents: 27273
diff changeset
   527
      (case lookup_type tsig t of
19515
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   528
        SOME (LogicalType n, _) => if length Ss <> n then error (bad_nargs t) else ()
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   529
      | SOME (decl, _) => error ("Illegal " ^ str_of_decl decl ^ ": " ^ quote t)
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   530
      | NONE => error (undecl_type t));
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   531
    val (Ss', S') = (map (cert_sort tsig) Ss, cert_sort tsig S)
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   532
      handle TYPE (msg, _, _) => error msg;
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   533
    val classes' = classes |> Sorts.add_arities pp ((t, map (fn c' => (c', Ss')) S'));
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   534
  in ((space, classes'), default, types) end);
19515
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   535
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   536
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   537
(* classrel *)
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   538
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   539
fun add_classrel pp rel tsig =
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   540
  tsig |> map_tsig (fn ((space, classes), default, types) =>
19515
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   541
    let
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   542
      val rel' = pairself (cert_class tsig) rel
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   543
        handle TYPE (msg, _, _) => error msg;
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   544
      val classes' = classes |> Sorts.add_classrel pp rel;
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   545
    in ((space, classes'), default, types) end);
19515
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   546
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   547
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   548
(* default sort *)
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   549
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   550
fun set_defsort S tsig = tsig |> map_tsig (fn (classes, _, types) =>
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   551
  (classes, cert_sort tsig S handle TYPE (msg, _, _) => error msg, types));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   552
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   553
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   554
(* types *)
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   555
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   556
local
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   557
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   558
fun err_neg_args c =
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   559
  error ("Negative number of arguments in type constructor declaration: " ^ quote c);
949
83c588d6fee9 Changed treatment of during type inference internally generated type
nipkow
parents: 621
diff changeset
   560
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   561
fun err_in_decls c decl decl' =
14906
2da524f3d785 tuned messages;
wenzelm
parents: 14830
diff changeset
   562
  let val s = str_of_decl decl and s' = str_of_decl decl' in
2da524f3d785 tuned messages;
wenzelm
parents: 14830
diff changeset
   563
    if s = s' then error ("Duplicate declaration of " ^ s ^ ": " ^ quote c)
14830
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
   564
    else error ("Conflict of " ^ s ^ " with " ^ s' ^ ": " ^ quote c)
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   565
  end;
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   566
27302
8d12ac6a3e1c type constructors now with markup
haftmann
parents: 27273
diff changeset
   567
fun new_decl naming tags (c, decl) (space, types) =
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   568
  let
27313
07754b7ba89d the_tags: explicit error message;
wenzelm
parents: 27302
diff changeset
   569
    val tags' = tags |> Position.default_properties (Position.thread_data ());
28991
694227dd3e8c dropped NameSpace.declare_base
haftmann
parents: 28965
diff changeset
   570
    val (c', space') = NameSpace.declare naming (Binding.name c) space;
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   571
    val types' =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   572
      (case Symtab.lookup types c' of
27302
8d12ac6a3e1c type constructors now with markup
haftmann
parents: 27273
diff changeset
   573
        SOME ((decl', _), _) => err_in_decls c' decl decl'
27313
07754b7ba89d the_tags: explicit error message;
wenzelm
parents: 27302
diff changeset
   574
      | NONE => Symtab.update (c', ((decl, tags'), serial ())) types);
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   575
  in (space', types') end;
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   576
27302
8d12ac6a3e1c type constructors now with markup
haftmann
parents: 27273
diff changeset
   577
fun the_decl (_, types) = fst o fst o the o Symtab.lookup types;
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   578
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   579
fun map_types f = map_tsig (fn (classes, default, types) =>
19579
b802d1804b77 replaced Sorts.DOMAIN by general Sorts.CLASS_ERROR;
wenzelm
parents: 19530
diff changeset
   580
  let
b802d1804b77 replaced Sorts.DOMAIN by general Sorts.CLASS_ERROR;
wenzelm
parents: 19530
diff changeset
   581
    val (space', tab') = f types;
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21116
diff changeset
   582
    val _ = NameSpace.intern space' "dummy" = "dummy" orelse
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21116
diff changeset
   583
      error "Illegal declaration of dummy type";
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   584
  in (classes, default, (space', tab')) end);
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   585
14989
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   586
fun syntactic types (Type (c, Ts)) =
27302
8d12ac6a3e1c type constructors now with markup
haftmann
parents: 27273
diff changeset
   587
      (case Symtab.lookup types c of SOME ((Nonterminal, _), _) => true | _ => false)
14989
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   588
        orelse exists (syntactic types) Ts
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   589
  | syntactic _ _ = false;
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   590
27302
8d12ac6a3e1c type constructors now with markup
haftmann
parents: 27273
diff changeset
   591
in
8d12ac6a3e1c type constructors now with markup
haftmann
parents: 27273
diff changeset
   592
8d12ac6a3e1c type constructors now with markup
haftmann
parents: 27273
diff changeset
   593
fun add_type naming tags (c, n) = if n < 0 then err_neg_args c else
8d12ac6a3e1c type constructors now with markup
haftmann
parents: 27273
diff changeset
   594
  map_types (new_decl naming tags (c, LogicalType n));
8d12ac6a3e1c type constructors now with markup
haftmann
parents: 27273
diff changeset
   595
8d12ac6a3e1c type constructors now with markup
haftmann
parents: 27273
diff changeset
   596
fun add_abbrev naming tags (a, vs, rhs) tsig = tsig |> map_types (fn types =>
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   597
  let
19250
932a50e2332f got rid of type Sign.sg;
wenzelm
parents: 18964
diff changeset
   598
    fun err msg = cat_error msg ("The error(s) above occurred in type abbreviation: " ^ quote a);
24274
cb9236269af1 type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents: 23655
diff changeset
   599
    val rhs' = strip_sorts (no_tvars (cert_typ_mode mode_syntax tsig rhs))
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   600
      handle TYPE (msg, _, _) => err msg;
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   601
  in
18964
67f572e03236 renamed gen_duplicates to duplicates;
wenzelm
parents: 18957
diff changeset
   602
    (case duplicates (op =) vs of
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   603
      [] => []
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   604
    | dups => err ("Duplicate variables on lhs: " ^ commas_quote dups));
27273
d54ae0bdad80 add_abbrev: check tfrees of rhs, not tvars (addresses a lapse introduced in 1.65);
wenzelm
parents: 26669
diff changeset
   605
    (case subtract (op =) vs (map #1 (Term.add_tfreesT rhs' [])) of
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   606
      [] => []
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   607
    | extras => err ("Extra variables on rhs: " ^ commas_quote extras));
27302
8d12ac6a3e1c type constructors now with markup
haftmann
parents: 27273
diff changeset
   608
    types |> new_decl naming tags (a, Abbreviation (vs, rhs', syntactic (#2 types) rhs'))
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   609
  end);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   610
27302
8d12ac6a3e1c type constructors now with markup
haftmann
parents: 27273
diff changeset
   611
fun add_nonterminal naming tags = map_types o new_decl naming tags o rpair Nonterminal;
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   612
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   613
fun merge_types (types1, types2) =
20674
93baed0f741c serial numbers for types;
wenzelm
parents: 20548
diff changeset
   614
  NameSpace.merge_tables (Library.eq_snd (op = : serial * serial -> bool)) (types1, types2)
23655
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 23178
diff changeset
   615
    handle Symtab.DUP d => err_in_decls d (the_decl types1 d) (the_decl types2 d);
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   616
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   617
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   618
26669
c27efd6de25d simplified hide_XXX interfaces;
wenzelm
parents: 26641
diff changeset
   619
fun hide_type fully c = map_tsig (fn (classes, default, (space, types)) =>
c27efd6de25d simplified hide_XXX interfaces;
wenzelm
parents: 26641
diff changeset
   620
  (classes, default, (NameSpace.hide fully c space, types)));
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   621
3790
95a47d8bcd69 eliminated raise_term, raise_typ;
wenzelm
parents: 3411
diff changeset
   622
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   623
(* merge type signatures *)
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   624
14830
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
   625
fun merge_tsigs pp (tsig1, tsig2) =
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   626
  let
19515
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   627
    val (TSig {classes = (space1, classes1), default = default1, types = types1,
26641
cf67665296c2 tsig: removed unnecessary universal witness;
wenzelm
parents: 26517
diff changeset
   628
      log_types = _}) = tsig1;
19515
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   629
    val (TSig {classes = (space2, classes2), default = default2, types = types2,
26641
cf67665296c2 tsig: removed unnecessary universal witness;
wenzelm
parents: 26517
diff changeset
   630
      log_types = _}) = tsig2;
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   631
19515
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   632
    val space' = NameSpace.merge (space1, space2);
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   633
    val classes' = Sorts.merge_algebra pp (classes1, classes2);
19515
9f650083da65 build classes/arities: refer to operations in sorts.ML;
wenzelm
parents: 19464
diff changeset
   634
    val default' = Sorts.inter_sort classes' (default1, default2);
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   635
    val types' = merge_types (types1, types2);
19642
ea7162f84677 more abstract interface to classes/arities;
wenzelm
parents: 19579
diff changeset
   636
  in build_tsig ((space', classes'), default', types') end;
14790
0d984ee030a1 major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents: 13666
diff changeset
   637
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   638
end;