src/Pure/term.ML
author wenzelm
Wed Dec 31 15:30:10 2008 +0100 (2008-12-31)
changeset 29269 5c25a2012975
parent 29265 5b4247055bd7
child 29270 0eade173f77e
permissions -rw-r--r--
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
tuned signature of structure Term;
wenzelm@9536
     1
(*  Title:      Pure/term.ML
wenzelm@9536
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@1364
     3
wenzelm@4444
     4
Simply typed lambda-calculus: types, terms, and basic operations.
clasohm@0
     5
*)
clasohm@0
     6
wenzelm@29257
     7
infix 9 $;
clasohm@1364
     8
infixr 5 -->;
wenzelm@4444
     9
infixr --->;
wenzelm@4444
    10
infix aconv;
clasohm@1364
    11
wenzelm@4444
    12
signature BASIC_TERM =
wenzelm@4444
    13
sig
wenzelm@22572
    14
  eqtype indexname
wenzelm@22572
    15
  eqtype class
wenzelm@22572
    16
  eqtype sort
wenzelm@22572
    17
  eqtype arity
wenzelm@4444
    18
  datatype typ =
wenzelm@4444
    19
    Type  of string * typ list |
wenzelm@4444
    20
    TFree of string * sort |
wenzelm@4444
    21
    TVar  of indexname * sort
wenzelm@16537
    22
  datatype term =
wenzelm@16537
    23
    Const of string * typ |
wenzelm@16537
    24
    Free of string * typ |
wenzelm@16537
    25
    Var of indexname * typ |
wenzelm@16537
    26
    Bound of int |
wenzelm@16537
    27
    Abs of string * typ * term |
wenzelm@17756
    28
    $ of term * term
wenzelm@16537
    29
  exception TYPE of string * typ list * term list
wenzelm@16537
    30
  exception TERM of string * term list
wenzelm@21353
    31
  val dummyS: sort
wenzelm@16710
    32
  val dummyT: typ
wenzelm@16710
    33
  val no_dummyT: typ -> typ
wenzelm@4444
    34
  val --> : typ * typ -> typ
wenzelm@4444
    35
  val ---> : typ list * typ -> typ
wenzelm@16710
    36
  val dest_Type: typ -> string * typ list
wenzelm@16710
    37
  val dest_TVar: typ -> indexname * sort
wenzelm@16710
    38
  val dest_TFree: typ -> string * sort
wenzelm@16710
    39
  val is_Bound: term -> bool
wenzelm@16710
    40
  val is_Const: term -> bool
wenzelm@16710
    41
  val is_Free: term -> bool
wenzelm@16710
    42
  val is_Var: term -> bool
wenzelm@4444
    43
  val is_TVar: typ -> bool
wenzelm@16710
    44
  val dest_Const: term -> string * typ
wenzelm@16710
    45
  val dest_Free: term -> string * typ
wenzelm@16710
    46
  val dest_Var: term -> indexname * typ
wenzelm@4444
    47
  val domain_type: typ -> typ
wenzelm@4480
    48
  val range_type: typ -> typ
wenzelm@4444
    49
  val binder_types: typ -> typ list
wenzelm@4444
    50
  val body_type: typ -> typ
wenzelm@4444
    51
  val strip_type: typ -> typ list * typ
wenzelm@16710
    52
  val type_of1: typ list * term -> typ
wenzelm@4444
    53
  val type_of: term -> typ
wenzelm@16710
    54
  val fastype_of1: typ list * term -> typ
wenzelm@4444
    55
  val fastype_of: term -> typ
wenzelm@10806
    56
  val list_abs: (string * typ) list * term -> term
haftmann@18927
    57
  val strip_abs: term -> (string * typ) list * 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
haftmann@18847
    66
  val map_atyps: (typ -> typ) -> typ -> typ
haftmann@18847
    67
  val map_aterms: (term -> term) -> term -> term
wenzelm@4444
    68
  val map_type_tvar: (indexname * sort -> typ) -> typ -> typ
wenzelm@4444
    69
  val map_type_tfree: (string * sort -> typ) -> typ -> typ
wenzelm@20548
    70
  val map_types: (typ -> typ) -> term -> term
wenzelm@16943
    71
  val fold_atyps: (typ -> 'a -> 'a) -> typ -> 'a -> 'a
wenzelm@16943
    72
  val fold_aterms: (term -> 'a -> 'a) -> term -> 'a -> 'a
wenzelm@16943
    73
  val fold_term_types: (term -> typ -> 'a -> 'a) -> term -> 'a -> 'a
wenzelm@16943
    74
  val fold_types: (typ -> 'a -> 'a) -> term -> 'a -> 'a
wenzelm@24483
    75
  val burrow_types: (typ list -> typ list) -> term list -> term list
haftmann@18927
    76
  val it_term_types: (typ * 'a -> 'a) -> term * 'a -> 'a
wenzelm@16943
    77
  val add_term_names: term * string list -> string list
wenzelm@16710
    78
  val aconv: term * term -> bool
wenzelm@4444
    79
  val propT: typ
wenzelm@4444
    80
  val strip_all_body: term -> term
wenzelm@4444
    81
  val strip_all_vars: term -> (string * typ) list
wenzelm@4444
    82
  val incr_bv: int * int * term -> term
wenzelm@4444
    83
  val incr_boundvars: int -> term -> term
wenzelm@4444
    84
  val add_loose_bnos: term * int * int list -> int list
wenzelm@4444
    85
  val loose_bnos: term -> int list
wenzelm@4444
    86
  val loose_bvar: term * int -> bool
wenzelm@4444
    87
  val loose_bvar1: term * int -> bool
wenzelm@4444
    88
  val subst_bounds: term list * term -> term
wenzelm@4444
    89
  val subst_bound: term * term -> term
wenzelm@4444
    90
  val betapply: term * term -> term
wenzelm@18183
    91
  val betapplys: term * term list -> term
wenzelm@4444
    92
  val subst_free: (term * term) list -> term -> term
wenzelm@4444
    93
  val abstract_over: term * term -> term
wenzelm@11922
    94
  val lambda: term -> term -> term
wenzelm@4444
    95
  val absfree: string * typ * term -> term
wenzelm@17786
    96
  val absdummy: typ * term -> term
wenzelm@4444
    97
  val list_abs_free: (string * typ) list * term -> term
wenzelm@4444
    98
  val list_all_free: (string * typ) list * term -> term
wenzelm@4444
    99
  val list_all: (string * typ) list * term -> term
wenzelm@16710
   100
  val subst_atomic: (term * term) list -> term -> term
wenzelm@16710
   101
  val typ_subst_atomic: (typ * typ) list -> typ -> typ
wenzelm@16710
   102
  val subst_atomic_types: (typ * typ) list -> term -> term
wenzelm@16710
   103
  val typ_subst_TVars: (indexname * typ) list -> typ -> typ
wenzelm@16710
   104
  val subst_TVars: (indexname * typ) list -> term -> term
wenzelm@16710
   105
  val subst_Vars: (indexname * term) list -> term -> term
wenzelm@16710
   106
  val subst_vars: (indexname * typ) list * (indexname * term) list -> term -> term
wenzelm@16710
   107
  val is_first_order: string list -> term -> bool
wenzelm@4444
   108
  val maxidx_of_typ: typ -> int
wenzelm@4444
   109
  val maxidx_of_typs: typ list -> int
wenzelm@4444
   110
  val maxidx_of_term: term -> int
wenzelm@4444
   111
  val add_term_consts: term * string list -> string list
nipkow@13646
   112
  val term_consts: term -> string list
wenzelm@19909
   113
  val exists_subtype: (typ -> bool) -> typ -> bool
wenzelm@20531
   114
  val exists_type: (typ -> bool) -> term -> bool
wenzelm@16943
   115
  val exists_subterm: (term -> bool) -> term -> bool
wenzelm@16710
   116
  val exists_Const: (string * typ -> bool) -> term -> bool
wenzelm@16710
   117
  val add_term_free_names: term * string list -> string list
wenzelm@16710
   118
  val add_typ_tvars: typ * (indexname * sort) list -> (indexname * sort) list
wenzelm@16710
   119
  val add_typ_tfree_names: typ * string list -> string list
wenzelm@16710
   120
  val add_typ_tfrees: typ * (string * sort) list -> (string * sort) list
wenzelm@16710
   121
  val add_typ_varnames: typ * string list -> string list
wenzelm@16710
   122
  val add_term_tvars: term * (indexname * sort) list -> (indexname * sort) list
wenzelm@16710
   123
  val add_term_tfrees: term * (string * sort) list -> (string * sort) list
wenzelm@16710
   124
  val add_term_tfree_names: term * string list -> string list
wenzelm@16710
   125
  val typ_tfrees: typ -> (string * sort) list
wenzelm@16710
   126
  val typ_tvars: typ -> (indexname * sort) list
wenzelm@16710
   127
  val term_tfrees: term -> (string * sort) list
wenzelm@16710
   128
  val term_tvars: term -> (indexname * sort) list
wenzelm@20160
   129
  val rename_wrt_term: term -> (string * 'a) list -> (string * 'a) list
wenzelm@15986
   130
  val show_question_marks: bool ref
wenzelm@4444
   131
end;
clasohm@0
   132
wenzelm@4444
   133
