src/Pure/term.ML
author wenzelm
Wed, 20 Feb 2002 00:53:53 +0100
changeset 12902 a23dc0b7566f
parent 12802 c69bd9754473
child 12981 f48de47c32d6
permissions -rw-r--r--
Symbol.bump_string;
wenzelm@9536
     1
(*  Title:      Pure/term.ML
clasohm@0
     2
    ID:         $Id$
wenzelm@9536
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   Cambridge University 1992
clasohm@1364
     5
wenzelm@4444
     6
Simply typed lambda-calculus: types, terms, and basic operations.
clasohm@0
     7
*)
clasohm@0
     8
clasohm@1364
     9
infix 9  $;
clasohm@1364
    10
infixr 5 -->;
wenzelm@4444
    11
infixr --->;
wenzelm@4444
    12
infix aconv;
clasohm@0
    13
wenzelm@4444
    14
signature BASIC_TERM =
wenzelm@4444
    15
sig
wenzelm@4444
    16
  type indexname
wenzelm@4444
    17
  type class
wenzelm@4444
    18
  type sort
wenzelm@4444
    19
  datatype typ =
wenzelm@4444
    20
    Type  of string * typ list |
wenzelm@4444
    21
    TFree of string * sort |
wenzelm@4444
    22
    TVar  of indexname * sort
wenzelm@4444
    23
  val --> : typ * typ -> typ
wenzelm@4444
    24
  val ---> : typ list * typ -> typ
wenzelm@4444
    25
  val is_TVar: typ -> bool
wenzelm@4444
    26
  val domain_type: typ -> typ
wenzelm@4480
    27
  val range_type: typ -> typ
wenzelm@4444
    28
  val binder_types: typ -> typ list
wenzelm@4444
    29
  val body_type: typ -> typ
wenzelm@4444
    30
  val strip_type: typ -> typ list * typ
wenzelm@4444
    31
  datatype term =
wenzelm@4444
    32
    Const of string * typ |
wenzelm@4444
    33
    Free of string * typ |
wenzelm@4444
    34
    Var of indexname * typ |
wenzelm@4444
    35
    Bound of int |
wenzelm@4444
    36
    Abs of string * typ * term |
wenzelm@4493
    37
    $ of term * term
berghofe@8408
    38
  structure Vartab : TABLE
berghofe@8408
    39
  structure Termtab : TABLE
wenzelm@4444
    40
  exception TYPE of string * typ list * term list
wenzelm@4444
    41
  exception TERM of string * term list
nipkow@7318
    42
  val is_Bound: term -> bool
wenzelm@4444
    43
  val is_Const: term -> bool
wenzelm@4444
    44
  val is_Free: term -> bool
wenzelm@4444
    45
  val is_Var: term -> bool
paulson@6033
    46
  val dest_Type: typ -> string * typ list
wenzelm@4444
    47
  val dest_Const: term -> string * typ
wenzelm@4444
    48
  val dest_Free: term -> string * typ
wenzelm@4444
    49
  val dest_Var: term -> indexname * typ
wenzelm@4444
    50
  val type_of: term -> typ
wenzelm@4444
    51
  val type_of1: typ list * term -> typ
wenzelm@4444
    52
  val fastype_of: term -> typ
wenzelm@4444
    53
  val fastype_of1: typ list * term -> typ
wenzelm@10806
    54
  val list_abs: (string * typ) list * term -> term
wenzelm@4444
    55
  val strip_abs_body: term -> term
wenzelm@4444
    56
  val strip_abs_vars: term -> (string * typ) list
wenzelm@4444
    57
  val strip_qnt_body: string -> term -> term
wenzelm@4444
    58
  val strip_qnt_vars: string -> term -> (string * typ) list
wenzelm@4444
    59
  val list_comb: term * term list -> term
wenzelm@4444
    60
  val strip_comb: term -> term * term list
wenzelm@4444
    61
  val head_of: term -> term
wenzelm@4444
    62
  val size_of_term: term -> int
wenzelm@4444
    63
  val map_type_tvar: (indexname * sort -> typ) -> typ -> typ
wenzelm@4444
    64
  val map_type_tfree: (string * sort -> typ) -> typ -> typ
wenzelm@4444
    65
  val map_term_types: (typ -> typ) -> term -> term
wenzelm@4444
    66
  val it_term_types: (typ * 'a -> 'a) -> term * 'a -> 'a
wenzelm@4444
    67
  val map_typ: (class -> class) -> (string -> string) -> typ -> typ
wenzelm@4444
    68
  val map_term:
wenzelm@4444
    69
     (class -> class) ->
wenzelm@4444
    70
     (string -> string) -> (string -> string) -> term -> term
wenzelm@4444
    71
  val foldl_atyps: ('a * typ -> 'a) -> 'a * typ -> 'a
wenzelm@8609
    72
  val foldl_term_types: (term -> 'a * typ -> 'a) -> 'a * term -> 'a
wenzelm@4444
    73
  val foldl_types: ('a * typ -> 'a) -> 'a * term -> 'a
wenzelm@4444
    74
  val foldl_aterms: ('a * term -> 'a) -> 'a * term -> 'a
wenzelm@6548
    75
  val foldl_map_aterms: ('a * term -> 'a * term) -> 'a * term -> 'a * term
wenzelm@4444
    76
  val dummyT: typ
wenzelm@4444
    77
  val logicC: class
wenzelm@4444
    78
  val logicS: sort
wenzelm@4444
    79
  val itselfT: typ -> typ
wenzelm@4444
    80
  val a_itselfT: typ
wenzelm@4444
    81
  val propT: typ
wenzelm@4444
    82
  val implies: term
wenzelm@4444
    83
  val all: typ -> term
wenzelm@4444
    84
  val equals: typ -> term
wenzelm@4444
    85
  val flexpair: typ -> term
wenzelm@4444
    86
  val strip_all_body: term -> term
wenzelm@4444
    87
  val strip_all_vars: term -> (string * typ) list
wenzelm@4444
    88
  val incr_bv: int * int * term -> term
wenzelm@4444
    89
  val incr_boundvars: int -> term -> term
wenzelm@4444
    90
  val add_loose_bnos: term * int * int list -> int list
wenzelm@4444
    91
  val loose_bnos: term -> int list
wenzelm@4444
    92
  val loose_bvar: term * int -> bool
wenzelm@4444
    93
  val loose_bvar1: term * int -> bool
wenzelm@4444
    94
  val subst_bounds: term list * term -> term
wenzelm@4444
    95
  val subst_bound: term * term -> term
wenzelm@4444
    96
  val subst_TVars: (indexname * typ) list -> term -> term
berghofe@8408
    97
  val subst_TVars_Vartab: typ Vartab.table -> term -> term
wenzelm@4444
    98
  val betapply: term * term -> term
wenzelm@4444
    99
  val eq_ix: indexname * indexname -> bool
wenzelm@4444
   100
  val ins_ix: indexname * indexname list -> indexname list
wenzelm@4444
   101
  val mem_ix: indexname * indexname list -> bool
wenzelm@4444
   102
  val eq_sort: sort * class list -> bool
wenzelm@4444
   103
  val mem_sort: sort * class list list -> bool
wenzelm@4444
   104
  val subset_sort: sort list * class list list -> bool
wenzelm@4444
   105
  val eq_set_sort: sort list * sort list -> bool
wenzelm@4444
   106
  val ins_sort: sort * class list list -> class list list
wenzelm@4444
   107
  val union_sort: sort list * sort list -> sort list
wenzelm@7638
   108
  val rems_sort: sort list * sort list -> sort list
wenzelm@4444
   109
  val aconv: term * term -> bool
wenzelm@4444
   110
  val aconvs: term list * term list -> bool
wenzelm@4444
   111
  val mem_term: term * term list -> bool
wenzelm@4444
   112
  val subset_term: term list * term list -> bool
wenzelm@4444
   113
  val eq_set_term: term list * term list -> bool
wenzelm@4444
   114
  val ins_term: term * term list -> term list
wenzelm@4444
   115
  val union_term: term list * term list -> term list
paulson@5585
   116
  val inter_term: term list * term list -> term list
wenzelm@4444
   117
  val could_unify: term * term -> bool
wenzelm@4444
   118
  val subst_free: (term * term) list -> term -> term
wenzelm@4444
   119
  val subst_atomic: (term * term) list -> term -> term
wenzelm@4444
   120
  val subst_vars: (indexname * typ) list * (indexname * term) list -> term -> term
wenzelm@4444
   121
  val typ_subst_TVars: (indexname * typ) list -> typ -> typ
berghofe@8408
   122
  val typ_subst_TVars_Vartab : typ Vartab.table -> typ -> typ
wenzelm@4444
   123
  val subst_Vars: (indexname * term) list -> term -> term
wenzelm@4444
   124
  val incr_tvar: int -> typ -> typ
wenzelm@4444
   125
  val xless: (string * int) * indexname -> bool
wenzelm@4444
   126
  val atless: term * term -> bool
wenzelm@4444
   127
  val insert_aterm: term * term list -> term list
wenzelm@4444
   128
  val abstract_over: term * term -> term
wenzelm@11922
   129
  val lambda: term -> term -> term
wenzelm@4444
   130
  val absfree: string * typ * term -> term
wenzelm@4444
   131
  val list_abs_free: (string * typ) list * term -> term
wenzelm@4444
   132
  val list_all_free: (string * typ) list * term -> term
wenzelm@4444
   133
  val list_all: (string * typ) list * term -> term
wenzelm@4444
   134
  val maxidx_of_typ: typ -> int
wenzelm@4444
   135
  val maxidx_of_typs: typ list -> int
wenzelm@4444
   136
  val maxidx_of_term: term -> int
wenzelm@4694
   137
  val read_radixint: int * string list -> int * string list
wenzelm@4694
   138
  val read_int: string list -> int * string list
