src/Pure/type.ML
author lcp
Tue, 21 Jun 1994 17:20:34 +0200
changeset 435 ca5356bd315a
parent 422 8f194014a9c8
child 450 382b5368ec21
permissions -rw-r--r--
Addition of cardinals and order types, various tidying
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$
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
     3
    Author:     Tobias Nipkow & Lawrence C Paulson
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
     5
Type classes and sorts. Type signatures. Type unification and inference.
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
     6
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
     7
TODO:
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
     8
  move type unification and inference to type_unify.ML (TypeUnify) (?)
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
     9
  rename args -> tycons, coreg -> arities (?)
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    10
  clean err msgs
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    12
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    13
signature TYPE =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    14
sig
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    15
  structure Symtab: SYMTAB
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    16
  val str_of_sort: sort -> string
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    17
  val str_of_arity: string * sort list * sort -> string
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    18
  type type_sig
200
39a931cc6558 Necessary changes to accomodate type abbreviations.
nipkow
parents: 189
diff changeset
    19
  val rep_tsig: type_sig ->
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    20
    {classes: class list,
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    21
     subclass: (class * class list) list,
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    22
     default: sort,
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    23
     args: (string * int) list,
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    24
     abbrs: (string * (indexname list * typ)) list,
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    25
     coreg: (string * (class * sort list) list) list}
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    26
  val defaultS: type_sig -> sort
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    27
  val tsig0: type_sig
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    28
  val logical_types: type_sig -> string list
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    29
  val extend_tsig: type_sig ->
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    30
    (class * class list) list * sort * (string list * int) list *
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    31
    (string list * (sort list * class)) list -> type_sig