signature TERM =
wenzelm@4444
   134
sig
wenzelm@4444
   135
  include BASIC_TERM
wenzelm@19394
   136
  val aT: sort -> typ
wenzelm@19394
   137
  val itselfT: typ -> typ
wenzelm@19394
   138
  val a_itselfT: typ
wenzelm@27335
   139
  val all: typ -> term
wenzelm@22908
   140
  val argument_type_of: term -> int -> typ
wenzelm@29257
   141
  val add_tvar_namesT: typ -> indexname list -> indexname list
wenzelm@29257
   142
  val add_tvar_names: term -> indexname list -> indexname list
wenzelm@16943
   143
  val add_tvarsT: typ -> (indexname * sort) list -> (indexname * sort) list
wenzelm@16943
   144
  val add_tvars: term -> (indexname * sort) list -> (indexname * sort) list
wenzelm@29257
   145
  val add_var_names: term -> indexname list -> indexname list
wenzelm@16943
   146
  val add_vars: term -> (indexname * typ) list -> (indexname * typ) list
wenzelm@29257
   147
  val add_tfree_namesT: typ -> string list -> string list
wenzelm@29257
   148
  val add_tfree_names: term -> string list -> string list
wenzelm@16943
   149
  val add_tfreesT: typ -> (string * sort) list -> (string * sort) list
wenzelm@16943
   150
  val add_tfrees: term -> (string * sort) list -> (string * sort) list
wenzelm@29257
   151
  val add_free_names: term -> string list -> string list
wenzelm@16943
   152
  val add_frees: term -> (string * typ) list -> (string * typ) list
wenzelm@25050
   153
  val hidden_polymorphism: term -> (indexname * sort) list
wenzelm@29269
   154
  val eq_ix: indexname * indexname -> bool
wenzelm@29269
   155
  val eq_tvar: (indexname * sort) * (indexname * sort) -> bool
wenzelm@29269
   156
  val eq_var: (indexname * typ) * (indexname * typ) -> bool
wenzelm@29269
   157
  val could_unify: term * term -> bool
haftmann@20109
   158
  val strip_abs_eta: int -> term -> (string * typ) list * term
wenzelm@12981
   159
  val match_bvars: (term * term) * (string * string) list -> (string * string) list
haftmann@22031
   160
  val map_abs_vars: (string -> string) -> term -> term
wenzelm@12981
   161
  val rename_abs: term -> term -> term -> term option
wenzelm@25050
   162
  val close_schematic_term: term -> term
wenzelm@16710
   163
  val maxidx_typ: typ -> int -> int
wenzelm@16710
   164
  val maxidx_typs: typ list -> int -> int
wenzelm@16710
   165
  val maxidx_term: term -> int -> int
wenzelm@24671
   166
  val has_abs: term -> bool
wenzelm@20239
   167
  val dest_abs: string * typ * term -> string * term
wenzelm@24762
   168
  val declare_typ_names: typ -> Name.context -> Name.context
wenzelm@20148
   169
  val declare_term_names: term -> Name.context -> Name.context
wenzelm@20160
   170
  val variant_frees: term -> (string * 'a) list -> (string * 'a) list
wenzelm@9536
   171
  val dummy_patternN: string
wenzelm@18253
   172
  val dummy_pattern: typ -> term
wenzelm@22723
   173
  val is_dummy_pattern: term -> bool
wenzelm@24733
   174
  val free_dummy_patterns: term -> Name.context -> term * Name.context
wenzelm@9536
   175
  val no_dummy_patterns: term -> term
wenzelm@24762
   176
  val replace_dummy_patterns: term -> int -> term * int
wenzelm@10552
   177
  val is_replaced_dummy_pattern: indexname -> bool
wenzelm@16035
   178
  val show_dummy_patterns: term -> term
wenzelm@14786
   179
  val string_of_vname: indexname -> string
wenzelm@14786
   180
  val string_of_vname': indexname -> string
wenzelm@4444
   181
end;
wenzelm@4444
   182
wenzelm@4444
   183
structure Term: TERM =
clasohm@1364
   184
struct
clasohm@0
   185
clasohm@0
   186
(*Indexnames can be quickly renamed by adding an offset to the integer part,
clasohm@0
   187
  for resolution.*)
wenzelm@16537
   188
type indexname = string * int;
clasohm@0
   189
wenzelm@4626
   190
(* Types are classified by sorts. *)
clasohm@0
   191
type class = string;
clasohm@0
   192
type sort  = class list;
wenzelm@14829
   193
type arity = string * sort list * sort;
clasohm@0
   194
clasohm@0
   195
(* The sorts attached to TFrees and TVars specify the sort of that variable *)
clasohm@0
   196
datatype typ = Type  of string * typ list
clasohm@0
   197
             | TFree of string * sort
wenzelm@9536
   198
             | TVar  of indexname * sort;
clasohm@0
   199
paulson@6033
   200
(*Terms.  Bound variables are indicated by depth number.
clasohm@0
   201
  Free variables, (scheme) variables and constants have names.
wenzelm@4626
   202
  An term is "closed" if every bound variable of level "lev"
wenzelm@13000
   203
  is enclosed by at least "lev" abstractions.
clasohm@0
   204
clasohm@0
   205
  It is possible to create meaningless terms containing loose bound vars
clasohm@0
   206
  or type mismatches.  But such terms are not allowed in rules. *)
clasohm@0
   207
wenzelm@13000
   208
datatype term =
clasohm@0
   209
    Const of string * typ
wenzelm@13000
   210
  | Free  of string * typ
clasohm@0
   211
  | Var   of indexname * typ
clasohm@0
   212
  | Bound of int
clasohm@0
   213
  | Abs   of string*typ*term
wenzelm@3965
   214
  | op $  of term*term;
clasohm@0
   215
wenzelm@16537
   216
(*Errors involving type mismatches*)
clasohm@0
   217
exception TYPE of string * typ list * term list;
clasohm@0
   218
wenzelm@16537
   219
(*Errors errors involving terms*)
clasohm@0
   220
exception TERM of string * term list;
clasohm@0
   221
clasohm@0
   222
(*Note variable naming conventions!
clasohm@0
   223
    a,b,c: string
clasohm@0
   224
    f,g,h: functions (including terms of function type)
clasohm@0
   225
    i,j,m,n: int
clasohm@0
   226
    t,u: term
clasohm@0
   227
    v,w: indexnames
clasohm@0
   228
    x,y: any
clasohm@0
   229
    A,B,C: term (denoting formulae)
clasohm@0
   230
    T,U: typ
clasohm@0
   231
*)
clasohm@0
   232
clasohm@0
   233
paulson@6033
   234
(** Types **)
paulson@6033
   235
wenzelm@21353
   236
(*dummies for type-inference etc.*)
wenzelm@21353
   237
val dummyS = [""];
wenzelm@16537
   238
val dummyT = Type ("dummy", []);
wenzelm@16537
   239
wenzelm@16537
   240
fun no_dummyT typ =
wenzelm@16537
   241
  let
wenzelm@16537
   242
    fun check (T as Type ("dummy", _)) =
wenzelm@16537
   243
          raise TYPE ("Illegal occurrence of '_' dummy type", [T], [])
wenzelm@16537
   244
      | check (Type (_, Ts)) = List.app check Ts
wenzelm@16537
   245
      | check _ = ();
wenzelm@16537
   246
  in check typ; typ end;
wenzelm@16537
   247
paulson@6033
   248
fun S --> T = Type("fun",[S,T]);
paulson@6033
   249
paulson@6033
   250
(*handy for multiple args: [T1,...,Tn]--->T  gives  T1-->(T2--> ... -->T)*)
skalberg@15570
   251
val op ---> = Library.foldr (op -->);
paulson@6033
   252
paulson@6033
   253
fun dest_Type (Type x) = x
paulson@6033
   254
  | dest_Type T = raise TYPE ("dest_Type", [T], []);
dixon@15914
   255
fun dest_TVar (TVar x) = x
dixon@15914
   256
  | dest_TVar T = raise TYPE ("dest_TVar", [T], []);
dixon@15914
   257
fun dest_TFree (TFree x) = x
dixon@15914
   258
  | dest_TFree T = raise TYPE ("dest_TFree", [T], []);
paulson@6033
   259
wenzelm@16537
   260
clasohm@0
   261
(** Discriminators **)
clasohm@0
   262
nipkow@7318
   263
fun is_Bound (Bound _) = true
nipkow@7318
   264
  | is_Bound _         = false;
nipkow@7318
   265
clasohm@0
   266
fun is_Const (Const _) = true
clasohm@0
   267
  | is_Const _ = false;
clasohm@0
   268
clasohm@0
   269
fun is_Free (Free _) = true
clasohm@0
   270
  | is_Free _ = false;
clasohm@0
   271
clasohm@0
   272
fun is_Var (Var _) = true
clasohm@0
   273
  | is_Var _ = false;
clasohm@0
   274
clasohm@0
   275
fun is_TVar (TVar _) = true
clasohm@0
   276
  | is_TVar _ = false;
clasohm@0
   277
wenzelm@16537
   278
clasohm@0
   279
(** Destructors **)
clasohm@0
   280
clasohm@0
   281
fun dest_Const (Const x) =  x
clasohm@0
   282
  | dest_Const t = raise TERM("dest_Const", [t]);