wenzelm@5986
   139
  val oct_char: string -> string
wenzelm@4444
   140
  val variant: string list -> string -> string
wenzelm@4444
   141
  val variantlist: string list * string list -> string list
wenzelm@4444
   142
  val variant_abs: string * typ * term -> string * term
wenzelm@4444
   143
  val rename_wrt_term: term -> (string * typ) list -> (string * typ) list
wenzelm@4444
   144
  val add_new_id: string list * string -> string list
wenzelm@4444
   145
  val add_typ_classes: typ * class list -> class list
wenzelm@4444
   146
  val add_typ_ixns: indexname list * typ -> indexname list
wenzelm@4444
   147
  val add_typ_tfree_names: typ * string list -> string list
wenzelm@4444
   148
  val add_typ_tfrees: typ * (string * sort) list -> (string * sort) list
wenzelm@4444
   149
  val typ_tfrees: typ -> (string * sort) list
wenzelm@4444
   150
  val add_typ_tvars: typ * (indexname * sort) list -> (indexname * sort) list
wenzelm@4444
   151
  val typ_tvars: typ -> (indexname * sort) list
wenzelm@4444
   152
  val add_typ_tycons: typ * string list -> string list
wenzelm@4444
   153
  val add_typ_varnames: typ * string list -> string list
wenzelm@4444
   154
  val add_term_classes: term * class list -> class list
wenzelm@4444
   155
  val add_term_consts: term * string list -> string list
wenzelm@4444
   156
  val add_term_frees: term * term list -> term list
wenzelm@4444
   157
  val term_frees: term -> term list
wenzelm@12802
   158
  val add_term_free_names: term * string list -> string list
wenzelm@4444
   159
  val add_term_names: term * string list -> string list
wenzelm@4444
   160
  val add_term_tfree_names: term * string list -> string list
wenzelm@4444
   161
  val add_term_tfrees: term * (string * sort) list -> (string * sort) list
wenzelm@4444
   162
  val term_tfrees: term -> (string * sort) list
wenzelm@4444
   163
  val add_term_tvar_ixns: term * indexname list -> indexname list
wenzelm@4444
   164
  val add_term_tvarnames: term * string list -> string list
wenzelm@4444
   165
  val add_term_tvars: term * (indexname * sort) list -> (indexname * sort) list
wenzelm@4444
   166
  val term_tvars: term -> (indexname * sort) list
wenzelm@4444
   167
  val add_term_tycons: term * string list -> string list
wenzelm@4444
   168
  val add_term_vars: term * term list -> term list
wenzelm@4444
   169
  val term_vars: term -> term list
wenzelm@4444
   170
  val exists_Const: (string * typ -> bool) -> term -> bool
nipkow@4631
   171
  val exists_subterm: (term -> bool) -> term -> bool
wenzelm@4444
   172
  val compress_type: typ -> typ
wenzelm@4444
   173
  val compress_term: term -> term
wenzelm@4444
   174
end;
clasohm@0
   175
wenzelm@4444
   176
signature TERM =
wenzelm@4444
   177
sig
wenzelm@4444
   178
  include BASIC_TERM
wenzelm@12499
   179
  val invent_names: string list -> string -> int -> string list
wenzelm@12499
   180
  val invent_type_names: string list -> int -> string list
wenzelm@12499
   181
  val add_tvarsT: (indexname * sort) list * typ -> (indexname * sort) list
wenzelm@12499
   182
  val add_tvars: (indexname * sort) list * term -> (indexname * sort) list
wenzelm@12499
   183
  val add_vars: (indexname * typ) list * term -> (indexname * typ) list
wenzelm@12499
   184
  val add_frees: (string * typ) list * term -> (string * typ) list
wenzelm@4444
   185
  val indexname_ord: indexname * indexname -> order
wenzelm@4444
   186
  val typ_ord: typ * typ -> order
wenzelm@4444
   187
  val typs_ord: typ list * typ list -> order
wenzelm@4444
   188
  val term_ord: term * term -> order
wenzelm@4444
   189
  val terms_ord: term list * term list -> order
wenzelm@4444
   190
  val termless: term * term -> bool
wenzelm@9536
   191
  val dummy_patternN: string
wenzelm@9536
   192
  val no_dummy_patterns: term -> term
wenzelm@9536
   193
  val replace_dummy_patterns: int * term -> int * term
wenzelm@10552
   194
  val is_replaced_dummy_pattern: indexname -> bool
wenzelm@4444
   195
end;
wenzelm@4444
   196
wenzelm@4444
   197
structure Term: TERM =
clasohm@1364
   198
struct
clasohm@0
   199
clasohm@0
   200
(*Indexnames can be quickly renamed by adding an offset to the integer part,
clasohm@0
   201
  for resolution.*)
clasohm@0
   202
type indexname = string*int;
clasohm@0
   203
wenzelm@4626
   204
(* Types are classified by sorts. *)
clasohm@0
   205
type class = string;
clasohm@0
   206
type sort  = class list;
clasohm@0
   207
clasohm@0
   208
(* The sorts attached to TFrees and TVars specify the sort of that variable *)
clasohm@0
   209
datatype typ = Type  of string * typ list
clasohm@0
   210
             | TFree of string * sort
wenzelm@9536
   211
             | TVar  of indexname * sort;
clasohm@0
   212
paulson@6033
   213
(*Terms.  Bound variables are indicated by depth number.
clasohm@0
   214
  Free variables, (scheme) variables and constants have names.
wenzelm@4626
   215
  An term is "closed" if every bound variable of level "lev"
clasohm@0
   216
  is enclosed by at least "lev" abstractions. 
clasohm@0
   217
clasohm@0
   218
  It is possible to create meaningless terms containing loose bound vars
clasohm@0
   219
  or type mismatches.  But such terms are not allowed in rules. *)
clasohm@0
   220
clasohm@0
   221
clasohm@0
   222
clasohm@0
   223
datatype term = 
clasohm@0
   224
    Const of string * typ
clasohm@0
   225
  | Free  of string * typ 
clasohm@0
   226
  | Var   of indexname * typ
clasohm@0
   227
  | Bound of int
clasohm@0
   228
  | Abs   of string*typ*term
wenzelm@3965
   229
  | op $  of term*term;
clasohm@0
   230
clasohm@0
   231
clasohm@0
   232
(*For errors involving type mismatches*)
clasohm@0
   233
exception TYPE of string * typ list * term list;
clasohm@0
   234
clasohm@0
   235
(*For system errors involving terms*)
clasohm@0
   236
exception TERM of string * term list;
clasohm@0
   237
clasohm@0
   238
clasohm@0
   239
(*Note variable naming conventions!
clasohm@0
   240
    a,b,c: string
clasohm@0
   241
    f,g,h: functions (including terms of function type)
clasohm@0
   242
    i,j,m,n: int
clasohm@0
   243
    t,u: term
clasohm@0
   244
    v,w: indexnames
clasohm@0
   245
    x,y: any
clasohm@0
   246
    A,B,C: term (denoting formulae)
clasohm@0
   247
    T,U: typ
clasohm@0
   248
*)
clasohm@0
   249
clasohm@0
   250
paulson@6033
   251
(** Types **)
paulson@6033
   252
paulson@6033
   253
fun S --> T = Type("fun",[S,T]);
paulson@6033
   254
paulson@6033
   255
(*handy for multiple args: [T1,...,Tn]--->T  gives  T1-->(T2--> ... -->T)*)
paulson@6033
   256
val op ---> = foldr (op -->);
paulson@6033
   257
paulson@6033
   258
fun dest_Type (Type x) = x
paulson@6033
   259
  | dest_Type T = raise TYPE ("dest_Type", [T], []);
paulson@6033
   260
paulson@6033
   261
clasohm@0
   262
(** Discriminators **)
clasohm@0
   263
nipkow@7318
   264
fun is_Bound (Bound _) = true
nipkow@7318
   265
  | is_Bound _         = false;
nipkow@7318
   266
clasohm@0
   267
fun is_Const (Const _) = true
clasohm@0
   268
  | is_Const _ = false;
clasohm@0
   269
clasohm@0
   270
fun is_Free (Free _) = true
clasohm@0
   271
  | is_Free _ = false;
clasohm@0
   272
clasohm@0
   273
fun is_Var (Var _) = true
clasohm@0
   274
  | is_Var _ = false;
clasohm@0
   275
clasohm@0
   276
fun is_TVar (TVar _) = true
clasohm@0
   277
  | is_TVar _ = false;
clasohm@0
   278
clasohm@0
   279
(** Destructors **)
clasohm@0
   280
clasohm@0
   281
fun dest_Const (Const x) =  x
clasohm@0
   282
  | dest_Const t = raise TERM("dest_Const", [t]);
clasohm@0
   283
clasohm@0
   284
fun dest_Free (Free x) =  x
clasohm@0
   285
  | dest_Free t = raise TERM("dest_Free", [t]);
clasohm@0
   286
clasohm@0
   287
fun dest_Var (Var x) =  x
clasohm@0
   288
  | dest_Var t = raise TERM("dest_Var", [t]);
clasohm@0
   289
clasohm@0
   290
paulson@4464
   291
fun domain_type (Type("fun", [T,_])) = T
paulson@4464
   292
and range_type  (Type("fun", [_,T])) = T;
paulson@4064
   293
clasohm@0
   294
(* maps  [T1,...,Tn]--->T  to the list  [T1,T2,...,Tn]*)
clasohm@0
   295
fun binder_types (Type("fun",[S,T])) = S :: binder_types T
clasohm@0
   296
  | binder_types _   =  [];
clasohm@0
   297
clasohm@0
   298
(* maps  [T1,...,Tn]--->T  to T*)
clasohm@0
   299
fun body_type (Type("fun",[S,T])) = body_type T
clasohm@0
   300
  | body_type T   =  T;
clasohm@0
   301
clasohm@0
   302
