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