src/Pure/term.ML
author wenzelm
Mon Jul 04 17:07:12 2005 +0200 (2005-07-04)
changeset 16678 dcbdb1373d78
parent 16667 f56080acd176
child 16710 3d6335ff3982
permissions -rw-r--r--
added fast_indexname_ord, fast_term_ord;
changed sort_ord, typ_ord, Vartab, Termtab: use fast orders;
added argument_type_of, dest_abs;
tuned;
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@1364
    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@14829
    19
  type arity
wenzelm@4444
    20
  datatype typ =
wenzelm@4444
    21
    Type  of string * typ list |
wenzelm@4444
    22
    TFree of string * sort |
wenzelm@4444
    23
    TVar  of indexname * sort
wenzelm@16537
    24
  datatype term =
wenzelm@16537
    25
    Const of string * typ |
wenzelm@16537
    26
    Free of string * typ |
wenzelm@16537
    27
    Var of indexname * typ |
wenzelm@16537
    28
    Bound of int |
wenzelm@16537
    29
    Abs of string * typ * term |
wenzelm@16537
    30
    op $ of term * term
wenzelm@16537
    31
  exception TYPE of string * typ list * term list
wenzelm@16537
    32
  exception TERM of string * term list
wenzelm@4444
    33
  val --> : typ * typ -> typ
wenzelm@4444
    34
  val ---> : typ list * typ -> typ
wenzelm@4444
    35
  val is_TVar: typ -> bool
paulson@15573
    36
  val is_funtype: typ -> bool
wenzelm@4444
    37
  val domain_type: typ -> typ
wenzelm@4480
    38
  val range_type: typ -> typ
wenzelm@4444
    39
  val binder_types: typ -> typ list
wenzelm@4444
    40
  val body_type: typ -> typ
wenzelm@4444
    41
  val strip_type: typ -> typ list * typ
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@16589
    46
  val is_first_order: string list -> term -> bool
paulson@6033
    47
  val dest_Type: typ -> string * typ list
dixon@15914
    48
  val dest_TVar: typ -> indexname * sort
dixon@15914
    49
  val dest_TFree: typ -> string * sort
wenzelm@4444
    50
  val dest_Const: term -> string * typ
wenzelm@4444
    51
  val dest_Free: term -> string * typ
wenzelm@4444
    52
  val dest_Var: term -> indexname * typ
wenzelm@4444
    53
  val type_of: term -> typ
wenzelm@4444
    54
  val type_of1: typ list * term -> typ
wenzelm@4444
    55
  val fastype_of: term -> typ
wenzelm@4444
    56
  val fastype_of1: typ list * term -> typ
wenzelm@10806
    57
  val list_abs: (string * typ) list * term -> term
wenzelm@4444
    58
  val strip_abs_body: term -> term
wenzelm@4444
    59
  val strip_abs_vars: term -> (string * typ) list
wenzelm@4444
    60
  val strip_qnt_body: string -> term -> term
wenzelm@4444
    61
  val strip_qnt_vars: string -> term -> (string * typ) list
wenzelm@4444
    62
  val list_comb: term * term list -> term
wenzelm@4444
    63
  val strip_comb: term -> term * term list
wenzelm@4444
    64
  val head_of: term -> term
wenzelm@4444
    65
  val size_of_term: term -> int
wenzelm@4444
    66
  val map_type_tvar: (indexname * sort -> typ) -> typ -> typ
wenzelm@4444
    67
  val map_type_tfree: (string * sort -> typ) -> typ -> typ
wenzelm@4444
    68
  val map_term_types: (typ -> typ) -> term -> term
wenzelm@4444
    69
  val it_term_types: (typ * 'a -> 'a) -> term * 'a -> 'a
wenzelm@16537
    70
  structure Vartab: TABLE
wenzelm@16537
    71
  structure Typtab: TABLE
wenzelm@16537
    72
  structure Termtab: TABLE
wenzelm@16537
    73
  val aconv: term * term -> bool
wenzelm@16537
    74
  val aconvs: term list * term list -> bool
wenzelm@4444
    75
  val foldl_atyps: ('a * typ -> 'a) -> 'a * typ -> 'a
wenzelm@8609
    76
  val foldl_term_types: (term -> 'a * typ -> 'a) -> 'a * term -> 'a
wenzelm@4444
    77
  val foldl_types: ('a * typ -> 'a) -> 'a * term -> 'a
wenzelm@4444
    78
  val foldl_aterms: ('a * term -> 'a) -> 'a * term -> 'a
wenzelm@6548
    79
  val foldl_map_aterms: ('a * term -> 'a * term) -> 'a * term -> 'a * term
wenzelm@15025
    80
  val add_term_varnames: indexname list * term -> indexname list
wenzelm@15025
    81
  val term_varnames: term -> indexname list
wenzelm@4444
    82
  val dummyT: typ
wenzelm@4444
    83
  val itselfT: typ -> typ
wenzelm@4444
    84
  val a_itselfT: typ
wenzelm@4444
    85
  val propT: typ
wenzelm@4444
    86
  val implies: term
wenzelm@4444
    87
  val all: typ -> term
wenzelm@4444
    88
  val equals: typ -> term
wenzelm@4444
    89
  val strip_all_body: term -> term
wenzelm@4444
    90
  val strip_all_vars: term -> (string * typ) list
wenzelm@4444
    91
  val incr_bv: int * int * term -> term
wenzelm@4444
    92
  val incr_boundvars: int -> term -> term
wenzelm@4444
    93
  val add_loose_bnos: term * int * int list -> int list
wenzelm@4444
    94
  val loose_bnos: term -> int list
wenzelm@4444
    95
  val loose_bvar: term * int -> bool
wenzelm@4444
    96
  val loose_bvar1: term * int -> bool
wenzelm@4444
    97
  val subst_bounds: term list * term -> term
wenzelm@4444
    98
  val subst_bound: term * term -> term
wenzelm@4444
    99
  val subst_TVars: (indexname * typ) list -> term -> term
wenzelm@4444
   100
  val betapply: term * term -> term
wenzelm@4444
   101
  val eq_ix: indexname * indexname -> bool
wenzelm@4444
   102
  val ins_ix: indexname * indexname list -> indexname list
wenzelm@4444
   103
  val mem_ix: indexname * indexname list -> bool
wenzelm@4444
   104
  val mem_term: term * term list -> bool
wenzelm@4444
   105
  val subset_term: term list * term list -> bool
wenzelm@4444
   106
  val eq_set_term: term list * term list -> bool
wenzelm@4444
   107
  val ins_term: term * term list -> term list
wenzelm@4444
   108
  val union_term: term list * term list -> term list
paulson@5585
   109
  val inter_term: term list * term list -> term list
wenzelm@4444
   110
  val could_unify: term * term -> bool
wenzelm@4444
   111
  val subst_free: (term * term) list -> term -> term
wenzelm@4444
   112
  val subst_atomic: (term * term) list -> term -> term
berghofe@15797
   113
  val typ_subst_atomic: (typ * typ) list -> typ -> typ
wenzelm@16678
   114
  val subst_atomic_types: (typ * typ) list -> term -> term
wenzelm@4444
   115
  val subst_vars: (indexname * typ) list * (indexname * term) list -> term -> term
wenzelm@4444
   116
  val typ_subst_TVars: (indexname * typ) list -> typ -> typ
wenzelm@4444
   117
  val subst_Vars: (indexname * term) list -> term -> term
wenzelm@4444
   118
  val incr_tvar: int -> typ -> typ
wenzelm@4444
   119
  val xless: (string * int) * indexname -> bool
wenzelm@4444
   120
  val atless: term * term -> bool
wenzelm@4444
   121
  val insert_aterm: term * term list -> term list
wenzelm@4444
   122
  val abstract_over: term * term -> term
wenzelm@11922
   123
  val lambda: term -> term -> term
wenzelm@4444
   124
  val absfree: string * typ * term -> term
wenzelm@4444
   125
  val list_abs_free: (string * typ) list * term -> term
wenzelm@4444
   126
  val list_all_free: (string * typ) list * term -> term
wenzelm@4444
   127
  val list_all: (string * typ) list * term -> term
wenzelm@4444
   128
  val maxidx_of_typ: typ -> int
wenzelm@4444
   129
  val maxidx_of_typs: typ list -> int
wenzelm@4444
   130
  val maxidx_of_term: term -> int
berghofe@13665
   131
  val maxidx_of_terms: term list -> int
wenzelm@4444
   132
  val variant: string list -> string -> string
wenzelm@4444
   133
  val variantlist: string list * string list -> string list
wenzelm@16537
   134
    (*note reversed order of args wrt. variant!*)
wenzelm@4444
   135
  val variant_abs: string * typ * term -> string * term
wenzelm@4444
   136
  val rename_wrt_term: term -> (string * typ) list -> (string * typ) list
wenzelm@4444
   137
  val add_new_id: string list * string -> string list
wenzelm@4444
   138
  val add_typ_classes: typ * class list -> class list
wenzelm@4444
   139
  val add_typ_ixns: indexname list * typ -> indexname list
wenzelm@4444
   140
  val add_typ_tfree_names: typ * string list -> string list
wenzelm@4444
   141
  val add_typ_tfrees: typ * (string * sort) list -> (string * sort) list
wenzelm@4444
   142
  val typ_tfrees: typ -> (string * sort) list
wenzelm@4444
   143
  val add_typ_tvars: typ * (indexname * sort) list -> (indexname * sort) list
wenzelm@4444
   144
  val typ_tvars: typ -> (indexname * sort) list
wenzelm@4444
   145
  val add_typ_tycons: typ * string list -> string list
wenzelm@4444
   146
  val add_typ_varnames: typ * string list -> string list
wenzelm@4444
   147
  val add_term_classes: term * class list -> class list
wenzelm@4444
   148
  val add_term_consts: term * string list -> string list
nipkow@13646
   149
  val term_consts: term -> string list
obua@16108
   150
  val term_constsT: term -> (string * typ) list
wenzelm@4444
   151
  val add_term_frees: term * term list -> term list
wenzelm@4444
   152
  val term_frees: term -> term list
wenzelm@12802
   153
  val add_term_free_names: term * string list -> string list
wenzelm@4444
   154
  val add_term_names: term * string list -> string list
wenzelm@4444
   155
  val add_term_tfree_names: term * string list -> string list
wenzelm@4444
   156
  val add_term_tfrees: term * (string * sort) list -> (string * sort) list
wenzelm@4444
   157
  val term_tfrees: term -> (string * sort) list
wenzelm@4444
   158
  val add_term_tvar_ixns: term * indexname list -> indexname list
wenzelm@4444
   159
  val add_term_tvarnames: term * string list -> string list
wenzelm@4444
   160
  val add_term_tvars: term * (indexname * sort) list -> (indexname * sort) list
wenzelm@4444
   161
  val term_tvars: term -> (indexname * sort) list
wenzelm@4444
   162
  val add_term_tycons: term * string list -> string list
wenzelm@4444
   163
  val add_term_vars: term * term list -> term list
wenzelm@4444
   164
  val term_vars: term -> term list
wenzelm@4444
   165
  val exists_Const: (string * typ -> bool) -> term -> bool
nipkow@4631
   166
  val exists_subterm: (term -> bool) -> term -> bool
wenzelm@4444
   167
  val compress_type: typ -> typ
wenzelm@4444
   168
  val compress_term: term -> term
wenzelm@15986
   169
  val show_question_marks: bool ref
wenzelm@4444
   170
end;
clasohm@0
   171
wenzelm@4444
   172
signature TERM =
wenzelm@4444
   173
sig
wenzelm@4444
   174
  include BASIC_TERM
wenzelm@16678
   175
  val fast_indexname_ord: indexname * indexname -> order