clasohm@0
   283
clasohm@0
   284
fun dest_Free (Free x) =  x
clasohm@0
   285
  | dest_Free t = raise TERM("dest_Free", [t]);
clasohm@0
   286
clasohm@0
   287
fun dest_Var (Var x) =  x
clasohm@0
   288
  | dest_Var t = raise TERM("dest_Var", [t]);
clasohm@0
   289
clasohm@0
   290
paulson@4464
   291
fun domain_type (Type("fun", [T,_])) = T
paulson@4464
   292
and range_type  (Type("fun", [_,T])) = T;
paulson@4064
   293
clasohm@0
   294
(* maps  [T1,...,Tn]--->T  to the list  [T1,T2,...,Tn]*)
clasohm@0
   295
fun binder_types (Type("fun",[S,T])) = S :: binder_types T
clasohm@0
   296
  | binder_types _   =  [];
clasohm@0
   297
clasohm@0
   298
(* maps  [T1,...,Tn]--->T  to T*)
clasohm@0
   299
fun body_type (Type("fun",[S,T])) = body_type T
clasohm@0
   300
  | body_type T   =  T;
clasohm@0
   301
clasohm@0
   302
(* maps  [T1,...,Tn]--->T  to   ([T1,T2,...,Tn], T)  *)
clasohm@0
   303
fun strip_type T : typ list * typ =
clasohm@0
   304
  (binder_types T, body_type T);
clasohm@0
   305
clasohm@0
   306
clasohm@0
   307
(*Compute the type of the term, checking that combinations are well-typed
clasohm@0
   308
  Ts = [T0,T1,...] holds types of bound variables 0, 1, ...*)
clasohm@0
   309
fun type_of1 (Ts, Const (_,T)) = T
clasohm@0
   310
  | type_of1 (Ts, Free  (_,T)) = T
skalberg@15570
   311
  | type_of1 (Ts, Bound i) = (List.nth (Ts,i)
skalberg@15570
   312
        handle Subscript => raise TYPE("type_of: bound variable", [], [Bound i]))
clasohm@0
   313
  | type_of1 (Ts, Var (_,T)) = T
clasohm@0
   314
  | type_of1 (Ts, Abs (_,T,body)) = T --> type_of1(T::Ts, body)
wenzelm@13000
   315
  | type_of1 (Ts, f$u) =
clasohm@0
   316
      let val U = type_of1(Ts,u)
clasohm@0
   317
          and T = type_of1(Ts,f)
clasohm@0
   318
      in case T of
wenzelm@9536
   319
            Type("fun",[T1,T2]) =>
wenzelm@9536
   320
              if T1=U then T2  else raise TYPE
wenzelm@9536
   321
                    ("type_of: type mismatch in application", [T1,U], [f$u])
wenzelm@13000
   322
          | _ => raise TYPE
wenzelm@9536
   323
                    ("type_of: function type is expected in application",
wenzelm@9536
   324
                     [T,U], [f$u])
clasohm@0
   325
      end;
clasohm@0
   326
clasohm@0
   327
fun type_of t : typ = type_of1 ([],t);
clasohm@0
   328
clasohm@0
   329
(*Determines the type of a term, with minimal checking*)
wenzelm@13000
   330
fun fastype_of1 (Ts, f$u) =
lcp@61
   331
    (case fastype_of1 (Ts,f) of
wenzelm@9536
   332
        Type("fun",[_,T]) => T
wenzelm@9536
   333
        | _ => raise TERM("fastype_of: expected function type", [f$u]))
lcp@61
   334
  | fastype_of1 (_, Const (_,T)) = T
lcp@61
   335
  | fastype_of1 (_, Free (_,T)) = T
skalberg@15570
   336
  | fastype_of1 (Ts, Bound i) = (List.nth(Ts,i)
skalberg@15570
   337
         handle Subscript => raise TERM("fastype_of: Bound", [Bound i]))
wenzelm@13000
   338
  | fastype_of1 (_, Var (_,T)) = T
lcp@61
   339
  | fastype_of1 (Ts, Abs (_,T,u)) = T --> fastype_of1 (T::Ts, u);
lcp@61
   340
lcp@61
   341
fun fastype_of t : typ = fastype_of1 ([],t);
clasohm@0
   342
wenzelm@16678
   343
(*Determine the argument type of a function*)
wenzelm@22908
   344
fun argument_type_of tm k =
wenzelm@16678
   345
  let
wenzelm@16678
   346
    fun argT i (Type ("fun", [T, U])) = if i = 0 then T else argT (i - 1) U
wenzelm@16678
   347
      | argT _ T = raise TYPE ("argument_type_of", [T], []);
wenzelm@16678
   348
wenzelm@16678
   349
    fun arg 0 _ (Abs (_, T, _)) = T
wenzelm@16678
   350
      | arg i Ts (Abs (_, T, t)) = arg (i - 1) (T :: Ts) t
wenzelm@16678
   351
      | arg i Ts (t $ _) = arg (i + 1) Ts t
wenzelm@16678
   352
      | arg i Ts a = argT i (fastype_of1 (Ts, a));
wenzelm@22908
   353
  in arg k [] tm end;
wenzelm@16678
   354
clasohm@0
   355
wenzelm@19473
   356
val list_abs = uncurry (fold_rev (fn (x, T) => fn t => Abs (x, T, t)));
wenzelm@10806
   357
haftmann@18927
   358
fun strip_abs (Abs (a, T, t)) =
haftmann@18927
   359
      let val (a', t') = strip_abs t
haftmann@18927
   360
      in ((a, T) :: a', t') end
haftmann@18927
   361
  | strip_abs t = ([], t);
haftmann@18927
   362
clasohm@0
   363
(* maps  (x1,...,xn)t   to   t  *)
wenzelm@13000
   364
fun strip_abs_body (Abs(_,_,t))  =  strip_abs_body t
clasohm@0
   365
  | strip_abs_body u  =  u;
clasohm@0
   366
clasohm@0
   367
(* maps  (x1,...,xn)t   to   [x1, ..., xn]  *)
wenzelm@13000
   368
fun strip_abs_vars (Abs(a,T,t))  =  (a,T) :: strip_abs_vars t
clasohm@0
   369
  | strip_abs_vars u  =  [] : (string*typ) list;
clasohm@0
   370
clasohm@0
   371
clasohm@0
   372
fun strip_qnt_body qnt =
clasohm@0
   373
let fun strip(tm as Const(c,_)$Abs(_,_,t)) = if c=qnt then strip t else tm
clasohm@0
   374
      | strip t = t
clasohm@0
   375
in strip end;
clasohm@0
   376
clasohm@0
   377
fun strip_qnt_vars qnt =
clasohm@0
   378
let fun strip(Const(c,_)$Abs(a,T,t)) = if c=qnt then (a,T)::strip t else []
clasohm@0
   379
      | strip t  =  [] : (string*typ) list
clasohm@0
   380
in strip end;
clasohm@0
   381
clasohm@0
   382
clasohm@0
   383
(* maps   (f, [t1,...,tn])  to  f(t1,...,tn) *)
skalberg@15570
   384
val list_comb : term * term list -> term = Library.foldl (op $);
clasohm@0
   385
clasohm@0
   386
clasohm@0
   387
(* maps   f(t1,...,tn)  to  (f, [t1,...,tn]) ; naturally tail-recursive*)
wenzelm@13000
   388
fun strip_comb u : term * term list =
clasohm@0
   389
    let fun stripc (f$t, ts) = stripc (f, t::ts)
wenzelm@13000
   390
        |   stripc  x =  x
clasohm@0
   391
    in  stripc(u,[])  end;
clasohm@0
   392
clasohm@0
   393
clasohm@0
   394
(* maps   f(t1,...,tn)  to  f , which is never a combination *)
clasohm@0
   395
fun head_of (f$t) = head_of f
clasohm@0
   396
  | head_of u = u;
clasohm@0
   397
wenzelm@16599
   398
(*number of atoms and abstractions in a term*)
wenzelm@16599
   399
fun size_of_term tm =
wenzelm@16599
   400
  let
wenzelm@16678
   401
    fun add_size (t $ u, n) = add_size (t, add_size (u, n))
wenzelm@16678
   402
      | add_size (Abs (_ ,_, t), n) = add_size (t, n + 1)
wenzelm@16678
   403
      | add_size (_, n) = n + 1;
wenzelm@16678
   404
  in add_size (tm, 0) end;
clasohm@0
   405
haftmann@18847
   406
fun map_atyps f (Type (a, Ts)) = Type (a, map (map_atyps f) Ts)
haftmann@18976
   407
  | map_atyps f T = f T;
haftmann@18847
   408
haftmann@18847
   409
fun map_aterms f (t $ u) = map_aterms f t $ map_aterms f u
haftmann@18847
   410
  | map_aterms f (Abs (a, T, t)) = Abs (a, T, map_aterms f t)
haftmann@18847
   411
  | map_aterms f t = f t;
haftmann@18847
   412
wenzelm@18981
   413
fun map_type_tvar f = map_atyps (fn TVar x => f x | T => T);
wenzelm@18981
   414
fun map_type_tfree f = map_atyps (fn TFree x => f x | T => T);
nipkow@949
   415
wenzelm@20548
   416
fun map_types f =
wenzelm@16678
   417
  let