(* maps  [T1,...,Tn]--->T  to   ([T1,T2,...,Tn], T)  *)
clasohm@0
   303
fun strip_type T : typ list * typ =
clasohm@0
   304
  (binder_types T, body_type T);
clasohm@0
   305
clasohm@0
   306
clasohm@0
   307
(*Compute the type of the term, checking that combinations are well-typed
clasohm@0
   308
  Ts = [T0,T1,...] holds types of bound variables 0, 1, ...*)
clasohm@0
   309
fun type_of1 (Ts, Const (_,T)) = T
clasohm@0
   310
  | type_of1 (Ts, Free  (_,T)) = T
clasohm@0
   311
  | type_of1 (Ts, Bound i) = (nth_elem (i,Ts)  
wenzelm@9536
   312
        handle LIST _ => raise TYPE("type_of: bound variable", [], [Bound i]))
clasohm@0
   313
  | type_of1 (Ts, Var (_,T)) = T
clasohm@0
   314
  | type_of1 (Ts, Abs (_,T,body)) = T --> type_of1(T::Ts, body)
clasohm@0
   315
  | type_of1 (Ts, f$u) = 
clasohm@0
   316
      let val U = type_of1(Ts,u)
clasohm@0
   317
          and T = type_of1(Ts,f)
clasohm@0
   318
      in case T of
wenzelm@9536
   319
            Type("fun",[T1,T2]) =>
wenzelm@9536
   320
              if T1=U then T2  else raise TYPE
wenzelm@9536
   321
                    ("type_of: type mismatch in application", [T1,U], [f$u])
wenzelm@9536
   322
          | _ => raise TYPE 
wenzelm@9536
   323
                    ("type_of: function type is expected in application",
wenzelm@9536
   324
                     [T,U], [f$u])
clasohm@0
   325
      end;
clasohm@0
   326
clasohm@0
   327
fun type_of t : typ = type_of1 ([],t);
clasohm@0
   328
clasohm@0
   329
(*Determines the type of a term, with minimal checking*)
lcp@61
   330
fun fastype_of1 (Ts, f$u) = 
lcp@61
   331
    (case fastype_of1 (Ts,f) of
wenzelm@9536
   332
        Type("fun",[_,T]) => T
wenzelm@9536
   333
        | _ => raise TERM("fastype_of: expected function type", [f$u]))
lcp@61
   334
  | fastype_of1 (_, Const (_,T)) = T
lcp@61
   335
  | fastype_of1 (_, Free (_,T)) = T
lcp@61
   336
  | fastype_of1 (Ts, Bound i) = (nth_elem(i,Ts)
wenzelm@9536
   337
         handle LIST _ => raise TERM("fastype_of: Bound", [Bound i]))
lcp@61
   338
  | fastype_of1 (_, Var (_,T)) = T 
lcp@61
   339
  | fastype_of1 (Ts, Abs (_,T,u)) = T --> fastype_of1 (T::Ts, u);
lcp@61
   340
lcp@61
   341
fun fastype_of t : typ = fastype_of1 ([],t);
clasohm@0
   342
clasohm@0
   343
wenzelm@10806
   344
val list_abs = foldr (fn ((x, T), t) => Abs (x, T, t));
wenzelm@10806
   345
clasohm@0
   346
(* maps  (x1,...,xn)t   to   t  *)
clasohm@0
   347
fun strip_abs_body (Abs(_,_,t))  =  strip_abs_body t  
clasohm@0
   348
  | strip_abs_body u  =  u;
clasohm@0
   349
clasohm@0
   350
(* maps  (x1,...,xn)t   to   [x1, ..., xn]  *)
clasohm@0
   351
fun strip_abs_vars (Abs(a,T,t))  =  (a,T) :: strip_abs_vars t 
clasohm@0
   352
  | strip_abs_vars u  =  [] : (string*typ) list;
clasohm@0
   353
clasohm@0
   354
clasohm@0
   355
fun strip_qnt_body qnt =
clasohm@0
   356
let fun strip(tm as Const(c,_)$Abs(_,_,t)) = if c=qnt then strip t else tm
clasohm@0
   357
      | strip t = t
clasohm@0
   358
in strip end;
clasohm@0
   359
clasohm@0
   360
fun strip_qnt_vars qnt =
clasohm@0
   361
let fun strip(Const(c,_)$Abs(a,T,t)) = if c=qnt then (a,T)::strip t else []
clasohm@0
   362
      | strip t  =  [] : (string*typ) list
clasohm@0
   363
in strip end;
clasohm@0
   364
clasohm@0
   365
clasohm@0
   366
(* maps   (f, [t1,...,tn])  to  f(t1,...,tn) *)
clasohm@0
   367
val list_comb : term * term list -> term = foldl (op $);
clasohm@0
   368
clasohm@0
   369
clasohm@0
   370
(* maps   f(t1,...,tn)  to  (f, [t1,...,tn]) ; naturally tail-recursive*)
clasohm@0
   371
fun strip_comb u : term * term list = 
clasohm@0
   372
    let fun stripc (f$t, ts) = stripc (f, t::ts)
clasohm@0
   373
        |   stripc  x =  x 
clasohm@0
   374
    in  stripc(u,[])  end;
clasohm@0
   375
clasohm@0
   376
clasohm@0
   377
(* maps   f(t1,...,tn)  to  f , which is never a combination *)
clasohm@0
   378
fun head_of (f$t) = head_of f
clasohm@0
   379
  | head_of u = u;
clasohm@0
   380
clasohm@0
   381
clasohm@0
   382
(*Number of atoms and abstractions in a term*)
clasohm@0
   383
fun size_of_term (Abs (_,_,body)) = 1 + size_of_term body
clasohm@0
   384
  | size_of_term (f$t) = size_of_term f  +  size_of_term t
clasohm@0
   385
  | size_of_term _ = 1;
clasohm@0
   386
nipkow@949
   387
fun map_type_tvar f (Type(a,Ts)) = Type(a, map (map_type_tvar f) Ts)
nipkow@949
   388
  | map_type_tvar f (T as TFree _) = T
nipkow@949
   389
  | map_type_tvar f (TVar x) = f x;
nipkow@949
   390
nipkow@949
   391
fun map_type_tfree f (Type(a,Ts)) = Type(a, map (map_type_tfree f) Ts)
nipkow@949
   392
  | map_type_tfree f (TFree x) = f x
nipkow@949
   393
  | map_type_tfree f (T as TVar _) = T;
nipkow@949
   394
clasohm@0
   395
(* apply a function to all types in a term *)
clasohm@0
   396
fun map_term_types f =
clasohm@0
   397
let fun map(Const(a,T)) = Const(a, f T)
clasohm@0
   398
      | map(Free(a,T)) = Free(a, f T)
clasohm@0
   399
      | map(Var(v,T)) = Var(v, f T)
clasohm@0
   400
      | map(t as Bound _)  = t
clasohm@0
   401
      | map(Abs(a,T,t)) = Abs(a, f T, map t)
clasohm@0
   402
      | map(f$t) = map f $ map t;
clasohm@0
   403
in map end;
clasohm@0
   404
clasohm@0
   405
(* iterate a function over all types in a term *)
clasohm@0
   406
fun it_term_types f =
clasohm@0
   407
let fun iter(Const(_,T), a) = f(T,a)
clasohm@0
   408
      | iter(Free(_,T), a) = f(T,a)
clasohm@0
   409
      | iter(Var(_,T), a) = f(T,a)
clasohm@0
   410
      | iter(Abs(_,T,t), a) = iter(t,f(T,a))
clasohm@0
   411
      | iter(f$u, a) = iter(f, iter(u, a))
clasohm@0
   412
      | iter(Bound _, a) = a
clasohm@0
   413
in iter end
clasohm@0
   414
clasohm@0
   415
clasohm@0
   416
(** Connectives of higher order logic **)
clasohm@0
   417
wenzelm@375
   418
val logicC: class = "logic";
wenzelm@375
   419
val logicS: sort = [logicC];
wenzelm@375
   420
wenzelm@375
   421
fun itselfT ty = Type ("itself", [ty]);
wenzelm@375
   422
val a_itselfT = itselfT (TFree ("'a", logicS));
wenzelm@375
   423
clasohm@0
   424
val propT : typ = Type("prop",[]);
clasohm@0
   425
clasohm@0
   426
val implies = Const("==>", propT-->propT-->propT);
clasohm@0
   427
clasohm@0
   428
fun all T = Const("all", (T-->propT)-->propT);
clasohm@0
   429
clasohm@0
   430
fun equals T = Const("==", T-->T-->propT);
clasohm@0
   431
clasohm@0
   432
fun flexpair T = Const("=?=", T-->T-->propT);
clasohm@0
   433
clasohm@0
   434
(* maps  !!x1...xn. t   to   t  *)
clasohm@0
   435
fun strip_all_body (Const("all",_)$Abs(_,_,t))  =  strip_all_body t  
clasohm@0
   436
  | strip_all_body t  =  t;
clasohm@0
   437
clasohm@0
   438
(* maps  !!x1...xn. t   to   [x1, ..., xn]  *)
clasohm@0
   439
fun strip_all_vars (Const("all",_)$Abs(a,T,t))  =
wenzelm@9536
   440
                (a,T) :: strip_all_vars t 
clasohm@0
   441
  | strip_all_vars t  =  [] : (string*typ) list;
clasohm@0
   442
clasohm@0
   443
(*increments a term's non-local bound variables
clasohm@0
   444
  required when moving a term within abstractions
clasohm@0
   445
     inc is  increment for bound variables
clasohm@0
   446
     lev is  level at which a bound variable is considered 'loose'*)
clasohm@0
   447
fun incr_bv (inc, lev, u as Bound i) = if i>=lev then Bound(i+inc) else u 
clasohm@0
   448
  | incr_bv (inc, lev, Abs(a,T,body)) =
