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