wenzelm@16537
   176
  val indexname_ord: indexname * indexname -> order
wenzelm@16537
   177
  val sort_ord: sort * sort -> order
wenzelm@16537
   178
  val typ_ord: typ * typ -> order
wenzelm@16678
   179
  val fast_term_ord: term * term -> order
wenzelm@16537
   180
  val term_ord: term * term -> order
wenzelm@16537
   181
  val hd_ord: term * term -> order
wenzelm@16537
   182
  val termless: term * term -> bool
nipkow@16570
   183
  val term_lpo: (string -> int) -> term * term -> order
wenzelm@12981
   184
  val match_bvars: (term * term) * (string * string) list -> (string * string) list
wenzelm@12981
   185
  val rename_abs: term -> term -> term -> term option
wenzelm@16678
   186
  val argument_type_of: term -> typ
wenzelm@12499
   187
  val invent_names: string list -> string -> int -> string list
wenzelm@16338
   188
  val map_typ: (string -> string) -> (string -> string) -> typ -> typ
wenzelm@16338
   189
  val map_term: (string -> string) -> (string -> string) -> (string -> string) -> term -> term
wenzelm@12499
   190
  val add_tvarsT: (indexname * sort) list * typ -> (indexname * sort) list
wenzelm@12499
   191
  val add_tvars: (indexname * sort) list * term -> (indexname * sort) list
wenzelm@12499
   192
  val add_vars: (indexname * typ) list * term -> (indexname * typ) list
wenzelm@12499
   193
  val add_frees: (string * typ) list * term -> (string * typ) list
wenzelm@16678
   194
  val dest_abs: string * typ * term -> string * term
wenzelm@14786
   195
  val no_dummyT: typ -> typ
wenzelm@9536
   196
  val dummy_patternN: string
wenzelm@9536
   197
  val no_dummy_patterns: term -> term
wenzelm@9536
   198
  val replace_dummy_patterns: int * term -> int * term
wenzelm@10552
   199
  val is_replaced_dummy_pattern: indexname -> bool
wenzelm@16035
   200
  val show_dummy_patterns: term -> term
wenzelm@13484
   201
  val adhoc_freeze_vars: term -> term * string list
wenzelm@14786
   202
  val string_of_vname: indexname -> string
wenzelm@14786
   203
  val string_of_vname': indexname -> string
nipkow@15612
   204
  val string_of_term: term -> string
wenzelm@4444
   205
end;
wenzelm@4444
   206
wenzelm@4444
   207
structure Term: TERM =
clasohm@1364
   208
struct
clasohm@0
   209
clasohm@0
   210
(*Indexnames can be quickly renamed by adding an offset to the integer part,
clasohm@0
   211
  for resolution.*)
wenzelm@16537
   212
type indexname = string * int;
clasohm@0
   213
wenzelm@4626
   214
(* Types are classified by sorts. *)
clasohm@0
   215
type class = string;
clasohm@0
   216
type sort  = class list;
wenzelm@14829
   217
type arity = string * sort list * sort;
clasohm@0
   218
clasohm@0
   219
(* The sorts attached to TFrees and TVars specify the sort of that variable *)
clasohm@0
   220
datatype typ = Type  of string * typ list
clasohm@0
   221
             | TFree of string * sort
wenzelm@9536
   222
             | TVar  of indexname * sort;
clasohm@0
   223
paulson@6033
   224
(*Terms.  Bound variables are indicated by depth number.
clasohm@0
   225
  Free variables, (scheme) variables and constants have names.
wenzelm@4626
   226
  An term is "closed" if every bound variable of level "lev"
wenzelm@13000
   227
  is enclosed by at least "lev" abstractions.
clasohm@0
   228
clasohm@0
   229
  It is possible to create meaningless terms containing loose bound vars
clasohm@0
   230
  or type mismatches.  But such terms are not allowed in rules. *)
clasohm@0
   231
wenzelm@13000
   232
datatype term =
clasohm@0
   233
    Const of string * typ
wenzelm@13000
   234
  | Free  of string * typ
clasohm@0
   235
  | Var   of indexname * typ
clasohm@0
   236
  | Bound of int
clasohm@0
   237
  | Abs   of string*typ*term
wenzelm@3965
   238
  | op $  of term*term;
clasohm@0
   239
wenzelm@16537
   240
(*Errors involving type mismatches*)
clasohm@0
   241
exception TYPE of string * typ list * term list;
clasohm@0
   242
wenzelm@16537
   243
(*Errors errors involving terms*)
clasohm@0
   244
exception TERM of string * term list;
clasohm@0
   245
clasohm@0
   246
(*Note variable naming conventions!
clasohm@0
   247
    a,b,c: string
clasohm@0
   248
    f,g,h: functions (including terms of function type)
clasohm@0
   249
    i,j,m,n: int
clasohm@0
   250
    t,u: term
clasohm@0
   251
    v,w: indexnames
clasohm@0
   252
    x,y: any
clasohm@0
   253
    A,B,C: term (denoting formulae)
clasohm@0
   254
    T,U: typ
clasohm@0
   255
*)
clasohm@0
   256
clasohm@0
   257
paulson@6033
   258
(** Types **)
paulson@6033
   259
wenzelm@16537
   260
(*dummy type for parsing and printing etc.*)
wenzelm@16537
   261
val dummyT = Type ("dummy", []);
wenzelm@16537
   262
wenzelm@16537
   263
fun no_dummyT typ =
wenzelm@16537
   264
  let
wenzelm@16537
   265
    fun check (T as Type ("dummy", _)) =
wenzelm@16537
   266
          raise TYPE ("Illegal occurrence of '_' dummy type", [T], [])
wenzelm@16537
   267
      | check (Type (_, Ts)) = List.app check Ts
wenzelm@16537
   268
      | check _ = ();
wenzelm@16537
   269
  in check typ; typ end;
wenzelm@16537
   270
paulson@6033
   271
fun S --> T = Type("fun",[S,T]);
paulson@6033
   272
paulson@6033
   273
(*handy for multiple args: [T1,...,Tn]--->T  gives  T1-->(T2--> ... -->T)*)
skalberg@15570
   274
val op ---> = Library.foldr (op -->);
paulson@6033
   275
paulson@6033
   276
fun dest_Type (Type x) = x
paulson@6033
   277
  | dest_Type T = raise TYPE ("dest_Type", [T], []);
dixon@15914
   278
fun dest_TVar (TVar x) = x
dixon@15914
   279
  | dest_TVar T = raise TYPE ("dest_TVar", [T], []);
dixon@15914
   280
fun dest_TFree (TFree x) = x
dixon@15914
   281
  | dest_TFree T = raise TYPE ("dest_TFree", [T], []);
paulson@6033
   282
wenzelm@16537
   283
clasohm@0
   284
(** Discriminators **)
clasohm@0
   285
nipkow@7318
   286
fun is_Bound (Bound _) = true
nipkow@7318
   287
  | is_Bound _         = false;
nipkow@7318
   288
clasohm@0
   289
fun is_Const (Const _) = true
clasohm@0
   290
  | is_Const _ = false;
clasohm@0
   291
clasohm@0
   292
fun is_Free (Free _) = true
clasohm@0
   293
  | is_Free _ = false;
clasohm@0
   294
clasohm@0
   295
fun is_Var (Var _) = true
clasohm@0
   296
  | is_Var _ = false;
clasohm@0
   297
clasohm@0
   298
fun is_TVar (TVar _) = true
clasohm@0
   299
  | is_TVar _ = false;
clasohm@0
   300
paulson@15573
   301
(*Differs from proofterm/is_fun in its treatment of TVar*)
paulson@15573
   302
fun is_funtype (Type("fun",[_,_])) = true
paulson@15573
   303
  | is_funtype _ = false;
paulson@15573
   304
wenzelm@16537
   305
clasohm@0
   306
(** Destructors **)
clasohm@0
   307
clasohm@0
   308
fun dest_Const (Const x) =  x
clasohm@0
   309
  | dest_Const t = raise TERM("dest_Const", [t]);
clasohm@0
   310
clasohm@0
   311
fun dest_Free (Free x) =  x
clasohm@0
   312
  | dest_Free t = raise TERM("dest_Free", [t]);
clasohm@0
   313
clasohm@0
   314
fun dest_Var (Var x) =  x
clasohm@0
   315
  | dest_Var t = raise TERM("dest_Var", [t]);
clasohm@0
   316
clasohm@0
   317
paulson@4464
   318
fun domain_type (Type("fun", [T,_])) = T
paulson@4464
   319
and range_type  (Type("fun", [_,T])) = T;
paulson@4064
   320
clasohm@0
   321
(* maps  [T1,...,Tn]--->T  to the list  [T1,T2,...,Tn]*)
clasohm@0
   322
fun binder_types (Type("fun",[S,T])) = S :: binder_types T
clasohm@0
   323
  | binder_types _   =  [];
clasohm@0
   324
clasohm@0
   325
(* maps  [T1,...,Tn]--->T  to T*)
clasohm@0
   326
fun body_type (Type("fun",[S,T])) = body_type T
clasohm@0
   327
  | body_type T   =  T;
clasohm@0
   328
clasohm@0
   329
(* maps  [T1,...,Tn]--->T  to   ([T1,T2,...,Tn], T)  *)
clasohm@0
   330
fun strip_type T : typ list * typ =
clasohm@0
   331
  (binder_types T, body_type T);
clasohm@0
   332
clasohm@0
   333
clasohm@0
   334
(*Compute the type of the term, checking that combinations are well-typed
clasohm@0
   335
  Ts = [T0,T1,...] holds types of bound variables 0, 1, ...*)
clasohm@0
   336
fun type_of1 (Ts, Const (_,T)) = T
clasohm@0
   337
  | type_of1 (Ts, Free  (_,T)) = T
skalberg@15570
   338
  | type_of1 (Ts, Bound i) = (List.nth (Ts,i)
skalberg@15570
   339
        handle Subscript => raise TYPE("type_of: bound variable", [], [Bound i]))
clasohm@0
   340
  | type_of1 (Ts, Var (_,T)) = T
clasohm@0
   341
  | type_of1 (Ts, Abs (_,T,body)) = T --> type_of1(T::Ts, body)
wenzelm@13000
   342
  | type_of1 (Ts, f$u) =
clasohm@0
   343
      let val U = type_of1(Ts,u)
clasohm@0
   344
          and T = type_of1(Ts,f)
clasohm@0
   345
      in case T of
wenzelm@9536
   346
            Type("fun",[T1,T2]) =>
wenzelm@9536
   347
              if T1=U then T2  else raise TYPE
wenzelm@9536
   348
                    ("type_of: type mismatch in application", [T1,U], [f$u])
wenzelm@13000
   349
          | _ => raise TYPE
wenzelm@9536
   350
                    ("type_of: function type is expected in application",
wenzelm@9536
   351
                     [T,U], [f$u])
clasohm@0
   352
      end;
clasohm@0
   353
clasohm@0
   354
fun type_of t : typ = type_of1 ([],t);
clasohm@0
   355
clasohm@0
   356
(*Determines the type of a term, with minimal checking*)
wenzelm@13000
   357
fun fastype_of1 (Ts, f$u) =
lcp@61
   358
    (case fastype_of1 (Ts,f) of
wenzelm@9536
   359
        Type("fun",[_,T]) => T
wenzelm@9536
   360
        | _ => raise TERM("fastype_of: expected function type", [f$u]))
lcp@61
   361
  | fastype_of1 (_, Const (_,T)) = T
lcp@61
   362
  | fastype_of1 (_, Free (_,T)) = T
skalberg@15570
   363
  | fastype_of1 (Ts, Bound i) = (List.nth(Ts,i)
skalberg@15570
   364
         handle Subscript => raise TERM("fastype_of: Bound", [Bound i]))