wenzelm@9536
   449
        Abs(a, T, incr_bv(inc,lev+1,body))
clasohm@0
   450
  | incr_bv (inc, lev, f$t) = 
clasohm@0
   451
      incr_bv(inc,lev,f) $ incr_bv(inc,lev,t)
clasohm@0
   452
  | incr_bv (inc, lev, u) = u;
clasohm@0
   453
clasohm@0
   454
fun incr_boundvars  0  t = t
clasohm@0
   455
  | incr_boundvars inc t = incr_bv(inc,0,t);
clasohm@0
   456
clasohm@0
   457
clasohm@0
   458
(*Accumulate all 'loose' bound vars referring to level 'lev' or beyond.
clasohm@0
   459
   (Bound 0) is loose at level 0 *)
clasohm@0
   460
fun add_loose_bnos (Bound i, lev, js) = 
wenzelm@9536
   461
        if i<lev then js  else  (i-lev) ins_int js
clasohm@0
   462
  | add_loose_bnos (Abs (_,_,t), lev, js) = add_loose_bnos (t, lev+1, js)
clasohm@0
   463
  | add_loose_bnos (f$t, lev, js) =
wenzelm@9536
   464
        add_loose_bnos (f, lev, add_loose_bnos (t, lev, js)) 
clasohm@0
   465
  | add_loose_bnos (_, _, js) = js;
clasohm@0
   466
clasohm@0
   467
fun loose_bnos t = add_loose_bnos (t, 0, []);
clasohm@0
   468
clasohm@0
   469
(* loose_bvar(t,k) iff t contains a 'loose' bound variable referring to
clasohm@0
   470
   level k or beyond. *)
clasohm@0
   471
fun loose_bvar(Bound i,k) = i >= k
clasohm@0
   472
  | loose_bvar(f$t, k) = loose_bvar(f,k) orelse loose_bvar(t,k)
clasohm@0
   473
  | loose_bvar(Abs(_,_,t),k) = loose_bvar(t,k+1)
clasohm@0
   474
  | loose_bvar _ = false;
clasohm@0
   475
nipkow@2792
   476
fun loose_bvar1(Bound i,k) = i = k
nipkow@2792
   477
  | loose_bvar1(f$t, k) = loose_bvar1(f,k) orelse loose_bvar1(t,k)
nipkow@2792
   478
  | loose_bvar1(Abs(_,_,t),k) = loose_bvar1(t,k+1)
nipkow@2792
   479
  | loose_bvar1 _ = false;
clasohm@0
   480
clasohm@0
   481
(*Substitute arguments for loose bound variables.
clasohm@0
   482
  Beta-reduction of arg(n-1)...arg0 into t replacing (Bound i) with (argi).
wenzelm@4626
   483
  Note that for ((%x y. c) a b), the bound vars in c are x=1 and y=0
wenzelm@9536
   484
        and the appropriate call is  subst_bounds([b,a], c) .
clasohm@0
   485
  Loose bound variables >=n are reduced by "n" to
clasohm@0
   486
     compensate for the disappearance of lambdas.
clasohm@0
   487
*)
clasohm@0
   488
fun subst_bounds (args: term list, t) : term = 
clasohm@0
   489
  let val n = length args;
clasohm@0
   490
      fun subst (t as Bound i, lev) =
wenzelm@9536
   491
           (if i<lev then  t    (*var is locally bound*)
wenzelm@9536
   492
            else  incr_boundvars lev (List.nth(args, i-lev))
wenzelm@9536
   493
                    handle Subscript => Bound(i-n)  (*loose: change it*))
wenzelm@9536
   494
        | subst (Abs(a,T,body), lev) = Abs(a, T,  subst(body,lev+1))
wenzelm@9536
   495
        | subst (f$t, lev) =  subst(f,lev)  $  subst(t,lev)
wenzelm@9536
   496
        | subst (t,lev) = t
clasohm@0
   497
  in   case args of [] => t  | _ => subst (t,0)  end;
clasohm@0
   498
paulson@2192
   499
(*Special case: one argument*)
paulson@2192
   500
fun subst_bound (arg, t) : term = 
paulson@2192
   501
  let fun subst (t as Bound i, lev) =
wenzelm@9536
   502
            if i<lev then  t    (*var is locally bound*)
wenzelm@9536
   503
            else  if i=lev then incr_boundvars lev arg
wenzelm@9536
   504
                           else Bound(i-1)  (*loose: change it*)
wenzelm@9536
   505
        | subst (Abs(a,T,body), lev) = Abs(a, T,  subst(body,lev+1))
wenzelm@9536
   506
        | subst (f$t, lev) =  subst(f,lev)  $  subst(t,lev)
wenzelm@9536
   507
        | subst (t,lev) = t
paulson@2192
   508
  in  subst (t,0)  end;
paulson@2192
   509
clasohm@0
   510
(*beta-reduce if possible, else form application*)
paulson@2192
   511
fun betapply (Abs(_,_,t), u) = subst_bound (u,t)
clasohm@0
   512
  | betapply (f,u) = f$u;
clasohm@0
   513
paulson@2192
   514
(** Equality, membership and insertion of indexnames (string*ints) **)
paulson@2192
   515
paulson@2192
   516
(*optimized equality test for indexnames.  Yields a huge gain under Poly/ML*)
wenzelm@2959
   517
