TFL/usyntax.ML
author wenzelm
Fri Oct 12 12:08:04 2001 +0200 (2001-10-12)
changeset 11729 a7da2e8b5762
parent 10769 70b9b0cfe05f
child 12340 24d31d47af1a
permissions -rw-r--r--
removed read_inst', no longer export insts';
wenzelm@10769
     1
(*  Title:      TFL/usyntax.ML
wenzelm@10769
     2
    ID:         $Id$
wenzelm@10769
     3
    Author:     Konrad Slind, Cambridge University Computer Laboratory
wenzelm@10769
     4
    Copyright   1997  University of Cambridge
wenzelm@10769
     5
wenzelm@10769
     6
Emulation of HOL's abstract syntax functions.
wenzelm@10769
     7
*)
wenzelm@10769
     8
wenzelm@10769
     9
signature USYNTAX =
wenzelm@10769
    10
sig
wenzelm@10769
    11
  datatype lambda = VAR   of {Name : string, Ty : typ}
wenzelm@10769
    12
                  | CONST of {Name : string, Ty : typ}
wenzelm@10769
    13
                  | COMB  of {Rator: term, Rand : term}
wenzelm@10769
    14
                  | LAMB  of {Bvar : term, Body : term}
wenzelm@10769
    15
wenzelm@10769
    16
  val alpha : typ
wenzelm@10769
    17
wenzelm@10769
    18
  (* Types *)
wenzelm@10769
    19
  val type_vars  : typ -> typ list
wenzelm@10769
    20
  val type_varsl : typ list -> typ list
wenzelm@10769
    21
  val mk_vartype : string -> typ
wenzelm@10769
    22
  val is_vartype : typ -> bool
wenzelm@10769
    23
  val strip_prod_type : typ -> typ list
wenzelm@10769
    24
wenzelm@10769
    25
  (* Terms *)
wenzelm@10769
    26
  val free_vars_lr : term -> term list
wenzelm@10769
    27
  val type_vars_in_term : term -> typ list
wenzelm@10769
    28
  val dest_term  : term -> lambda
wenzelm@10769
    29
wenzelm@10769
    30
  (* Prelogic *)
wenzelm@10769
    31
  val inst      : (typ*typ) list -> term -> term
wenzelm@10769
    32
wenzelm@10769
    33
  (* Construction routines *)
wenzelm@10769
    34
  val mk_abs    :{Bvar  : term, Body : term} -> term
wenzelm@10769
    35
wenzelm@10769
    36
  val mk_imp    :{ant : term, conseq :  term} -> term
wenzelm@10769
    37
  val mk_select :{Bvar : term, Body : term} -> term
wenzelm@10769
    38
  val mk_forall :{Bvar : term, Body : term} -> term
wenzelm@10769
    39
  val mk_exists :{Bvar : term, Body : term} -> term
wenzelm@10769
    40
  val mk_conj   :{conj1 : term, conj2 : term} -> term
wenzelm@10769
    41
  val mk_disj   :{disj1 : term, disj2 : term} -> term
wenzelm@10769
    42
  val mk_pabs   :{varstruct : term, body : term} -> term
wenzelm@10769
    43
wenzelm@10769
    44
  (* Destruction routines *)
wenzelm@10769
    45
  val dest_const: term -> {Name : string, Ty : typ}
wenzelm@10769
    46
  val dest_comb : term -> {Rator : term, Rand : term}
wenzelm@10769
    47
  val dest_abs  : string list -> term -> {Bvar : term, Body : term} * string list
wenzelm@10769
    48
  val dest_eq     : term -> {lhs : term, rhs : term}
wenzelm@10769
    49
  val dest_imp    : term -> {ant : term, conseq : term}
wenzelm@10769
    50
  val dest_forall : term -> {Bvar : term, Body : term}
wenzelm@10769
    51
  val dest_exists : term -> {Bvar : term, Body : term}
wenzelm@10769
    52
  val dest_neg    : term -> term
wenzelm@10769
    53
  val dest_conj   : term -> {conj1 : term, conj2 : term}