wenzelm@13000
   365
  | fastype_of1 (_, Var (_,T)) = T
lcp@61
   366
  | fastype_of1 (Ts, Abs (_,T,u)) = T --> fastype_of1 (T::Ts, u);
lcp@61
   367
lcp@61
   368
fun fastype_of t : typ = fastype_of1 ([],t);
clasohm@0
   369
wenzelm@16678
   370
(*Determine the argument type of a function*)
wenzelm@16678
   371
fun argument_type_of tm =
wenzelm@16678
   372
  let
wenzelm@16678
   373
    fun argT i (Type ("fun", [T, U])) = if i = 0 then T else argT (i - 1) U
wenzelm@16678
   374
      | argT _ T = raise TYPE ("argument_type_of", [T], []);
wenzelm@16678
   375
wenzelm@16678
   376
    fun arg 0 _ (Abs (_, T, _)) = T
wenzelm@16678
   377
      | arg i Ts (Abs (_, T, t)) = arg (i - 1) (T :: Ts) t
wenzelm@16678
   378
      | arg i Ts (t $ _) = arg (i + 1) Ts t
wenzelm@16678
   379
      | arg i Ts a = argT i (fastype_of1 (Ts, a));
wenzelm@16678
   380
  in arg 0 [] tm end;
wenzelm@16678
   381
clasohm@0
   382
skalberg@15570
   383
val list_abs = Library.foldr (fn ((x, T), t) => Abs (x, T, t));
wenzelm@10806
   384
clasohm@0
   385
(* maps  (x1,...,xn)t   to   t  *)
wenzelm@13000
   386
fun strip_abs_body (Abs(_,_,t))  =  strip_abs_body t
clasohm@0
   387
  | strip_abs_body u  =  u;
clasohm@0
   388
clasohm@0
   389
(* maps  (x1,...,xn)t   to   [x1, ..., xn]  *)
wenzelm@13000
   390
fun strip_abs_vars (Abs(a,T,t))  =  (a,T) :: strip_abs_vars t
clasohm@0
   391
  | strip_abs_vars u  =  [] : (string*typ) list;
clasohm@0
   392
clasohm@0
   393
clasohm@0
   394
fun strip_qnt_body qnt =
clasohm@0
   395
let fun strip(tm as Const(c,_)$Abs(_,_,t)) = if c=qnt then strip t else tm
clasohm@0
   396
      | strip t = t
clasohm@0
   397
in strip end;
clasohm@0
   398
clasohm@0
   399
fun strip_qnt_vars qnt =
clasohm@0
   400
let fun strip(Const(c,_)$Abs(a,T,t)) = if c=qnt then (a,T)::strip t else []
clasohm@0
   401
      | strip t  =  [] : (string*typ) list
clasohm@0
   402
in strip end;
clasohm@0
   403
clasohm@0
   404
clasohm@0
   405
(* maps   (f, [t1,...,tn])  to  f(t1,...,tn) *)
skalberg@15570
   406
val list_comb : term * term list -> term = Library.foldl (op $);
clasohm@0
   407
clasohm@0
   408
clasohm@0
   409
(* maps   f(t1,...,tn)  to  (f, [t1,...,tn]) ; naturally tail-recursive*)
wenzelm@13000
   410
fun strip_comb u : term * term list =
clasohm@0
   411
    let fun stripc (f$t, ts) = stripc (f, t::ts)
wenzelm@13000
   412
        |   stripc  x =  x
clasohm@0
   413
    in  stripc(u,[])  end;
clasohm@0
   414
clasohm@0
   415
clasohm@0
   416
(* maps   f(t1,...,tn)  to  f , which is never a combination *)
clasohm@0
   417
fun head_of (f$t) = head_of f
clasohm@0
   418
  | head_of u = u;
clasohm@0
   419
clasohm@0
   420
wenzelm@16599
   421
(*number of atoms and abstractions in a term*)
wenzelm@16599
   422
fun size_of_term tm =
wenzelm@16599
   423
  let
wenzelm@16678
   424
    fun add_size (t $ u, n) = add_size (t, add_size (u, n))
wenzelm@16678
   425
      | add_size (Abs (_ ,_, t), n) = add_size (t, n + 1)
wenzelm@16678
   426
      | add_size (_, n) = n + 1;
wenzelm@16678
   427
  in add_size (tm, 0) end;
clasohm@0
   428
wenzelm@16678
   429
fun map_type_tvar f =
wenzelm@16678
   430
  let
wenzelm@16678
   431
    fun map_aux (Type (a, Ts)) = Type (a, map map_aux Ts)
wenzelm@16678
   432
      | map_aux (TVar x) = f x
wenzelm@16678
   433
      | map_aux T = T;
wenzelm@16678
   434
  in map_aux end;
nipkow@949
   435
wenzelm@16678
   436
fun map_type_tfree f =
wenzelm@16678
   437
  let
wenzelm@16678
   438
    fun map_aux (Type (a, Ts)) = Type (a, map map_aux Ts)
wenzelm@16678
   439
      | map_aux (TFree x) = f x
wenzelm@16678
   440
      | map_aux T = T;
wenzelm@16678
   441
  in map_aux end;
nipkow@949
   442
clasohm@0
   443
fun map_term_types f =
wenzelm@16678
   444
  let
wenzelm@16678
   445
    fun map_aux (Const (a, T)) = Const (a, f T)
wenzelm@16678
   446
      | map_aux (Free (a, T)) = Free (a, f T)
wenzelm@16678
   447
      | map_aux (Var (v, T)) = Var (v, f T)
wenzelm@16678
   448
      | map_aux (t as Bound _)  = t
wenzelm@16678
   449
      | map_aux (Abs (a, T, t)) = Abs (a, f T, map_aux t)
wenzelm@16678
   450
      | map_aux (t $ u) = map_aux t $ map_aux u;
wenzelm@16678
   451
  in map_aux end;
clasohm@0
   452
clasohm@0
   453
(* iterate a function over all types in a term *)
clasohm@0
   454
fun it_term_types f =
clasohm@0
   455
let fun iter(Const(_,T), a) = f(T,a)
clasohm@0
   456
      | iter(Free(_,T), a) = f(T,a)
clasohm@0
   457
      | iter(Var(_,T), a) = f(T,a)
clasohm@0
   458
      | iter(Abs(_,T,t), a) = iter(t,f(T,a))
clasohm@0
   459
      | iter(f$u, a) = iter(f, iter(u, a))
clasohm@0
   460
      | iter(Bound _, a) = a
clasohm@0
   461
in iter end
clasohm@0
   462
clasohm@0
   463
wenzelm@16678
   464
(** Comparing terms, types, sorts etc. **)
wenzelm@16537
   465
wenzelm@16678
   466
(* fast syntactic comparison *)
wenzelm@16678
   467
wenzelm@16678
   468
fun fast_indexname_ord ((x, i), (y, j)) =
wenzelm@16678
   469
  (case int_ord (i, j) of EQUAL => fast_string_ord (x, y) | ord => ord);
wenzelm@16537
   470
wenzelm@16599
   471
fun sort_ord SS =
wenzelm@16599
   472
  if pointer_eq SS then EQUAL
wenzelm@16678
   473
  else list_ord fast_string_ord SS;
wenzelm@16678
   474
wenzelm@16678
   475
local
wenzelm@16537
   476
wenzelm@16678
   477
fun cons_nr (TVar _) = 0
wenzelm@16678
   478
  | cons_nr (TFree _) = 1
wenzelm@16678
   479
  | cons_nr (Type _) = 2;
wenzelm@16537
   480
wenzelm@16678
   481
in
wenzelm@16537
   482
wenzelm@16537
   483
fun typ_ord TU =
wenzelm@16537
   484
  if pointer_eq TU then EQUAL
wenzelm@16537
   485
  else