wenzelm@16678
   418
    fun map_aux (Const (a, T)) = Const (a, f T)
wenzelm@16678
   419
      | map_aux (Free (a, T)) = Free (a, f T)
wenzelm@16678
   420
      | map_aux (Var (v, T)) = Var (v, f T)
wenzelm@16678
   421
      | map_aux (t as Bound _)  = t
wenzelm@16678
   422
      | map_aux (Abs (a, T, t)) = Abs (a, f T, map_aux t)
wenzelm@16678
   423
      | map_aux (t $ u) = map_aux t $ map_aux u;
wenzelm@16678
   424
  in map_aux end;
clasohm@0
   425
clasohm@0
   426
(* iterate a function over all types in a term *)
clasohm@0
   427
fun it_term_types f =
clasohm@0
   428
let fun iter(Const(_,T), a) = f(T,a)
clasohm@0
   429
      | iter(Free(_,T), a) = f(T,a)
clasohm@0
   430
      | iter(Var(_,T), a) = f(T,a)
clasohm@0
   431
      | iter(Abs(_,T,t), a) = iter(t,f(T,a))
clasohm@0
   432
      | iter(f$u, a) = iter(f, iter(u, a))
clasohm@0
   433
      | iter(Bound _, a) = a
clasohm@0
   434
in iter end
clasohm@0
   435
clasohm@0
   436
wenzelm@16943
   437
(* fold types and terms *)
wenzelm@16943
   438
wenzelm@16943
   439
(*fold atoms of type*)
wenzelm@16943
   440
fun fold_atyps f (Type (_, Ts)) = fold (fold_atyps f) Ts
wenzelm@16943
   441
  | fold_atyps f T = f T;
wenzelm@16943
   442
wenzelm@16943
   443
(*fold atoms of term*)
wenzelm@16943
   444
fun fold_aterms f (t $ u) = fold_aterms f t #> fold_aterms f u
wenzelm@16943
   445
  | fold_aterms f (Abs (_, _, t)) = fold_aterms f t
wenzelm@16943
   446
  | fold_aterms f a = f a;
wenzelm@16943
   447
wenzelm@16943
   448
(*fold types of term*)
wenzelm@16943
   449
fun fold_term_types f (t as Const (_, T)) = f t T
wenzelm@16943
   450
  | fold_term_types f (t as Free (_, T)) = f t T
wenzelm@16943
   451
  | fold_term_types f (t as Var (_, T)) = f t T
wenzelm@16943
   452
  | fold_term_types f (Bound _) = I
wenzelm@16943
   453
  | fold_term_types f (t as Abs (_, T, b)) = f t T #> fold_term_types f b
wenzelm@16943
   454
  | fold_term_types f (t $ u) = fold_term_types f t #> fold_term_types f u;
wenzelm@16943
   455
wenzelm@16943
   456
fun fold_types f = fold_term_types (K f);
wenzelm@16943
   457
wenzelm@24483
   458
fun replace_types (Const (c, _)) (T :: Ts) = (Const (c, T), Ts)
wenzelm@24483
   459
  | replace_types (Free (x, _)) (T :: Ts) = (Free (x, T), Ts)
wenzelm@24483
   460
  | replace_types (Var (xi, _)) (T :: Ts) = (Var (xi, T), Ts)
wenzelm@24483
   461
  | replace_types (Bound i) Ts = (Bound i, Ts)
wenzelm@24483
   462
  | replace_types (Abs (x, _, b)) (T :: Ts) =
wenzelm@24483
   463
      let val (b', Ts') = replace_types b Ts