wenzelm@10769
    54
  val dest_disj   : term -> {disj1 : term, disj2 : term}
wenzelm@10769
    55
  val dest_pair   : term -> {fst : term, snd : term}
wenzelm@10769
    56
  val dest_pabs   : string list -> term -> {varstruct : term, body : term, used : string list}
wenzelm@10769
    57
wenzelm@10769
    58
  val lhs   : term -> term
wenzelm@10769
    59
  val rhs   : term -> term
wenzelm@10769
    60
  val rand  : term -> term
wenzelm@10769
    61
wenzelm@10769
    62
  (* Query routines *)
wenzelm@10769
    63
  val is_imp    : term -> bool
wenzelm@10769
    64
  val is_forall : term -> bool
wenzelm@10769
    65
  val is_exists : term -> bool
wenzelm@10769
    66
  val is_neg    : term -> bool
wenzelm@10769
    67
  val is_conj   : term -> bool
wenzelm@10769
    68
  val is_disj   : term -> bool
wenzelm@10769
    69
  val is_pair   : term -> bool
wenzelm@10769
    70
  val is_pabs   : term -> bool
wenzelm@10769
    71
wenzelm@10769
    72
  (* Construction of a term from a list of Preterms *)
wenzelm@10769
    73
  val list_mk_abs    : (term list * term) -> term
wenzelm@10769
    74
  val list_mk_imp    : (term list * term) -> term
wenzelm@10769
    75
  val list_mk_forall : (term list * term) -> term
wenzelm@10769
    76
  val list_mk_conj   : term list -> term
wenzelm@10769
    77
wenzelm@10769
    78
  (* Destructing a term to a list of Preterms *)
wenzelm@10769
    79
  val strip_comb     : term -> (term * term list)
wenzelm@10769
    80
  val strip_abs      : term -> (term list * term)
wenzelm@10769
    81
  val strip_imp      : term -> (term list * term)
wenzelm@10769
    82
  val strip_forall   : term -> (term list * term)
wenzelm@10769
    83
  val strip_exists   : term -> (term list * term)
wenzelm@10769
    84
  val strip_disj     : term -> term list
wenzelm@10769
    85
wenzelm@10769
    86
  (* Miscellaneous *)
wenzelm@10769
    87
  val mk_vstruct : typ -> term list -> term
wenzelm@10769
    88
  val gen_all    : term -> term
wenzelm@10769
    89
  val find_term  : (term -> bool) -> term -> term option
wenzelm@10769
    90
  val dest_relation : term -> term * term * term
wenzelm@10769
    91
  val is_WFR : term -> bool
wenzelm@10769
    92
  val ARB : typ -> term
wenzelm@10769
    93
end;
wenzelm@10769
    94
wenzelm@10769
    95
structure USyntax: USYNTAX =
wenzelm@10769
    96
struct
wenzelm@10769
    97
wenzelm@10769
    98
infix 4 ##;
wenzelm@10769
    99
wenzelm@10769
   100
fun USYN_ERR func mesg = Utils.ERR {module = "USyntax", func = func, mesg = mesg};
wenzelm@10769
   101
wenzelm@10769
   102
wenzelm@10769
   103
(*---------------------------------------------------------------------------
wenzelm@10769
   104
 *
wenzelm@10769
   105
 *                            Types
wenzelm@10769
   106
 *
wenzelm@10769
   107
 *---------------------------------------------------------------------------*)
wenzelm@10769
   108
val mk_prim_vartype = TVar;
wenzelm@10769
   109
fun mk_vartype s = mk_prim_vartype ((s, 0), HOLogic.termS);
wenzelm@10769
   110
wenzelm@10769
   111
