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