src/Pure/type.ML
author berghofe
Fri, 01 Jul 2005 14:19:36 +0200
changeset 16650 bd4f7149ba1e
parent 16444 80c8f742c6fc
child 16885 cabcd33cde18
permissions -rw-r--r--
Moved eq_type from envir.ML to type.ML
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
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    ID:         $Id$
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
     3
    Author:     Tobias Nipkow, Lawrence C Paulson, and Markus Wenzel
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
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
     5
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
     6
matching and unification of types, extend and merge type signatures.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     8
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     9
signature TYPE =
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
    10
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
    11
  (*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
    12
  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
    13
    LogicalType of int |
14989
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
    14
    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
    15
    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
    16
  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
    17
  val rep_tsig: tsig ->
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
    18
   {classes: NameSpace.T * Sorts.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
    19
    default: sort,
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
    20
    types: (decl * stamp) NameSpace.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
    21
    arities: Sorts.arities,
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
    log_types: 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
    23
    witness: (typ * sort) option}
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 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
    25
  val classes: tsig -> class 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
    26
  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
    27
  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
    28
  val universal_witness: tsig -> (typ * sort) option
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 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
    30
  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
    31
  val of_sort: tsig -> typ * 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
    32
  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
    33
  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
    34
  val witness_sorts: tsig -> sort list -> sort list -> (typ * sort) list
14993
802f3732a54e tuned certify_typ/term;
wenzelm
parents: 14989
diff changeset
    35
  val cert_typ: tsig -> typ -> typ
802f3732a54e tuned certify_typ/term;
wenzelm
parents: 14989
diff changeset
    36
  val cert_typ_syntax: tsig -> typ -> typ
16340
fd027bb32896 renamed cert_typ_raw to cert_typ_abbrev;
wenzelm
parents: 16289
diff changeset
    37
  val cert_typ_abbrev: tsig -> typ -> typ
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
    38
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
    39
  (*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
    40
  val strip_sorts: typ -> typ
621
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
    41
  val no_tvars: typ -> typ
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
    42
  val varifyT: typ -> typ
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
    43
  val unvarifyT: typ -> typ
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
    44
  val varify: term * (string * sort) list -> term * ((string * sort) * indexname) list
16289
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
    45
  val freeze_thaw_type: typ -> typ * (typ -> typ)
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
    46
  val freeze_type: typ -> typ
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
    47
  val freeze_thaw: term -> term * (term -> term)
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
    48
  val freeze: term -> term
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
    49
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
    50
  (*matching and unification*)
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
    51
  exception TYPE_MATCH
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
    52
  type tyenv
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
    53
  val lookup: tyenv * (indexname * sort) -> typ option
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
    54
  val typ_match: tsig -> tyenv * (typ * typ) -> 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
    55
  val typ_instance: tsig -> typ * typ -> bool
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
    56
  exception TUNIFY
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
    57
  val unify: tsig -> tyenv * int -> typ * typ -> tyenv * int
450
382b5368ec21 added raw_unify;
wenzelm
parents: 422
diff changeset
    58
  val raw_unify: typ * typ -> bool
16650
bd4f7149ba1e Moved eq_type from envir.ML to type.ML
berghofe
parents: 16444
diff changeset
    59
  val eq_type: tyenv -> typ * typ -> bool
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    60
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
    61
  (*extend and merge type signatures*)
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
    62
  val add_classes: Pretty.pp -> NameSpace.naming -> (bstring * class list) list -> tsig -> tsig
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
    63
  val hide_classes: bool -> string list -> tsig -> tsig
14830
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
    64
  val add_classrel: Pretty.pp -> (class * class) list -> 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
    65
  val set_defsort: sort -> tsig -> tsig
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
    66
  val add_types: NameSpace.naming -> (bstring * int) list -> tsig -> tsig
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
    67
  val add_abbrevs: NameSpace.naming -> (string * string list * typ) list -> tsig -> tsig
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
    68
  val add_nonterminals: NameSpace.naming -> string list -> tsig -> tsig
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
    69
  val hide_types: bool -> string list -> tsig -> tsig
14830
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
    70
  val add_arities: Pretty.pp -> arity list -> tsig -> tsig
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
    71
  val merge_tsigs: Pretty.pp -> tsig * tsig -> tsig
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
    72
end;
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
    73
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
    74
structure Type: TYPE =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    75
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    76
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
    77
(** type signatures and certified types **)
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
    78
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
    79
(* 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
    80
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
    81
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
    82
  LogicalType of int |
14989
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
    83
  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
    84
  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
    85
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
    86
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
    87
  | 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
    88
  | 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
    89
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
(* 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
    92
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
    93
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
    94
  TSig of {
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
    95
    classes: NameSpace.T * Sorts.classes,   (*declared classes with proper subclass relation*)
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
    96
    default: sort,                          (*default sort on input*)
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
    97
    types: (decl * stamp) NameSpace.table,  (*declared types*)
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
    98
    arities: Sorts.arities,                 (*image specification of types wrt. sorts*)
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
    99
    log_types: string list,                 (*logical types sorted by number of arguments*)
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   100
    witness: (typ * sort) option};          (*witness for non-emptiness of strictest 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
   101
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
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
   103
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
fun make_tsig (classes, default, types, arities, log_types, witness) =
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
   105
  TSig {classes = classes, default = default, types = types, arities = arities,
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
   106
    log_types = log_types, witness = witness};
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
   107
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
   108
fun build_tsig (classes, default, types, arities) =
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
  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
   110
    val log_types =
16444
80c8f742c6fc Symtab.fold;
wenzelm
parents: 16370
diff changeset
   111
      Symtab.fold (fn (c, (LogicalType n, _)) => cons (c, n) | _ => I) (#2 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
   112
      |> Library.sort (Library.int_ord o pairself #2) |> map #1;
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
   113
    val witness =
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   114
      (case Sorts.witness_sorts (#2 classes, arities) log_types [] [Graph.keys (#2 classes)] of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   115
        [w] => SOME w | _ => NONE);
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
  in make_tsig (classes, default, types, arities, log_types, witness) 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
   117
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   118
fun map_tsig f (TSig {classes, default, types, arities, log_types = _, witness = _}) =
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
   119
  build_tsig (f (classes, default, types, arities));
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
   120
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   121
val empty_tsig =
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   122
  build_tsig ((NameSpace.empty, Graph.empty), [], NameSpace.empty_table, Symtab.empty);
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
   123
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
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
   125
(* 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
   126
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   127
fun classes (TSig {classes = (_, C), ...}) = Graph.keys 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
   128
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
   129
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
   130
fun universal_witness (TSig {witness, ...}) = witness;
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
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   132
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
   133
fun subsort (TSig {classes, ...}) = Sorts.sort_le (#2 classes);
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   134
fun of_sort (TSig {classes, arities, ...}) = Sorts.of_sort (#2 classes, arities);
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
   135
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   136
fun cert_class (TSig {classes, ...}) = Sorts.certify_class (#2 classes);
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   137
fun cert_sort (TSig {classes, ...}) = Sorts.certify_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
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
   139
fun witness_sorts (tsig as TSig {classes, arities, log_types, ...}) =
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   140
  Sorts.witness_sorts (#2 classes, arities) 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
   141
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
   142
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
   143
(* 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
   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
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
   146
fun undecl_type c = "Undeclared type constructor: " ^ quote c;
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
   147
14998
wenzelm
parents: 14993
diff changeset
   148
local
wenzelm
parents: 14993
diff changeset
   149
wenzelm
parents: 14993
diff changeset
   150
fun inst_typ env (Type (c, Ts)) = Type (c, map (inst_typ env) Ts)
16289
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   151
  | inst_typ env (T as TFree (x, _)) = if_none (Library.assoc_string (env, x)) T
14998
wenzelm
parents: 14993
diff changeset
   152
  | inst_typ _ T = T;
wenzelm
parents: 14993
diff changeset
   153
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
   154
fun certify_typ normalize syntax tsig 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
   155
  let
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   156
    val TSig {classes = (_, classes), types = (_, types), ...} = 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
   157
    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
   158
14989
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   159
    val check_syntax =
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   160
      if syntax then K ()
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   161
      else fn c => err ("Illegal occurrence of syntactic type: " ^ quote c);
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   162
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   163
    fun cert (T as Type (c, Ts)) =
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   164
          let
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   165
            val Ts' = map cert Ts;
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   166
            fun nargs n = if length Ts <> n then err (bad_nargs c) else ();
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   167
          in
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
   168
            (case Symtab.lookup (types, c) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   169
              SOME (LogicalType n, _) => (nargs n; Type (c, Ts'))
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   170
            | SOME (Abbreviation (vs, U, syn), _) => (nargs (length vs);
14989
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   171
                if syn then check_syntax c else ();
14993
802f3732a54e tuned certify_typ/term;
wenzelm
parents: 14989
diff changeset
   172
                if normalize then inst_typ (vs ~~ Ts') U
14989
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   173
                else Type (c, Ts'))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   174
            | SOME (Nonterminal, _) => (nargs 0; check_syntax c; T)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   175
            | 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
   176
          end
14989
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   177
      | cert (TFree (x, S)) = TFree (x, Sorts.certify_sort classes S)
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   178
      | cert (TVar (xi as (_, i), S)) =
14993
802f3732a54e tuned certify_typ/term;
wenzelm
parents: 14989
diff changeset
   179
          if i < 0 then
802f3732a54e tuned certify_typ/term;
wenzelm
parents: 14989
diff changeset
   180
            err ("Malformed type variable: " ^ quote (Term.string_of_vname xi))
14989
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   181
          else TVar (xi, Sorts.certify_sort classes 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
   182
14989
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   183
    val ty' = cert ty;
14993
802f3732a54e tuned certify_typ/term;
wenzelm
parents: 14989
diff changeset
   184
  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
   185
14998
wenzelm
parents: 14993
diff changeset
   186
in
wenzelm
parents: 14993
diff changeset
   187
16340
fd027bb32896 renamed cert_typ_raw to cert_typ_abbrev;
wenzelm
parents: 16289
diff changeset
   188
val cert_typ        = certify_typ true false;
fd027bb32896 renamed cert_typ_raw to cert_typ_abbrev;
wenzelm
parents: 16289
diff changeset
   189
val cert_typ_syntax = certify_typ true true;
fd027bb32896 renamed cert_typ_raw to cert_typ_abbrev;
wenzelm
parents: 16289
diff changeset
   190
val cert_typ_abbrev = certify_typ false true;
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
   191
14998
wenzelm
parents: 14993
diff changeset
   192
end;
wenzelm
parents: 14993
diff changeset
   193
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
   194
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
   195
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
   196
(** 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
   197
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
   198
(* 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
   199
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
   200
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
   201
  | 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
   202
  | 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
   203
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
   204
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
   205
(* no_tvars *)
621
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
   206
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
   207
fun no_tvars T =
12501
36b2ac65e18d varify returns newly introduced variables;
wenzelm
parents: 12314
diff changeset
   208
  (case typ_tvars T of [] => T
36b2ac65e18d varify returns newly introduced variables;
wenzelm
parents: 12314
diff changeset
   209
  | vs => raise TYPE ("Illegal schematic type variable(s): " ^
14830
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
   210
      commas_quote (map (Term.string_of_vname o #1) vs), [T], []));
621
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
   211
7641
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   212
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   213
(* varify, unvarify *)
621
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
   214
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   215
val varifyT = map_type_tfree (fn (a, S) => TVar ((a, 0), S));
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   216
621
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
   217
fun unvarifyT (Type (a, Ts)) = Type (a, map unvarifyT Ts)
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
   218
  | unvarifyT (TVar ((a, 0), S)) = TFree (a, S)
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
   219
  | unvarifyT T = T;
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
   220
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
   221
fun varify (t, fixed) =
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
   222
  let
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   223
    val fs = add_term_tfrees (t, []) \\ fixed;
621
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
   224
    val ixns = add_term_tvar_ixns (t, []);
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   225
    val fmap = fs ~~ map (rpair 0) (variantlist (map fst fs, map #1 ixns))
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   226
    fun thaw (f as (a, S)) =
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   227
      (case assoc (fmap, f) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   228
        NONE => TFree f
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   229
      | SOME b => TVar (b, S));
12501
36b2ac65e18d varify returns newly introduced variables;
wenzelm
parents: 12314
diff changeset
   230
  in (map_term_types (map_type_tfree thaw) t, fmap) end;
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   231
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   232
7641
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   233
(* 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
   234
7641
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   235
local
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   236
16289
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   237
fun new_name (ix, (pairs, used)) =
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   238
  let val v = variant used (string_of_indexname ix)
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   239
  in ((ix, v) :: pairs, v :: used) end;
621
9d8791da0208 improved expand_typ (now handles TVars);
wenzelm
parents: 585
diff changeset
   240
16289
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   241
fun freeze_one alist (ix, sort) =
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   242
  TFree (the (assoc_string_int (alist, ix)), sort)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   243
    handle Option =>
3790
95a47d8bcd69 eliminated raise_term, raise_typ;
wenzelm
parents: 3411
diff changeset
   244
      raise TYPE ("Failure during freezing of ?" ^ string_of_indexname ix, [], []);
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   245
16289
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   246
fun thaw_one alist (a, sort) = TVar (the (assoc_string (alist, a)), sort)
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   247
  handle Option => TFree (a, sort);
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   248
10495
284ee538991c export freeze_thaw_type;
wenzelm
parents: 9504
diff changeset
   249
in
284ee538991c export freeze_thaw_type;
wenzelm
parents: 9504
diff changeset
   250
284ee538991c export freeze_thaw_type;
wenzelm
parents: 9504
diff changeset
   251
(*this sort of code could replace unvarifyT*)
7641
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   252
fun freeze_thaw_type T =
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   253
  let
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   254
    val used = add_typ_tfree_names (T, [])
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   255
    and tvars = map #1 (add_typ_tvars (T, []));
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   256
    val (alist, _) = foldr new_name ([], used) tvars;
7641
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   257
  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
   258
16289
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   259
val freeze_type = #1 o freeze_thaw_type;
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   260
3411
163f8f4a42d7 Removal of freeze_vars and thaw_vars. New freeze_thaw
paulson
parents: 3175
diff changeset
   261
fun freeze_thaw t =
7641
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   262
  let
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   263
    val used = it_term_types add_typ_tfree_names (t, [])
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   264
    and tvars = map #1 (it_term_types add_typ_tvars (t, []));
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   265
    val (alist, _) = foldr new_name ([], used) tvars;
7641
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   266
  in
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   267
    (case alist of
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   268
      [] => (t, fn x => x) (*nothing to do!*)
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   269
    | _ => (map_term_types (map_type_tvar (freeze_one alist)) t,
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   270
      map_term_types (map_type_tfree (thaw_one (map swap alist)))))
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   271
  end;
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   272
16289
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   273
val freeze = #1 o freeze_thaw;
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   274
7641
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   275
end;
f058df348272 added witness_sorts, univ_witness;
wenzelm
parents: 7247
diff changeset
   276
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   277
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   278
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
   279
(** matching and unification of types **)
8899
99266fe189a1 removed is_type_abbr;
wenzelm
parents: 8721
diff changeset
   280
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   281
type tyenv = (sort * typ) Vartab.table;
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   282
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   283
fun tvar_clash ixn S S' = raise TYPE ("Type variable " ^
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   284
  quote (Term.string_of_vname ixn) ^ " has two distinct sorts",
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   285
  [TVar (ixn, S), TVar (ixn, S')], []);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   286
16289
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   287
fun lookup (tye, (ixn, S)) =
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   288
  (case Vartab.lookup (tye, ixn) of
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   289
    NONE => NONE
16289
958207815931 added Type.freeze(_type);
wenzelm
parents: 15797
diff changeset
   290
  | 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
   291
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   292
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
   293
(* matching *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   294
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   295
exception TYPE_MATCH;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   296
16340
fd027bb32896 renamed cert_typ_raw to cert_typ_abbrev;
wenzelm
parents: 16289
diff changeset
   297
fun typ_match tsig (tyenv, TU) =
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   298
  let
16340
fd027bb32896 renamed cert_typ_raw to cert_typ_abbrev;
wenzelm
parents: 16289
diff changeset
   299
    fun match (TVar (v, S), T) subs =
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   300
          (case lookup (subs, (v, S)) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   301
            NONE =>
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   302
              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
   303
              else raise TYPE_MATCH
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   304
          | 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
   305
      | match (Type (a, Ts), Type (b, Us)) subs =
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   306
          if a <> b then raise TYPE_MATCH
16340
fd027bb32896 renamed cert_typ_raw to cert_typ_abbrev;
wenzelm
parents: 16289
diff changeset
   307
          else fold match (Ts ~~ Us) subs
fd027bb32896 renamed cert_typ_raw to cert_typ_abbrev;
wenzelm
parents: 16289
diff changeset
   308
      | match (TFree x, TFree y) subs =
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   309
          if x = y then subs else raise TYPE_MATCH
16340
fd027bb32896 renamed cert_typ_raw to cert_typ_abbrev;
wenzelm
parents: 16289
diff changeset
   310
      | match _ _ = raise TYPE_MATCH;
fd027bb32896 renamed cert_typ_raw to cert_typ_abbrev;
wenzelm
parents: 16289
diff changeset
   311
  in match TU tyenv end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   312
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
   313
fun typ_instance tsig (T, U) =
8406
a217b0cd304d Type.unify and Type.typ_match now use Vartab instead of association lists.
berghofe
parents: 7641
diff changeset
   314
  (typ_match tsig (Vartab.empty, (U, T)); true) handle TYPE_MATCH => false;
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   315
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   316
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
   317
(* unification *)
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   318
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   319
exception TUNIFY;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   320
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
   321
(*occurs_check*)
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   322
fun occurs v tye =
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   323
  let
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   324
    fun occ (Type (_, Ts)) = exists occ Ts
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   325
      | occ (TFree _) = false
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   326
      | occ (TVar (w, S)) =
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   327
          eq_ix (v, w) orelse
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   328
            (case lookup (tye, (w, S)) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   329
              NONE => false
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   330
            | SOME U => occ U);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   331
  in occ end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   332
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
   333
(*chase variable assignments; if devar returns a type var then it must be unassigned*)
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   334
fun devar (T as TVar v, tye) =
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   335
      (case  lookup (tye, v) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   336
        SOME U => devar (U, tye)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   337
      | NONE => T)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   338
  | devar (T, tye) = T;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   339
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   340
fun unify (tsig as TSig {classes = (_, classes), arities, ...}) (tyenv, maxidx) TU =
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   341
  let
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   342
    val tyvar_count = ref maxidx;
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   343
    fun gen_tyvar S = TVar (("'a", inc tyvar_count), S);
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   344
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   345
    fun mg_domain a 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
   346
      Sorts.mg_domain (classes, arities) a S handle Sorts.DOMAIN _ => raise TUNIFY;
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   347
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   348
    fun meet ((_, []), tye) = tye
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   349
      | 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
   350
          if Sorts.sort_le classes (S', S) then tye
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   351
          else Vartab.update_new ((xi, (S',
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   352
            gen_tyvar (Sorts.inter_sort classes (S', S)))), tye)
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   353
      | 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
   354
          if Sorts.sort_le classes (S', S) then tye
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   355
          else raise TUNIFY
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   356
      | meet ((Type (a, Ts), S), tye) = meets ((Ts, mg_domain a S), tye)
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   357
    and meets (([], []), tye) = tye
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   358
      | meets ((T :: Ts, S :: Ss), tye) =
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   359
          meets ((Ts, Ss), meet ((devar (T, tye), S), tye))
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   360
      | meets _ = sys_error "meets";
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   361
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   362
    fun unif ((ty1, ty2), tye) =
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   363
      (case (devar (ty1, tye), devar (ty2, tye)) of
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   364
        (T as TVar (v, S1), U as TVar (w, S2)) =>
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   365
          if eq_ix (v, w) then
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   366
            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
   367
          else if Sorts.sort_le classes (S1, S2) then
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   368
            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
   369
          else if Sorts.sort_le classes (S2, S1) then
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   370
            Vartab.update_new ((v, (S1, U)), tye)
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   371
          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
   372
            let val S = gen_tyvar (Sorts.inter_sort classes (S1, S2)) in
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   373
              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
   374
            end
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   375
      | (TVar (v, S), T) =>
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   376
          if occurs v tye T then raise TUNIFY
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   377
          else meet ((T, S), Vartab.update_new ((v, (S, T)), tye))
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   378
      | (T, TVar (v, S)) =>
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   379
          if occurs v tye T then raise TUNIFY
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   380
          else meet ((T, S), Vartab.update_new ((v, (S, T)), tye))
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   381
      | (Type (a, Ts), Type (b, Us)) =>
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   382
          if a <> b then raise TUNIFY
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   383
          else foldr unif tye (Ts ~~ Us)
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   384
      | (T, U) => if T = U then tye else raise TUNIFY);
12528
b8bc541a4544 tuned interface of unify, param;
wenzelm
parents: 12501
diff changeset
   385
  in (unif (TU, tyenv), ! tyvar_count) end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   386
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
   387
(*purely structural unification *)
450
382b5368ec21 added raw_unify;
wenzelm
parents: 422
diff changeset
   388
fun raw_unify (ty1, ty2) =
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
   389
  (unify empty_tsig (Vartab.empty, 0) (strip_sorts ty1, strip_sorts ty2); true)
450
382b5368ec21 added raw_unify;
wenzelm
parents: 422
diff changeset
   390
    handle TUNIFY => false;
382b5368ec21 added raw_unify;
wenzelm
parents: 422
diff changeset
   391
16650
bd4f7149ba1e Moved eq_type from envir.ML to type.ML
berghofe
parents: 16444
diff changeset
   392
(*check whether two types are equal with respect to a type environment*)
bd4f7149ba1e Moved eq_type from envir.ML to type.ML
berghofe
parents: 16444
diff changeset
   393
fun eq_type tye (T, T') =
bd4f7149ba1e Moved eq_type from envir.ML to type.ML
berghofe
parents: 16444
diff changeset
   394
  (case (devar (T, tye), devar (T', tye)) of
bd4f7149ba1e Moved eq_type from envir.ML to type.ML
berghofe
parents: 16444
diff changeset
   395
     (Type (s, Ts), Type (s', Ts')) =>
bd4f7149ba1e Moved eq_type from envir.ML to type.ML
berghofe
parents: 16444
diff changeset
   396
       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
   397
   | (U, U') => U = U');
bd4f7149ba1e Moved eq_type from envir.ML to type.ML
berghofe
parents: 16444
diff changeset
   398
450
382b5368ec21 added raw_unify;
wenzelm
parents: 422
diff changeset
   399
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   400
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
   401
(** 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
   402
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
   403
(* arities *)
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
   404
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
   405
local
1435
aefcd255ed4a Removed bug in type unification. Negative indexes are not used any longer.
nipkow
parents: 1392
diff changeset
   406
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
   407
fun err_decl t decl = error ("Illegal " ^ str_of_decl decl ^ ": " ^ quote t);
14830
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
   408
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   409
fun for_classes _ NONE = ""
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   410
  | for_classes pp (SOME (c1, c2)) =
14830
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
   411
      " for classes " ^ Pretty.string_of_classrel pp [c1, c2];
3790
95a47d8bcd69 eliminated raise_term, raise_typ;
wenzelm
parents: 3411
diff changeset
   412
14830
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
   413
fun err_conflict pp t cc (c, Ss) (c', Ss') =
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
   414
  error ("Conflict of type arities" ^ for_classes pp cc ^ ":\n  " ^
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
   415
    Pretty.string_of_arity pp (t, Ss, [c]) ^ " and\n  " ^
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
   416
    Pretty.string_of_arity pp (t, Ss', [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
   417
14830
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
   418
fun coregular pp C t (c, Ss) ars =
3790
95a47d8bcd69 eliminated raise_term, raise_typ;
wenzelm
parents: 3411
diff changeset
   419
  let
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
   420
    fun conflict (c', Ss') =
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
   421
      if Sorts.class_le C (c, c') andalso not (Sorts.sorts_le C (Ss, Ss')) then
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   422
        SOME ((c, c'), (c', Ss'))
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
   423
      else if Sorts.class_le C (c', c) andalso not (Sorts.sorts_le C (Ss', Ss)) then
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   424
        SOME ((c', c), (c', Ss'))
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   425
      else NONE;
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
   426
  in
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
    (case Library.get_first conflict ars of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   428
      SOME ((c1, c2), (c', Ss')) => err_conflict pp t (SOME (c1, c2)) (c, Ss) (c', Ss')
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   429
    | NONE => (c, Ss) :: ars)
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
   430
  end;
3790
95a47d8bcd69 eliminated raise_term, raise_typ;
wenzelm
parents: 3411
diff changeset
   431
16340
fd027bb32896 renamed cert_typ_raw to cert_typ_abbrev;
wenzelm
parents: 16289
diff changeset
   432
fun insert pp C t (c, Ss) ars =
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
   433
  (case assoc_string (ars, c) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   434
    NONE => coregular pp C t (c, Ss) ars
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   435
  | SOME Ss' =>
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
   436
      if Sorts.sorts_le C (Ss, Ss') then ars
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
   437
      else if Sorts.sorts_le C (Ss', Ss)
14830
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
   438
      then coregular pp C t (c, Ss) (ars \ (c, Ss'))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   439
      else err_conflict pp t NONE (c, Ss) (c, Ss'));
2587
ac51a89627ed added eq_sort (will move to sorts.ML eventually);
wenzelm
parents: 2272
diff changeset
   440
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
   441
fun complete C (c, Ss) = map (rpair Ss) (Graph.all_succs C [c]);
3790
95a47d8bcd69 eliminated raise_term, raise_typ;
wenzelm
parents: 3411
diff changeset
   442
16340
fd027bb32896 renamed cert_typ_raw to cert_typ_abbrev;
wenzelm
parents: 16289
diff changeset
   443
fun insert_arities pp classes (t, ars) arities =
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
  let val ars' =
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
   445
    Symtab.lookup_multi (arities, t)
16340
fd027bb32896 renamed cert_typ_raw to cert_typ_abbrev;
wenzelm
parents: 16289
diff changeset
   446
    |> fold_rev (fold_rev (insert pp classes t)) (map (complete classes) ars)
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
  in Symtab.update ((t, ars'), arities) end;
3790
95a47d8bcd69 eliminated raise_term, raise_typ;
wenzelm
parents: 3411
diff changeset
   448
16444
80c8f742c6fc Symtab.fold;
wenzelm
parents: 16370
diff changeset
   449
fun insert_table pp classes = Symtab.fold (fn (t, ars) =>
80c8f742c6fc Symtab.fold;
wenzelm
parents: 16370
diff changeset
   450
  insert_arities pp classes (t, map (apsnd (map (Sorts.norm_sort classes))) ars));
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
   451
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
   452
in
2587
ac51a89627ed added eq_sort (will move to sorts.ML eventually);
wenzelm
parents: 2272
diff changeset
   453
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   454
fun add_arities pp decls tsig = tsig |> map_tsig (fn (classes, default, types, arities) =>
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
   455
  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
   456
    fun prep (t, Ss, S) =
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   457
      (case Symtab.lookup (#2 types, t) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   458
        SOME (LogicalType n, _) =>
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
   459
          if length Ss = n then
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
   460
            (t, map (cert_sort tsig) Ss, cert_sort tsig S)
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
   461
              handle TYPE (msg, _, _) => error 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
   462
          else error (bad_nargs t)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   463
      | SOME (decl, _) => err_decl t decl
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   464
      | NONE => error (undecl_type t));
4603
53b2463ca84c added param;
wenzelm
parents: 4142
diff changeset
   465
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
   466
    val ars = decls |> map ((fn (t, Ss, S) => (t, map (fn c => (c, Ss)) S)) o prep);
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   467
    val arities' = fold (insert_arities pp (#2 classes)) ars arities;
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
   468
  in (classes, default, types, arities') end);
12528
b8bc541a4544 tuned interface of unify, param;
wenzelm
parents: 12501
diff changeset
   469
14830
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
   470
fun rebuild_arities pp classes arities =
16444
80c8f742c6fc Symtab.fold;
wenzelm
parents: 16370
diff changeset
   471
  Symtab.empty
80c8f742c6fc Symtab.fold;
wenzelm
parents: 16370
diff changeset
   472
  |> insert_table pp classes arities;
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
   473
14830
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
   474
fun merge_arities pp classes (arities1, arities2) =
16444
80c8f742c6fc Symtab.fold;
wenzelm
parents: 16370
diff changeset
   475
  Symtab.empty
80c8f742c6fc Symtab.fold;
wenzelm
parents: 16370
diff changeset
   476
  |> insert_table pp classes arities1
80c8f742c6fc Symtab.fold;
wenzelm
parents: 16370
diff changeset
   477
  |> insert_table pp classes arities2;
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
   478
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
   479
end;
4603
53b2463ca84c added param;
wenzelm
parents: 4142
diff changeset
   480
53b2463ca84c added param;
wenzelm
parents: 4142
diff changeset
   481
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
   482
(* 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
   483
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
   484
local
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   485
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
   486
fun err_dup_classes cs =
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
   487
  error ("Duplicate declaration of class(es): " ^ commas_quote cs);
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
   488
14830
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
   489
fun err_cyclic_classes pp css =
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
   490
  error (cat_lines (map (fn cs =>
14830
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
   491
    "Cycle in class relation: " ^ Pretty.string_of_classrel pp cs) css));
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
   492
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   493
fun add_class pp naming (c, cs) tsig =
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   494
  tsig |> map_tsig (fn ((space, classes), default, types, arities) =>
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   495
    let
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   496
      val c' = NameSpace.full naming c;
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   497
      val cs' = map (cert_class tsig) cs
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   498
        handle TYPE (msg, _, _) => error msg;
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   499
      val space' = space |> NameSpace.declare naming c';
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   500
      val classes' = classes |> Graph.new_node (c', stamp ())
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   501
        handle Graph.DUP dup => err_dup_classes [dup];
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   502
      val classes'' = classes' |> fold Graph.add_edge_trans_acyclic (map (pair c') cs')
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   503
        handle Graph.CYCLES css => err_cyclic_classes pp css;
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   504
    in ((space', classes''), default, types, arities) end);
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   505
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
   506
in
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   507
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   508
val add_classes = fold oo add_class;
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
   509
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   510
fun add_classrel pp ps tsig =
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   511
  tsig |> map_tsig (fn ((space, classes), default, types, arities) =>
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   512
    let
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   513
      val ps' = map (pairself (cert_class tsig)) ps
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   514
        handle TYPE (msg, _, _) => error msg;
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   515
      val classes' = classes |> fold Graph.add_edge_trans_acyclic ps'
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   516
        handle Graph.CYCLES css => err_cyclic_classes pp css;
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   517
      val default' = default |> Sorts.norm_sort classes';
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   518
      val arities' = arities |> rebuild_arities pp classes';
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   519
    in ((space, classes'), default', types, arities') end);
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   520
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   521
fun merge_classes pp ((space1, classes1), (space2, classes2)) =
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
   522
  let
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   523
    val space = NameSpace.merge (space1, space2);
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   524
    val classes =
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   525
      Graph.merge_trans_acyclic (op =) (classes1, classes2)
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   526
        handle Graph.DUPS cs => err_dup_classes cs
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   527
          | Graph.CYCLES css => err_cyclic_classes pp css;
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   528
  in (space, classes) 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
   529
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
   530
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
   531
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   532
fun hide_classes fully cs = map_tsig (fn ((space, classes), default, types, arities) =>
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   533
  ((fold (NameSpace.hide fully) cs space, classes), default, types, arities));
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   534
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
   535
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
   536
(* 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
   537
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   538
fun set_defsort S tsig = tsig |> map_tsig (fn (classes, _, types, arities) =>
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
   539
  (classes, cert_sort tsig S handle TYPE (msg, _, _) => error msg, types, arities));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   540
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   541
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
   542
(* 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
   543
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
   544
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
   545
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
   546
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
   547
  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
   548
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
   549
fun err_in_decls c decl decl' =
14906
2da524f3d785 tuned messages;
wenzelm
parents: 14830
diff changeset
   550
  let val s = str_of_decl decl and s' = str_of_decl decl' in
2da524f3d785 tuned messages;
wenzelm
parents: 14830
diff changeset
   551
    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
   552
    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
   553
  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
   554
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   555
fun new_decl naming (c, decl) (space, types) =
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   556
  let
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   557
    val c' = NameSpace.full naming c;
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   558
    val space' = NameSpace.declare naming c' space;
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   559
    val types' =
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   560
      (case Symtab.lookup (types, c') of
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   561
        SOME (decl', _) => err_in_decls c' decl decl'
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   562
      | NONE => Symtab.update ((c', (decl, stamp ())), types));
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   563
  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
   564
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   565
fun the_decl (_, types) c = fst (the (Symtab.lookup (types, 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
   566
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   567
fun change_types f = map_tsig (fn (classes, default, types, arities) =>
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
   568
  (classes, default, f types, arities));
2964
557a11310988 moved classes / sorts to sorts.ML;
wenzelm
parents: 2753
diff changeset
   569
14989
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   570
fun syntactic types (Type (c, Ts)) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14998
diff changeset
   571
      (case Symtab.lookup (types, c) of SOME (Nonterminal, _) => true | _ => false)
14989
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   572
        orelse exists (syntactic types) Ts
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   573
  | syntactic _ _ = false;
5a5d076a9863 tuned certify_typ;
wenzelm
parents: 14906
diff changeset
   574
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   575
fun add_abbrev naming (a, vs, rhs) tsig = tsig |> change_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
   576
  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
   577
    fun err msg =
14906
2da524f3d785 tuned messages;
wenzelm
parents: 14830
diff changeset
   578
      error (msg ^ "\nThe error(s) above occurred in type abbreviation: " ^ quote a);
16340
fd027bb32896 renamed cert_typ_raw to cert_typ_abbrev;
wenzelm
parents: 16289
diff changeset
   579
    val rhs' = compress_type (strip_sorts (no_tvars (cert_typ_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
   580
      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
   581
  in
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
   582
    (case duplicates vs of
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
   583
      [] => []
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
   584
    | dups => err ("Duplicate variables on lhs: " ^ commas_quote dups));
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
   585
    (case gen_rems (op =) (map (#1 o #1) (typ_tvars rhs'), vs) of
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
   586
      [] => []
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
   587
    | extras => err ("Extra variables on rhs: " ^ commas_quote extras));
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   588
    types |> new_decl naming (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
   589
  end);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   590
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
   591
in
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
   592
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   593
fun add_types naming ps = change_types (fold (new_decl naming) (ps |> map (fn (c, n) =>
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
   594
  if n < 0 then err_neg_args c else (c, LogicalType n))));
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
   595
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   596
val add_abbrevs = fold o add_abbrev;
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   597
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   598
fun add_nonterminals naming = change_types o fold (new_decl naming) o map (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
   599
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
fun merge_types (types1, types2) =
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   601
  NameSpace.merge_tables Library.eq_snd (types1, types2) handle Symtab.DUPS (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
   602
    err_in_decls d (the_decl types1 d) (the_decl types2 d);
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
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   605
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   606
fun hide_types fully cs = map_tsig (fn (classes, default, (space, types), arities) =>
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   607
  (classes, default, (fold (NameSpace.hide fully) cs space, types), arities));
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   608
3790
95a47d8bcd69 eliminated raise_term, raise_typ;
wenzelm
parents: 3411
diff changeset
   609
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
   610
(* 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
   611
14830
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
   612
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
   613
  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
   614
    val (TSig {classes = classes1, default = default1, types = types1, arities = arities1,
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
   615
      log_types = _, witness = _}) = tsig1;
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
    val (TSig {classes = classes2, default = default2, types = types2, arities = arities2,
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
      log_types = _, witness = _}) = tsig2;
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
   618
14830
faa4865ba1ce removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents: 14790
diff changeset
   619
    val classes' = merge_classes pp (classes1, classes2);
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   620
    val default' = Sorts.inter_sort (#2 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
   621
    val types' = merge_types (types1, types2);
16370
033d890fe91f name space of classes and types maintained in tsig;
wenzelm
parents: 16340
diff changeset
   622
    val arities' = merge_arities pp (#2 classes') (arities1, arities2);
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
  in build_tsig (classes', default', types', arities') 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
   624
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   625
end;