fun eq_ix ((a, i):indexname, (a',i'):indexname) = (a=a') andalso i=i';
paulson@2192
   518
paulson@2192
   519
(*membership in a list, optimized version for indexnames*)
wenzelm@2959
   520
fun mem_ix (_, []) = false
paulson@2192
   521
  | mem_ix (x, y :: ys) = eq_ix(x,y) orelse mem_ix (x, ys);
paulson@2192
   522
paulson@2192
   523
(*insertion into list, optimized version for indexnames*)
paulson@2192
   524
fun ins_ix (x,xs) = if mem_ix (x, xs) then xs else x :: xs;
paulson@2192
   525
clasohm@0
   526
(*Tests whether 2 terms are alpha-convertible and have same type.
wenzelm@4626
   527
  Note that constants may have more than one type.*)
clasohm@0
   528
fun (Const(a,T)) aconv (Const(b,U)) = a=b  andalso  T=U
paulson@2752
   529
  | (Free(a,T))  aconv (Free(b,U))  = a=b  andalso  T=U
paulson@2752
   530
  | (Var(v,T))   aconv (Var(w,U))   = eq_ix(v,w)  andalso  T=U
paulson@2752
   531
  | (Bound i)    aconv (Bound j)    = i=j
clasohm@0
   532
  | (Abs(_,T,t)) aconv (Abs(_,U,u)) = t aconv u  andalso  T=U
paulson@2752
   533
  | (f$t)        aconv (g$u)        = (f aconv g) andalso (t aconv u)
clasohm@0
   534
  | _ aconv _  =  false;
clasohm@0
   535
paulson@2176
   536
(** Membership, insertion, union for terms **)
paulson@2176
   537
paulson@2176
   538
fun mem_term (_, []) = false
paulson@2176
   539
  | mem_term (t, t'::ts) = t aconv t' orelse mem_term(t,ts);
paulson@2176
   540
paulson@2182
   541
fun subset_term ([], ys) = true
paulson@2182
   542
  | subset_term (x :: xs, ys) = mem_term (x, ys) andalso subset_term(xs, ys);
paulson@2182
   543
paulson@2182
   544
fun eq_set_term (xs, ys) =
paulson@2182
   545
    xs = ys orelse (subset_term (xs, ys) andalso subset_term (ys, xs));
paulson@2182
   546
paulson@2176
   547
fun ins_term(t,ts) = if mem_term(t,ts) then ts else t :: ts;
paulson@2176
   548
paulson@2176
   549
fun union_term (xs, []) = xs
paulson@2176
   550
  | union_term ([], ys) = ys
paulson@2176
   551
  | union_term ((x :: xs), ys) = union_term (xs, ins_term (x, ys));
paulson@2176
   552
paulson@5585
   553
fun inter_term ([], ys) = []
paulson@5585
   554
  | inter_term (x::xs, ys) =
paulson@5585
   555
      if mem_term (x,ys) then x :: inter_term(xs,ys) else inter_term(xs,ys);
paulson@5585
   556
paulson@2176
   557
(** Equality, membership and insertion of sorts (string lists) **)
paulson@2176
   558
paulson@2176
   559
fun eq_sort ([]:sort, []) = true
paulson@2176
   560
  | eq_sort ((s::ss), (t::ts)) = s=t andalso eq_sort(ss,ts)
paulson@2176
   561
  | eq_sort (_, _) = false;
paulson@2176
   562
paulson@2176
   563
fun mem_sort (_:sort, []) = false
paulson@2176
   564
  | mem_sort (S, S'::Ss) = eq_sort (S, S') orelse mem_sort(S,Ss);
paulson@2176
   565
paulson@2176
   566
fun ins_sort(S,Ss) = if mem_sort(S,Ss) then Ss else S :: Ss;
paulson@2176
   567
paulson@2176
   568
fun union_sort (xs, []) = xs
paulson@2176
   569
  | union_sort ([], ys) = ys
paulson@2176
   570
  | union_sort ((x :: xs), ys) = union_sort (xs, ins_sort (x, ys));
paulson@2176
   571
paulson@2182
   572
fun subset_sort ([], ys) = true
paulson@2182
   573
  | subset_sort (x :: xs, ys) = mem_sort (x, ys) andalso subset_sort(xs, ys);
paulson@2182
   574
paulson@2182
   575
fun eq_set_sort (xs, ys) =
paulson@2182
   576
    xs = ys orelse (subset_sort (xs, ys) andalso subset_sort (ys, xs));
paulson@2182
   577
wenzelm@7638
   578
val rems_sort = gen_rems eq_sort;
wenzelm@7638
   579
clasohm@0
   580
(*are two term lists alpha-convertible in corresponding elements?*)
clasohm@0
   581
fun aconvs ([],[]) = true
clasohm@0
   582
  | aconvs (t::ts, u::us) = t aconv u andalso aconvs(ts,us)
clasohm@0
   583
  | aconvs _ = false;
clasohm@0
   584
clasohm@0
   585
(*A fast unification filter: true unless the two terms cannot be unified. 
clasohm@0
   586
  Terms must be NORMAL.  Treats all Vars as distinct. *)
clasohm@0
   587
fun could_unify (t,u) =
clasohm@0
   588
  let fun matchrands (f$t, g$u) = could_unify(t,u) andalso  matchrands(f,g)
wenzelm@9536
   589
        | matchrands _ = true
clasohm@0
   590
  in case (head_of t , head_of u) of
wenzelm@9536
   591
        (_, Var _) => true
clasohm@0
   592
      | (Var _, _) => true
clasohm@0
   593
      | (Const(a,_), Const(b,_)) =>  a=b andalso matchrands(t,u)
clasohm@0
   594
      | (Free(a,_), Free(b,_)) =>  a=b andalso matchrands(t,u)
clasohm@0
   595
      | (Bound i, Bound j) =>  i=j andalso matchrands(t,u)
clasohm@0
   596
      | (Abs _, _) =>  true   (*because of possible eta equality*)
clasohm@0
   597
      | (_, Abs _) =>  true
clasohm@0
   598
      | _ => false
clasohm@0
   599
  end;
clasohm@0
   600
clasohm@0
   601
(*Substitute new for free occurrences of old in a term*)
clasohm@0
   602
fun subst_free [] = (fn t=>t)
clasohm@0
   603
  | subst_free pairs =
clasohm@0
   604
      let fun substf u = 
wenzelm@9536
   605
            case gen_assoc (op aconv) (pairs, u) of
wenzelm@9536
   606
                Some u' => u'
wenzelm@9536
   607
              | None => (case u of Abs(a,T,t) => Abs(a, T, substf t)
wenzelm@9536
   608
                                 | t$u' => substf t $ substf u'
wenzelm@9536
   609
                                 | _ => u)
clasohm@0
   610
      in  substf  end;
clasohm@0
   611
clasohm@0
   612
(*a total, irreflexive ordering on index names*)
clasohm@0
   613
fun xless ((a,i), (b,j): indexname) = i<j  orelse  (i=j andalso a<b);
clasohm@0
   614
clasohm@0
   615
clasohm@0
   616
(*Abstraction of the term "body" over its occurrences of v, 
clasohm@0
   617
    which must contain no loose bound variables.
clasohm@0
   618
  The resulting term is ready to become the body of an Abs.*)
clasohm@0
   619
fun abstract_over (v,body) =
clasohm@0
   620
  let fun abst (lev,u) = if (v aconv u) then (Bound lev) else
clasohm@0
   621
      (case u of
clasohm@0
   622
          Abs(a,T,t) => Abs(a, T, abst(lev+1, t))
wenzelm@9536
   623
        | f$rand => abst(lev,f) $ abst(lev,rand)
wenzelm@9536
   624
        | _ => u)
clasohm@0
   625
  in  abst(0,body)  end;
clasohm@0
   626
wenzelm@11922
   627
fun lambda v t =
wenzelm@11922
   628
  let val (x, T) = dest_Free v
wenzelm@11922
   629
  in Abs (x, T, abstract_over (v, t)) end;
clasohm@0
   630
clasohm@0
   631
(*Form an abstraction over a free variable.*)
clasohm@0
   632
fun absfree (a,T,body) = Abs(a, T, abstract_over (Free(a,T), body));
clasohm@0
   633
clasohm@0
   634
(*Abstraction over a list of free variables*)
clasohm@0
   635
fun list_abs_free ([ ] ,     t) = t
clasohm@0
   636
  | list_abs_free ((a,T)::vars, t) = 
clasohm@0
   637
      absfree(a, T, list_abs_free(vars,t));
clasohm@0
   638
clasohm@0
   639
(*Quantification over a list of free variables*)
clasohm@0
   640
fun list_all_free ([], t: term) = t
clasohm@0
   641
  | list_all_free ((a,T)::vars, t) = 
clasohm@0
   642
        (all T) $ (absfree(a, T, list_all_free(vars,t)));
clasohm@0
   643
clasohm@0
   644
(*Quantification over a list of variables (already bound in body) *)
clasohm@0
   645
fun list_all ([], t) = t
clasohm@0
   646
  | list_all ((a,T)::vars, t) = 
clasohm@0
   647
        (all T) $ (Abs(a, T, list_all(vars,t)));
clasohm@0
   648
clasohm@0
   649
(*Replace the ATOMIC term ti by ui;    instl = [(t1,u1), ..., (tn,un)]. 
clasohm@0
   650
  A simultaneous substitution:  [ (a,b), (b,a) ] swaps a and b.  *)
clasohm@0
   651
fun subst_atomic [] t = t : term
clasohm@0
   652
  | subst_atomic (instl: (term*term) list) t =
clasohm@0
   653
      let fun subst (Abs(a,T,body)) = Abs(a, T, subst body)
wenzelm@9536
   654
            | subst (f$t') = subst f $ subst t'
nipkow@9721
   655
            | subst t = if_none (assoc(instl,t)) t
clasohm@0
   656
      in  subst t  end;
clasohm@0
   657
lcp@728
   658
(*Substitute for type Vars in a type*)
clasohm@0
   659
fun typ_subst_TVars iTs T = if null iTs then T else
clasohm@0
   660
  let fun subst(Type(a,Ts)) = Type(a, map subst Ts)
wenzelm@9536
   661
        | subst(T as TFree _) = T
nipkow@9721
   662
        | subst(T as TVar(ixn,_)) = if_none (assoc(iTs,ixn)) T
clasohm@0
   663
  in subst T end;
clasohm@0
   664
lcp@728
   665
(*Substitute for type Vars in a term*)
clasohm@0
   666
val subst_TVars = map_term_types o typ_subst_TVars;
clasohm@0
   667
lcp@728
   668
(*Substitute for Vars in a term; see also envir/norm_term*)
clasohm@0
   669
fun subst_Vars itms t = if null itms then t else
nipkow@9721
   670
  let fun subst(v as Var(ixn,_)) = if_none (assoc(itms,ixn)) v
clasohm@0
   671
        | subst(Abs(a,T,t)) = Abs(a,T,subst t)
clasohm@0
   672
        | subst(f$t) = subst f $ subst t
clasohm@0
   673
        | subst(t) = t
clasohm@0
   674
  in subst t end;
clasohm@0
   675
lcp@728
   676
(*Substitute for type/term Vars in a term; see also envir/norm_term*)
clasohm@0
   677
fun subst_vars(iTs,itms) = if null iTs then subst_Vars itms else
clasohm@0
   678
  let fun subst(Const(a,T)) = Const(a,typ_subst_TVars iTs T)
clasohm@0
   679
        | subst(Free(a,T)) = Free(a,typ_subst_TVars iTs T)
clasohm@0
   680
        | subst(v as Var(ixn,T)) = (case assoc(itms,ixn) of
clasohm@0
   681
            None   => Var(ixn,typ_subst_TVars iTs T)
clasohm@0
   682
          | Some t => t)
clasohm@0
   683
        | subst(b as Bound _) = b
clasohm@0
   684
        | subst(Abs(a,T,t)) = Abs(a,typ_subst_TVars iTs T,subst t)
clasohm@0
   685
        | subst(f$t) = subst f $ subst t
clasohm@0
   686
  in subst end;
clasohm@0
   687
clasohm@0
   688
clasohm@0
   689
(*Computing the maximum index of a typ*)
paulson@2146
   690
fun maxidx_of_typ(Type(_,Ts)) = maxidx_of_typs Ts
clasohm@0
   691
  | maxidx_of_typ(TFree _) = ~1
paulson@2146
   692
  | maxidx_of_typ(TVar((_,i),_)) = i
paulson@2146
   693
and maxidx_of_typs [] = ~1
paulson@2146
   694
  | maxidx_of_typs (T::Ts) = Int.max(maxidx_of_typ T, maxidx_of_typs Ts);
clasohm@0
   695
clasohm@0
   696
clasohm@0
   697
(*Computing the maximum index of a term*)
clasohm@0
   698
fun maxidx_of_term (Const(_,T)) = maxidx_of_typ T
clasohm@0
   699
  | maxidx_of_term (Bound _) = ~1
clasohm@0
   700
  | maxidx_of_term (Free(_,T)) = maxidx_of_typ T
paulson@2146
   701
  | maxidx_of_term (Var ((_,i), T)) = Int.max(i, maxidx_of_typ T)
paulson@2146
   702
  | maxidx_of_term (Abs (_,T,u)) = Int.max(maxidx_of_term u, maxidx_of_typ T)
paulson@2146
   703
  | maxidx_of_term (f$t) = Int.max(maxidx_of_term f,  maxidx_of_term t);
clasohm@0
   704
clasohm@0
   705
clasohm@0
   706
(* Increment the index of all Poly's in T by k *)
nipkow@949
   707
fun incr_tvar k = map_type_tvar (fn ((a,i),S) => TVar((a,i+k),S));
clasohm@0
   708
clasohm@0
   709
clasohm@0
   710
(**** Syntax-related declarations ****)
clasohm@0
   711
clasohm@0
   712
wenzelm@4626
   713
(*Dummy type for parsing and printing.  Will be replaced during type inference. *)
clasohm@0
   714
val dummyT = Type("dummy",[]);
clasohm@0
   715
wenzelm@4694
   716
(*read a numeral of the given radix, normally 10*)
wenzelm@4694
   717
fun read_radixint (radix: int, cs) : int * string list =
clasohm@0
   718
  let val zero = ord"0"
clasohm@0
   719
      val limit = zero+radix
clasohm@0
   720
      fun scan (num,[]) = (num,[])
wenzelm@9536
   721
        | scan (num, c::cs) =
wenzelm@9536
   722
              if  zero <= ord c  andalso  ord c < limit
wenzelm@9536
   723
              then scan(radix*num + ord c - zero, cs)
wenzelm@9536
   724
              else (num, c::cs)
clasohm@0
   725
  in  scan(0,cs)  end;
clasohm@0
   726
wenzelm@5986
   727
fun read_int cs = read_radixint (10, cs);
wenzelm@5986
   728
wenzelm@5986
   729
fun octal s = #1 (read_radixint (8, explode s));
wenzelm@5986
   730
val oct_char = chr o octal;
clasohm@0
   731
clasohm@0
   732
clasohm@0
   733
(*** Printing ***)
clasohm@0
   734
clasohm@0
   735
(*Makes a variant of the name c distinct from the names in bs.
wenzelm@12306
   736
  First attaches the suffix "a" and then increments this;
wenzelm@12306
   737
  preserves a suffix of underscores "_". *)
wenzelm@12306
   738
fun variant bs name =
wenzelm@12306
   739
  let
wenzelm@12306
   740
    val (c, u) = pairself implode (Library.take_suffix (equal "_") (Symbol.explode name));
wenzelm@12902
   741
    fun vary2 c = if ((c ^ u) mem_string bs) then vary2 (Symbol.bump_string c) else c;
wenzelm@12306
   742
    fun vary1 c = if ((c ^ u) mem_string bs) then vary2 (c ^ "a") else c;
wenzelm@12306
   743
  in vary1 (if c = "" then "u" else c) ^ u end;
clasohm@0
   744
clasohm@0
   745
(*Create variants of the list of names, with priority to the first ones*)
clasohm@0
   746
fun variantlist ([], used) = []
clasohm@0
   747
  | variantlist(b::bs, used) = 
clasohm@0
   748
      let val b' = variant used b
clasohm@0
   749
      in  b' :: variantlist (bs, b'::used)  end;
clasohm@0
   750
wenzelm@12499
   751
fun invent_names used prfx n = variantlist (Library.replicate n prfx, prfx :: used);
wenzelm@12499
   752
fun invent_type_names used = invent_names used "'";
wenzelm@11353
   753
wenzelm@4017
   754
wenzelm@4017
   755
wenzelm@4017
   756
(** Consts etc. **)
wenzelm@4017
   757
wenzelm@4017
   758
fun add_typ_classes (Type (_, Ts), cs) = foldr add_typ_classes (Ts, cs)
wenzelm@4017
   759
  | add_typ_classes (TFree (_, S), cs) = S union cs
wenzelm@4017
   760
  | add_typ_classes (TVar (_, S), cs) = S union cs;
wenzelm@4017
   761
wenzelm@4017
   762
fun add_typ_tycons (Type (c, Ts), cs) = foldr add_typ_tycons (Ts, c ins cs)
wenzelm@4017
   763
  | add_typ_tycons (_, cs) = cs;
wenzelm@4017
   764
wenzelm@4017
   765
val add_term_classes = it_term_types add_typ_classes;
wenzelm@4017
   766
val add_term_tycons = it_term_types add_typ_tycons;
wenzelm@4017
   767
wenzelm@9319
   768
fun add_term_consts (Const (c, _), cs) = c ins_string cs
wenzelm@4017
   769
  | add_term_consts (t $ u, cs) = add_term_consts (t, add_term_consts (u, cs))
wenzelm@4017
   770
  | add_term_consts (Abs (_, _, t), cs) = add_term_consts (t, cs)
wenzelm@4017
   771
  | add_term_consts (_, cs) = cs;
wenzelm@4017
   772
oheimb@4185
   773
fun exists_Const P t = let
wenzelm@9536
   774
        fun ex (Const c      ) = P c
wenzelm@9536
   775
        |   ex (t $ u        ) = ex t orelse ex u
wenzelm@9536
   776
        |   ex (Abs (_, _, t)) = ex t
wenzelm@9536
   777
        |   ex _               = false
oheimb@4185
   778
    in ex t end;
wenzelm@4017
   779
nipkow@4631
   780
fun exists_subterm P =
nipkow@4631
   781
  let fun ex t = P t orelse
nipkow@4631
   782
                 (case t of
nipkow@4631
   783
                    u $ v        => ex u orelse ex v
nipkow@4631
   784
                  | Abs(_, _, u) => ex u
nipkow@4631
   785
                  | _            => false)
nipkow@4631
   786
  in ex end;
nipkow@4631
   787
wenzelm@4017
   788
(*map classes, tycons*)
wenzelm@4017
   789
fun map_typ f g (Type (c, Ts)) = Type (g c, map (map_typ f g) Ts)
wenzelm@4017
   790
  | map_typ f _ (TFree (x, S)) = TFree (x, map f S)
wenzelm@4017
   791
  | map_typ f _ (TVar (xi, S)) = TVar (xi, map f S);
wenzelm@4017
   792
wenzelm@4017
   793
(*map classes, tycons, consts*)
wenzelm@4017
   794
fun map_term f g h (Const (c, T)) = Const (h c, map_typ f g T)
wenzelm@4017
   795
  | map_term f g _ (Free (x, T)) = Free (x, map_typ f g T)
wenzelm@4017
   796
  | map_term f g _ (Var (xi, T)) = Var (xi, map_typ f g T)
wenzelm@4017
   797
  | map_term _ _ _ (t as Bound _) = t
wenzelm@4017
   798
  | map_term f g h (Abs (x, T, t)) = Abs (x, map_typ f g T, map_term f g h t)
wenzelm@4017
   799
  | map_term f g h (t $ u) = map_term f g h t $ map_term f g h u;
wenzelm@4017
   800
wenzelm@4017
   801
wenzelm@4017
   802
clasohm@0
   803
(** TFrees and TVars **)
clasohm@0
   804
clasohm@0
   805
(*maps  (bs,v)  to   v'::bs    this reverses the identifiers bs*)
clasohm@0
   806
fun add_new_id (bs, c) : string list =  variant bs c  ::  bs;
clasohm@0
   807
wenzelm@12802
   808
(*Accumulates the names of Frees in the term, suppressing duplicates.*)
wenzelm@12802
   809
fun add_term_free_names (Free(a,_), bs) = a ins_string bs
wenzelm@12802
   810
  | add_term_free_names (f$u, bs) = add_term_free_names (f, add_term_free_names(u, bs))
wenzelm@12802
   811
  | add_term_free_names (Abs(_,_,t), bs) = add_term_free_names(t,bs)
wenzelm@12802
   812
  | add_term_free_names (_, bs) = bs;
wenzelm@12802
   813
clasohm@0
   814
(*Accumulates the names in the term, suppressing duplicates.
clasohm@0
   815
  Includes Frees and Consts.  For choosing unambiguous bound var names.*)
wenzelm@10666
   816
fun add_term_names (Const(a,_), bs) = NameSpace.base a ins_string bs
paulson@2176
   817
  | add_term_names (Free(a,_), bs) = a ins_string bs
clasohm@0
   818
  | add_term_names (f$u, bs) = add_term_names (f, add_term_names(u, bs))
clasohm@0
   819
  | add_term_names (Abs(_,_,t), bs) = add_term_names(t,bs)
clasohm@0
   820
  | add_term_names (_, bs) = bs;
clasohm@0
   821
clasohm@0
   822
(*Accumulates the TVars in a type, suppressing duplicates. *)
clasohm@0
   823
fun add_typ_tvars(Type(_,Ts),vs) = foldr add_typ_tvars (Ts,vs)
clasohm@0
   824
  | add_typ_tvars(TFree(_),vs) = vs
clasohm@0
   825
  | add_typ_tvars(TVar(v),vs) = v ins vs;
clasohm@0
   826
clasohm@0
   827
(*Accumulates the TFrees in a type, suppressing duplicates. *)
clasohm@0
   828
fun add_typ_tfree_names(Type(_,Ts),fs) = foldr add_typ_tfree_names (Ts,fs)
paulson@2176
   829
  | add_typ_tfree_names(TFree(f,_),fs) = f ins_string fs
clasohm@0
   830
  | add_typ_tfree_names(TVar(_),fs) = fs;
clasohm@0
   831
clasohm@0
   832
fun add_typ_tfrees(Type(_,Ts),fs) = foldr add_typ_tfrees (Ts,fs)
clasohm@0
   833
  | add_typ_tfrees(TFree(f),fs) = f ins fs
clasohm@0
   834
  | add_typ_tfrees(TVar(_),fs) = fs;
clasohm@0
   835
nipkow@949
   836
fun add_typ_varnames(Type(_,Ts),nms) = foldr add_typ_varnames (Ts,nms)
paulson@2176
   837
  | add_typ_varnames(TFree(nm,_),nms) = nm ins_string nms
paulson@2176
   838
  | add_typ_varnames(TVar((nm,_),_),nms) = nm ins_string nms;
nipkow@949
   839
clasohm@0
   840
(*Accumulates the TVars in a term, suppressing duplicates. *)
clasohm@0
   841
val add_term_tvars = it_term_types add_typ_tvars;
clasohm@0
   842
clasohm@0
   843
(*Accumulates the TFrees in a term, suppressing duplicates. *)
clasohm@0
   844
val add_term_tfrees = it_term_types add_typ_tfrees;
clasohm@0
   845
val add_term_tfree_names = it_term_types add_typ_tfree_names;
clasohm@0
   846
nipkow@949
   847
val add_term_tvarnames = it_term_types add_typ_varnames;
nipkow@949
   848
clasohm@0
   849
(*Non-list versions*)
clasohm@0
   850
fun typ_tfrees T = add_typ_tfrees(T,[]);
clasohm@0
   851
fun typ_tvars T = add_typ_tvars(T,[]);
clasohm@0
   852
fun term_tfrees t = add_term_tfrees(t,[]);
clasohm@0
   853
fun term_tvars t = add_term_tvars(t,[]);
clasohm@0
   854
nipkow@949
   855
(*special code to enforce left-to-right collection of TVar-indexnames*)
nipkow@949
   856
nipkow@949
   857
fun add_typ_ixns(ixns,Type(_,Ts)) = foldl add_typ_ixns (ixns,Ts)
paulson@2176
   858
  | add_typ_ixns(ixns,TVar(ixn,_)) = if mem_ix (ixn, ixns) then ixns 
wenzelm@9536
   859
                                     else ixns@[ixn]
nipkow@949
   860
  | add_typ_ixns(ixns,TFree(_)) = ixns;
nipkow@949
   861
nipkow@949
   862
fun add_term_tvar_ixns(Const(_,T),ixns) = add_typ_ixns(ixns,T)
nipkow@949
   863
  | add_term_tvar_ixns(Free(_,T),ixns) = add_typ_ixns(ixns,T)
nipkow@949
   864
  | add_term_tvar_ixns(Var(_,T),ixns) = add_typ_ixns(ixns,T)
nipkow@949
   865
  | add_term_tvar_ixns(Bound _,ixns) = ixns
nipkow@949
   866
  | add_term_tvar_ixns(Abs(_,T,t),ixns) =
nipkow@949
   867
      add_term_tvar_ixns(t,add_typ_ixns(ixns,T))
nipkow@949
   868
  | add_term_tvar_ixns(f$t,ixns) =
nipkow@949
   869
      add_term_tvar_ixns(t,add_term_tvar_ixns(f,ixns));
nipkow@949
   870
clasohm@0
   871
(** Frees and Vars **)
clasohm@0
   872
clasohm@0
   873
(*a partial ordering (not reflexive) for atomic terms*)
clasohm@0
   874
fun atless (Const (a,_), Const (b,_))  =  a<b
clasohm@0
   875
  | atless (Free (a,_), Free (b,_)) =  a<b
clasohm@0
   876
  | atless (Var(v,_), Var(w,_))  =  xless(v,w)
clasohm@0
   877
  | atless (Bound i, Bound j)  =   i<j
clasohm@0
   878
  | atless _  =  false;
clasohm@0
   879
clasohm@0
   880
(*insert atomic term into partially sorted list, suppressing duplicates (?)*)
clasohm@0
   881
fun insert_aterm (t,us) =
clasohm@0
   882
  let fun inserta [] = [t]
clasohm@0
   883
        | inserta (us as u::us') = 
wenzelm@9536
   884
              if atless(t,u) then t::us
wenzelm@9536
   885
              else if t=u then us (*duplicate*)
wenzelm@9536
   886
              else u :: inserta(us')
clasohm@0
   887
  in  inserta us  end;
clasohm@0
   888
clasohm@0
   889
(*Accumulates the Vars in the term, suppressing duplicates*)
clasohm@0
   890
fun add_term_vars (t, vars: term list) = case t of
clasohm@0
   891
    Var   _ => insert_aterm(t,vars)
clasohm@0
   892
  | Abs (_,_,body) => add_term_vars(body,vars)
clasohm@0
   893
  | f$t =>  add_term_vars (f, add_term_vars(t, vars))
clasohm@0
   894
  | _ => vars;
clasohm@0
   895
clasohm@0
   896
fun term_vars t = add_term_vars(t,[]);
clasohm@0
   897
clasohm@0
   898
(*Accumulates the Frees in the term, suppressing duplicates*)
clasohm@0
   899
fun add_term_frees (t, frees: term list) = case t of
clasohm@0
   900
    Free   _ => insert_aterm(t,frees)
clasohm@0
   901
  | Abs (_,_,body) => add_term_frees(body,frees)
clasohm@0
   902
  | f$t =>  add_term_frees (f, add_term_frees(t, frees))
clasohm@0
   903
  | _ => frees;
clasohm@0
   904
clasohm@0
   905
fun term_frees t = add_term_frees(t,[]);
clasohm@0
   906
clasohm@0
   907
(*Given an abstraction over P, replaces the bound variable by a Free variable
clasohm@0
   908
  having a unique name. *)
clasohm@0
   909
fun variant_abs (a,T,P) =
clasohm@0
   910
  let val b = variant (add_term_names(P,[])) a
paulson@2192
   911
  in  (b,  subst_bound (Free(b,T), P))  end;
clasohm@0
   912
clasohm@0
   913
(* renames and reverses the strings in vars away from names *)
clasohm@0
   914
fun rename_aTs names vars : (string*typ)list =
clasohm@0
   915
  let fun rename_aT (vars,(a,T)) =
wenzelm@9536
   916
                (variant (map #1 vars @ names) a, T) :: vars
clasohm@0
   917
  in foldl rename_aT ([],vars) end;
clasohm@0
   918
clasohm@0
   919
fun rename_wrt_term t = rename_aTs (add_term_names(t,[]));
clasohm@1364
   920
paulson@1417
   921
wenzelm@4286
   922
(* left-ro-right traversal *)
wenzelm@4286
   923
wenzelm@4286
   924
(*foldl atoms of type*)
wenzelm@4286
   925
fun foldl_atyps f (x, Type (_, Ts)) = foldl (foldl_atyps f) (x, Ts)
wenzelm@4286
   926
  | foldl_atyps f x_atom = f x_atom;
wenzelm@4286
   927
wenzelm@4286
   928
(*foldl atoms of term*)
wenzelm@4286
   929
fun foldl_aterms f (x, t $ u) = foldl_aterms f (foldl_aterms f (x, t), u)
wenzelm@4286
   930
  | foldl_aterms f (x, Abs (_, _, t)) = foldl_aterms f (x, t)
wenzelm@4286
   931
  | foldl_aterms f x_atom = f x_atom;
wenzelm@4286
   932
wenzelm@6548
   933
fun foldl_map_aterms f (x, t $ u) =
wenzelm@6548
   934
      let val (x', t') = foldl_map_aterms f (x, t); val (x'', u') = foldl_map_aterms f (x', u);
wenzelm@6548
   935
      in (x'', t' $ u') end
wenzelm@6548
   936
  | foldl_map_aterms f (x, Abs (a, T, t)) =
wenzelm@6548
   937
      let val (x', t') = foldl_map_aterms f (x, t) in (x', Abs (a, T, t')) end
wenzelm@6548
   938
  | foldl_map_aterms f x_atom = f x_atom;
wenzelm@6548
   939
wenzelm@4286
   940
(*foldl types of term*)
wenzelm@8609
   941
fun foldl_term_types f (x, t as Const (_, T)) = f t (x, T)
wenzelm@8609
   942
  | foldl_term_types f (x, t as Free (_, T)) = f t (x, T)
wenzelm@8609
   943
  | foldl_term_types f (x, t as Var (_, T)) = f t (x, T)
wenzelm@8609
   944
  | foldl_term_types f (x, Bound _) = x
wenzelm@8609
   945
  | foldl_term_types f (x, t as Abs (_, T, b)) = foldl_term_types f (f t (x, T), b)
wenzelm@8609
   946
  | foldl_term_types f (x, t $ u) = foldl_term_types f (foldl_term_types f (x, t), u);
wenzelm@8609
   947
wenzelm@8609
   948
fun foldl_types f = foldl_term_types (fn _ => f);
wenzelm@4286
   949
wenzelm@12499
   950
(*collect variables*)
wenzelm@12499
   951
val add_tvarsT = foldl_atyps (fn (vs, TVar v) => v ins vs | (vs, _) => vs);
wenzelm@12499
   952
val add_tvars = foldl_types add_tvarsT;
wenzelm@12499
   953
val add_vars = foldl_aterms (fn (vs, Var v) => v ins vs | (vs, _) => vs);
wenzelm@12499
   954
val add_frees = foldl_aterms (fn (vs, Free v) => v ins vs | (vs, _) => vs);
wenzelm@12499
   955
wenzelm@4286
   956
paulson@1417
   957
wenzelm@4444
   958
(** type and term orders **)
wenzelm@4444
   959
wenzelm@4444
   960
fun indexname_ord ((x, i), (y, j)) =
wenzelm@4444
   961
  (case int_ord (i, j) of EQUAL => string_ord (x, y) | ord => ord);
wenzelm@4444
   962
wenzelm@4444
   963
wenzelm@4444
   964
(* typ_ord *)
wenzelm@4444
   965
wenzelm@4444
   966
(*assumes that TFrees / TVars with the same name have same sorts*)
wenzelm@4444
   967
fun typ_ord (Type (a, Ts), Type (b, Us)) =
wenzelm@4444
   968
      (case string_ord (a, b) of EQUAL => typs_ord (Ts, Us) | ord => ord)
wenzelm@4444
   969
  | typ_ord (Type _, _) = GREATER
wenzelm@4444
   970
  | typ_ord (TFree _, Type _) = LESS
wenzelm@4444
   971
  | typ_ord (TFree (a, _), TFree (b, _)) = string_ord (a, b)
wenzelm@4444
   972
  | typ_ord (TFree _, TVar _) = GREATER
wenzelm@4444
   973
  | typ_ord (TVar _, Type _) = LESS
wenzelm@4444
   974
  | typ_ord (TVar _, TFree _) = LESS
wenzelm@4444
   975
  | typ_ord (TVar (xi, _), TVar (yj, _)) = indexname_ord (xi, yj)
wenzelm@4444
   976
and typs_ord Ts_Us = list_ord typ_ord Ts_Us;
wenzelm@4444
   977
wenzelm@4444
   978
wenzelm@4444
   979
(* term_ord *)
wenzelm@4444
   980
wenzelm@4444
   981
(*a linear well-founded AC-compatible ordering for terms:
wenzelm@4444
   982
  s < t <=> 1. size(s) < size(t) or
wenzelm@4444
   983
            2. size(s) = size(t) and s=f(...) and t=g(...) and f<g or
wenzelm@4444
   984
            3. size(s) = size(t) and s=f(s1..sn) and t=f(t1..tn) and
wenzelm@4444
   985
               (s1..sn) < (t1..tn) (lexicographically)*)
wenzelm@4444
   986
wenzelm@4444
   987
fun dest_hd (Const (a, T)) = (((a, 0), T), 0)
wenzelm@4444
   988
  | dest_hd (Free (a, T)) = (((a, 0), T), 1)
wenzelm@4444
   989
  | dest_hd (Var v) = (v, 2)
wenzelm@4444
   990
  | dest_hd (Bound i) = ((("", i), dummyT), 3)
wenzelm@4444
   991
  | dest_hd (Abs (_, T, _)) = ((("", 0), T), 4);
wenzelm@4444
   992
wenzelm@4444
   993
fun term_ord (Abs (_, T, t), Abs(_, U, u)) =
wenzelm@4444
   994
      (case term_ord (t, u) of EQUAL => typ_ord (T, U) | ord => ord)
wenzelm@4444
   995
  | term_ord (t, u) =
wenzelm@4444
   996
      (case int_ord (size_of_term t, size_of_term u) of
wenzelm@4444
   997
        EQUAL =>
wenzelm@4444
   998
          let val (f, ts) = strip_comb t and (g, us) = strip_comb u in
wenzelm@4444
   999
            (case hd_ord (f, g) of EQUAL => terms_ord (ts, us) | ord => ord)
wenzelm@4444
  1000
          end
wenzelm@4444
  1001
      | ord => ord)
wenzelm@4444
  1002
and hd_ord (f, g) =
wenzelm@4444
  1003
  prod_ord (prod_ord indexname_ord typ_ord) int_ord (dest_hd f, dest_hd g)
wenzelm@4444
  1004
and terms_ord (ts, us) = list_ord term_ord (ts, us);
wenzelm@4444
  1005
wenzelm@4444
  1006
fun termless tu = (term_ord tu = LESS);
wenzelm@4444
  1007
berghofe@8408
  1008
structure Vartab = TableFun(type key = indexname val ord = indexname_ord);
berghofe@8408
  1009
structure Termtab = TableFun(type key = term val ord = term_ord);
berghofe@8408
  1010
berghofe@8408
  1011
(*Substitute for type Vars in a type, version using Vartab*)
berghofe@8408
  1012
fun typ_subst_TVars_Vartab iTs T = if Vartab.is_empty iTs then T else
berghofe@8408
  1013
  let fun subst(Type(a, Ts)) = Type(a, map subst Ts)
wenzelm@9536
  1014
        | subst(T as TFree _) = T
wenzelm@9536
  1015
        | subst(T as TVar(ixn, _)) =
berghofe@8408
  1016
            (case Vartab.lookup (iTs, ixn) of None => T | Some(U) => U)
berghofe@8408
  1017
  in subst T end;
berghofe@8408
  1018
berghofe@8408
  1019
(*Substitute for type Vars in a term, version using Vartab*)
berghofe@8408
  1020
val subst_TVars_Vartab = map_term_types o typ_subst_TVars_Vartab;
wenzelm@4444
  1021
wenzelm@4444
  1022
paulson@1426
  1023
(*** Compression of terms and types by sharing common subtrees.  
paulson@1426
  1024
     Saves 50-75% on storage requirements.  As it is fairly slow, 
paulson@1426
  1025
     it is called only for axioms, stored theorems, etc. ***)
paulson@1417
  1026
paulson@1417
  1027
(** Sharing of types **)
paulson@1417
  1028
paulson@1417
  1029
fun atomic_tag (Type (a,_)) = if a<>"fun" then a else raise Match
paulson@1417
  1030
  | atomic_tag (TFree (a,_)) = a
paulson@1417
  1031
  | atomic_tag (TVar ((a,_),_)) = a;
paulson@1417
  1032
paulson@1417
  1033
fun type_tag (Type("fun",[S,T])) = atomic_tag S ^ type_tag T
paulson@1417
  1034
  | type_tag T = atomic_tag T;
paulson@1417
  1035
wenzelm@4487
  1036
val memo_types = ref (Symtab.empty : typ list Symtab.table);
paulson@1417
  1037
oheimb@4188
  1038
(* special case of library/find_first *)
paulson@1417
  1039
fun find_type (T, []: typ list) = None
paulson@1417
  1040
  | find_type (T, T'::Ts) =
paulson@1417
  1041
       if T=T' then Some T'
paulson@1417
  1042
       else find_type (T, Ts);
paulson@1417
  1043
paulson@1417
  1044
fun compress_type T =
paulson@1417
  1045
  let val tag = type_tag T
wenzelm@6963
  1046
      val tylist = Symtab.lookup_multi (!memo_types, tag)
paulson@1417
  1047
  in  
paulson@1417
  1048
      case find_type (T,tylist) of
wenzelm@9536
  1049
        Some T' => T'
paulson@1417
  1050
      | None => 
wenzelm@9536
  1051
            let val T' =
wenzelm@9536
  1052
                case T of
wenzelm@9536
  1053
                    Type (a,Ts) => Type (a, map compress_type Ts)
wenzelm@9536
  1054
                  | _ => T
wenzelm@9536
  1055
            in  memo_types := Symtab.update ((tag, T'::tylist), !memo_types);
wenzelm@9536
  1056
                T
wenzelm@9536
  1057
            end
paulson@1417
  1058
  end
paulson@1417
  1059
  handle Match =>
paulson@1417
  1060
      let val Type (a,Ts) = T
paulson@1417
  1061
      in  Type (a, map compress_type Ts)  end;
paulson@1417
  1062
paulson@1417
  1063
(** Sharing of atomic terms **)
paulson@1417
  1064
wenzelm@4487
  1065
val memo_terms = ref (Symtab.empty : term list Symtab.table);
paulson@1417
  1066
oheimb@4188
  1067
(* special case of library/find_first *)
paulson@1417
  1068
fun find_term (t, []: term list) = None
paulson@1417
  1069
  | find_term (t, t'::ts) =
paulson@1417
  1070
       if t=t' then Some t'
paulson@1417
  1071
       else find_term (t, ts);
paulson@1417
  1072
paulson@1417
  1073
fun const_tag (Const (a,_)) = a
paulson@1417
  1074
  | const_tag (Free (a,_))  = a
paulson@1417
  1075
  | const_tag (Var ((a,i),_)) = a
paulson@1417
  1076
  | const_tag (t as Bound _)  = ".B.";
paulson@1417
  1077
paulson@1417
  1078
fun share_term (t $ u) = share_term t $ share_term u
paulson@1417
  1079
  | share_term (Abs (a,T,u)) = Abs (a, T, share_term u)
paulson@1417
  1080
  | share_term t =
paulson@1417
  1081
      let val tag = const_tag t
wenzelm@9536
  1082
          val ts = Symtab.lookup_multi (!memo_terms, tag)
paulson@1417
  1083
      in 
wenzelm@9536
  1084
          case find_term (t,ts) of
wenzelm@9536
  1085
              Some t' => t'
wenzelm@9536
  1086
            | None => (memo_terms := Symtab.update ((tag, t::ts), !memo_terms);
wenzelm@9536
  1087
                       t)
paulson@1417
  1088
      end;
paulson@1417
  1089
paulson@1417
  1090
val compress_term = share_term o map_term_types compress_type;
paulson@1417
  1091
wenzelm@4444
  1092
wenzelm@9536
  1093
(* dummy patterns *)
wenzelm@9536
  1094
wenzelm@9536
  1095
val dummy_patternN = "dummy_pattern";
wenzelm@9536
  1096
wenzelm@9536
  1097
fun is_dummy_pattern (Const ("dummy_pattern", _)) = true
wenzelm@9536
  1098
  | is_dummy_pattern _ = false;
wenzelm@9536
  1099
wenzelm@9536
  1100
fun no_dummy_patterns tm =
wenzelm@9536
  1101
  if not (foldl_aterms (fn (b, t) => b orelse is_dummy_pattern t) (false, tm)) then tm
wenzelm@9536
  1102
  else raise TERM ("Illegal occurrence of '_' dummy pattern", [tm]);
wenzelm@9536
  1103
wenzelm@11903
  1104
fun replace_dummy Ts (i, Const ("dummy_pattern", T)) =
wenzelm@11903
  1105
      (i + 1, list_comb (Var (("_dummy_", i), Ts ---> T), map Bound (0 upto length Ts - 1)))
wenzelm@11903
  1106
  | replace_dummy Ts (i, Abs (x, T, t)) =
wenzelm@11903
  1107
      let val (i', t') = replace_dummy (T :: Ts) (i, t)
wenzelm@11903
  1108
      in (i', Abs (x, T, t')) end
wenzelm@11903
  1109
  | replace_dummy Ts (i, t $ u) =
wenzelm@11903
  1110
      let val (i', t') = replace_dummy Ts (i, t); val (i'', u') = replace_dummy Ts (i', u)
wenzelm@11903
  1111
      in (i'', t' $ u') end
wenzelm@11903
  1112
  | replace_dummy _ (i, a) = (i, a);
wenzelm@11903
  1113
wenzelm@11903
  1114
val replace_dummy_patterns = replace_dummy [];
wenzelm@9536
  1115
wenzelm@10552
  1116
fun is_replaced_dummy_pattern ("_dummy_", _) = true
wenzelm@10552
  1117
  | is_replaced_dummy_pattern _ = false;
wenzelm@9536
  1118
clasohm@1364
  1119
end;
clasohm@1364
  1120
wenzelm@4444
  1121
wenzelm@4444
  1122
structure BasicTerm: BASIC_TERM = Term;
wenzelm@4444
  1123
open BasicTerm;