(* But internally, it's useful *)
wenzelm@10769
   112
fun dest_vtype (TVar x) = x
wenzelm@10769
   113
  | dest_vtype _ = raise USYN_ERR "dest_vtype" "not a flexible type variable";
wenzelm@10769
   114
wenzelm@10769
   115
val is_vartype = can dest_vtype;
wenzelm@10769
   116
wenzelm@10769
   117
val type_vars  = map mk_prim_vartype o typ_tvars
wenzelm@10769
   118
fun type_varsl L = distinct (Utils.rev_itlist (curry op @ o type_vars) L []);
wenzelm@10769
   119
wenzelm@10769
   120
val alpha  = mk_vartype "'a"
wenzelm@10769
   121
val beta   = mk_vartype "'b"
wenzelm@10769
   122
wenzelm@10769
   123
val strip_prod_type = HOLogic.prodT_factors;
wenzelm@10769
   124
wenzelm@10769
   125
wenzelm@10769
   126
wenzelm@10769
   127
(*---------------------------------------------------------------------------
wenzelm@10769
   128
 *
wenzelm@10769
   129
 *                              Terms
wenzelm@10769
   130
 *
wenzelm@10769
   131
 *---------------------------------------------------------------------------*)
wenzelm@10769
   132
wenzelm@10769
   133
(* Free variables, in order of occurrence, from left to right in the
wenzelm@10769
   134
 * syntax tree. *)
wenzelm@10769
   135
fun free_vars_lr tm =
wenzelm@10769
   136
  let fun memb x = let fun m[] = false | m(y::rst) = (x=y)orelse m rst in m end
wenzelm@10769
   137
      fun add (t, frees) = case t of
wenzelm@10769
   138
            Free   _ => if (memb t frees) then frees else t::frees
wenzelm@10769
   139
          | Abs (_,_,body) => add(body,frees)
wenzelm@10769
   140
          | f$t =>  add(t, add(f, frees))
wenzelm@10769
   141
          | _ => frees
wenzelm@10769
   142
  in rev(add(tm,[]))
wenzelm@10769
   143
  end;
wenzelm@10769
   144
wenzelm@10769
   145
wenzelm@10769
   146
wenzelm@10769
   147
val type_vars_in_term = map mk_prim_vartype o term_tvars;
wenzelm@10769
   148
wenzelm@10769
   149
wenzelm@10769
   150
wenzelm@10769
   151
(* Prelogic *)
wenzelm@10769
   152
fun dest_tybinding (v,ty) = (#1(dest_vtype v),ty)
wenzelm@10769
   153
fun inst theta = subst_vars (map dest_tybinding theta,[])
wenzelm@10769
   154
wenzelm@10769
   155
wenzelm@10769
   156
(* Construction routines *)
wenzelm@10769
   157
wenzelm@10769
   158
fun mk_abs{Bvar as Var((s,_),ty),Body}  = Abs(s,ty,abstract_over(Bvar,Body))
wenzelm@10769
   159
  | mk_abs{Bvar as Free(s,ty),Body}  = Abs(s,ty,abstract_over(Bvar,Body))
wenzelm@10769
   160
  | mk_abs _ = raise USYN_ERR "mk_abs" "Bvar is not a variable";
wenzelm@10769
   161
wenzelm@10769
   162
wenzelm@10769
   163
fun mk_imp{ant,conseq} =
wenzelm@10769
   164
   let val c = Const("op -->",HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT)
wenzelm@10769
   165
   in list_comb(c,[ant,conseq])
wenzelm@10769
   166
   end;
wenzelm@10769
   167
wenzelm@10769
   168
fun mk_select (r as {Bvar,Body}) =
wenzelm@10769
   169
  let val ty = type_of Bvar
wenzelm@10769
   170
      val c = Const("Eps",(ty --> HOLogic.boolT) --> ty)
wenzelm@10769
   171
  in list_comb(c,[mk_abs r])
wenzelm@10769
   172
  end;
wenzelm@10769
   173
wenzelm@10769
   174
fun mk_forall (r as {Bvar,Body}) =
wenzelm@10769
   175
  let val ty = type_of Bvar
wenzelm@10769
   176
      val c = Const("All",(ty --> HOLogic.boolT) --> HOLogic.boolT)
wenzelm@10769
   177
  in list_comb(c,[mk_abs r])
wenzelm@10769
   178
  end;
wenzelm@10769
   179
wenzelm@10769
   180
fun mk_exists (r as {Bvar,Body}) =
wenzelm@10769
   181
  let val ty = type_of Bvar
wenzelm@10769
   182
      val c = Const("Ex",(ty --> HOLogic.boolT) --> HOLogic.boolT)
wenzelm@10769
   183
  in list_comb(c,[mk_abs r])
wenzelm@10769
   184
  end;
wenzelm@10769
   185
wenzelm@10769
   186
wenzelm@10769
   187
fun mk_conj{conj1,conj2} =
wenzelm@10769
   188
   let val c = Const("op &",HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT)
wenzelm@10769
   189
   in list_comb(c,[conj1,conj2])
wenzelm@10769
   190
   end;
wenzelm@10769
   191
wenzelm@10769
   192
fun mk_disj{disj1,disj2} =
wenzelm@10769
   193
   let val c = Const("op |",HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT)
wenzelm@10769
   194
   in list_comb(c,[disj1,disj2])
wenzelm@10769
   195
   end;
wenzelm@10769
   196
wenzelm@10769
   197
fun prod_ty ty1 ty2 = HOLogic.mk_prodT (ty1,ty2);
wenzelm@10769
   198
wenzelm@10769
   199
local
wenzelm@10769
   200
fun mk_uncurry(xt,yt,zt) =
wenzelm@10769
   201
    Const("split",(xt --> yt --> zt) --> prod_ty xt yt --> zt)
wenzelm@10769
   202
fun dest_pair(Const("Pair",_) $ M $ N) = {fst=M, snd=N}
wenzelm@10769
   203
  | dest_pair _ = raise USYN_ERR "dest_pair" "not a pair"
wenzelm@10769
   204
fun is_var (Var _) = true | is_var (Free _) = true | is_var _ = false
wenzelm@10769
   205
in
wenzelm@10769
   206
fun mk_pabs{varstruct,body} =
wenzelm@10769
   207
 let fun mpa (varstruct, body) =
wenzelm@10769
   208
       if is_var varstruct
wenzelm@10769
   209
       then mk_abs {Bvar = varstruct, Body = body}
wenzelm@10769
   210
       else let val {fst, snd} = dest_pair varstruct
wenzelm@10769
   211
            in mk_uncurry (type_of fst, type_of snd, type_of body) $
wenzelm@10769
   212
               mpa (fst, mpa (snd, body))
wenzelm@10769
   213
            end
wenzelm@10769
   214
 in mpa (varstruct, body) end
wenzelm@10769
   215
 handle TYPE _ => raise USYN_ERR "mk_pabs" "";
wenzelm@10769
   216
end;
wenzelm@10769
   217
wenzelm@10769
   218
(* Destruction routines *)
wenzelm@10769
   219
wenzelm@10769
   220
datatype lambda = VAR   of {Name : string, Ty : typ}
wenzelm@10769
   221
                | CONST of {Name : string, Ty : typ}
wenzelm@10769
   222
                | COMB  of {Rator: term, Rand : term}
wenzelm@10769
   223
                | LAMB  of {Bvar : term, Body : term};
wenzelm@10769
   224
wenzelm@10769
   225
wenzelm@10769
   226
fun dest_term(Var((s,i),ty)) = VAR{Name = s, Ty = ty}
wenzelm@10769
   227
  | dest_term(Free(s,ty))    = VAR{Name = s, Ty = ty}
wenzelm@10769
   228
  | dest_term(Const(s,ty))   = CONST{Name = s, Ty = ty}
wenzelm@10769
   229
  | dest_term(M$N)           = COMB{Rator=M,Rand=N}
wenzelm@10769
   230
  | dest_term(Abs(s,ty,M))   = let  val v = Free(s,ty)
wenzelm@10769
   231
                               in LAMB{Bvar = v, Body = betapply (M,v)}
wenzelm@10769
   232
                               end
wenzelm@10769
   233
  | dest_term(Bound _)       = raise USYN_ERR "dest_term" "Bound";
wenzelm@10769
   234
wenzelm@10769
   235
fun dest_const(Const(s,ty)) = {Name = s, Ty = ty}
wenzelm@10769
   236
  | dest_const _ = raise USYN_ERR "dest_const" "not a constant";
wenzelm@10769
   237
wenzelm@10769
   238
fun dest_comb(t1 $ t2) = {Rator = t1, Rand = t2}
wenzelm@10769
   239
  | dest_comb _ =  raise USYN_ERR "dest_comb" "not a comb";
wenzelm@10769
   240
wenzelm@10769
   241
fun dest_abs used (a as Abs(s, ty, M)) =
wenzelm@10769
   242
     let
wenzelm@10769
   243
       val s' = variant used s;
wenzelm@10769
   244
       val v = Free(s', ty);
wenzelm@10769
   245
     in ({Bvar = v, Body = betapply (a,v)}, s'::used)
wenzelm@10769
   246
     end
wenzelm@10769
   247
  | dest_abs _ _ =  raise USYN_ERR "dest_abs" "not an abstraction";
wenzelm@10769
   248
wenzelm@10769
   249
fun dest_eq(Const("op =",_) $ M $ N) = {lhs=M, rhs=N}
wenzelm@10769
   250
  | dest_eq _ = raise USYN_ERR "dest_eq" "not an equality";
wenzelm@10769
   251
wenzelm@10769
   252
fun dest_imp(Const("op -->",_) $ M $ N) = {ant=M, conseq=N}
wenzelm@10769
   253
  | dest_imp _ = raise USYN_ERR "dest_imp" "not an implication";
wenzelm@10769
   254
wenzelm@10769
   255
fun dest_forall(Const("All",_) $ (a as Abs _)) = fst (dest_abs [] a)
wenzelm@10769
   256
  | dest_forall _ = raise USYN_ERR "dest_forall" "not a forall";
wenzelm@10769
   257
wenzelm@10769
   258
fun dest_exists(Const("Ex",_) $ (a as Abs _)) = fst (dest_abs [] a)
wenzelm@10769
   259
  | dest_exists _ = raise USYN_ERR "dest_exists" "not an existential";
wenzelm@10769
   260
wenzelm@10769
   261
fun dest_neg(Const("not",_) $ M) = M
wenzelm@10769
   262
  | dest_neg _ = raise USYN_ERR "dest_neg" "not a negation";
wenzelm@10769
   263
wenzelm@10769
   264
fun dest_conj(Const("op &",_) $ M $ N) = {conj1=M, conj2=N}
wenzelm@10769
   265
  | dest_conj _ = raise USYN_ERR "dest_conj" "not a conjunction";
wenzelm@10769
   266
wenzelm@10769
   267
fun dest_disj(Const("op |",_) $ M $ N) = {disj1=M, disj2=N}
wenzelm@10769
   268
  | dest_disj _ = raise USYN_ERR "dest_disj" "not a disjunction";
wenzelm@10769
   269
wenzelm@10769
   270
fun mk_pair{fst,snd} =
wenzelm@10769
   271
   let val ty1 = type_of fst
wenzelm@10769
   272
       val ty2 = type_of snd
wenzelm@10769
   273
       val c = Const("Pair",ty1 --> ty2 --> prod_ty ty1 ty2)
wenzelm@10769
   274
   in list_comb(c,[fst,snd])
wenzelm@10769
   275
   end;
wenzelm@10769
   276
wenzelm@10769
   277
fun dest_pair(Const("Pair",_) $ M $ N) = {fst=M, snd=N}
wenzelm@10769
   278
  | dest_pair _ = raise USYN_ERR "dest_pair" "not a pair";
wenzelm@10769
   279
wenzelm@10769
   280
wenzelm@10769
   281
local  fun ucheck t = (if #Name(dest_const t) = "split" then t
wenzelm@10769
   282
                       else raise Match)
wenzelm@10769
   283
in
wenzelm@10769
   284
fun dest_pabs used tm =
wenzelm@10769
   285
   let val ({Bvar,Body}, used') = dest_abs used tm
wenzelm@10769
   286
   in {varstruct = Bvar, body = Body, used = used'}
wenzelm@10769
   287
   end handle Utils.ERR _ =>
wenzelm@10769
   288
          let val {Rator,Rand} = dest_comb tm
wenzelm@10769
   289
              val _ = ucheck Rator
wenzelm@10769
   290
              val {varstruct = lv, body, used = used'} = dest_pabs used Rand
wenzelm@10769
   291
              val {varstruct = rv, body, used = used''} = dest_pabs used' body
wenzelm@10769
   292
          in {varstruct = mk_pair {fst = lv, snd = rv}, body = body, used = used''}
wenzelm@10769
   293
          end
wenzelm@10769
   294
end;
wenzelm@10769
   295
wenzelm@10769
   296
wenzelm@10769
   297
val lhs   = #lhs o dest_eq
wenzelm@10769
   298
val rhs   = #rhs o dest_eq
wenzelm@10769
   299
val rand  = #Rand o dest_comb
wenzelm@10769
   300
wenzelm@10769
   301
wenzelm@10769
   302
(* Query routines *)
wenzelm@10769
   303
val is_imp    = can dest_imp
wenzelm@10769
   304
val is_forall = can dest_forall
wenzelm@10769
   305
val is_exists = can dest_exists
wenzelm@10769
   306
val is_neg    = can dest_neg
wenzelm@10769
   307
val is_conj   = can dest_conj
wenzelm@10769
   308
val is_disj   = can dest_disj
wenzelm@10769
   309
val is_pair   = can dest_pair
wenzelm@10769
   310
val is_pabs   = can (dest_pabs [])
wenzelm@10769
   311
wenzelm@10769
   312
wenzelm@10769
   313
(* Construction of a cterm from a list of Terms *)
wenzelm@10769
   314
wenzelm@10769
   315
fun list_mk_abs(L,tm) = Utils.itlist (fn v => fn M => mk_abs{Bvar=v, Body=M}) L tm;
wenzelm@10769
   316
wenzelm@10769
   317
(* These others are almost never used *)
wenzelm@10769
   318
fun list_mk_imp(A,c) = Utils.itlist(fn a => fn tm => mk_imp{ant=a,conseq=tm}) A c;
wenzelm@10769
   319
fun list_mk_forall(V,t) = Utils.itlist(fn v => fn b => mk_forall{Bvar=v, Body=b})V t;
wenzelm@10769
   320
val list_mk_conj = Utils.end_itlist(fn c1 => fn tm => mk_conj{conj1=c1, conj2=tm})
wenzelm@10769
   321
wenzelm@10769
   322
wenzelm@10769
   323
(* Need to reverse? *)
wenzelm@10769
   324
fun gen_all tm = list_mk_forall(term_frees tm, tm);
wenzelm@10769
   325
wenzelm@10769
   326
(* Destructing a cterm to a list of Terms *)
wenzelm@10769
   327
fun strip_comb tm =
wenzelm@10769
   328
   let fun dest(M$N, A) = dest(M, N::A)
wenzelm@10769
   329
         | dest x = x
wenzelm@10769
   330
   in dest(tm,[])
wenzelm@10769
   331
   end;
wenzelm@10769
   332
wenzelm@10769
   333
fun strip_abs(tm as Abs _) =
wenzelm@10769
   334
       let val ({Bvar,Body}, _) = dest_abs [] tm
wenzelm@10769
   335
           val (bvs, core) = strip_abs Body
wenzelm@10769
   336
       in (Bvar::bvs, core)
wenzelm@10769
   337
       end
wenzelm@10769
   338
  | strip_abs M = ([],M);
wenzelm@10769
   339
wenzelm@10769
   340
wenzelm@10769
   341
fun strip_imp fm =
wenzelm@10769
   342
   if (is_imp fm)
wenzelm@10769
   343
   then let val {ant,conseq} = dest_imp fm
wenzelm@10769
   344
            val (was,wb) = strip_imp conseq
wenzelm@10769
   345
        in ((ant::was), wb)
wenzelm@10769
   346
        end
wenzelm@10769
   347
   else ([],fm);
wenzelm@10769
   348
wenzelm@10769
   349
fun strip_forall fm =
wenzelm@10769
   350
   if (is_forall fm)
wenzelm@10769
   351
   then let val {Bvar,Body} = dest_forall fm
wenzelm@10769
   352
            val (bvs,core) = strip_forall Body
wenzelm@10769
   353
        in ((Bvar::bvs), core)
wenzelm@10769
   354
        end
wenzelm@10769
   355
   else ([],fm);
wenzelm@10769
   356
wenzelm@10769
   357
wenzelm@10769
   358
fun strip_exists fm =
wenzelm@10769
   359
   if (is_exists fm)
wenzelm@10769
   360
   then let val {Bvar, Body} = dest_exists fm
wenzelm@10769
   361
            val (bvs,core) = strip_exists Body
wenzelm@10769
   362
        in (Bvar::bvs, core)
wenzelm@10769
   363
        end
wenzelm@10769
   364
   else ([],fm);
wenzelm@10769
   365
wenzelm@10769
   366
fun strip_disj w =
wenzelm@10769
   367
   if (is_disj w)
wenzelm@10769
   368
   then let val {disj1,disj2} = dest_disj w
wenzelm@10769
   369
        in (strip_disj disj1@strip_disj disj2)
wenzelm@10769
   370
        end
wenzelm@10769
   371
   else [w];
wenzelm@10769
   372
wenzelm@10769
   373
wenzelm@10769
   374
(* Miscellaneous *)
wenzelm@10769
   375
wenzelm@10769
   376
fun mk_vstruct ty V =
wenzelm@10769
   377
  let fun follow_prod_type (Type("*",[ty1,ty2])) vs =
wenzelm@10769
   378
              let val (ltm,vs1) = follow_prod_type ty1 vs
wenzelm@10769
   379
                  val (rtm,vs2) = follow_prod_type ty2 vs1
wenzelm@10769
   380
              in (mk_pair{fst=ltm, snd=rtm}, vs2) end
wenzelm@10769
   381
        | follow_prod_type _ (v::vs) = (v,vs)
wenzelm@10769
   382
  in #1 (follow_prod_type ty V)  end;
wenzelm@10769
   383
wenzelm@10769
   384
wenzelm@10769
   385
(* Search a term for a sub-term satisfying the predicate p. *)
wenzelm@10769
   386
fun find_term p =
wenzelm@10769
   387
   let fun find tm =
wenzelm@10769
   388
      if (p tm) then Some tm
wenzelm@10769
   389
      else case tm of
wenzelm@10769
   390
          Abs(_,_,body) => find body
wenzelm@10769
   391
        | (t$u)         => (case find t of None => find u | some => some)
wenzelm@10769
   392
        | _             => None
wenzelm@10769
   393
   in find
wenzelm@10769
   394
   end;
wenzelm@10769
   395
wenzelm@10769
   396
fun dest_relation tm =
wenzelm@10769
   397
   if (type_of tm = HOLogic.boolT)
wenzelm@10769
   398
   then let val (Const("op :",_) $ (Const("Pair",_)$y$x) $ R) = tm
wenzelm@10769
   399
        in (R,y,x)
wenzelm@10769
   400
        end handle Bind => raise USYN_ERR "dest_relation" "unexpected term structure"
wenzelm@10769
   401
   else raise USYN_ERR "dest_relation" "not a boolean term";
wenzelm@10769
   402
wenzelm@10769
   403
fun is_WFR (Const("Wellfounded_Recursion.wf",_)$_) = true
wenzelm@10769
   404
  | is_WFR _                 = false;
wenzelm@10769
   405
wenzelm@10769
   406
fun ARB ty = mk_select{Bvar=Free("v",ty),
wenzelm@10769
   407
                       Body=Const("True",HOLogic.boolT)};
wenzelm@10769
   408
wenzelm@10769
   409
end;