wenzelm@24483
   464
      in (Abs (x, T, b'), Ts') end
wenzelm@24483
   465
  | replace_types (t $ u) Ts =
wenzelm@24483
   466
      let
wenzelm@24483
   467
        val (t', Ts') = replace_types t Ts;
wenzelm@24483
   468
        val (u', Ts'') = replace_types u Ts';
wenzelm@24483
   469
      in (t' $ u', Ts'') end;
wenzelm@24483
   470
wenzelm@24483
   471
fun burrow_types f ts =
wenzelm@24483
   472
  let
wenzelm@24483
   473
    val Ts = rev (fold (fold_types cons) ts []);
wenzelm@24483
   474
    val Ts' = f Ts;
wenzelm@24483
   475
    val (ts', []) = fold_map replace_types ts Ts';
wenzelm@24483
   476
  in ts' end;
wenzelm@24483
   477
wenzelm@16943
   478
(*collect variables*)
wenzelm@29257
   479
val add_tvar_namesT = fold_atyps (fn TVar (xi, _) => insert (op =) xi | _ => I);
wenzelm@29257
   480
val add_tvar_names = fold_types add_tvar_namesT;
wenzelm@16943
   481
val add_tvarsT = fold_atyps (fn TVar v => insert (op =) v | _ => I);
wenzelm@16943
   482
val add_tvars = fold_types add_tvarsT;
wenzelm@29257
   483
val add_var_names = fold_aterms (fn Var (xi, _) => insert (op =) xi | _ => I);
wenzelm@16943
   484
val add_vars = fold_aterms (fn Var v => insert (op =) v | _ => I);
wenzelm@29257
   485
val add_tfree_namesT = fold_atyps (fn TFree (xi, _) => insert (op =) xi | _ => I);
wenzelm@29257
   486
val add_tfree_names = fold_types add_tfree_namesT;
wenzelm@16943
   487
val add_tfreesT = fold_atyps (fn TFree v => insert (op =) v | _ => I);
wenzelm@16943
   488
val add_tfrees = fold_types add_tfreesT;
wenzelm@29257
   489
val add_free_names = fold_aterms (fn Free (x, _) => insert (op =) x | _ => I);
wenzelm@16943
   490
val add_frees = fold_aterms (fn Free v => insert (op =) v | _ => I);
wenzelm@16943
   491
wenzelm@25050
   492
(*extra type variables in a term, not covered by its type*)
wenzelm@25050
   493
fun hidden_polymorphism t =
wenzelm@21682
   494
  let
wenzelm@25050
   495
    val T = fastype_of t;
wenzelm@21682
   496
    val tvarsT = add_tvarsT T [];
wenzelm@21682
   497
    val extra_tvars = fold_types (fold_atyps
wenzelm@21682
   498
      (fn TVar v => if member (op =) tvarsT v then I else insert (op =) v | _ => I)) t [];
wenzelm@21682
   499
  in extra_tvars end;
wenzelm@21682
   500
wenzelm@16943
   501
wenzelm@25050
   502
wenzelm@29269
   503
(** Comparing terms **)
wenzelm@29269
   504
wenzelm@29269
   505
(* variables *)
wenzelm@29269
   506
wenzelm@29269
   507
fun eq_ix ((x, i): indexname, (y, j)) = i = j andalso x = y;
wenzelm@16537
   508
wenzelm@29269
   509
fun eq_tvar ((xi, S: sort), (xi', S')) = eq_ix (xi, xi') andalso S = S';
wenzelm@29269
   510
fun eq_var ((xi, T: typ), (xi', T')) = eq_ix (xi, xi') andalso T = T';
wenzelm@29269
   511
wenzelm@29269
   512
wenzelm@29269
   513
(* alpha equivalence *)
wenzelm@20511
   514
wenzelm@20511
   515
fun tm1 aconv tm2 =
wenzelm@20511
   516
  pointer_eq (tm1, tm2) orelse
wenzelm@20511
   517
    (case (tm1, tm2) of
wenzelm@20511
   518
      (t1 $ u1, t2 $ u2) => t1 aconv t2 andalso u1 aconv u2
wenzelm@20511
   519
    | (Abs (_, T1, t1), Abs (_, T2, t2)) => t1 aconv t2 andalso T1 = T2
wenzelm@20511
   520
    | (a1, a2) => a1 = a2);
wenzelm@20511
   521
wenzelm@20511
   522
wenzelm@29269
   523
(*A fast unification filter: true unless the two terms cannot be unified.
wenzelm@29269
   524
  Terms must be NORMAL.  Treats all Vars as distinct. *)
wenzelm@29269
   525
fun could_unify (t, u) =
wenzelm@29269
   526
  let
wenzelm@29269
   527
    fun matchrands (f $ t) (g $ u) = could_unify (t, u) andalso matchrands f g
wenzelm@29269
   528
      | matchrands _ _ = true;
wenzelm@29269
   529
  in
wenzelm@29269
   530
    case (head_of t, head_of u) of
wenzelm@29269
   531
      (_, Var _) => true
wenzelm@29269
   532
    | (Var _, _) => true
wenzelm@29269
   533
    | (Const (a, _), Const (b, _)) => a = b andalso matchrands t u
wenzelm@29269
   534
    | (Free (a, _), Free (b, _)) => a = b andalso matchrands t u
wenzelm@29269
   535
    | (Bound i, Bound j) => i = j andalso matchrands t u
wenzelm@29269
   536
    | (Abs _, _) => true   (*because of possible eta equality*)
wenzelm@29269
   537
    | (_, Abs _) => true
wenzelm@29269
   538
    | _ => false
wenzelm@29269
   539
  end;
wenzelm@16678
   540
nipkow@16570
   541
wenzelm@16537
   542
clasohm@0
   543
(** Connectives of higher order logic **)
clasohm@0
   544
wenzelm@24850
   545
fun aT S = TFree (Name.aT, S);
wenzelm@19394
   546
wenzelm@375
   547
fun itselfT ty = Type ("itself", [ty]);
wenzelm@24850
   548
val a_itselfT = itselfT (TFree (Name.aT, []));
wenzelm@375
   549
clasohm@0
   550
val propT : typ = Type("prop",[]);
clasohm@0
   551
clasohm@0
   552
fun all T = Const("all", (T-->propT)-->propT);
clasohm@0
   553
clasohm@0
   554
(* maps  !!x1...xn. t   to   t  *)
wenzelm@13000
   555
fun strip_all_body (Const("all",_)$Abs(_,_,t))  =  strip_all_body t
clasohm@0
   556
  | strip_all_body t  =  t;
clasohm@0
   557
clasohm@0
   558
(* maps  !!x1...xn. t   to   [x1, ..., xn]  *)
clasohm@0
   559
fun strip_all_vars (Const("all",_)$Abs(a,T,t))  =
wenzelm@13000
   560
                (a,T) :: strip_all_vars t
clasohm@0
   561
  | strip_all_vars t  =  [] : (string*typ) list;
clasohm@0
   562
clasohm@0
   563
(*increments a term's non-local bound variables
clasohm@0
   564
  required when moving a term within abstractions
clasohm@0
   565
     inc is  increment for bound variables
clasohm@0
   566
     lev is  level at which a bound variable is considered 'loose'*)
wenzelm@13000
   567
fun incr_bv (inc, lev, u as Bound i) = if i>=lev then Bound(i+inc) else u
clasohm@0
   568
  | incr_bv (inc, lev, Abs(a,T,body)) =
wenzelm@9536
   569
        Abs(a, T, incr_bv(inc,lev+1,body))
wenzelm@13000
   570
  | incr_bv (inc, lev, f$t) =
clasohm@0
   571
      incr_bv(inc,lev,f) $ incr_bv(inc,lev,t)
clasohm@0
   572
  | incr_bv (inc, lev, u) = u;
clasohm@0
   573
clasohm@0
   574
fun incr_boundvars  0  t = t
clasohm@0
   575
  | incr_boundvars inc t = incr_bv(inc,0,t);
clasohm@0
   576
wenzelm@12981
   577
(*Scan a pair of terms; while they are similar,
wenzelm@12981
   578
  accumulate corresponding bound vars in "al"*)
wenzelm@12981
   579
fun match_bvs(Abs(x,_,s),Abs(y,_,t), al) =
wenzelm@12981
   580
      match_bvs(s, t, if x="" orelse y="" then al
wenzelm@12981
   581
                                          else (x,y)::al)
wenzelm@12981
   582
  | match_bvs(f$s, g$t, al) = match_bvs(f,g,match_bvs(s,t,al))
wenzelm@12981
   583
  | match_bvs(_,_,al) = al;
wenzelm@12981
   584
wenzelm@12981
   585
(* strip abstractions created by parameters *)
wenzelm@12981
   586
fun match_bvars((s,t),al) = match_bvs(strip_abs_body s, strip_abs_body t, al);
wenzelm@12981
   587
haftmann@22031
   588
fun map_abs_vars f (t $ u) = map_abs_vars f t $ map_abs_vars f u
haftmann@22031
   589
  | map_abs_vars f (Abs (a, T, t)) = Abs (f a, T, map_abs_vars f t)
haftmann@22031
   590
  | map_abs_vars f t = t;
haftmann@22031
   591
wenzelm@12981
   592
fun rename_abs pat obj t =
wenzelm@12981
   593
  let
wenzelm@12981
   594
    val ren = match_bvs (pat, obj, []);
wenzelm@12981
   595
    fun ren_abs (Abs (x, T, b)) =
wenzelm@18942
   596
          Abs (the_default x (AList.lookup (op =) ren x), T, ren_abs b)
wenzelm@12981
   597
      | ren_abs (f $ t) = ren_abs f $ ren_abs t
wenzelm@12981
   598
      | ren_abs t = t
skalberg@15531
   599
  in if null ren then NONE else SOME (ren_abs t) end;
clasohm@0
   600
clasohm@0
   601
(*Accumulate all 'loose' bound vars referring to level 'lev' or beyond.
clasohm@0
   602
   (Bound 0) is loose at level 0 *)
wenzelm@13000
   603
fun add_loose_bnos (Bound i, lev, js) =
haftmann@20854
   604
        if i<lev then js else insert (op =) (i - lev) js
clasohm@0
   605
  | add_loose_bnos (Abs (_,_,t), lev, js) = add_loose_bnos (t, lev+1, js)
clasohm@0
   606
  | add_loose_bnos (f$t, lev, js) =
wenzelm@13000
   607
        add_loose_bnos (f, lev, add_loose_bnos (t, lev, js))
clasohm@0
   608
  | add_loose_bnos (_, _, js) = js;
clasohm@0
   609
clasohm@0
   610
fun loose_bnos t = add_loose_bnos (t, 0, []);
clasohm@0
   611
clasohm@0
   612
(* loose_bvar(t,k) iff t contains a 'loose' bound variable referring to
clasohm@0
   613
   level k or beyond. *)
clasohm@0
   614
fun loose_bvar(Bound i,k) = i >= k
clasohm@0
   615
  | loose_bvar(f$t, k) = loose_bvar(f,k) orelse loose_bvar(t,k)
clasohm@0
   616
  | loose_bvar(Abs(_,_,t),k) = loose_bvar(t,k+1)
clasohm@0
   617
  | loose_bvar _ = false;
clasohm@0
   618
nipkow@2792
   619
fun loose_bvar1(Bound i,k) = i = k
nipkow@2792
   620
  | loose_bvar1(f$t, k) = loose_bvar1(f,k) orelse loose_bvar1(t,k)
nipkow@2792
   621
  | loose_bvar1(Abs(_,_,t),k) = loose_bvar1(t,k+1)
nipkow@2792
   622
  | loose_bvar1 _ = false;
clasohm@0
   623
clasohm@0
   624
(*Substitute arguments for loose bound variables.
clasohm@0
   625
  Beta-reduction of arg(n-1)...arg0 into t replacing (Bound i) with (argi).
wenzelm@4626
   626
  Note that for ((%x y. c) a b), the bound vars in c are x=1 and y=0
wenzelm@9536
   627
        and the appropriate call is  subst_bounds([b,a], c) .
clasohm@0
   628
  Loose bound variables >=n are reduced by "n" to
clasohm@0
   629
     compensate for the disappearance of lambdas.
clasohm@0
   630
*)
wenzelm@13000
   631
fun subst_bounds (args: term list, t) : term =
wenzelm@19065
   632
  let
wenzelm@19065
   633
    exception SAME;
wenzelm@19065
   634
    val n = length args;
wenzelm@19065
   635
    fun subst (t as Bound i, lev) =
wenzelm@19065
   636
         (if i < lev then raise SAME   (*var is locally bound*)
wenzelm@19065
   637
          else incr_boundvars lev (List.nth (args, i - lev))
wenzelm@19065
   638
            handle Subscript => Bound (i - n))  (*loose: change it*)
wenzelm@19065
   639
      | subst (Abs (a, T, body), lev) = Abs (a, T, subst (body, lev + 1))
wenzelm@19065
   640
      | subst (f $ t, lev) =
wenzelm@19065
   641
          (subst (f, lev) $ (subst (t, lev) handle SAME => t) handle SAME => f $ subst (t, lev))
wenzelm@19065
   642
      | subst _ = raise SAME;
wenzelm@19065
   643
  in case args of [] => t | _ => (subst (t, 0) handle SAME => t) end;
clasohm@0
   644
paulson@2192
   645
(*Special case: one argument*)
wenzelm@13000
   646
fun subst_bound (arg, t) : term =
wenzelm@19065
   647
  let
wenzelm@19065
   648
    exception SAME;
wenzelm@19065
   649
    fun subst (Bound i, lev) =
wenzelm@19065
   650
          if i < lev then raise SAME   (*var is locally bound*)
wenzelm@19065
   651
          else if i = lev then incr_boundvars lev arg
wenzelm@19065
   652
          else Bound (i - 1)   (*loose: change it*)
wenzelm@19065
   653
      | subst (Abs (a, T, body), lev) = Abs (a, T, subst (body, lev + 1))
wenzelm@19065
   654
      | subst (f $ t, lev) =
wenzelm@19065
   655
          (subst (f, lev) $ (subst (t, lev) handle SAME => t) handle SAME => f $ subst (t, lev))
wenzelm@19065
   656
      | subst _ = raise SAME;
wenzelm@19065
   657
  in subst (t, 0) handle SAME => t end;
paulson@2192
   658
clasohm@0
   659
(*beta-reduce if possible, else form application*)
paulson@2192
   660
fun betapply (Abs(_,_,t), u) = subst_bound (u,t)
clasohm@0
   661
  | betapply (f,u) = f$u;
clasohm@0
   662
wenzelm@18183
   663
val betapplys = Library.foldl betapply;
wenzelm@18183
   664
wenzelm@14786
   665
haftmann@20109
   666
(*unfolding abstractions with substitution
haftmann@20109
   667
  of bound variables and implicit eta-expansion*)
haftmann@20109
   668
fun strip_abs_eta k t =
haftmann@20109
   669
  let
wenzelm@20122
   670
    val used = fold_aterms (fn Free (v, _) => Name.declare v | _ => I) t Name.context;
haftmann@20109
   671
    fun strip_abs t (0, used) = (([], t), (0, used))
haftmann@20109
   672
      | strip_abs (Abs (v, T, t)) (k, used) =
haftmann@20109
   673
          let
wenzelm@20122
   674
            val ([v'], used') = Name.variants [v] used;
haftmann@21013
   675
            val t' = subst_bound (Free (v', T), t);
wenzelm@20122
   676
            val ((vs, t''), (k', used'')) = strip_abs t' (k - 1, used');
wenzelm@20122
   677
          in (((v', T) :: vs, t''), (k', used'')) end
haftmann@20109
   678
      | strip_abs t (k, used) = (([], t), (k, used));
haftmann@20109
   679
    fun expand_eta [] t _ = ([], t)
haftmann@20109
   680
      | expand_eta (T::Ts) t used =
haftmann@20109
   681
          let
wenzelm@20122
   682
            val ([v], used') = Name.variants [""] used;
wenzelm@20122
   683
            val (vs, t') = expand_eta Ts (t $ Free (v, T)) used';
haftmann@20109
   684
          in ((v, T) :: vs, t') end;
haftmann@20109
   685
    val ((vs1, t'), (k', used')) = strip_abs t (k, used);
haftmann@20109
   686
    val Ts = (fst o chop k' o fst o strip_type o fastype_of) t';
haftmann@20109
   687
    val (vs2, t'') = expand_eta Ts t' used';
haftmann@20109
   688
  in (vs1 @ vs2, t'') end;
haftmann@20109
   689
haftmann@20109
   690
clasohm@0
   691
(*Substitute new for free occurrences of old in a term*)
wenzelm@29256
   692
fun subst_free [] = I
clasohm@0
   693
  | subst_free pairs =
wenzelm@13000
   694
      let fun substf u =
haftmann@17314
   695
            case AList.lookup (op aconv) pairs u of
skalberg@15531
   696
                SOME u' => u'
skalberg@15531
   697
              | NONE => (case u of Abs(a,T,t) => Abs(a, T, substf t)
wenzelm@9536
   698
                                 | t$u' => substf t $ substf u'
wenzelm@9536
   699
                                 | _ => u)
clasohm@0
   700
      in  substf  end;
clasohm@0
   701
wenzelm@13000
   702
(*Abstraction of the term "body" over its occurrences of v,
clasohm@0
   703
    which must contain no loose bound variables.
clasohm@0
   704
  The resulting term is ready to become the body of an Abs.*)
wenzelm@16882
   705
fun abstract_over (v, body) =
wenzelm@16882
   706
  let
wenzelm@16990
   707
    exception SAME;
wenzelm@16990
   708
    fun abs lev tm =
wenzelm@16990
   709
      if v aconv tm then Bound lev
wenzelm@16882
   710
      else
wenzelm@16990
   711
        (case tm of
wenzelm@16990
   712
          Abs (a, T, t) => Abs (a, T, abs (lev + 1) t)
wenzelm@16990
   713
        | t $ u => (abs lev t $ (abs lev u handle SAME => u) handle SAME => t $ abs lev u)
wenzelm@16990
   714
        | _ => raise SAME);
wenzelm@16990
   715
  in abs 0 body handle SAME => body end;
clasohm@0
   716
wenzelm@21975
   717
fun lambda v t =
wenzelm@21975
   718
  let val x =
wenzelm@21975
   719
    (case v of
wenzelm@21975
   720
      Const (x, _) => NameSpace.base x
wenzelm@21975
   721
    | Free (x, _) => x
wenzelm@21975
   722
    | Var ((x, _), _) => x
wenzelm@24850
   723
    | _ => Name.uu)
wenzelm@21975
   724
  in Abs (x, fastype_of v, abstract_over (v, t)) end;
clasohm@0
   725
clasohm@0
   726
(*Form an abstraction over a free variable.*)
wenzelm@21975
   727
fun absfree (a,T,body) = Abs (a, T, abstract_over (Free (a, T), body));
wenzelm@24850
   728
fun absdummy (T, body) = Abs (Name.internal Name.uu, T, body);
clasohm@0
   729
clasohm@0
   730
(*Abstraction over a list of free variables*)
clasohm@0
   731
fun list_abs_free ([ ] ,     t) = t
wenzelm@13000
   732
  | list_abs_free ((a,T)::vars, t) =
clasohm@0
   733
      absfree(a, T, list_abs_free(vars,t));
clasohm@0
   734
clasohm@0
   735
(*Quantification over a list of free variables*)
clasohm@0
   736
fun list_all_free ([], t: term) = t
wenzelm@13000
   737
  | list_all_free ((a,T)::vars, t) =
clasohm@0
   738
        (all T) $ (absfree(a, T, list_all_free(vars,t)));
clasohm@0
   739
clasohm@0
   740
(*Quantification over a list of variables (already bound in body) *)
clasohm@0
   741
fun list_all ([], t) = t
wenzelm@13000
   742
  | list_all ((a,T)::vars, t) =
clasohm@0
   743
        (all T) $ (Abs(a, T, list_all(vars,t)));
clasohm@0
   744
wenzelm@16678
   745
(*Replace the ATOMIC term ti by ui;    inst = [(t1,u1), ..., (tn,un)].
clasohm@0
   746
  A simultaneous substitution:  [ (a,b), (b,a) ] swaps a and b.  *)
wenzelm@16678
   747
fun subst_atomic [] tm = tm
wenzelm@16678
   748
  | subst_atomic inst tm =
wenzelm@16678
   749
      let
wenzelm@16678
   750
        fun subst (Abs (a, T, body)) = Abs (a, T, subst body)
wenzelm@16678
   751
          | subst (t $ u) = subst t $ subst u
wenzelm@18942
   752
          | subst t = the_default t (AList.lookup (op aconv) inst t);
wenzelm@16678
   753
      in subst tm end;
clasohm@0
   754
wenzelm@16678
   755
(*Replace the ATOMIC type Ti by Ui;    inst = [(T1,U1), ..., (Tn,Un)].*)
wenzelm@16678
   756
fun typ_subst_atomic [] ty = ty
wenzelm@16678
   757
  | typ_subst_atomic inst ty =
wenzelm@16678
   758
      let
wenzelm@16678
   759
        fun subst (Type (a, Ts)) = Type (a, map subst Ts)
wenzelm@18942
   760
          | subst T = the_default T (AList.lookup (op = : typ * typ -> bool) inst T);
wenzelm@16678
   761
      in subst ty end;
berghofe@15797
   762
wenzelm@16678
   763
fun subst_atomic_types [] tm = tm
wenzelm@20548
   764
  | subst_atomic_types inst tm = map_types (typ_subst_atomic inst) tm;
wenzelm@16678
   765
wenzelm@16678
   766
fun typ_subst_TVars [] ty = ty
wenzelm@16678
   767
  | typ_subst_TVars inst ty =
wenzelm@16678
   768
      let
wenzelm@16678
   769
        fun subst (Type (a, Ts)) = Type (a, map subst Ts)
wenzelm@18942
   770
          | subst (T as TVar (xi, _)) = the_default T (AList.lookup (op =) inst xi)
wenzelm@16678
   771
          | subst T = T;
wenzelm@16678
   772
      in subst ty end;
clasohm@0
   773
wenzelm@16678
   774
fun subst_TVars [] tm = tm
wenzelm@20548
   775
  | subst_TVars inst tm = map_types (typ_subst_TVars inst) tm;
clasohm@0
   776
wenzelm@16678
   777
fun subst_Vars [] tm = tm
wenzelm@16678
   778
  | subst_Vars inst tm =
wenzelm@16678
   779
      let
wenzelm@18942
   780
        fun subst (t as Var (xi, _)) = the_default t (AList.lookup (op =) inst xi)
wenzelm@16678
   781
          | subst (Abs (a, T, t)) = Abs (a, T, subst t)
wenzelm@16678
   782
          | subst (t $ u) = subst t $ subst u
wenzelm@16678
   783
          | subst t = t;
wenzelm@16678
   784
      in subst tm end;
clasohm@0
   785
wenzelm@16678
   786
fun subst_vars ([], []) tm = tm
wenzelm@16678
   787
  | subst_vars ([], inst) tm = subst_Vars inst tm
wenzelm@16678
   788
  | subst_vars (instT, inst) tm =
wenzelm@16678
   789
      let
wenzelm@16678
   790
        fun subst (Const (a, T)) = Const (a, typ_subst_TVars instT T)
wenzelm@16678
   791
          | subst (Free (a, T)) = Free (a, typ_subst_TVars instT T)
wenzelm@16678
   792
          | subst (t as Var (xi, T)) =
haftmann@17271
   793
              (case AList.lookup (op =) inst xi of
wenzelm@16678
   794
                NONE => Var (xi, typ_subst_TVars instT T)
wenzelm@16678
   795
              | SOME t => t)
wenzelm@16678
   796
          | subst (t as Bound _) = t
wenzelm@16678
   797
          | subst (Abs (a, T, t)) = Abs (a, typ_subst_TVars instT T, subst t)
wenzelm@16678
   798
          | subst (t $ u) = subst t $ subst u;
wenzelm@16678
   799
      in subst tm end;
clasohm@0
   800
wenzelm@25050
   801
fun close_schematic_term t =
wenzelm@25050
   802
  let
wenzelm@25050
   803
    val extra_types = map (fn v => Const ("TYPE", itselfT (TVar v))) (hidden_polymorphism t);
wenzelm@25050
   804
    val extra_terms = map Var (rev (add_vars t []));
wenzelm@25050
   805
  in fold_rev lambda (extra_types @ extra_terms) t end;
wenzelm@25050
   806
wenzelm@25050
   807
clasohm@0
   808
paulson@15573
   809
(** Identifying first-order terms **)
paulson@15573
   810
wenzelm@20199
   811
(*Differs from proofterm/is_fun in its treatment of TVar*)
wenzelm@29256
   812
fun is_funtype (Type ("fun", [_, _])) = true
wenzelm@20199
   813
  | is_funtype _ = false;
wenzelm@20199
   814
paulson@15573
   815
(*Argument Ts is a reverse list of binder types, needed if term t contains Bound vars*)
wenzelm@29256
   816
fun has_not_funtype Ts t = not (is_funtype (fastype_of1 (Ts, t)));
paulson@15573
   817
wenzelm@16537
   818
(*First order means in all terms of the form f(t1,...,tn) no argument has a
paulson@16589
   819
  function type. The supplied quantifiers are excluded: their argument always
paulson@16589
   820
  has a function type through a recursive call into its body.*)
wenzelm@16667
   821
fun is_first_order quants =
paulson@16589
   822
  let fun first_order1 Ts (Abs (_,T,body)) = first_order1 (T::Ts) body
wenzelm@16667
   823
        | first_order1 Ts (Const(q,_) $ Abs(a,T,body)) =
wenzelm@20664
   824
            member (op =) quants q  andalso   (*it is a known quantifier*)
paulson@16589
   825
            not (is_funtype T)   andalso first_order1 (T::Ts) body
wenzelm@16667
   826
        | first_order1 Ts t =
wenzelm@16667
   827
            case strip_comb t of
wenzelm@16667
   828
                 (Var _, ts) => forall (first_order1 Ts andf has_not_funtype Ts) ts
wenzelm@16667
   829
               | (Free _, ts) => forall (first_order1 Ts andf has_not_funtype Ts) ts
wenzelm@16667
   830
               | (Const _, ts) => forall (first_order1 Ts andf has_not_funtype Ts) ts
wenzelm@16667
   831
               | (Bound _, ts) => forall (first_order1 Ts andf has_not_funtype Ts) ts
wenzelm@16667
   832
               | (Abs _, ts) => false (*not in beta-normal form*)
wenzelm@16667
   833
               | _ => error "first_order: unexpected case"
paulson@16589
   834
    in  first_order1 []  end;
paulson@15573
   835
wenzelm@16710
   836
wenzelm@16990
   837
(* maximum index of typs and terms *)
clasohm@0
   838
wenzelm@16710
   839
fun maxidx_typ (TVar ((_, j), _)) i = Int.max (i, j)
wenzelm@16710
   840
  | maxidx_typ (Type (_, Ts)) i = maxidx_typs Ts i
wenzelm@16710
   841
  | maxidx_typ (TFree _) i = i
wenzelm@16710
   842
and maxidx_typs [] i = i
wenzelm@16710
   843
  | maxidx_typs (T :: Ts) i = maxidx_typs Ts (maxidx_typ T i);
clasohm@0
   844
wenzelm@16710
   845
fun maxidx_term (Var ((_, j), T)) i = maxidx_typ T (Int.max (i, j))
wenzelm@16710
   846
  | maxidx_term (Const (_, T)) i = maxidx_typ T i
wenzelm@16710
   847
  | maxidx_term (Free (_, T)) i = maxidx_typ T i
wenzelm@16710
   848
  | maxidx_term (Bound _) i = i
wenzelm@16710
   849
  | maxidx_term (Abs (_, T, t)) i = maxidx_term t (maxidx_typ T i)
wenzelm@16710
   850
  | maxidx_term (t $ u) i = maxidx_term u (maxidx_term t i);
clasohm@0
   851
wenzelm@16710
   852
fun maxidx_of_typ T = maxidx_typ T ~1;
wenzelm@16710
   853
fun maxidx_of_typs Ts = maxidx_typs Ts ~1;
wenzelm@16710
   854
fun maxidx_of_term t = maxidx_term t ~1;
berghofe@13665
   855
clasohm@0
   856
clasohm@0
   857
clasohm@0
   858
(**** Syntax-related declarations ****)
clasohm@0
   859
wenzelm@19909
   860
(* substructure *)
wenzelm@4017
   861
wenzelm@19909
   862
fun exists_subtype P =
wenzelm@19909
   863
  let
wenzelm@19909
   864
    fun ex ty = P ty orelse
wenzelm@19909
   865
      (case ty of Type (_, Ts) => exists ex Ts | _ => false);
wenzelm@19909
   866
  in ex end;
nipkow@13646
   867
wenzelm@20531
   868
fun exists_type P =
wenzelm@20531
   869
  let
wenzelm@20531
   870
    fun ex (Const (_, T)) = P T
wenzelm@20531
   871
      | ex (Free (_, T)) = P T
wenzelm@20531
   872
      | ex (Var (_, T)) = P T
wenzelm@20531
   873
      | ex (Bound _) = false
wenzelm@20531
   874
      | ex (Abs (_, T, t)) = P T orelse ex t
wenzelm@20531
   875
      | ex (t $ u) = ex t orelse ex u;
wenzelm@20531
   876
  in ex end;
wenzelm@20531
   877
wenzelm@16943
   878
fun exists_subterm P =
wenzelm@16943
   879
  let
wenzelm@16943
   880
    fun ex tm = P tm orelse
wenzelm@16943
   881
      (case tm of
wenzelm@16943
   882
        t $ u => ex t orelse ex u
wenzelm@16943
   883
      | Abs (_, _, t) => ex t
wenzelm@16943
   884
      | _ => false);
wenzelm@16943
   885
  in ex end;
obua@16108
   886
wenzelm@24671
   887
fun has_abs (Abs _) = true
wenzelm@24671
   888
  | has_abs (t $ u) = has_abs t orelse has_abs u
wenzelm@24671
   889
  | has_abs _ = false;
wenzelm@24671
   890
wenzelm@24671
   891
wenzelm@19909
   892
wenzelm@19909
   893
(** Consts etc. **)
wenzelm@19909
   894
wenzelm@19909
   895
fun add_term_consts (Const (c, _), cs) = insert (op =) c cs
wenzelm@19909
   896
  | add_term_consts (t $ u, cs) = add_term_consts (t, add_term_consts (u, cs))
wenzelm@19909
   897
  | add_term_consts (Abs (_, _, t), cs) = add_term_consts (t, cs)
wenzelm@19909
   898
  | add_term_consts (_, cs) = cs;
wenzelm@19909
   899
wenzelm@19909
   900
fun term_consts t = add_term_consts(t,[]);
wenzelm@19909
   901
wenzelm@16943
   902
fun exists_Const P = exists_subterm (fn Const c => P c | _ => false);
nipkow@4631
   903
wenzelm@4017
   904
clasohm@0
   905
(** TFrees and TVars **)
clasohm@0
   906
wenzelm@12802
   907
(*Accumulates the names of Frees in the term, suppressing duplicates.*)
haftmann@20854
   908
fun add_term_free_names (Free(a,_), bs) = insert (op =) a bs
wenzelm@12802
   909
  | add_term_free_names (f$u, bs) = add_term_free_names (f, add_term_free_names(u, bs))
wenzelm@12802
   910
  | add_term_free_names (Abs(_,_,t), bs) = add_term_free_names(t,bs)
wenzelm@12802
   911
  | add_term_free_names (_, bs) = bs;
wenzelm@12802
   912
clasohm@0
   913
(*Accumulates the names in the term, suppressing duplicates.
clasohm@0
   914
  Includes Frees and Consts.  For choosing unambiguous bound var names.*)
haftmann@20854
   915
fun add_term_names (Const(a,_), bs) = insert (op =) (NameSpace.base a) bs
haftmann@20854
   916
  | add_term_names (Free(a,_), bs) = insert (op =) a bs
clasohm@0
   917
  | add_term_names (f$u, bs) = add_term_names (f, add_term_names(u, bs))
clasohm@0
   918
  | add_term_names (Abs(_,_,t), bs) = add_term_names(t,bs)
clasohm@0
   919
  | add_term_names (_, bs) = bs;
clasohm@0
   920
clasohm@0
   921
(*Accumulates the TVars in a type, suppressing duplicates. *)
wenzelm@23178
   922
fun add_typ_tvars(Type(_,Ts),vs) = List.foldr add_typ_tvars vs Ts
clasohm@0
   923
  | add_typ_tvars(TFree(_),vs) = vs
wenzelm@16294
   924
  | add_typ_tvars(TVar(v),vs) = insert (op =) v vs;
clasohm@0
   925
clasohm@0
   926
(*Accumulates the TFrees in a type, suppressing duplicates. *)
wenzelm@23178
   927
fun add_typ_tfree_names(Type(_,Ts),fs) = List.foldr add_typ_tfree_names fs Ts
haftmann@20854
   928
  | add_typ_tfree_names(TFree(f,_),fs) = insert (op =) f fs
clasohm@0
   929
  | add_typ_tfree_names(TVar(_),fs) = fs;
clasohm@0
   930
wenzelm@23178
   931
fun add_typ_tfrees(Type(_,Ts),fs) = List.foldr add_typ_tfrees fs Ts
wenzelm@16294
   932
  | add_typ_tfrees(TFree(f),fs) = insert (op =) f fs
clasohm@0
   933
  | add_typ_tfrees(TVar(_),fs) = fs;
clasohm@0
   934
wenzelm@23178
   935
fun add_typ_varnames(Type(_,Ts),nms) = List.foldr add_typ_varnames nms Ts
haftmann@20854
   936
  | add_typ_varnames(TFree(nm,_),nms) = insert (op =) nm nms
haftmann@20854
   937
  | add_typ_varnames(TVar((nm,_),_),nms) = insert (op =) nm nms;
nipkow@949
   938
clasohm@0
   939
(*Accumulates the TVars in a term, suppressing duplicates. *)
clasohm@0
   940
val add_term_tvars = it_term_types add_typ_tvars;
clasohm@0
   941
clasohm@0
   942
(*Accumulates the TFrees in a term, suppressing duplicates. *)
clasohm@0
   943
val add_term_tfrees = it_term_types add_typ_tfrees;
clasohm@0
   944
val add_term_tfree_names = it_term_types add_typ_tfree_names;
clasohm@0
   945
clasohm@0
   946
(*Non-list versions*)
clasohm@0
   947
fun typ_tfrees T = add_typ_tfrees(T,[]);
clasohm@0
   948
fun typ_tvars T = add_typ_tvars(T,[]);
clasohm@0
   949
fun term_tfrees t = add_term_tfrees(t,[]);
clasohm@0
   950
fun term_tvars t = add_term_tvars(t,[]);
clasohm@0
   951
wenzelm@16537
   952
wenzelm@20199
   953
(* dest abstraction *)
clasohm@0
   954
wenzelm@16678
   955
fun dest_abs (x, T, body) =
wenzelm@16678
   956
  let
wenzelm@16678
   957
    fun name_clash (Free (y, _)) = (x = y)
wenzelm@16678
   958
      | name_clash (t $ u) = name_clash t orelse name_clash u
wenzelm@16678
   959
      | name_clash (Abs (_, _, t)) = name_clash t
wenzelm@16678
   960
      | name_clash _ = false;
wenzelm@16678
   961
  in
wenzelm@27335
   962
    if name_clash body then dest_abs (Name.variant [x] x, T, body)    (*potentially slow*)
wenzelm@16678
   963
    else (x, subst_bound (Free (x, T), body))
wenzelm@16678
   964
  end;
wenzelm@16678
   965
wenzelm@20160
   966
wenzelm@20160
   967
(* renaming variables *)
clasohm@0
   968
wenzelm@24762
   969
val declare_typ_names = fold_atyps (fn TFree (a, _) => Name.declare a | _ => I);
wenzelm@24762
   970
wenzelm@20239
   971
fun declare_term_names tm =
wenzelm@20239
   972
  fold_aterms
wenzelm@20239
   973
    (fn Const (a, _) => Name.declare (NameSpace.base a)
wenzelm@20239
   974
      | Free (a, _) => Name.declare a
wenzelm@20239
   975
      | _ => I) tm #>
wenzelm@24762
   976
  fold_types declare_typ_names tm;
wenzelm@20239
   977
wenzelm@20160
   978
fun variant_frees t frees =
wenzelm@20160
   979
  fst (Name.variants (map fst frees) (declare_term_names t Name.context)) ~~ map snd frees;
wenzelm@20160
   980
wenzelm@20160
   981
fun rename_wrt_term t frees = rev (variant_frees t frees);  (*reversed result!*)
clasohm@1364
   982
paulson@1417
   983
wenzelm@9536
   984
(* dummy patterns *)
wenzelm@9536
   985
wenzelm@9536
   986
val dummy_patternN = "dummy_pattern";
wenzelm@9536
   987
wenzelm@18253
   988
fun dummy_pattern T = Const (dummy_patternN, T);
wenzelm@18253
   989
wenzelm@9536
   990
fun is_dummy_pattern (Const ("dummy_pattern", _)) = true
wenzelm@9536
   991
  | is_dummy_pattern _ = false;
wenzelm@9536
   992
wenzelm@9536
   993
fun no_dummy_patterns tm =
haftmann@16787
   994
  if not (fold_aterms (fn t => fn b => b orelse is_dummy_pattern t) tm false) then tm
wenzelm@9536
   995
  else raise TERM ("Illegal occurrence of '_' dummy pattern", [tm]);
wenzelm@9536
   996
wenzelm@24733
   997
fun free_dummy_patterns (Const ("dummy_pattern", T)) used =
wenzelm@24850
   998
      let val [x] = Name.invents used Name.uu 1
wenzelm@24733
   999
      in (Free (Name.internal x, T), Name.declare x used) end
wenzelm@24733
  1000
  | free_dummy_patterns (Abs (x, T, b)) used =
wenzelm@24733
  1001
      let val (b', used') = free_dummy_patterns b used
wenzelm@24733
  1002
      in (Abs (x, T, b'), used') end
wenzelm@24733
  1003
  | free_dummy_patterns (t $ u) used =
wenzelm@24733
  1004
      let
wenzelm@24733
  1005
        val (t', used') = free_dummy_patterns t used;
wenzelm@24733
  1006
        val (u', used'') = free_dummy_patterns u used';
wenzelm@24733
  1007
      in (t' $ u', used'') end
wenzelm@24733
  1008
  | free_dummy_patterns a used = (a, used);
wenzelm@24733
  1009
wenzelm@24762
  1010
fun replace_dummy Ts (Const ("dummy_pattern", T)) i =
wenzelm@24762
  1011
      (list_comb (Var (("_dummy_", i), Ts ---> T), map Bound (0 upto length Ts - 1)), i + 1)
wenzelm@24762
  1012
  | replace_dummy Ts (Abs (x, T, t)) i =
wenzelm@24762
  1013
      let val (t', i') = replace_dummy (T :: Ts) t i
wenzelm@24762
  1014
      in (Abs (x, T, t'), i') end
wenzelm@24762
  1015
  | replace_dummy Ts (t $ u) i =
wenzelm@24762
  1016
      let
wenzelm@24762
  1017
        val (t', i') = replace_dummy Ts t i;
wenzelm@24762
  1018
        val (u', i'') = replace_dummy Ts u i';
wenzelm@24762
  1019
      in (t' $ u', i'') end
wenzelm@24762
  1020
  | replace_dummy _ a i = (a, i);
wenzelm@11903
  1021
wenzelm@11903
  1022
val replace_dummy_patterns = replace_dummy [];
wenzelm@9536
  1023
wenzelm@10552
  1024
fun is_replaced_dummy_pattern ("_dummy_", _) = true
wenzelm@10552
  1025
  | is_replaced_dummy_pattern _ = false;
wenzelm@9536
  1026
wenzelm@16035
  1027
fun show_dummy_patterns (Var (("_dummy_", _), T)) = Const ("dummy_pattern", T)
wenzelm@16035
  1028
  | show_dummy_patterns (t $ u) = show_dummy_patterns t $ show_dummy_patterns u
wenzelm@16035
  1029
  | show_dummy_patterns (Abs (x, T, t)) = Abs (x, T, show_dummy_patterns t)
wenzelm@16035
  1030
  | show_dummy_patterns a = a;
wenzelm@16035
  1031
wenzelm@13484
  1032
wenzelm@20100
  1033
(* display variables *)
wenzelm@20100
  1034
wenzelm@15986
  1035
val show_question_marks = ref true;
berghofe@15472
  1036
wenzelm@14786
  1037
fun string_of_vname (x, i) =
wenzelm@14786
  1038
  let
wenzelm@15986
  1039
    val question_mark = if ! show_question_marks then "?" else "";
wenzelm@15986
  1040
    val idx = string_of_int i;
wenzelm@15986
  1041
    val dot =
wenzelm@15986
  1042
      (case rev (Symbol.explode x) of
wenzelm@15986
  1043
        _ :: "\\<^isub>" :: _ => false
wenzelm@15986
  1044
      | _ :: "\\<^isup>" :: _ => false
wenzelm@15986
  1045
      | c :: _ => Symbol.is_digit c
wenzelm@15986
  1046
      | _ => true);
wenzelm@14786
  1047
  in
wenzelm@15986
  1048
    if dot then question_mark ^ x ^ "." ^ idx
wenzelm@15986
  1049
    else if i <> 0 then question_mark ^ x ^ idx
wenzelm@15986
  1050
    else question_mark ^ x
wenzelm@14786
  1051
  end;
wenzelm@14786
  1052
wenzelm@14786
  1053
fun string_of_vname' (x, ~1) = x
wenzelm@14786
  1054
  | string_of_vname' xi = string_of_vname xi;
wenzelm@14786
  1055
clasohm@1364
  1056
end;
clasohm@1364
  1057
wenzelm@4444
  1058
structure BasicTerm: BASIC_TERM = Term;
wenzelm@4444
  1059
open BasicTerm;