422
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
    32
  val ext_tsig_subclass: type_sig -> (class * class) list -> type_sig
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
    33
  val ext_tsig_defsort: type_sig -> sort -> type_sig
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    34
  val ext_tsig_abbrs: type_sig -> (string * (indexname list * typ)) list
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    35
    -> type_sig
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    36
  val merge_tsigs: type_sig * type_sig -> type_sig
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    37
  val subsort: type_sig -> sort * sort -> bool
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    38
  val norm_sort: type_sig -> sort -> sort
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    39
  val rem_sorts: typ -> typ
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    40
  val cert_typ: type_sig -> typ -> typ
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    41
  val norm_typ: type_sig -> typ -> typ
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    42
  val freeze: (indexname -> bool) -> term -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    43
  val freeze_vars: typ -> typ
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    44
  val infer_types: type_sig * typ Symtab.table * (indexname -> typ option) *
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    45
    (indexname -> sort option) * typ * term -> term * (indexname * typ) list
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    46
  val inst_term_tvars: type_sig * (indexname * typ) list -> term -> term
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    47
  val thaw_vars: typ -> typ
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    48
  val typ_errors: type_sig -> typ * string list -> string list
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    49
  val typ_instance: type_sig * typ * typ -> bool
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    50
  val typ_match: type_sig -> (indexname * typ) list * (typ * typ)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    51
    -> (indexname * typ) list
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    52
  val unify: type_sig -> (typ * typ) * (indexname * typ) list
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    53
    -> (indexname * typ) list
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    54
  val varifyT: typ -> typ
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    55
  val varify: term * string list -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    56
  exception TUNIFY
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    57
  exception TYPE_MATCH
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    58
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    59
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    60
functor TypeFun(structure Symtab: SYMTAB and Syntax: SYNTAX): TYPE =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    61
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    63
structure Symtab = Symtab;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    64
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    65
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    66
(*** type classes and sorts ***)
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    67
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    68
(*
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    69
  Classes denote (possibly empty) collections of types (e.g. sets of types)
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    70
  and are partially ordered by 'inclusion'. They are represented by strings.
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    71
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    72
  Sorts are intersections of finitely many classes. They are represented by
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    73
  lists of classes.
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    74
*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    75
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    76
type domain = sort list;
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    77
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    78
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    79
(* print sorts and arities *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    80
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    81
fun str_of_sort [c] = c
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    82
  | str_of_sort cs = parents "{" "}" (commas cs);
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    83
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    84
fun str_of_dom dom = parents "(" ")" (commas (map str_of_sort dom));
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    85
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    86
fun str_of_arity (t, [], S) = t ^ " :: " ^ str_of_sort S
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    87
  | str_of_arity (t, SS, S) =
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    88
      t ^ " :: " ^ str_of_dom SS ^ " " ^ str_of_sort S;
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    89
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    90
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    91
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    92
(*** type signatures ***)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    93
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    94
(*
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    95
  classes:
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    96
    a list of all declared classes;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    97
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
    98
  subclass:
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
    99
    an association list representing the subclass relation; (c, cs) is
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   100
    interpreted as "c is a proper subclass of all elemenst of cs"; note that
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   101
    c itself is not a memeber of cs;
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   102
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   103
  default:
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   104
    the default sort attached to all unconstrained type vars;
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   105
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   106
  args:
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   107
    an association list of all declared types with the number of their
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   108
    arguments;
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   109
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   110
  abbrs:
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   111
    an association list of type abbreviations;
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   112
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   113
  coreg:
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   114
    a two-fold association list of all type arities; (t, al) means that type
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   115
    constructor t has the arities in al; an element (c, ss) of al represents
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   116
    the arity (ss)c;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   117
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   118
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   119
datatype type_sig =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   120
  TySg of {
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   121
    classes: class list,
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   122
    subclass: (class * class list) list,
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   123
    default: sort,
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   124
    args: (string * int) list,
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   125
    abbrs: (string * (indexname list * typ)) list,
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   126
    coreg: (string * (class * domain) list) list};
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   127
189
831a9a7ab9f3 added rep_tsig
nipkow
parents: 162
diff changeset
   128
fun rep_tsig (TySg comps) = comps;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   129
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   130
fun defaultS (TySg {default, ...}) = default;
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   131
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   132
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   133
(* error messages *)    (* FIXME move? *)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   134
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   135
fun undcl_class c = "Undeclared class " ^ quote c;
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   136
val err_undcl_class = error o undcl_class;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   137
422
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   138
fun err_dup_classes cs =
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   139
  error ("Duplicate declaration of class(es) " ^ commas_quote cs);
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   140
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   141
fun undcl_type c = "Undeclared type constructor " ^ quote c;
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   142
val err_undcl_type = error o undcl_type;
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   143
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   144
fun err_dup_tycon c =
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   145
  error ("Duplicate declaration of type constructor " ^ quote c);
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   146
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   147
fun err_neg_args c =
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   148
  error ("Negative number of arguments of type constructor " ^ quote c);
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   149
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   150
fun err_dup_tyabbr c =
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   151
  error ("Duplicate declaration of type abbreviation " ^ quote c);
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   152
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   153
fun ty_confl c = "Conflicting type constructor and abbreviation " ^ quote c;
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   154
val err_ty_confl = error o ty_confl;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   155
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   156
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   157
(* 'leq' checks the partial order on classes according to the
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   158
   statements in the association list 'a' (i.e.'subclass')
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   159
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   160
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   161
fun less a (C, D) = case assoc (a, C) of
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   162
     Some(ss) => D mem ss
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   163
   | None => err_undcl_class (C) ;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   164
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   165
fun leq a (C, D)  =  C = D orelse less a (C, D);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   166
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   167
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   168
(* logical_types *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   169
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   170
(*return all logical types of tsig, i.e. all types t with some arity t::(ss)c
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   171
  and c <= logic*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   172
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   173
fun logical_types tsig =
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   174
  let
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   175
    val TySg {subclass, coreg, args, ...} = tsig;
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   176
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   177
    fun log_class c = leq subclass (c, logicC);
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   178
    fun log_type t = exists (log_class o #1) (assocs coreg t);
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   179
  in
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   180
    filter log_type (map #1 args)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   181
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   182
162
58d54dc482d1 added logical_types
nipkow
parents: 0
diff changeset
   183
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   184
(* 'sortorder' checks the ordering on sets of classes, i.e. on sorts:
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   185
   S1 <= S2 , iff for every class C2 in S2 there exists a class C1 in S1
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   186
   with C1 <= C2 (according to an association list 'a')
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   187
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   188
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   189
fun sortorder a (S1, S2) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   190
  forall  (fn C2 => exists  (fn C1 => leq a (C1, C2))  S1)  S2;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   191
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   192
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   193
(* 'inj' inserts a new class C into a given class set S (i.e.sort) only if
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   194
  there exists no class in S which is <= C;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   195
  the resulting set is minimal if S was minimal
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   196
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   197
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   198
fun inj a (C, S) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   199
  let fun inj1 [] = [C]
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   200
        | inj1 (D::T) = if leq a (D, C) then D::T
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   201
                        else if leq a (C, D) then inj1 T
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   202
                             else D::(inj1 T)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   203
  in inj1 S end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   204
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   205
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   206
(* 'union_sort' forms the minimal union set of two sorts S1 and S2
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   207
   under the assumption that S2 is minimal *)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   208
(* FIXME rename to inter_sort (?) *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   209
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   210
fun union_sort a = foldr (inj a);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   211
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   212
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   213
(* 'elementwise_union' forms elementwise the minimal union set of two
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   214
   sort lists under the assumption that the two lists have the same length
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   215
*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   216
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   217
fun elementwise_union a (Ss1, Ss2) = map (union_sort a) (Ss1~~Ss2);
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   218
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   219
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   220
(* 'lew' checks for two sort lists the ordering for all corresponding list
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   221
   elements (i.e. sorts) *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   222
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   223
fun lew a (w1, w2) = forall (sortorder a)  (w1~~w2);
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   224
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   225
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   226
(* 'is_min' checks if a class C is minimal in a given sort S under the
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   227
   assumption that S contains C *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   228
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   229
fun is_min a S C = not (exists (fn (D) => less a (D, C)) S);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   230
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   231
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   232
(* 'min_sort' reduces a sort to its minimal classes *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   233
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   234
fun min_sort a S = distinct(filter (is_min a S) S);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   235
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   236
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   237
(* 'min_domain' minimizes the domain sorts of type declarationsl;
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   238
   the function will be applied on the type declarations in extensions *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   239
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   240
fun min_domain subclass =
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   241
  let fun one_min (f, (doms, ran)) = (f, (map (min_sort subclass) doms, ran))
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   242
  in map one_min end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   243
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   244
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   245
(* 'min_filter' filters a list 'ars' consisting of arities (domain * class)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   246
   and gives back a list of those range classes whose domains meet the
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   247
   predicate 'pred' *)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   248
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   249
fun min_filter a pred ars =
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   250
  let fun filt ([], l) = l
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   251
        | filt ((c, x)::xs, l) = if pred(x) then filt (xs, inj a (c, l))
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   252
                               else filt (xs, l)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   253
  in filt (ars, []) end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   254
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   255
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   256
(* 'cod_above' filters all arities whose domains are elementwise >= than
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   257
   a given domain 'w' and gives back a list of the corresponding range
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   258
   classes *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   259
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   260
fun cod_above (a, w, ars) = min_filter a (fn w' => lew a (w, w')) ars;
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   261
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   262
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   263
200
39a931cc6558 Necessary changes to accomodate type abbreviations.
nipkow
parents: 189
diff changeset
   264
(*Instantiation of type variables in types*)
39a931cc6558 Necessary changes to accomodate type abbreviations.
nipkow
parents: 189
diff changeset
   265
(*Pre: instantiations obey restrictions! *)
39a931cc6558 Necessary changes to accomodate type abbreviations.
nipkow
parents: 189
diff changeset
   266
fun inst_typ tye =
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   267
  let fun inst(Type(a, Ts)) = Type(a, map inst Ts)
200
39a931cc6558 Necessary changes to accomodate type abbreviations.
nipkow
parents: 189
diff changeset
   268
        | inst(T as TFree _) = T
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   269
        | inst(T as TVar(v, _)) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   270
            (case assoc(tye, v) of Some U => inst U | None => T)
200
39a931cc6558 Necessary changes to accomodate type abbreviations.
nipkow
parents: 189
diff changeset
   271
  in inst end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   272
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   273
(* 'least_sort' returns for a given type its maximum sort:
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   274
   - type variables, free types: the sort brought with
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   275
   - type constructors: recursive determination of the maximum sort of the
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   276
                    arguments if the type is declared in 'coreg' of the
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   277
                    given type signature  *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   278
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   279
fun least_sort (tsig as TySg{subclass, coreg, ...}) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   280
  let fun ls(T as Type(a, Ts)) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   281
                 (case assoc (coreg, a) of
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   282
                          Some(ars) => cod_above(subclass, map ls Ts, ars)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   283
                        | None => raise TYPE(undcl_type a, [T], []))
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   284
        | ls(TFree(a, S)) = S
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   285
        | ls(TVar(a, S)) = S
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   286
  in ls end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   287
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   288
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   289
fun check_has_sort(tsig as TySg{subclass, coreg, ...}, T, S) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   290
  if sortorder subclass ((least_sort tsig T), S) then ()
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   291
  else raise TYPE("Type not of sort " ^ (str_of_sort S), [T], [])
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   292
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   293
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   294
(*Instantiation of type variables in types *)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   295
fun inst_typ_tvars(tsig, tye) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   296
  let fun inst(Type(a, Ts)) = Type(a, map inst Ts)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   297
        | inst(T as TFree _) = T
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   298
        | inst(T as TVar(v, S)) = (case assoc(tye, v) of
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   299
                None => T | Some(U) => (check_has_sort(tsig, U, S); U))
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   300
  in inst end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   301
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   302
(*Instantiation of type variables in terms *)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   303
fun inst_term_tvars(tsig, tye) = map_term_types (inst_typ_tvars(tsig, tye));
200
39a931cc6558 Necessary changes to accomodate type abbreviations.
nipkow
parents: 189
diff changeset
   304
39a931cc6558 Necessary changes to accomodate type abbreviations.
nipkow
parents: 189
diff changeset
   305
39a931cc6558 Necessary changes to accomodate type abbreviations.
nipkow
parents: 189
diff changeset
   306
(* expand_typ *)
39a931cc6558 Necessary changes to accomodate type abbreviations.
nipkow
parents: 189
diff changeset
   307
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   308
fun expand_typ (TySg {abbrs, ...}) ty =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   309
  let
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   310
    fun exptyp (Type (a, Ts)) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   311
          (case assoc (abbrs, a) of
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   312
            Some (vs, U) => exptyp (inst_typ (vs ~~ Ts) U)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   313
          | None => Type (a, map exptyp Ts))
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   314
      | exptyp T = T
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   315
  in
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   316
    exptyp ty
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   317
  end;
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   318
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   319
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   320
(* norm_typ *)      (* FIXME norm sorts *)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   321
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   322
val norm_typ = expand_typ;
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   323
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   324
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   325
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   326
(** type matching **)
200
39a931cc6558 Necessary changes to accomodate type abbreviations.
nipkow
parents: 189
diff changeset
   327
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   328
exception TYPE_MATCH;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   329
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   330
(*typ_match (s, (U, T)) = s' <==> s'(U) = T and s' is an extension of s*)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   331
fun typ_match tsig =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   332
  let
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   333
    fun match (subs, (TVar (v, S), T)) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   334
          (case assoc (subs, v) of
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   335
            None => ((v, (check_has_sort (tsig, T, S); T)) :: subs
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   336
              handle TYPE _ => raise TYPE_MATCH)
422
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   337
          | Some U => if U = T then subs else raise TYPE_MATCH)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   338
      | match (subs, (Type (a, Ts), Type (b, Us))) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   339
          if a <> b then raise TYPE_MATCH
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   340
          else foldl match (subs, Ts ~~ Us)
422
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   341
      | match (subs, (TFree x, TFree y)) =
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   342
          if x = y then subs else raise TYPE_MATCH
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   343
      | match _ = raise TYPE_MATCH;
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   344
  in match end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   345
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   346
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   347
fun typ_instance (tsig, T, U) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   348
  (typ_match tsig ([], (U, T)); true) handle TYPE_MATCH => false;
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   349
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   350
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   351
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   352
(** build type signatures **)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   353
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   354
fun make_tsig (classes, subclass, default, args, abbrs, coreg) =
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   355
  TySg {classes = classes, subclass = subclass, default = default,
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   356
    args = args, abbrs = abbrs, coreg = coreg};
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   357
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   358
val tsig0 = make_tsig ([], [], [], [], [], []);
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   359
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   360
401
324ad2e02826 replaced "logic" by logicC;
wenzelm
parents: 256
diff changeset
   361
(* sorts *)
324ad2e02826 replaced "logic" by logicC;
wenzelm
parents: 256
diff changeset
   362
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   363
fun subsort (TySg {subclass, ...}) (S1, S2) =
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   364
  sortorder subclass (S1, S2);
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   365
401
324ad2e02826 replaced "logic" by logicC;
wenzelm
parents: 256
diff changeset
   366
fun norm_sort (TySg {subclass, ...}) S =
324ad2e02826 replaced "logic" by logicC;
wenzelm
parents: 256
diff changeset
   367
  sort_strings (min_sort subclass S);
324ad2e02826 replaced "logic" by logicC;
wenzelm
parents: 256
diff changeset
   368
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   369
fun rem_sorts (Type (a, tys)) = Type (a, map rem_sorts tys)
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   370
  | rem_sorts (TFree (x, _)) = TFree (x, [])
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   371
  | rem_sorts (TVar (xi, _)) = TVar (xi, []);
401
324ad2e02826 replaced "logic" by logicC;
wenzelm
parents: 256
diff changeset
   372
324ad2e02826 replaced "logic" by logicC;
wenzelm
parents: 256
diff changeset
   373
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   374
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   375
fun twice(a) = error("Type constructor " ^a^ " has already been declared.");
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   376
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   377
fun tyab_conflict(a) = error("Can't declare type "^(quote a)^"!\nAn abbreviation with this name exists already.");
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   378
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   379
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   380
(* typ_errors *)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   381
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   382
(*check validity of (not necessarily normal) type; accumulate error messages*)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   383
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   384
fun typ_errors tsig (typ, errors) =
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   385
  let
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   386
    val TySg {classes, args, abbrs, ...} = tsig;
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   387
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   388
    fun class_err (errs, c) =
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   389
      if c mem classes then errs
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   390
      else undcl_class c ins errs;
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   391
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   392
    val sort_err = foldl class_err;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   393
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   394
    fun typ_errs (Type (c, Us), errs) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   395
          let
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   396
            val errs' = foldr typ_errs (Us, errs);
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   397
            fun nargs n =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   398
              if n = length Us then errs'
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   399
              else ("Wrong number of arguments: " ^ quote c) ins errs';
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   400
          in
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   401
            (case assoc (args, c) of
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   402
              Some n => nargs n
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   403
            | None =>
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   404
                (case assoc (abbrs, c) of
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   405
                  Some (vs, _) => nargs (length vs)
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   406
                | None => undcl_type c ins errs))
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   407
          end
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   408
    | typ_errs (TFree (_, S), errs) = sort_err (errs, S)
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   409
    | typ_errs (TVar ((x, i), S), errs) =
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   410
        if i < 0 then
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   411
          ("Negative index for TVar " ^ quote x) ins sort_err (errs, S)
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   412
        else sort_err (errs, S);
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   413
  in
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   414
    typ_errs (typ, errors)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   415
  end;
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   416
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   417
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   418
(* cert_typ *)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   419
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   420
(*check and normalize typ wrt. tsig; errors are indicated by exception TYPE*)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   421
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   422
fun cert_typ tsig ty =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   423
  (case typ_errors tsig (ty, []) of
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   424
    [] => norm_typ tsig ty
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   425
  | errs => raise_type (cat_lines errs) [ty] []);
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   426
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   427
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   428
422
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   429
(** merge type signatures **)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   430
422
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   431
(*'assoc_union' merges two association lists if the contents associated
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   432
  the keys are lists*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   433
422
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   434
fun assoc_union (as1, []) = as1
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   435
  | assoc_union (as1, (key, l2) :: as2) =
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   436
      (case assoc (as1, key) of
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   437
        Some l1 => assoc_union (overwrite (as1, (key, l1 union l2)), as2)
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   438
      | None => assoc_union ((key, l2) :: as1, as2));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   439
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   440
422
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   441
(* merge subclass *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   442
422
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   443
fun merge_subclass (subclass1, subclass2) =
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   444
  let val subclass = transitive_closure (assoc_union (subclass1, subclass2)) in
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   445
    if exists (op mem) subclass then
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   446
      error ("Cyclic class structure!")   (* FIXME improve msg, raise TERM *)
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   447
    else subclass
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   448
  end;
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   449
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   450
422
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   451
(* coregularity *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   452
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   453
(* 'is_unique_decl' checks if there exists just one declaration t:(Ss)C *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   454
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   455
fun is_unique_decl coreg (t, (s, w)) = case assoc2 (coreg, (t, s)) of
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   456
      Some(w1) => if w = w1 then () else
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   457
        error("There are two declarations\n" ^
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   458
              str_of_arity(t, w, [s]) ^ " and\n" ^
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   459
              str_of_arity(t, w1, [s]) ^ "\n" ^
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   460
              "with the same result class.")
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   461
    | None => ();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   462
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   463
(* 'restr2' checks if there are two declarations t:(Ss1)C1 and t:(Ss2)C2
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   464
   such that C1 >= C2 then Ss1 >= Ss2 (elementwise) *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   465
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   466
fun subs (classes, subclass) C =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   467
  let fun sub (rl, l) = if leq subclass (l, C) then l::rl else rl
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   468
  in foldl sub ([], classes) end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   469
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   470
fun coreg_err(t, (w1, C), (w2, D)) =
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   471
    error("Declarations " ^ str_of_arity(t, w1, [C]) ^ " and "
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   472
                          ^ str_of_arity(t, w2, [D]) ^ " are in conflict");
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   473
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   474
fun restr2 classes (subclass, coreg) (t, (s, w)) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   475
  let fun restr ([], test) = ()
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   476
        | restr (s1::Ss, test) =
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   477
            (case assoc2 (coreg, (t, s1)) of
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   478
              Some dom =>
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   479
                if lew subclass (test (w, dom))
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   480
                then restr (Ss, test)
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   481
                else coreg_err (t, (w, s), (dom, s1))
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   482
            | None => restr (Ss, test))
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   483
      fun forward (t, (s, w)) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   484
        let val s_sups = case assoc (subclass, s) of
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   485
                   Some(s_sups) => s_sups | None => err_undcl_class(s);
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   486
        in restr (s_sups, I) end
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   487
      fun backward (t, (s, w)) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   488
        let val s_subs = subs (classes, subclass) s
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   489
        in restr (s_subs, fn (x, y) => (y, x)) end
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   490
  in (backward (t, (s, w)); forward (t, (s, w))) end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   491
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   492
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   493
fun varying_decls t =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   494
  error ("Type constructor " ^ quote t ^ " has varying number of arguments");
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   495
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   496
422
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   497
(* 'merge_coreg' builds the union of two 'coreg' lists;
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   498
   it only checks the two restriction conditions and inserts afterwards
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   499
   all elements of the second list into the first one *)
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   500
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   501
fun merge_coreg classes subclass1 =
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   502
  let fun test_ar classes (t, ars1) (coreg1, (s, w)) =
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   503
        (is_unique_decl coreg1 (t, (s, w));
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   504
         restr2 classes (subclass1, coreg1) (t, (s, w));
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   505
         overwrite (coreg1, (t, (s, w) ins ars1)));
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   506
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   507
      fun merge_c (coreg1, (c as (t, ars2))) = case assoc (coreg1, t) of
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   508
          Some(ars1) => foldl (test_ar classes (t, ars1)) (coreg1, ars2)
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   509
        | None => c::coreg1
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   510
  in foldl merge_c end;
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   511
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   512
fun merge_args (args, (t, n)) =
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   513
  (case assoc (args, t) of
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   514
    Some m => if m = n then args else varying_decls t
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   515
  | None => (t, n) :: args);
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   516
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   517
(* FIXME raise TERM *)
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   518
fun merge_abbrs (abbrs1, abbrs2) =
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   519
  let
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   520
    val abbrs = abbrs1 union abbrs2;
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   521
    val names = map fst abbrs;
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   522
  in
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   523
    (case duplicates names of
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   524
      [] => abbrs
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   525
    | dups => error ("Duplicate declaration of type abbreviations: " ^
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   526
        commas_quote dups))
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   527
  end;
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   528
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   529
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   530
(* 'merge_tsigs' takes the above declared functions to merge two type
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   531
  signatures *)
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   532
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   533
fun merge_tsigs(TySg{classes=classes1, default=default1, subclass=subclass1, args=args1,
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   534
           coreg=coreg1, abbrs=abbrs1},
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   535
          TySg{classes=classes2, default=default2, subclass=subclass2, args=args2,
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   536
           coreg=coreg2, abbrs=abbrs2}) =
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   537
  let val classes' = classes1 union classes2;
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   538
      val subclass' = merge_subclass (subclass1, subclass2);
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   539
      val args' = foldl merge_args (args1, args2)
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   540
      val coreg' = merge_coreg classes' subclass' (coreg1, coreg2);
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   541
      val default' = min_sort subclass' (default1 @ default2);
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   542
      val abbrs' = merge_abbrs(abbrs1, abbrs2);
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   543
  in TySg{classes=classes' , default=default', subclass=subclass', args=args',
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   544
          coreg=coreg' , abbrs = abbrs' }
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   545
  end;
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   546
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   547
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   548
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   549
(*** extend type signatures ***)
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   550
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   551
(** add classes **)
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   552
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   553
(* FIXME use? *)
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   554
fun add_classes classes cs =
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   555
  (case cs inter classes of
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   556
    [] => cs @ classes
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   557
  | dups => err_dup_classes cs);
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   558
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   559
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   560
(* 'add_class' adds a new class to the list of all existing classes *)
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   561
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   562
fun add_class (classes, (s, _)) =
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   563
  if s mem classes then error("Class " ^ s ^ " declared twice.")
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   564
  else s :: classes;
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   565
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   566
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   567
(*'add_subclass' adds a tuple consisting of a new class (the new class has
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   568
  already been inserted into the 'classes' list) and its superclasses (they
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   569
  must be declared in 'classes' too) to the 'subclass' list of the given type
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   570
  signature; furthermore all inherited superclasses according to the
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   571
  superclasses brought with are inserted and there is a check that there are
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   572
  no cycles (i.e. C <= D <= C, with C <> D);*)
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   573
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   574
fun add_subclass classes (subclass, (s, ges)) =
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   575
let fun upd (subclass, s') = if s' mem classes then
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   576
        let val ges' = the (assoc (subclass, s))
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   577
        in case assoc (subclass, s') of
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   578
             Some sups => if s mem sups
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   579
                           then error(" Cycle :" ^ s^" <= "^ s'^" <= "^ s )
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   580
                           else overwrite (subclass, (s, sups union ges'))
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   581
           | None => subclass
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   582
         end
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   583
         else err_undcl_class(s')
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   584
in foldl upd (subclass@[(s, ges)], ges) end;
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   585
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   586
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   587
(* 'extend_classes' inserts all new classes into the corresponding
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   588
   lists ('classes', 'subclass') if possible *)
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   589
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   590
fun extend_classes (classes, subclass, newclasses) =
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   591
  if newclasses = [] then (classes, subclass) else
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   592
  let val classes' = foldl add_class (classes, newclasses);
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   593
      val subclass' = foldl (add_subclass classes') (subclass, newclasses);
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   594
  in (classes', subclass') end;
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   595
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   596
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   597
(* ext_tsig_subclass *)
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   598
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   599
fun ext_tsig_subclass tsig pairs =
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   600
  let
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   601
    val TySg {classes, subclass, default, args, abbrs, coreg} = tsig;
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   602
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   603
    (* FIXME clean! *)
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   604
    val subclass' =
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   605
      merge_subclass (subclass, map (fn (c1, c2) => (c1, [c2])) pairs);
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   606
  in
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   607
    make_tsig (classes, subclass', default, args, abbrs, coreg)
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   608
  end;
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   609
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   610
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   611
(* ext_tsig_defsort *)
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   612
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   613
fun ext_tsig_defsort (TySg {classes, subclass, args, abbrs, coreg, ...}) default =
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   614
  make_tsig (classes, subclass, default, args, abbrs, coreg);
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   615
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   616
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   617
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   618
(** add arities **)
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   619
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   620
(* 'coregular' checks
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   621
   - the two restriction conditions 'is_unique_decl' and 'restr2'
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   622
   - if the classes in the new type declarations are known in the
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   623
     given type signature
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   624
   - if one type constructor has always the same number of arguments;
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   625
   if one type declaration has passed all checks it is inserted into
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   626
   the 'coreg' association list of the given type signatrure  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   627
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   628
fun coregular (classes, subclass, args) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   629
  let fun ex C = if C mem classes then () else err_undcl_class(C);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   630
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   631
      fun addar(w, C) (coreg, t) = case assoc(args, t) of
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   632
            Some(n) => if n <> length w then varying_decls(t) else
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   633
                     (is_unique_decl coreg (t, (C, w));
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   634
                      (seq o seq) ex w;
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   635
                      restr2 classes (subclass, coreg) (t, (C, w));
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   636
                      let val ars = the (assoc(coreg, t))
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   637
                      in overwrite(coreg, (t, (C, w) ins ars)) end)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   638
          | None => err_undcl_type(t);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   639
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   640
      fun addts(coreg, (ts, ar)) = foldl (addar ar) (coreg, ts)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   641
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   642
  in addts end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   643
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   644
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   645
(* 'close' extends the 'coreg' association list after all new type
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   646
   declarations have been inserted successfully:
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   647
   for every declaration t:(Ss)C , for all classses D with C <= D:
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   648
      if there is no declaration t:(Ss')C' with C < C' and C' <= D
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   649
      then insert the declaration t:(Ss)D into 'coreg'
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   650
   this means, if there exists a declaration t:(Ss)C and there is
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   651
   no declaration t:(Ss')D with C <=D then the declaration holds
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   652
   for all range classes more general than C *)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   653
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   654
fun close (coreg, subclass) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   655
  let fun check sl (l, (s, dom)) = case assoc (subclass, s) of
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   656
          Some(sups) =>
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   657
            let fun close_sup (l, sup) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   658
                  if exists (fn s'' => less subclass (s, s'') andalso
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   659
                                       leq subclass (s'', sup)) sl
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   660
                  then l
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   661
                  else (sup, dom)::l
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   662
            in foldl close_sup (l, sups) end
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   663
        | None => l;
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   664
      fun ext (s, l) = (s, foldl (check (map #1 l)) (l, l));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   665
  in map ext coreg end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   666
422
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   667
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   668
(** add types **)
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   669
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   670
fun add_types (aca, (ts, n)) =
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   671
  let
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   672
    fun add_type ((args, coreg, abbrs), t) =
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   673
      case assoc(args, t) of              (* FIXME from new *)
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   674
        Some _ => twice(t)
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   675
      | None =>
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   676
          (case assoc(abbrs, t) of
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   677
            Some _ => tyab_conflict(t)
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   678
          | None => ((t, n)::args, (t, [])::coreg, abbrs))
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   679
  in
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   680
    if n < 0 then     (* FIXME err_neg_args *)
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   681
      error ("Type constructor cannot have negative number of arguments")
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   682
    else foldl add_type (aca, ts)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   683
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   684
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   685
422
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   686
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   687
(** add type abbreviations **)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   688
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   689
fun abbr_errors tsig (a, (lhs_vs, rhs)) =
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   690
  let
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   691
    val TySg {args, abbrs, ...} = tsig;
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   692
    val rhs_vs = map #1 (typ_tvars rhs);
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   693
    val show_idxs = commas_quote o map fst;
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   694
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   695
    val dup_lhs_vars =
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   696
      (case duplicates lhs_vs of
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   697
        [] => []
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   698
      | vs => ["Duplicate variables on lhs: " ^ show_idxs vs]);
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   699
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   700
    val extra_rhs_vars =
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   701
      (case gen_rems (op =) (rhs_vs, lhs_vs) of
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   702
        [] => []
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   703
      | vs => ["Extra variables on rhs: " ^ show_idxs vs]);
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   704
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   705
    val tycon_confl =
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   706
      if is_none (assoc (args, a)) then []
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   707
      else [ty_confl a];
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   708
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   709
    val dup_abbr =
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   710
      if is_none (assoc (abbrs, a)) then []
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   711
      else ["Duplicate declaration of abbreviation"];
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   712
  in
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   713
    dup_lhs_vars @ extra_rhs_vars @ tycon_confl @ dup_abbr @
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   714
      typ_errors tsig (rhs, [])
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   715
  end;
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   716
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   717
fun add_abbr (tsig, abbr as (a, _)) =
422
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   718
  let val TySg {classes, subclass, default, args, coreg, abbrs} = tsig in
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   719
    (case abbr_errors tsig abbr of
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   720
      [] => make_tsig (classes, subclass, default, args, abbr :: abbrs, coreg)
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   721
    | errs => (seq writeln errs;
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   722
        error ("The error(s) above occurred in type abbreviation " ^ quote a)))
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   723
  end;
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   724
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   725
fun ext_tsig_abbrs tsig abbrs = foldl add_abbr (tsig, abbrs);
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   726
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   727
200
39a931cc6558 Necessary changes to accomodate type abbreviations.
nipkow
parents: 189
diff changeset
   728
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   729
(* 'extend_tsig' takes the above described check- and extend-functions to
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   730
   extend a given type signature with new classes and new type declarations *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   731
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   732
fun extend_tsig (TySg{classes, default, subclass, args, coreg, abbrs})
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   733
            (newclasses, newdefault, types, arities) =
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   734
  let
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   735
    val (classes', subclass') = extend_classes(classes, subclass, newclasses);
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   736
    val (args', coreg', _) = foldl add_types ((args, coreg, abbrs), types);
422
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   737
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   738
    val old_coreg = map #1 coreg;
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   739
    val coreg'' =
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   740
      foldl (coregular (classes', subclass', args'))
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   741
        (coreg', min_domain subclass' arities);
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   742
    val coreg''' = close (coreg'', subclass');
422
8f194014a9c8 added ext_tsig_subclass, ext_tsig_defsort;
wenzelm
parents: 416
diff changeset
   743
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   744
    val default' = if null newdefault then default else newdefault;
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   745
  in
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   746
    TySg {classes = classes', subclass = subclass', default = default',
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   747
      args = args', coreg = coreg''', abbrs = abbrs}
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   748
  end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   749
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   750
416
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   751
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   752
12f9f36e4484 restored functor sig;
wenzelm
parents: 401
diff changeset
   753
(*** type unification and inference ***)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   754
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   755
(*
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   756
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   757
Input:
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   758
- a 'raw' term which contains only dummy types and some explicit type
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   759
  constraints encoded as terms.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   760
- the expected type of the term.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   761
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   762
Output:
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   763
- the correctly typed term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   764
- the substitution needed to unify the actual type of the term with its
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   765
  expected type; only the TVars in the expected type are included.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   766
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   767
During type inference all TVars in the term have negative index. This keeps
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   768
them apart from normal TVars, which is essential, because at the end the type
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   769
of the term is unified with the expected type, which contains normal TVars.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   770
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   771
1. Add initial type information to the term (add_types).
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   772
   This freezes (freeze_vars) TVars in explicitly provided types (eg
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   773
   constraints or defaults) by turning them into TFrees.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   774
2. Carry out type inference, possibly introducing new negative TVars.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   775
3. Unify actual and expected type.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   776
4. Turn all (negative) TVars into unique new TFrees (freeze).
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   777
5. Thaw all TVars frozen in step 1 (thaw_vars).
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   778
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   779
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   780
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   781
(*Raised if types are not unifiable*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   782
exception TUNIFY;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   783
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   784
val tyvar_count = ref(~1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   785
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   786
fun tyinit() = (tyvar_count := ~1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   787
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   788
fun new_tvar_inx() = (tyvar_count := !tyvar_count-1; !tyvar_count)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   789
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   790
(*
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   791
Generate new TVar.  Index is < ~1 to distinguish it from TVars generated from
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   792
variable names (see id_type).  Name is arbitrary because index is new.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   793
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   794
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   795
fun gen_tyvar(S) = TVar(("'a", new_tvar_inx()), S);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   796
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   797
(*Occurs check: type variable occurs in type?*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   798
fun occ v tye =
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   799
  let fun occ(Type(_, Ts)) = exists occ Ts
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   800
        | occ(TFree _) = false
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   801
        | occ(TVar(w, _)) = v=w orelse
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   802
                           (case assoc(tye, w) of
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   803
                              None   => false
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   804
                            | Some U => occ U);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   805
  in occ end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   806
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   807
(*Chase variable assignments in tye.
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   808
  If devar (T, tye) returns a type var then it must be unassigned.*)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   809
fun devar (T as TVar(v, _), tye) = (case  assoc(tye, v)  of
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   810
          Some U =>  devar (U, tye)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   811
        | None   =>  T)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   812
  | devar (T, tye) = T;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   813
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   814
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   815
(* 'dom' returns for a type constructor t the list of those domains
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   816
   which deliver a given range class C *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   817
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   818
fun dom coreg t C = case assoc2 (coreg, (t, C)) of
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   819
    Some(Ss) => Ss
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   820
  | None => raise TUNIFY;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   821
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   822
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   823
(* 'Dom' returns the union of all domain lists of 'dom' for a given sort S
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   824
   (i.e. a set of range classes ); the union is carried out elementwise
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   825
   for the seperate sorts in the domains *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   826
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   827
fun Dom (subclass, coreg) (t, S) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   828
  let val domlist = map (dom coreg t) S;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   829
  in if null domlist then []
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   830
     else foldl (elementwise_union subclass) (hd domlist, tl domlist)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   831
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   832
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   833
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   834
fun W ((T, S), tsig as TySg{subclass, coreg, ...}, tye) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   835
  let fun Wd ((T, S), tye) = W ((devar (T, tye), S), tsig, tye)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   836
      fun Wk(T as TVar(v, S')) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   837
              if sortorder subclass (S', S) then tye
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   838
              else (v, gen_tyvar(union_sort subclass (S', S)))::tye
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   839
        | Wk(T as TFree(v, S')) = if sortorder subclass (S', S) then tye
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   840
                                 else raise TUNIFY
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   841
        | Wk(T as Type(f, Ts)) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   842
           if null S then tye
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   843
           else foldr Wd (Ts~~(Dom (subclass, coreg) (f, S)) , tye)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   844
  in Wk(T) end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   845
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   846
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   847
(* Order-sorted Unification of Types (U)  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   848
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   849
(* Precondition: both types are well-formed w.r.t. type constructor arities *)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   850
fun unify (tsig as TySg{subclass, coreg, ...}) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   851
  let fun unif ((T, U), tye) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   852
        case (devar(T, tye), devar(U, tye)) of
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   853
          (T as TVar(v, S1), U as TVar(w, S2)) =>
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   854
             if v=w then tye else
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   855
             if sortorder subclass (S1, S2) then (w, T)::tye else
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   856
             if sortorder subclass (S2, S1) then (v, U)::tye
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   857
             else let val nu = gen_tyvar (union_sort subclass (S1, S2))
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   858
                  in (v, nu)::(w, nu)::tye end
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   859
        | (T as TVar(v, S), U) =>
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   860
             if occ v tye U then raise TUNIFY else W ((U, S), tsig, (v, U)::tye)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   861
        | (U, T as TVar (v, S)) =>
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   862
             if occ v tye U then raise TUNIFY else W ((U, S), tsig, (v, U)::tye)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   863
        | (Type(a, Ts), Type(b, Us)) =>
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   864
             if a<>b then raise TUNIFY else foldr unif (Ts~~Us, tye)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   865
        | (T, U) => if T=U then tye else raise TUNIFY
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   866
  in unif end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   867
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   868
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   869
(*Type inference for polymorphic term*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   870
fun infer tsig =
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   871
  let fun inf(Ts, Const (_, T), tye) = (T, tye)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   872
        | inf(Ts, Free  (_, T), tye) = (T, tye)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   873
        | inf(Ts, Bound i, tye) = ((nth_elem(i, Ts) , tye)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   874
          handle LIST _=> raise TYPE ("loose bound variable", [], [Bound i]))
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   875
        | inf(Ts, Var (_, T), tye) = (T, tye)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   876
        | inf(Ts, Abs (_, T, body), tye) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   877
            let val (U, tye') = inf(T::Ts, body, tye) in  (T-->U, tye')  end
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   878
        | inf(Ts, f$u, tye) =
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   879
            let val (U, tyeU) = inf(Ts, u, tye);
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   880
                val (T, tyeT) = inf(Ts, f, tyeU);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   881
                fun err s =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   882
                  raise TYPE(s, [inst_typ tyeT T, inst_typ tyeT U], [f$u])
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   883
            in case T of
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   884
                 Type("fun", [T1, T2]) =>
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   885
                   ( (T2, unify tsig ((T1, U), tyeT))
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   886
                     handle TUNIFY => err"type mismatch in application" )
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   887
               | TVar _ =>
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   888
                   let val T2 = gen_tyvar([])
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   889
                   in (T2, unify tsig ((T, U-->T2), tyeT))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   890
                      handle TUNIFY => err"type mismatch in application"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   891
                   end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   892
               | _ => err"rator must have function type"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   893
           end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   894
  in inf end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   895
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   896
fun freeze_vars(Type(a, Ts)) = Type(a, map freeze_vars Ts)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   897
  | freeze_vars(T as TFree _) = T
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   898
  | freeze_vars(TVar(v, S)) = TFree(Syntax.string_of_vname v, S);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   899
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   900
(* Attach a type to a constant *)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   901
fun type_const (a, T) = Const(a, incr_tvar (new_tvar_inx()) T);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   902
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   903
(*Find type of ident.  If not in table then use ident's name for tyvar
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   904
  to get consistent typing.*)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   905
fun new_id_type a = TVar(("'"^a, new_tvar_inx()), []);
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   906
fun type_of_ixn(types, ixn as (a, _)) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   907
        case types ixn of Some T => freeze_vars T | None => TVar(("'"^a, ~1), []);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   908
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   909
fun constrain(term, T) = Const(Syntax.constrainC, T-->T) $ term;
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   910
fun constrainAbs(Abs(a, _, body), T) = Abs(a, T, body);
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   911
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   912
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   913
(*
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   914
  Attach types to a term. Input is a "parse tree" containing dummy types.
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   915
  Type constraints are translated and checked for validity wrt tsig. TVars in
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   916
  constraints are frozen.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   917
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   918
  The atoms in the resulting term satisfy the following spec:
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   919
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   920
  Const (a, T):
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   921
    T is a renamed copy of the generic type of a; renaming decreases index of
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   922
    all TVars by new_tvar_inx(), which is less than ~1. The index of all
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   923
    TVars in the generic type must be 0 for this to work!
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   924
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   925
  Free (a, T), Var (ixn, T):
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   926
    T is either the frozen default type of a or TVar (("'"^a, ~1), [])
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   927
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   928
  Abs (a, T, _):
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   929
    T is either a type constraint or TVar (("'" ^ a, i), []), where i is
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   930
    generated by new_tvar_inx(). Thus different abstractions can have the
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   931
    bound variables of the same name but different types.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   932
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   933
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   934
(* FIXME replace const_tab by (const_typ: string -> typ option) (?) *)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   935
(* FIXME improve handling of sort constraints *)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   936
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   937
fun add_types (tsig, const_tab, types, sorts) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   938
  let
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   939
    val S0 = defaultS tsig;
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   940
    fun defS0 ixn = if_none (sorts ixn) S0;
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   941
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   942
    fun prepareT typ =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   943
      freeze_vars (cert_typ tsig (Syntax.typ_of_term defS0 typ));
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   944
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   945
    fun add (Const (a, _)) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   946
          (case Symtab.lookup (const_tab, a) of
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   947
            Some T => type_const (a, T)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   948
          | None => raise_type ("No such constant: " ^ quote a) [] [])
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   949
      | add (Bound i) = Bound i
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   950
      | add (Free (a, _)) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   951
          (case Symtab.lookup (const_tab, a) of
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   952
            Some T => type_const (a, T)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   953
          | None => Free (a, type_of_ixn (types, (a, ~1))))
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   954
      | add (Var (ixn, _)) = Var (ixn, type_of_ixn (types, ixn))
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   955
      | add (Abs (a, _, body)) = Abs (a, new_id_type a, add body)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   956
      | add ((f as Const (a, _) $ t1) $ t2) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   957
          if a = Syntax.constrainC then
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   958
            constrain (add t1, prepareT t2)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   959
          else if a = Syntax.constrainAbsC then
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   960
            constrainAbs (add t1, prepareT t2)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   961
          else add f $ add t2
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   962
      | add (f $ t) = add f $ add t;
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   963
  in add end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   964
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   965
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   966
(* Post-Processing *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   967
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   968
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   969
(*Instantiation of type variables in terms*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   970
fun inst_types tye = map_term_types (inst_typ tye);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   971
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   972
(*Delete explicit constraints -- occurrences of "_constrain" *)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   973
fun unconstrain (Abs(a, T, t)) = Abs(a, T, unconstrain t)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   974
  | unconstrain ((f as Const(a, _)) $ t) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   975
      if a=Syntax.constrainC then unconstrain t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   976
      else unconstrain f $ unconstrain t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   977
  | unconstrain (f$t) = unconstrain f $ unconstrain t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   978
  | unconstrain (t) = t;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   979
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   980
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   981
(* Turn all TVars which satisfy p into new TFrees *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   982
fun freeze p t =
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   983
  let val fs = add_term_tfree_names(t, []);
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   984
      val inxs = filter p (add_term_tvar_ixns(t, []));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   985
      val vmap = inxs ~~ variantlist(map #1 inxs, fs);
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   986
      fun free(Type(a, Ts)) = Type(a, map free Ts)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   987
        | free(T as TVar(v, S)) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   988
            (case assoc(vmap, v) of None => T | Some(a) => TFree(a, S))
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   989
        | free(T as TFree _) = T
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   990
  in map_term_types free t end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   991
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   992
(* Thaw all TVars that were frozen in freeze_vars *)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   993
fun thaw_vars(Type(a, Ts)) = Type(a, map thaw_vars Ts)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   994
  | thaw_vars(T as TFree(a, S)) = (case explode a of
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   995
          "?"::"'"::vn => let val ((b, i), _) = Syntax.scan_varname vn
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   996
                          in TVar(("'"^b, i), S) end
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
   997
        | _ => T)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   998
  | thaw_vars(T) = T;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   999
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1000
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1001
fun restrict tye =
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
  1002
  let fun clean(tye1, ((a, i), T)) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
  1003
        if i < 0 then tye1 else ((a, i), inst_typ tye T) :: tye1
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
  1004
  in foldl clean ([], tye) end
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1005
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1006
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1007
(*Infer types for term t using tables. Check that t's type and T unify *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1008
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
  1009
fun infer_term (tsig, const_tab, types, sorts, T, t) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
  1010
  let val u = add_types (tsig, const_tab, types, sorts) t;
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
  1011
      val (U, tye) = infer tsig ([], u, []);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1012
      val uu = unconstrain u;
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
  1013
      val tye' = unify tsig ((T, U), tye) handle TUNIFY => raise TYPE
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
  1014
        ("Term does not have expected type", [T, U], [inst_types tye uu])
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1015
      val Ttye = restrict tye' (* restriction to TVars in T *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1016
      val all = Const("", Type("", map snd Ttye)) $ (inst_types tye' uu)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1017
        (* all is a dummy term which contains all exported TVars *)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
  1018
      val Const(_, Type(_, Ts)) $ u'' =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
  1019
            map_term_types thaw_vars (freeze (fn (_, i) => i<0) all)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1020
        (* turn all internally generated TVars into TFrees
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1021
           and thaw all initially frozen TVars *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1022
  in (u'', (map fst Ttye) ~~ Ts) end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1023
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1024
fun infer_types args = (tyinit(); infer_term args);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1025
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1026
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1027
(* Turn TFrees into TVars to allow types & axioms to be written without "?" *)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
  1028
fun varifyT (Type (a, Ts)) = Type (a, map varifyT Ts)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
  1029
  | varifyT (TFree (a, S)) = TVar ((a, 0), S)
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
  1030
  | varifyT T = T;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1031
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1032
(* Turn TFrees except those in fixed into new TVars *)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
  1033
fun varify (t, fixed) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
  1034
  let val fs = add_term_tfree_names(t, []) \\ fixed;
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
  1035
      val ixns = add_term_tvar_ixns(t, []);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1036
      val fmap = fs ~~ variantlist(fs, map #1 ixns)
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
  1037
      fun thaw(Type(a, Ts)) = Type(a, map thaw Ts)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1038
        | thaw(T as TVar _) = T
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
  1039
        | thaw(T as TFree(a, S)) =
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
  1040
            (case assoc(fmap, a) of None => T | Some b => TVar((b, 0), S))
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1041
  in map_term_types thaw t end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1042
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1043
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
  1044
end;
256
b401c3d06024 (this is a preliminary release)
wenzelm
parents: 200
diff changeset
  1045