wenzelm@16537
   486
    (case TU of
wenzelm@16678
   487
      (Type (a, Ts), Type (b, Us)) =>
wenzelm@16678
   488
        (case fast_string_ord (a, b) of EQUAL => list_ord typ_ord (Ts, Us) | ord => ord)
wenzelm@16678
   489
    | (TFree (a, S), TFree (b, S')) =>
wenzelm@16678
   490
        (case fast_string_ord (a, b) of EQUAL => sort_ord (S, S') | ord => ord)
wenzelm@16678
   491
    | (TVar (xi, S), TVar (yj, S')) =>
wenzelm@16678
   492
        (case fast_indexname_ord (xi, yj) of EQUAL => sort_ord (S, S') | ord => ord)
wenzelm@16678
   493
    | (T, U) => int_ord (cons_nr T, cons_nr U));
wenzelm@16678
   494
wenzelm@16678
   495
end;
wenzelm@16678
   496
wenzelm@16678
   497
local
wenzelm@16678
   498
wenzelm@16678
   499
fun cons_nr (Const _) = 0
wenzelm@16678
   500
  | cons_nr (Free _) = 1
wenzelm@16678
   501
  | cons_nr (Var _) = 2
wenzelm@16678
   502
  | cons_nr (Bound _) = 3
wenzelm@16678
   503
  | cons_nr (Abs _) = 4
wenzelm@16678
   504
  | cons_nr (_ $ _) = 5;
wenzelm@16678
   505
wenzelm@16678
   506
fun struct_ord (Abs (_, _, t), Abs (_, _, u)) = struct_ord (t, u)
wenzelm@16678
   507
  | struct_ord (t1 $ t2, u1 $ u2) =
wenzelm@16678
   508
      (case struct_ord (t1, u1) of EQUAL => struct_ord (t2, u2) | ord => ord)
wenzelm@16678
   509
  | struct_ord (t, u) = int_ord (cons_nr t, cons_nr u);
wenzelm@16678
   510
wenzelm@16678
   511
fun atoms_ord (Abs (_, _, t), Abs (_, _, u)) = atoms_ord (t, u)
wenzelm@16678
   512
  | atoms_ord (t1 $ t2, u1 $ u2) =
wenzelm@16678
   513
      (case atoms_ord (t1, u1) of EQUAL => atoms_ord (t2, u2) | ord => ord)
wenzelm@16678
   514
  | atoms_ord (Const (a, _), Const (b, _)) = fast_string_ord (a, b)
wenzelm@16678
   515
  | atoms_ord (Free (x, _), Free (y, _)) = fast_string_ord (x, y)
wenzelm@16678
   516
  | atoms_ord (Var (xi, _), Var (yj, _)) = fast_indexname_ord (xi, yj)
wenzelm@16678
   517
  | atoms_ord (Bound i, Bound j) = int_ord (i, j)
wenzelm@16678
   518
  | atoms_ord _ = sys_error "atoms_ord";
wenzelm@16678
   519
wenzelm@16678
   520
fun types_ord (Abs (_, T, t), Abs (_, U, u)) =
wenzelm@16678
   521
      (case typ_ord (T, U) of EQUAL => types_ord (t, u) | ord => ord)
wenzelm@16678
   522
  | types_ord (t1 $ t2, u1 $ u2) =
wenzelm@16678
   523
      (case types_ord (t1, u1) of EQUAL => types_ord (t2, u2) | ord => ord)
wenzelm@16678
   524
  | types_ord (Const (_, T), Const (_, U)) = typ_ord (T, U)
wenzelm@16678
   525
  | types_ord (Free (_, T), Free (_, U)) = typ_ord (T, U)
wenzelm@16678
   526
  | types_ord (Var (_, T), Var (_, U)) = typ_ord (T, U)
wenzelm@16678
   527
  | types_ord (Bound _, Bound _) = EQUAL
wenzelm@16678
   528
  | types_ord _ = sys_error "types_ord";
wenzelm@16678
   529
wenzelm@16678
   530
in
wenzelm@16678
   531
wenzelm@16678
   532
fun fast_term_ord tu =
wenzelm@16678
   533
  if pointer_eq tu then EQUAL
wenzelm@16678
   534
  else
wenzelm@16678
   535
    (case struct_ord tu of
wenzelm@16678
   536
      EQUAL => (case atoms_ord tu of EQUAL => types_ord tu | ord => ord)
wenzelm@16678
   537
    | ord => ord);
wenzelm@16678
   538
wenzelm@16678
   539
fun op aconv tu = (fast_term_ord tu = EQUAL);
wenzelm@16678
   540
fun aconvs ts_us = (list_ord fast_term_ord ts_us = EQUAL);
wenzelm@16678
   541
wenzelm@16678
   542
structure Vartab = TableFun(type key = indexname val ord = fast_indexname_ord);
wenzelm@16678
   543
structure Typtab = TableFun(type key = typ val ord = typ_ord);
wenzelm@16678
   544
structure Termtab = TableFun(type key = term val ord = fast_term_ord);
wenzelm@16678
   545
wenzelm@16678
   546
end;
wenzelm@16537
   547
wenzelm@16537
   548
wenzelm@16537
   549
(* term_ord *)
wenzelm@16537
   550
wenzelm@16537
   551
(*a linear well-founded AC-compatible ordering for terms:
wenzelm@16537
   552
  s < t <=> 1. size(s) < size(t) or
wenzelm@16537
   553
            2. size(s) = size(t) and s=f(...) and t=g(...) and f<g or
wenzelm@16537
   554
            3. size(s) = size(t) and s=f(s1..sn) and t=f(t1..tn) and
wenzelm@16537
   555
               (s1..sn) < (t1..tn) (lexicographically)*)
wenzelm@16678
   556
wenzelm@16678
   557
fun indexname_ord ((x, i), (y, j)) =
wenzelm@16678
   558
  (case int_ord (i, j) of EQUAL => string_ord (x, y) | ord => ord);
wenzelm@16678
   559
wenzelm@16667
   560
local
wenzelm@16667
   561
wenzelm@16667
   562
fun hd_depth (t $ _, n) = hd_depth (t, n + 1)
wenzelm@16667
   563
  | hd_depth p = p;
wenzelm@16537
   564
wenzelm@16537
   565
fun dest_hd (Const (a, T)) = (((a, 0), T), 0)
wenzelm@16537
   566
  | dest_hd (Free (a, T)) = (((a, 0), T), 1)
wenzelm@16537
   567
  | dest_hd (Var v) = (v, 2)
wenzelm@16537
   568
  | dest_hd (Bound i) = ((("", i), dummyT), 3)
wenzelm@16537
   569
  | dest_hd (Abs (_, T, _)) = ((("", 0), T), 4);
wenzelm@16537
   570
wenzelm@16667
   571
in
wenzelm@16667
   572
wenzelm@16537
   573
fun term_ord tu =
wenzelm@16537
   574
  if pointer_eq tu then EQUAL
wenzelm@16537
   575
  else
wenzelm@16537
   576
    (case tu of
wenzelm@16537
   577
      (Abs (_, T, t), Abs(_, U, u)) =>
wenzelm@16537
   578
        (case term_ord (t, u) of EQUAL => typ_ord (T, U) | ord => ord)
wenzelm@16667
   579
    | (t, u) =>
wenzelm@16537
   580
        (case int_ord (size_of_term t, size_of_term u) of
wenzelm@16537
   581
          EQUAL =>
wenzelm@16537
   582
            let
wenzelm@16667
   583
              val (f, m) = hd_depth (t, 0)
wenzelm@16667
   584
              and (g, n) = hd_depth (u, 0);
wenzelm@16667
   585
            in
wenzelm@16667
   586
              (case hd_ord (f, g) of EQUAL =>
wenzelm@16667
   587
                (case int_ord (m, n) of EQUAL => args_ord (t, u) | ord => ord)
wenzelm@16667
   588
              | ord => ord)
wenzelm@16667
   589
            end
wenzelm@16537
   590
        | ord => ord))
wenzelm@16537
   591
and hd_ord (f, g) =
wenzelm@16537
   592
  prod_ord (prod_ord indexname_ord typ_ord) int_ord (dest_hd f, dest_hd g)
wenzelm@16667
   593
and args_ord (f $ t, g $ u) =
wenzelm@16667
   594
      (case args_ord (f, g) of EQUAL => term_ord (t, u) | ord => ord)
wenzelm@16667
   595
  | args_ord _ = EQUAL;
wenzelm@16537
   596
wenzelm@16537
   597
fun termless tu = (term_ord tu = LESS);
wenzelm@16537
   598
wenzelm@16667
   599
end;
wenzelm@16667
   600
wenzelm@16667
   601
wenzelm@16667
   602
(** Lexicographic path order on terms **)
wenzelm@16667
   603
wenzelm@16667
   604
(*
nipkow@16570
   605
  See Baader & Nipkow, Term rewriting, CUP 1998.
nipkow@16570
   606
  Without variables.  Const, Var, Bound, Free and Abs are treated all as
nipkow@16570
   607
  constants.
nipkow@16570
   608
nipkow@16570
   609
  f_ord maps strings to integers and serves two purposes:
nipkow@16570
   610
  - Predicate on constant symbols.  Those that are not recognised by f_ord
nipkow@16570
   611
    must be mapped to ~1.
nipkow@16570
   612
  - Order on the recognised symbols.  These must be mapped to distinct
nipkow@16570
   613
    integers >= 0.
nipkow@16570
   614
wenzelm@16667
   615
*)
nipkow@16570
   616
nipkow@16570
   617
local
wenzelm@16667
   618
fun dest_hd f_ord (Const (a, T)) =
nipkow@16570
   619
      let val ord = f_ord a in
nipkow@16570
   620
        if ord = ~1 then ((1, ((a, 0), T)), 0) else ((0, (("", ord), T)), 0)
nipkow@16570
   621
      end
nipkow@16570
   622
  | dest_hd _ (Free (a, T)) = ((1, ((a, 0), T)), 0)
nipkow@16570
   623
  | dest_hd _ (Var v) = ((1, v), 1)
nipkow@16570
   624
  | dest_hd _ (Bound i) = ((1, (("", i), dummyT)), 2)
nipkow@16570
   625
  | dest_hd _ (Abs (_, T, _)) = ((1, (("", 0), T)), 3);
nipkow@16570
   626
nipkow@16570
   627
fun term_lpo f_ord (s, t) =
nipkow@16570
   628
  let val (f, ss) = strip_comb s and (g, ts) = strip_comb t in
nipkow@16570
   629
    if forall (fn si => term_lpo f_ord (si, t) = LESS) ss
nipkow@16570
   630
    then case hd_ord f_ord (f, g) of
wenzelm@16667
   631
        GREATER =>
wenzelm@16667
   632
          if forall (fn ti => term_lpo f_ord (s, ti) = GREATER) ts
wenzelm@16667
   633
          then GREATER else LESS
nipkow@16570
   634
      | EQUAL =>
wenzelm@16667
   635
          if forall (fn ti => term_lpo f_ord (s, ti) = GREATER) ts
wenzelm@16667
   636
          then list_ord (term_lpo f_ord) (ss, ts)
wenzelm@16667
   637
          else LESS
nipkow@16570
   638
      | LESS => LESS
nipkow@16570
   639
    else GREATER
nipkow@16570
   640
  end
nipkow@16570
   641
and hd_ord f_ord (f, g) = case (f, g) of
nipkow@16570
   642
    (Abs (_, T, t), Abs (_, U, u)) =>
nipkow@16570
   643
      (case term_lpo f_ord (t, u) of EQUAL => typ_ord (T, U) | ord => ord)
nipkow@16570
   644
  | (_, _) => prod_ord (prod_ord int_ord
nipkow@16570
   645
                  (prod_ord indexname_ord typ_ord)) int_ord
nipkow@16570
   646
                (dest_hd f_ord f, dest_hd f_ord g)
nipkow@16570
   647
in
nipkow@16570
   648
val term_lpo = term_lpo
nipkow@16570
   649
end;
nipkow@16570
   650
wenzelm@16537
   651
clasohm@0
   652
(** Connectives of higher order logic **)
clasohm@0
   653
wenzelm@375
   654
fun itselfT ty = Type ("itself", [ty]);
wenzelm@14854
   655
val a_itselfT = itselfT (TFree ("'a", []));
wenzelm@375
   656
clasohm@0
   657
val propT : typ = Type("prop",[]);
clasohm@0
   658
clasohm@0
   659
val implies = Const("==>", propT-->propT-->propT);
clasohm@0
   660
clasohm@0
   661
fun all T = Const("all", (T-->propT)-->propT);
clasohm@0
   662
clasohm@0
   663
fun equals T = Const("==", T-->T-->propT);
clasohm@0
   664
clasohm@0
   665
(* maps  !!x1...xn. t   to   t  *)
wenzelm@13000
   666
fun strip_all_body (Const("all",_)$Abs(_,_,t))  =  strip_all_body t
clasohm@0
   667
  | strip_all_body t  =  t;
clasohm@0
   668
clasohm@0
   669
(* maps  !!x1...xn. t   to   [x1, ..., xn]  *)
clasohm@0
   670
fun strip_all_vars (Const("all",_)$Abs(a,T,t))  =
wenzelm@13000
   671
                (a,T) :: strip_all_vars t
clasohm@0
   672
  | strip_all_vars t  =  [] : (string*typ) list;
clasohm@0
   673
clasohm@0
   674
(*increments a term's non-local bound variables
clasohm@0
   675
  required when moving a term within abstractions
clasohm@0
   676
     inc is  increment for bound variables
clasohm@0
   677
     lev is  level at which a bound variable is considered 'loose'*)
wenzelm@13000
   678
fun incr_bv (inc, lev, u as Bound i) = if i>=lev then Bound(i+inc) else u
clasohm@0
   679
  | incr_bv (inc, lev, Abs(a,T,body)) =
wenzelm@9536
   680
        Abs(a, T, incr_bv(inc,lev+1,body))
wenzelm@13000
   681
  | incr_bv (inc, lev, f$t) =
clasohm@0
   682
      incr_bv(inc,lev,f) $ incr_bv(inc,lev,t)
clasohm@0
   683
  | incr_bv (inc, lev, u) = u;
clasohm@0
   684
clasohm@0
   685
fun incr_boundvars  0  t = t
clasohm@0
   686
  | incr_boundvars inc t = incr_bv(inc,0,t);
clasohm@0
   687
wenzelm@12981
   688
(*Scan a pair of terms; while they are similar,
wenzelm@12981
   689
  accumulate corresponding bound vars in "al"*)
wenzelm@12981
   690
fun match_bvs(Abs(x,_,s),Abs(y,_,t), al) =
wenzelm@12981
   691
      match_bvs(s, t, if x="" orelse y="" then al
wenzelm@12981
   692
                                          else (x,y)::al)
wenzelm@12981
   693
  | match_bvs(f$s, g$t, al) = match_bvs(f,g,match_bvs(s,t,al))
wenzelm@12981
   694
  | match_bvs(_,_,al) = al;
wenzelm@12981
   695
wenzelm@12981
   696
(* strip abstractions created by parameters *)
wenzelm@12981
   697
fun match_bvars((s,t),al) = match_bvs(strip_abs_body s, strip_abs_body t, al);
wenzelm@12981
   698
wenzelm@12981
   699
fun rename_abs pat obj t =
wenzelm@12981
   700
  let
wenzelm@12981
   701
    val ren = match_bvs (pat, obj, []);
wenzelm@12981
   702
    fun ren_abs (Abs (x, T, b)) =
wenzelm@16678
   703
          Abs (if_none (assoc_string (ren, x)) x, T, ren_abs b)
wenzelm@12981
   704
      | ren_abs (f $ t) = ren_abs f $ ren_abs t
wenzelm@12981
   705
      | ren_abs t = t
skalberg@15531
   706
  in if null ren then NONE else SOME (ren_abs t) end;
clasohm@0
   707
clasohm@0
   708
(*Accumulate all 'loose' bound vars referring to level 'lev' or beyond.
clasohm@0
   709
   (Bound 0) is loose at level 0 *)
wenzelm@13000
   710
fun add_loose_bnos (Bound i, lev, js) =
wenzelm@9536
   711
        if i<lev then js  else  (i-lev) ins_int js
clasohm@0
   712
  | add_loose_bnos (Abs (_,_,t), lev, js) = add_loose_bnos (t, lev+1, js)
clasohm@0
   713
  | add_loose_bnos (f$t, lev, js) =
wenzelm@13000
   714
        add_loose_bnos (f, lev, add_loose_bnos (t, lev, js))
clasohm@0
   715
  | add_loose_bnos (_, _, js) = js;
clasohm@0
   716
clasohm@0
   717
fun loose_bnos t = add_loose_bnos (t, 0, []);
clasohm@0
   718
clasohm@0
   719
(* loose_bvar(t,k) iff t contains a 'loose' bound variable referring to
clasohm@0
   720
   level k or beyond. *)
clasohm@0
   721
fun loose_bvar(Bound i,k) = i >= k
clasohm@0
   722
  | loose_bvar(f$t, k) = loose_bvar(f,k) orelse loose_bvar(t,k)
clasohm@0
   723
  | loose_bvar(Abs(_,_,t),k) = loose_bvar(t,k+1)
clasohm@0
   724
  | loose_bvar _ = false;
clasohm@0
   725
nipkow@2792
   726
fun loose_bvar1(Bound i,k) = i = k
nipkow@2792
   727
  | loose_bvar1(f$t, k) = loose_bvar1(f,k) orelse loose_bvar1(t,k)
nipkow@2792
   728
  | loose_bvar1(Abs(_,_,t),k) = loose_bvar1(t,k+1)
nipkow@2792
   729
  | loose_bvar1 _ = false;
clasohm@0
   730
clasohm@0
   731
(*Substitute arguments for loose bound variables.
clasohm@0
   732
  Beta-reduction of arg(n-1)...arg0 into t replacing (Bound i) with (argi).
wenzelm@4626
   733
  Note that for ((%x y. c) a b), the bound vars in c are x=1 and y=0
wenzelm@9536
   734
        and the appropriate call is  subst_bounds([b,a], c) .
clasohm@0
   735
  Loose bound variables >=n are reduced by "n" to
clasohm@0
   736
     compensate for the disappearance of lambdas.
clasohm@0
   737
*)
wenzelm@13000
   738
fun subst_bounds (args: term list, t) : term =
clasohm@0
   739
  let val n = length args;
clasohm@0
   740
      fun subst (t as Bound i, lev) =
wenzelm@9536
   741
           (if i<lev then  t    (*var is locally bound*)
wenzelm@9536
   742
            else  incr_boundvars lev (List.nth(args, i-lev))
wenzelm@9536
   743
                    handle Subscript => Bound(i-n)  (*loose: change it*))
wenzelm@9536
   744
        | subst (Abs(a,T,body), lev) = Abs(a, T,  subst(body,lev+1))
wenzelm@9536
   745
        | subst (f$t, lev) =  subst(f,lev)  $  subst(t,lev)
wenzelm@9536
   746
        | subst (t,lev) = t
clasohm@0
   747
  in   case args of [] => t  | _ => subst (t,0)  end;
clasohm@0
   748
paulson@2192
   749
(*Special case: one argument*)
wenzelm@13000
   750
fun subst_bound (arg, t) : term =
paulson@2192
   751
  let fun subst (t as Bound i, lev) =
wenzelm@9536
   752
            if i<lev then  t    (*var is locally bound*)
wenzelm@9536
   753
            else  if i=lev then incr_boundvars lev arg
wenzelm@9536
   754
                           else Bound(i-1)  (*loose: change it*)
wenzelm@9536
   755
        | subst (Abs(a,T,body), lev) = Abs(a, T,  subst(body,lev+1))
wenzelm@9536
   756
        | subst (f$t, lev) =  subst(f,lev)  $  subst(t,lev)
wenzelm@9536
   757
        | subst (t,lev) = t
paulson@2192
   758
  in  subst (t,0)  end;
paulson@2192
   759
clasohm@0
   760
(*beta-reduce if possible, else form application*)
paulson@2192
   761
fun betapply (Abs(_,_,t), u) = subst_bound (u,t)
clasohm@0
   762
  | betapply (f,u) = f$u;
clasohm@0
   763
wenzelm@14786
   764
paulson@2192
   765
(** Equality, membership and insertion of indexnames (string*ints) **)
paulson@2192
   766
paulson@2192
   767
(*optimized equality test for indexnames.  Yields a huge gain under Poly/ML*)
wenzelm@2959
   768
fun eq_ix ((a, i):indexname, (a',i'):indexname) = (a=a') andalso i=i';
paulson@2192
   769
paulson@2192
   770
(*membership in a list, optimized version for indexnames*)
wenzelm@2959
   771
fun mem_ix (_, []) = false
paulson@2192
   772
  | mem_ix (x, y :: ys) = eq_ix(x,y) orelse mem_ix (x, ys);
paulson@2192
   773
paulson@2192
   774
(*insertion into list, optimized version for indexnames*)
paulson@2192
   775
fun ins_ix (x,xs) = if mem_ix (x, xs) then xs else x :: xs;
paulson@2192
   776
clasohm@0
   777
paulson@2176
   778
(** Membership, insertion, union for terms **)
paulson@2176
   779
paulson@2176
   780
fun mem_term (_, []) = false
paulson@2176
   781
  | mem_term (t, t'::ts) = t aconv t' orelse mem_term(t,ts);
paulson@2176
   782
paulson@2182
   783
fun subset_term ([], ys) = true
paulson@2182
   784
  | subset_term (x :: xs, ys) = mem_term (x, ys) andalso subset_term(xs, ys);
paulson@2182
   785
paulson@2182
   786
fun eq_set_term (xs, ys) =
paulson@2182
   787
    xs = ys orelse (subset_term (xs, ys) andalso subset_term (ys, xs));
paulson@2182
   788
paulson@2176
   789
fun ins_term(t,ts) = if mem_term(t,ts) then ts else t :: ts;
paulson@2176
   790
paulson@2176
   791
fun union_term (xs, []) = xs
paulson@2176
   792
  | union_term ([], ys) = ys
paulson@2176
   793
  | union_term ((x :: xs), ys) = union_term (xs, ins_term (x, ys));
paulson@2176
   794
paulson@5585
   795
fun inter_term ([], ys) = []
paulson@5585
   796
  | inter_term (x::xs, ys) =
paulson@5585
   797
      if mem_term (x,ys) then x :: inter_term(xs,ys) else inter_term(xs,ys);
paulson@5585
   798
wenzelm@13000
   799
(*A fast unification filter: true unless the two terms cannot be unified.
clasohm@0
   800
  Terms must be NORMAL.  Treats all Vars as distinct. *)
clasohm@0
   801
fun could_unify (t,u) =
clasohm@0
   802
  let fun matchrands (f$t, g$u) = could_unify(t,u) andalso  matchrands(f,g)
wenzelm@9536
   803
        | matchrands _ = true
clasohm@0
   804
  in case (head_of t , head_of u) of
wenzelm@9536
   805
        (_, Var _) => true
clasohm@0
   806
      | (Var _, _) => true
clasohm@0
   807
      | (Const(a,_), Const(b,_)) =>  a=b andalso matchrands(t,u)
clasohm@0
   808
      | (Free(a,_), Free(b,_)) =>  a=b andalso matchrands(t,u)
clasohm@0
   809
      | (Bound i, Bound j) =>  i=j andalso matchrands(t,u)
clasohm@0
   810
      | (Abs _, _) =>  true   (*because of possible eta equality*)
clasohm@0
   811
      | (_, Abs _) =>  true
clasohm@0
   812
      | _ => false
clasohm@0
   813
  end;
clasohm@0
   814
clasohm@0
   815
(*Substitute new for free occurrences of old in a term*)
clasohm@0
   816
fun subst_free [] = (fn t=>t)
clasohm@0
   817
  | subst_free pairs =
wenzelm@13000
   818
      let fun substf u =
wenzelm@9536
   819
            case gen_assoc (op aconv) (pairs, u) of
skalberg@15531
   820
                SOME u' => u'
skalberg@15531
   821
              | NONE => (case u of Abs(a,T,t) => Abs(a, T, substf t)
wenzelm@9536
   822
                                 | t$u' => substf t $ substf u'
wenzelm@9536
   823
                                 | _ => u)
clasohm@0
   824
      in  substf  end;
clasohm@0
   825
clasohm@0
   826
(*a total, irreflexive ordering on index names*)
clasohm@0
   827
fun xless ((a,i), (b,j): indexname) = i<j  orelse  (i=j andalso a<b);
clasohm@0
   828
clasohm@0
   829
wenzelm@13000
   830
(*Abstraction of the term "body" over its occurrences of v,
clasohm@0
   831
    which must contain no loose bound variables.
clasohm@0
   832
  The resulting term is ready to become the body of an Abs.*)
clasohm@0
   833
fun abstract_over (v,body) =
clasohm@0
   834
  let fun abst (lev,u) = if (v aconv u) then (Bound lev) else
clasohm@0
   835
      (case u of
clasohm@0
   836
          Abs(a,T,t) => Abs(a, T, abst(lev+1, t))
wenzelm@9536
   837
        | f$rand => abst(lev,f) $ abst(lev,rand)
wenzelm@9536
   838
        | _ => u)
clasohm@0
   839
  in  abst(0,body)  end;
clasohm@0
   840
berghofe@13665
   841
fun lambda (v as Free (x, T)) t = Abs (x, T, abstract_over (v, t))
berghofe@13665
   842
  | lambda (v as Var ((x, _), T)) t = Abs (x, T, abstract_over (v, t))
berghofe@13665
   843
  | lambda v t = raise TERM ("lambda", [v, t]);
clasohm@0
   844
clasohm@0
   845
(*Form an abstraction over a free variable.*)
clasohm@0
   846
fun absfree (a,T,body) = Abs(a, T, abstract_over (Free(a,T), body));
clasohm@0
   847
clasohm@0
   848
(*Abstraction over a list of free variables*)
clasohm@0
   849
fun list_abs_free ([ ] ,     t) = t
wenzelm@13000
   850
  | list_abs_free ((a,T)::vars, t) =
clasohm@0
   851
      absfree(a, T, list_abs_free(vars,t));
clasohm@0
   852
clasohm@0
   853
(*Quantification over a list of free variables*)
clasohm@0
   854
fun list_all_free ([], t: term) = t
wenzelm@13000
   855
  | list_all_free ((a,T)::vars, t) =
clasohm@0
   856
        (all T) $ (absfree(a, T, list_all_free(vars,t)));
clasohm@0
   857
clasohm@0
   858
(*Quantification over a list of variables (already bound in body) *)
clasohm@0
   859
fun list_all ([], t) = t
wenzelm@13000
   860
  | list_all ((a,T)::vars, t) =
clasohm@0
   861
        (all T) $ (Abs(a, T, list_all(vars,t)));
clasohm@0
   862
wenzelm@16678
   863
(*Replace the ATOMIC term ti by ui;    inst = [(t1,u1), ..., (tn,un)].
clasohm@0
   864
  A simultaneous substitution:  [ (a,b), (b,a) ] swaps a and b.  *)
wenzelm@16678
   865
fun subst_atomic [] tm = tm
wenzelm@16678
   866
  | subst_atomic inst tm =
wenzelm@16678
   867
      let
wenzelm@16678
   868
        fun subst (Abs (a, T, body)) = Abs (a, T, subst body)
wenzelm@16678
   869
          | subst (t $ u) = subst t $ subst u
wenzelm@16678
   870
          | subst t = if_none (gen_assoc (op aconv) (inst, t)) t;
wenzelm@16678
   871
      in subst tm end;
clasohm@0
   872
wenzelm@16678
   873
(*Replace the ATOMIC type Ti by Ui;    inst = [(T1,U1), ..., (Tn,Un)].*)
wenzelm@16678
   874
fun typ_subst_atomic [] ty = ty
wenzelm@16678
   875
  | typ_subst_atomic inst ty =
wenzelm@16678
   876
      let
wenzelm@16678
   877
        fun subst (Type (a, Ts)) = Type (a, map subst Ts)
wenzelm@16678
   878
          | subst T = if_none (gen_assoc (op = : typ * typ -> bool) (inst, T)) T;
wenzelm@16678
   879
      in subst ty end;
berghofe@15797
   880
wenzelm@16678
   881
fun subst_atomic_types [] tm = tm
wenzelm@16678
   882
  | subst_atomic_types inst tm = map_term_types (typ_subst_atomic inst) tm;
wenzelm@16678
   883
wenzelm@16678
   884
fun typ_subst_TVars [] ty = ty
wenzelm@16678
   885
  | typ_subst_TVars inst ty =
wenzelm@16678
   886
      let
wenzelm@16678
   887
        fun subst (Type (a, Ts)) = Type (a, map subst Ts)
wenzelm@16678
   888
          | subst (T as TVar (xi, _)) = if_none (assoc_string_int (inst, xi)) T
wenzelm@16678
   889
          | subst T = T;
wenzelm@16678
   890
      in subst ty end;
clasohm@0
   891
wenzelm@16678
   892
fun subst_TVars [] tm = tm
wenzelm@16678
   893
  | subst_TVars inst tm = map_term_types (typ_subst_TVars inst) tm;
clasohm@0
   894
wenzelm@16678
   895
(*see also Envir.norm_term*)
wenzelm@16678
   896
fun subst_Vars [] tm = tm
wenzelm@16678
   897
  | subst_Vars inst tm =
wenzelm@16678
   898
      let
wenzelm@16678
   899
        fun subst (t as Var (xi, _)) = if_none (assoc_string_int (inst, xi)) t
wenzelm@16678
   900
          | subst (Abs (a, T, t)) = Abs (a, T, subst t)
wenzelm@16678
   901
          | subst (t $ u) = subst t $ subst u
wenzelm@16678
   902
          | subst t = t;
wenzelm@16678
   903
      in subst tm end;
clasohm@0
   904
wenzelm@16678
   905
(*see also Envir.norm_term*)
wenzelm@16678
   906
fun subst_vars ([], []) tm = tm
wenzelm@16678
   907
  | subst_vars ([], inst) tm = subst_Vars inst tm
wenzelm@16678
   908
  | subst_vars (instT, inst) tm =
wenzelm@16678
   909
      let
wenzelm@16678
   910
        fun subst (Const (a, T)) = Const (a, typ_subst_TVars instT T)
wenzelm@16678
   911
          | subst (Free (a, T)) = Free (a, typ_subst_TVars instT T)
wenzelm@16678
   912
          | subst (t as Var (xi, T)) =
wenzelm@16678
   913
              (case assoc_string_int (inst, xi) of
wenzelm@16678
   914
                NONE => Var (xi, typ_subst_TVars instT T)
wenzelm@16678
   915
              | SOME t => t)
wenzelm@16678
   916
          | subst (t as Bound _) = t
wenzelm@16678
   917
          | subst (Abs (a, T, t)) = Abs (a, typ_subst_TVars instT T, subst t)
wenzelm@16678
   918
          | subst (t $ u) = subst t $ subst u;
wenzelm@16678
   919
      in subst tm end;
clasohm@0
   920
clasohm@0
   921
paulson@15573
   922
(** Identifying first-order terms **)
paulson@15573
   923
paulson@15573
   924
(*Argument Ts is a reverse list of binder types, needed if term t contains Bound vars*)
paulson@15573
   925
fun has_not_funtype Ts t = not (is_funtype (fastype_of1 (Ts,t)));
paulson@15573
   926
wenzelm@16537
   927
(*First order means in all terms of the form f(t1,...,tn) no argument has a
paulson@16589
   928
  function type. The supplied quantifiers are excluded: their argument always
paulson@16589
   929
  has a function type through a recursive call into its body.*)
wenzelm@16667
   930
fun is_first_order quants =
paulson@16589
   931
  let fun first_order1 Ts (Abs (_,T,body)) = first_order1 (T::Ts) body
wenzelm@16667
   932
        | first_order1 Ts (Const(q,_) $ Abs(a,T,body)) =
wenzelm@16667
   933
            q mem_string quants  andalso   (*it is a known quantifier*)
paulson@16589
   934
            not (is_funtype T)   andalso first_order1 (T::Ts) body
wenzelm@16667
   935
        | first_order1 Ts t =
wenzelm@16667
   936
            case strip_comb t of
wenzelm@16667
   937
                 (Var _, ts) => forall (first_order1 Ts andf has_not_funtype Ts) ts
wenzelm@16667
   938
               | (Free _, ts) => forall (first_order1 Ts andf has_not_funtype Ts) ts
wenzelm@16667
   939
               | (Const _, ts) => forall (first_order1 Ts andf has_not_funtype Ts) ts
wenzelm@16667
   940
               | (Bound _, ts) => forall (first_order1 Ts andf has_not_funtype Ts) ts
wenzelm@16667
   941
               | (Abs _, ts) => false (*not in beta-normal form*)
wenzelm@16667
   942
               | _ => error "first_order: unexpected case"
paulson@16589
   943
    in  first_order1 []  end;
paulson@15573
   944
clasohm@0
   945
(*Computing the maximum index of a typ*)
paulson@2146
   946
fun maxidx_of_typ(Type(_,Ts)) = maxidx_of_typs Ts
clasohm@0
   947
  | maxidx_of_typ(TFree _) = ~1
paulson@2146
   948
  | maxidx_of_typ(TVar((_,i),_)) = i
paulson@2146
   949
and maxidx_of_typs [] = ~1
paulson@2146
   950
  | maxidx_of_typs (T::Ts) = Int.max(maxidx_of_typ T, maxidx_of_typs Ts);
clasohm@0
   951
clasohm@0
   952
clasohm@0
   953
(*Computing the maximum index of a term*)
clasohm@0
   954
fun maxidx_of_term (Const(_,T)) = maxidx_of_typ T
clasohm@0
   955
  | maxidx_of_term (Bound _) = ~1
clasohm@0
   956
  | maxidx_of_term (Free(_,T)) = maxidx_of_typ T
paulson@2146
   957
  | maxidx_of_term (Var ((_,i), T)) = Int.max(i, maxidx_of_typ T)
paulson@2146
   958
  | maxidx_of_term (Abs (_,T,u)) = Int.max(maxidx_of_term u, maxidx_of_typ T)
paulson@2146
   959
  | maxidx_of_term (f$t) = Int.max(maxidx_of_term f,  maxidx_of_term t);
clasohm@0
   960
skalberg@15570
   961
fun maxidx_of_terms ts = Library.foldl Int.max (~1, map maxidx_of_term ts);
berghofe@13665
   962
clasohm@0
   963
clasohm@0
   964
(* Increment the index of all Poly's in T by k *)
wenzelm@16678
   965
fun incr_tvar 0 T = T
wenzelm@16678
   966
  | incr_tvar k T = map_type_tvar (fn ((a,i),S) => TVar((a,i+k),S)) T;
clasohm@0
   967
clasohm@0
   968
clasohm@0
   969
(**** Syntax-related declarations ****)
clasohm@0
   970
clasohm@0
   971
(*** Printing ***)
clasohm@0
   972
wenzelm@14676
   973
(*Makes a variant of a name distinct from the names in bs.
wenzelm@14676
   974
  First attaches the suffix and then increments this;
wenzelm@12306
   975
  preserves a suffix of underscores "_". *)
wenzelm@12306
   976
fun variant bs name =
wenzelm@12306
   977
  let
wenzelm@12306
   978
    val (c, u) = pairself implode (Library.take_suffix (equal "_") (Symbol.explode name));
wenzelm@12902
   979
    fun vary2 c = if ((c ^ u) mem_string bs) then vary2 (Symbol.bump_string c) else c;
wenzelm@14676
   980
    fun vary1 c = if ((c ^ u) mem_string bs) then vary2 (Symbol.bump_init c) else c;
wenzelm@12306
   981
  in vary1 (if c = "" then "u" else c) ^ u end;
clasohm@0
   982
clasohm@0
   983
(*Create variants of the list of names, with priority to the first ones*)
clasohm@0
   984
fun variantlist ([], used) = []
wenzelm@13000
   985
  | variantlist(b::bs, used) =
clasohm@0
   986
      let val b' = variant used b
clasohm@0
   987
      in  b' :: variantlist (bs, b'::used)  end;
clasohm@0
   988
wenzelm@14695
   989
(*Invent fresh names*)
wenzelm@14695
   990
fun invent_names _ _ 0 = []
wenzelm@14695
   991
  | invent_names used a n =
wenzelm@14695
   992
      let val b = Symbol.bump_string a in
wenzelm@14695
   993
        if a mem_string used then invent_names used b n
wenzelm@14695
   994
        else a :: invent_names used b (n - 1)
wenzelm@14695
   995
      end;
wenzelm@11353
   996
wenzelm@16537
   997
wenzelm@4017
   998
(** Consts etc. **)
wenzelm@4017
   999
skalberg@15574
  1000
fun add_typ_classes (Type (_, Ts), cs) = foldr add_typ_classes cs Ts
wenzelm@4017
  1001
  | add_typ_classes (TFree (_, S), cs) = S union cs
wenzelm@4017
  1002
  | add_typ_classes (TVar (_, S), cs) = S union cs;
wenzelm@4017
  1003
wenzelm@16294
  1004
fun add_typ_tycons (Type (c, Ts), cs) = foldr add_typ_tycons (c ins_string cs) Ts
wenzelm@4017
  1005
  | add_typ_tycons (_, cs) = cs;
wenzelm@4017
  1006
wenzelm@4017
  1007
val add_term_classes = it_term_types add_typ_classes;
wenzelm@4017
  1008
val add_term_tycons = it_term_types add_typ_tycons;
wenzelm@4017
  1009
wenzelm@9319
  1010
fun add_term_consts (Const (c, _), cs) = c ins_string cs
wenzelm@4017
  1011
  | add_term_consts (t $ u, cs) = add_term_consts (t, add_term_consts (u, cs))
wenzelm@4017
  1012
  | add_term_consts (Abs (_, _, t), cs) = add_term_consts (t, cs)
wenzelm@4017
  1013
  | add_term_consts (_, cs) = cs;
wenzelm@4017
  1014
obua@16108
  1015
fun add_term_constsT (Const c, cs) = c::cs
obua@16108
  1016
  | add_term_constsT (t $ u, cs) = add_term_constsT (t, add_term_constsT (u, cs))
obua@16108
  1017
  | add_term_constsT (Abs (_, _, t), cs) = add_term_constsT (t, cs)
obua@16108
  1018
  | add_term_constsT (_, cs) = cs;
obua@16108
  1019
nipkow@13646
  1020
fun term_consts t = add_term_consts(t,[]);
nipkow@13646
  1021
obua@16108
  1022
fun term_constsT t = add_term_constsT(t,[]);
obua@16108
  1023
oheimb@4185
  1024
fun exists_Const P t = let
wenzelm@9536
  1025
        fun ex (Const c      ) = P c
wenzelm@9536
  1026
        |   ex (t $ u        ) = ex t orelse ex u
wenzelm@9536
  1027
        |   ex (Abs (_, _, t)) = ex t
wenzelm@9536
  1028
        |   ex _               = false
oheimb@4185
  1029
    in ex t end;
wenzelm@4017
  1030
nipkow@4631
  1031
fun exists_subterm P =
nipkow@4631
  1032
  let fun ex t = P t orelse
nipkow@4631
  1033
                 (case t of
nipkow@4631
  1034
                    u $ v        => ex u orelse ex v
nipkow@4631
  1035
                  | Abs(_, _, u) => ex u
nipkow@4631
  1036
                  | _            => false)
nipkow@4631
  1037
  in ex end;
nipkow@4631
  1038
wenzelm@4017
  1039
(*map classes, tycons*)
wenzelm@4017
  1040
fun map_typ f g (Type (c, Ts)) = Type (g c, map (map_typ f g) Ts)
wenzelm@4017
  1041
  | map_typ f _ (TFree (x, S)) = TFree (x, map f S)
wenzelm@4017
  1042
  | map_typ f _ (TVar (xi, S)) = TVar (xi, map f S);
wenzelm@4017
  1043
wenzelm@4017
  1044
(*map classes, tycons, consts*)
wenzelm@4017
  1045
fun map_term f g h (Const (c, T)) = Const (h c, map_typ f g T)
wenzelm@4017
  1046
  | map_term f g _ (Free (x, T)) = Free (x, map_typ f g T)
wenzelm@4017
  1047
  | map_term f g _ (Var (xi, T)) = Var (xi, map_typ f g T)
wenzelm@4017
  1048
  | map_term _ _ _ (t as Bound _) = t
wenzelm@4017
  1049
  | map_term f g h (Abs (x, T, t)) = Abs (x, map_typ f g T, map_term f g h t)
wenzelm@4017
  1050
  | map_term f g h (t $ u) = map_term f g h t $ map_term f g h u;
wenzelm@4017
  1051
wenzelm@4017
  1052
clasohm@0
  1053
(** TFrees and TVars **)
clasohm@0
  1054
clasohm@0
  1055
(*maps  (bs,v)  to   v'::bs    this reverses the identifiers bs*)
clasohm@0
  1056
fun add_new_id (bs, c) : string list =  variant bs c  ::  bs;
clasohm@0
  1057
wenzelm@12802
  1058
(*Accumulates the names of Frees in the term, suppressing duplicates.*)
wenzelm@12802
  1059
fun add_term_free_names (Free(a,_), bs) = a ins_string bs
wenzelm@12802
  1060
  | add_term_free_names (f$u, bs) = add_term_free_names (f, add_term_free_names(u, bs))
wenzelm@12802
  1061
  | add_term_free_names (Abs(_,_,t), bs) = add_term_free_names(t,bs)
wenzelm@12802
  1062
  | add_term_free_names (_, bs) = bs;
wenzelm@12802
  1063
clasohm@0
  1064
(*Accumulates the names in the term, suppressing duplicates.
clasohm@0
  1065
  Includes Frees and Consts.  For choosing unambiguous bound var names.*)
wenzelm@10666
  1066
fun add_term_names (Const(a,_), bs) = NameSpace.base a ins_string bs
paulson@2176
  1067
  | add_term_names (Free(a,_), bs) = a ins_string bs
clasohm@0
  1068
  | add_term_names (f$u, bs) = add_term_names (f, add_term_names(u, bs))
clasohm@0
  1069
  | add_term_names (Abs(_,_,t), bs) = add_term_names(t,bs)
clasohm@0
  1070
  | add_term_names (_, bs) = bs;
clasohm@0
  1071
clasohm@0
  1072
(*Accumulates the TVars in a type, suppressing duplicates. *)
skalberg@15574
  1073
fun add_typ_tvars(Type(_,Ts),vs) = foldr add_typ_tvars vs Ts
clasohm@0
  1074
  | add_typ_tvars(TFree(_),vs) = vs
wenzelm@16294
  1075
  | add_typ_tvars(TVar(v),vs) = insert (op =) v vs;
clasohm@0
  1076
clasohm@0
  1077
(*Accumulates the TFrees in a type, suppressing duplicates. *)
skalberg@15574
  1078
fun add_typ_tfree_names(Type(_,Ts),fs) = foldr add_typ_tfree_names fs Ts
paulson@2176
  1079
  | add_typ_tfree_names(TFree(f,_),fs) = f ins_string fs
clasohm@0
  1080
  | add_typ_tfree_names(TVar(_),fs) = fs;
clasohm@0
  1081
skalberg@15574
  1082
fun add_typ_tfrees(Type(_,Ts),fs) = foldr add_typ_tfrees fs Ts
wenzelm@16294
  1083
  | add_typ_tfrees(TFree(f),fs) = insert (op =) f fs
clasohm@0
  1084
  | add_typ_tfrees(TVar(_),fs) = fs;
clasohm@0
  1085
skalberg@15574
  1086
fun add_typ_varnames(Type(_,Ts),nms) = foldr add_typ_varnames nms Ts
paulson@2176
  1087
  | add_typ_varnames(TFree(nm,_),nms) = nm ins_string nms
paulson@2176
  1088
  | add_typ_varnames(TVar((nm,_),_),nms) = nm ins_string nms;
nipkow@949
  1089
clasohm@0
  1090
(*Accumulates the TVars in a term, suppressing duplicates. *)
clasohm@0
  1091
val add_term_tvars = it_term_types add_typ_tvars;
clasohm@0
  1092
clasohm@0
  1093
(*Accumulates the TFrees in a term, suppressing duplicates. *)
clasohm@0
  1094
val add_term_tfrees = it_term_types add_typ_tfrees;
clasohm@0
  1095
val add_term_tfree_names = it_term_types add_typ_tfree_names;
clasohm@0
  1096
nipkow@949
  1097
val add_term_tvarnames = it_term_types add_typ_varnames;
nipkow@949
  1098
clasohm@0
  1099
(*Non-list versions*)
clasohm@0
  1100
fun typ_tfrees T = add_typ_tfrees(T,[]);
clasohm@0
  1101
fun typ_tvars T = add_typ_tvars(T,[]);
clasohm@0
  1102
fun term_tfrees t = add_term_tfrees(t,[]);
clasohm@0
  1103
fun term_tvars t = add_term_tvars(t,[]);
clasohm@0
  1104
nipkow@949
  1105
(*special code to enforce left-to-right collection of TVar-indexnames*)
nipkow@949
  1106
skalberg@15570
  1107
fun add_typ_ixns(ixns,Type(_,Ts)) = Library.foldl add_typ_ixns (ixns,Ts)
wenzelm@13000
  1108
  | add_typ_ixns(ixns,TVar(ixn,_)) = if mem_ix (ixn, ixns) then ixns
wenzelm@9536
  1109
                                     else ixns@[ixn]
nipkow@949
  1110
  | add_typ_ixns(ixns,TFree(_)) = ixns;
nipkow@949
  1111
nipkow@949
  1112
fun add_term_tvar_ixns(Const(_,T),ixns) = add_typ_ixns(ixns,T)
nipkow@949
  1113
  | add_term_tvar_ixns(Free(_,T),ixns) = add_typ_ixns(ixns,T)
nipkow@949
  1114
  | add_term_tvar_ixns(Var(_,T),ixns) = add_typ_ixns(ixns,T)
nipkow@949
  1115
  | add_term_tvar_ixns(Bound _,ixns) = ixns
nipkow@949
  1116
  | add_term_tvar_ixns(Abs(_,T,t),ixns) =
nipkow@949
  1117
      add_term_tvar_ixns(t,add_typ_ixns(ixns,T))
nipkow@949
  1118
  | add_term_tvar_ixns(f$t,ixns) =
nipkow@949
  1119
      add_term_tvar_ixns(t,add_term_tvar_ixns(f,ixns));
nipkow@949
  1120
wenzelm@16537
  1121
clasohm@0
  1122
(** Frees and Vars **)
clasohm@0
  1123
clasohm@0
  1124
(*a partial ordering (not reflexive) for atomic terms*)
clasohm@0
  1125
fun atless (Const (a,_), Const (b,_))  =  a<b
clasohm@0
  1126
  | atless (Free (a,_), Free (b,_)) =  a<b
clasohm@0
  1127
  | atless (Var(v,_), Var(w,_))  =  xless(v,w)
clasohm@0
  1128
  | atless (Bound i, Bound j)  =   i<j
clasohm@0
  1129
  | atless _  =  false;
clasohm@0
  1130
clasohm@0
  1131
(*insert atomic term into partially sorted list, suppressing duplicates (?)*)
clasohm@0
  1132
fun insert_aterm (t,us) =
clasohm@0
  1133
  let fun inserta [] = [t]
wenzelm@13000
  1134
        | inserta (us as u::us') =
wenzelm@9536
  1135
              if atless(t,u) then t::us
wenzelm@9536
  1136
              else if t=u then us (*duplicate*)
wenzelm@9536
  1137
              else u :: inserta(us')
clasohm@0
  1138
  in  inserta us  end;
clasohm@0
  1139
clasohm@0
  1140
(*Accumulates the Vars in the term, suppressing duplicates*)
clasohm@0
  1141
fun add_term_vars (t, vars: term list) = case t of
clasohm@0
  1142
    Var   _ => insert_aterm(t,vars)
clasohm@0
  1143
  | Abs (_,_,body) => add_term_vars(body,vars)
clasohm@0
  1144
  | f$t =>  add_term_vars (f, add_term_vars(t, vars))
clasohm@0
  1145
  | _ => vars;
clasohm@0
  1146
clasohm@0
  1147
fun term_vars t = add_term_vars(t,[]);
clasohm@0
  1148
clasohm@0
  1149
(*Accumulates the Frees in the term, suppressing duplicates*)
clasohm@0
  1150
fun add_term_frees (t, frees: term list) = case t of
clasohm@0
  1151
    Free   _ => insert_aterm(t,frees)
clasohm@0
  1152
  | Abs (_,_,body) => add_term_frees(body,frees)
clasohm@0
  1153
  | f$t =>  add_term_frees (f, add_term_frees(t, frees))
clasohm@0
  1154
  | _ => frees;
clasohm@0
  1155
clasohm@0
  1156
fun term_frees t = add_term_frees(t,[]);
clasohm@0
  1157
clasohm@0
  1158
(*Given an abstraction over P, replaces the bound variable by a Free variable
wenzelm@16678
  1159
  having a unique name*)
clasohm@0
  1160
fun variant_abs (a,T,P) =
clasohm@0
  1161
  let val b = variant (add_term_names(P,[])) a
paulson@2192
  1162
  in  (b,  subst_bound (Free(b,T), P))  end;
clasohm@0
  1163
wenzelm@16678
  1164
fun dest_abs (x, T, body) =
wenzelm@16678
  1165
  let
wenzelm@16678
  1166
    fun name_clash (Free (y, _)) = (x = y)
wenzelm@16678
  1167
      | name_clash (t $ u) = name_clash t orelse name_clash u
wenzelm@16678
  1168
      | name_clash (Abs (_, _, t)) = name_clash t
wenzelm@16678
  1169
      | name_clash _ = false;
wenzelm@16678
  1170
  in
wenzelm@16678
  1171
    if name_clash body then
wenzelm@16678
  1172
      dest_abs (variant [x] x, T, body)    (*potentially slow, but rarely happens*)
wenzelm@16678
  1173
    else (x, subst_bound (Free (x, T), body))
wenzelm@16678
  1174
  end;
wenzelm@16678
  1175
clasohm@0
  1176
(* renames and reverses the strings in vars away from names *)
clasohm@0
  1177
fun rename_aTs names vars : (string*typ)list =
clasohm@0
  1178
  let fun rename_aT (vars,(a,T)) =
wenzelm@9536
  1179
                (variant (map #1 vars @ names) a, T) :: vars
skalberg@15570
  1180
  in Library.foldl rename_aT ([],vars) end;
clasohm@0
  1181
clasohm@0
  1182
fun rename_wrt_term t = rename_aTs (add_term_names(t,[]));
clasohm@1364
  1183
paulson@1417
  1184
wenzelm@4286
  1185
(* left-ro-right traversal *)
wenzelm@4286
  1186
wenzelm@4286
  1187
(*foldl atoms of type*)
skalberg@15570
  1188
fun foldl_atyps f (x, Type (_, Ts)) = Library.foldl (foldl_atyps f) (x, Ts)
wenzelm@4286
  1189
  | foldl_atyps f x_atom = f x_atom;
wenzelm@4286
  1190
wenzelm@4286
  1191
(*foldl atoms of term*)
wenzelm@4286
  1192
fun foldl_aterms f (x, t $ u) = foldl_aterms f (foldl_aterms f (x, t), u)
wenzelm@4286
  1193
  | foldl_aterms f (x, Abs (_, _, t)) = foldl_aterms f (x, t)
wenzelm@4286
  1194
  | foldl_aterms f x_atom = f x_atom;
wenzelm@4286
  1195
wenzelm@6548
  1196
fun foldl_map_aterms f (x, t $ u) =
wenzelm@6548
  1197
      let val (x', t') = foldl_map_aterms f (x, t); val (x'', u') = foldl_map_aterms f (x', u);
wenzelm@6548
  1198
      in (x'', t' $ u') end
wenzelm@6548
  1199
  | foldl_map_aterms f (x, Abs (a, T, t)) =
wenzelm@6548
  1200
      let val (x', t') = foldl_map_aterms f (x, t) in (x', Abs (a, T, t')) end
wenzelm@6548
  1201
  | foldl_map_aterms f x_atom = f x_atom;
wenzelm@6548
  1202
wenzelm@4286
  1203
(*foldl types of term*)
wenzelm@8609
  1204
fun foldl_term_types f (x, t as Const (_, T)) = f t (x, T)
wenzelm@8609
  1205
  | foldl_term_types f (x, t as Free (_, T)) = f t (x, T)
wenzelm@8609
  1206
  | foldl_term_types f (x, t as Var (_, T)) = f t (x, T)
wenzelm@8609
  1207
  | foldl_term_types f (x, Bound _) = x
wenzelm@8609
  1208
  | foldl_term_types f (x, t as Abs (_, T, b)) = foldl_term_types f (f t (x, T), b)
wenzelm@8609
  1209
  | foldl_term_types f (x, t $ u) = foldl_term_types f (foldl_term_types f (x, t), u);
wenzelm@8609
  1210
wenzelm@8609
  1211
fun foldl_types f = foldl_term_types (fn _ => f);
wenzelm@4286
  1212
wenzelm@12499
  1213
(*collect variables*)
wenzelm@16294
  1214
val add_tvarsT = foldl_atyps (fn (vs, TVar v) => insert (op =) v vs | (vs, _) => vs);
wenzelm@12499
  1215
val add_tvars = foldl_types add_tvarsT;
wenzelm@16294
  1216
val add_vars = foldl_aterms (fn (vs, Var v) => insert (op =) v vs | (vs, _) => vs);
wenzelm@16294
  1217
val add_frees = foldl_aterms (fn (vs, Free v) => insert (op =) v vs | (vs, _) => vs);
wenzelm@12499
  1218
wenzelm@15025
  1219
(*collect variable names*)
wenzelm@15025
  1220
val add_term_varnames = foldl_aterms (fn (xs, Var (x, _)) => ins_ix (x, xs) | (xs, _) => xs);
wenzelm@15025
  1221
fun term_varnames t = add_term_varnames ([], t);
wenzelm@4286
  1222
paulson@1417
  1223
wenzelm@4444
  1224
wenzelm@13000
  1225
(*** Compression of terms and types by sharing common subtrees.
wenzelm@13000
  1226
     Saves 50-75% on storage requirements.  As it is a bit slow,
wenzelm@13000
  1227
     it should be called only for axioms, stored theorems, etc.
wenzelm@13000
  1228
     Recorded term and type fragments are never disposed. ***)
paulson@1417
  1229
wenzelm@16338
  1230
paulson@1417
  1231
(** Sharing of types **)
paulson@1417
  1232
wenzelm@13000
  1233
val memo_types = ref (Typtab.empty: typ Typtab.table);
paulson@1417
  1234
paulson@1417
  1235
fun compress_type T =
wenzelm@13000
  1236
  (case Typtab.lookup (! memo_types, T) of
skalberg@15531
  1237
    SOME T' => T'
skalberg@15531
  1238
  | NONE =>
wenzelm@13000
  1239
      let val T' = (case T of Type (a, Ts) => Type (a, map compress_type Ts) | _ => T)
wenzelm@13000
  1240
      in memo_types := Typtab.update ((T', T'), ! memo_types); T' end);
wenzelm@13000
  1241
paulson@1417
  1242
paulson@1417
  1243
(** Sharing of atomic terms **)
paulson@1417
  1244
wenzelm@13000
  1245
val memo_terms = ref (Termtab.empty : term Termtab.table);
paulson@1417
  1246
paulson@1417
  1247
fun share_term (t $ u) = share_term t $ share_term u
wenzelm@13000
  1248
  | share_term (Abs (a, T, u)) = Abs (a, T, share_term u)
paulson@1417
  1249
  | share_term t =
wenzelm@13000
  1250
      (case Termtab.lookup (! memo_terms, t) of
skalberg@15531
  1251
        SOME t' => t'
skalberg@15531
  1252
      | NONE => (memo_terms := Termtab.update ((t, t), ! memo_terms); t));
paulson@1417
  1253
paulson@1417
  1254
val compress_term = share_term o map_term_types compress_type;
paulson@1417
  1255
wenzelm@4444
  1256
wenzelm@9536
  1257
(* dummy patterns *)
wenzelm@9536
  1258
wenzelm@9536
  1259
val dummy_patternN = "dummy_pattern";
wenzelm@9536
  1260
wenzelm@9536
  1261
fun is_dummy_pattern (Const ("dummy_pattern", _)) = true
wenzelm@9536
  1262
  | is_dummy_pattern _ = false;
wenzelm@9536
  1263
wenzelm@9536
  1264
fun no_dummy_patterns tm =
wenzelm@9536
  1265
  if not (foldl_aterms (fn (b, t) => b orelse is_dummy_pattern t) (false, tm)) then tm
wenzelm@9536
  1266
  else raise TERM ("Illegal occurrence of '_' dummy pattern", [tm]);
wenzelm@9536
  1267
wenzelm@11903
  1268
fun replace_dummy Ts (i, Const ("dummy_pattern", T)) =
wenzelm@11903
  1269
      (i + 1, list_comb (Var (("_dummy_", i), Ts ---> T), map Bound (0 upto length Ts - 1)))
wenzelm@11903
  1270
  | replace_dummy Ts (i, Abs (x, T, t)) =
wenzelm@11903
  1271
      let val (i', t') = replace_dummy (T :: Ts) (i, t)
wenzelm@11903
  1272
      in (i', Abs (x, T, t')) end
wenzelm@11903
  1273
  | replace_dummy Ts (i, t $ u) =
wenzelm@11903
  1274
      let val (i', t') = replace_dummy Ts (i, t); val (i'', u') = replace_dummy Ts (i', u)
wenzelm@11903
  1275
      in (i'', t' $ u') end
wenzelm@11903
  1276
  | replace_dummy _ (i, a) = (i, a);
wenzelm@11903
  1277
wenzelm@11903
  1278
val replace_dummy_patterns = replace_dummy [];
wenzelm@9536
  1279
wenzelm@10552
  1280
fun is_replaced_dummy_pattern ("_dummy_", _) = true
wenzelm@10552
  1281
  | is_replaced_dummy_pattern _ = false;
wenzelm@9536
  1282
wenzelm@16035
  1283
fun show_dummy_patterns (Var (("_dummy_", _), T)) = Const ("dummy_pattern", T)
wenzelm@16035
  1284
  | show_dummy_patterns (t $ u) = show_dummy_patterns t $ show_dummy_patterns u
wenzelm@16035
  1285
  | show_dummy_patterns (Abs (x, T, t)) = Abs (x, T, show_dummy_patterns t)
wenzelm@16035
  1286
  | show_dummy_patterns a = a;
wenzelm@16035
  1287
wenzelm@13484
  1288
wenzelm@13484
  1289
(* adhoc freezing *)
wenzelm@13484
  1290
wenzelm@13484
  1291
fun adhoc_freeze_vars tm =
wenzelm@13484
  1292
  let
wenzelm@13484
  1293
    fun mk_inst (var as Var ((a, i), T)) =
wenzelm@13484
  1294
      let val x = a ^ Library.gensym "_" ^ string_of_int i
wenzelm@13484
  1295
      in ((var,  Free(x, T)), x) end;
wenzelm@13484
  1296
    val (insts, xs) = split_list (map mk_inst (term_vars tm));
wenzelm@13484
  1297
  in (subst_atomic insts tm, xs) end;
wenzelm@13484
  1298
wenzelm@13484
  1299
wenzelm@14786
  1300
(* string_of_vname *)
wenzelm@14786
  1301
wenzelm@15986
  1302
val show_question_marks = ref true;
berghofe@15472
  1303
wenzelm@14786
  1304
fun string_of_vname (x, i) =
wenzelm@14786
  1305
  let
wenzelm@15986
  1306
    val question_mark = if ! show_question_marks then "?" else "";
wenzelm@15986
  1307
    val idx = string_of_int i;
wenzelm@15986
  1308
    val dot =
wenzelm@15986
  1309
      (case rev (Symbol.explode x) of
wenzelm@15986
  1310
        _ :: "\\<^isub>" :: _ => false
wenzelm@15986
  1311
      | _ :: "\\<^isup>" :: _ => false
wenzelm@15986
  1312
      | c :: _ => Symbol.is_digit c
wenzelm@15986
  1313
      | _ => true);
wenzelm@14786
  1314
  in
wenzelm@15986
  1315
    if dot then question_mark ^ x ^ "." ^ idx
wenzelm@15986
  1316
    else if i <> 0 then question_mark ^ x ^ idx
wenzelm@15986
  1317
    else question_mark ^ x
wenzelm@14786
  1318
  end;
wenzelm@14786
  1319
wenzelm@14786
  1320
fun string_of_vname' (x, ~1) = x
wenzelm@14786
  1321
  | string_of_vname' xi = string_of_vname xi;
wenzelm@14786
  1322
nipkow@15612
  1323
fun string_of_term (Const(s,_)) = s
nipkow@15612
  1324
  | string_of_term (Free(s,_)) = s
nipkow@15612
  1325
  | string_of_term (Var(ix,_)) = string_of_vname ix
nipkow@15612
  1326
  | string_of_term (Bound i) = string_of_int i
nipkow@15612
  1327
  | string_of_term (Abs(x,_,t)) = "%" ^ x ^ ". " ^ string_of_term t
nipkow@15612
  1328
  | string_of_term (s $ t) =
nipkow@15612
  1329
      "(" ^ string_of_term s ^ " " ^ string_of_term t ^ ")"
nipkow@15612
  1330
clasohm@1364
  1331
end;
clasohm@1364
  1332
wenzelm@4444
  1333
structure BasicTerm: BASIC_TERM = Term;
wenzelm@4444
  1334
open BasicTerm;