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