TFL/usyntax.sml
author mueller
Fri, 30 May 1997 16:37:20 +0200
changeset 3380 2986e3b1f86a
parent 3353 9112a2efb9a3
child 3391 5e45dd3b64e9
permissions -rw-r--r--
trivial changes to incorporate CTL.thy and Example.ML in html file;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3302
404fe31fd8d2 New headers and other minor changes
paulson
parents: 3245
diff changeset
     1
(*  Title:      TFL/usyntax
404fe31fd8d2 New headers and other minor changes
paulson
parents: 3245
diff changeset
     2
    ID:         $Id$
404fe31fd8d2 New headers and other minor changes
paulson
parents: 3245
diff changeset
     3
    Author:     Konrad Slind, Cambridge University Computer Laboratory
404fe31fd8d2 New headers and other minor changes
paulson
parents: 3245
diff changeset
     4
    Copyright   1997  University of Cambridge
404fe31fd8d2 New headers and other minor changes
paulson
parents: 3245
diff changeset
     5
404fe31fd8d2 New headers and other minor changes
paulson
parents: 3245
diff changeset
     6
Emulation of HOL's abstract syntax functions
404fe31fd8d2 New headers and other minor changes
paulson
parents: 3245
diff changeset
     7
*)
404fe31fd8d2 New headers and other minor changes
paulson
parents: 3245
diff changeset
     8
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     9
structure USyntax : USyntax_sig =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    10
struct
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    11
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    12
structure Utils = Utils;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    13
open Utils;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    14
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    15
infix 4 ##;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    16
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
    17
fun USYN_ERR{func,mesg} = Utils.ERR{module = "USyntax", func = func, mesg = mesg};
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    18
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    19
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    20
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    21
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    22
 *                            Types 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    23
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    24
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    25
val mk_prim_vartype = TVar;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    26
fun mk_vartype s = mk_prim_vartype((s,0),["term"]);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    27
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    28
(* But internally, it's useful *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    29
fun dest_vtype (TVar x) = x
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
    30
  | dest_vtype _ = raise USYN_ERR{func = "dest_vtype", 
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    31
                             mesg = "not a flexible type variable"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    32
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    33
val is_vartype = Utils.can dest_vtype;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    34
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    35
val type_vars  = map mk_prim_vartype o typ_tvars
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
    36
fun type_varsl L = Utils.mk_set (curry op=)
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
    37
                      (Utils.rev_itlist (curry op @ o type_vars) L []);
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    38
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    39
val alpha  = mk_vartype "'a"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    40
val beta   = mk_vartype "'b"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    41
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    42
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    43
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    44
(* What nonsense *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    45
nonfix -->; 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    46
val --> = -->;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    47
infixr 3 -->;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    48
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
    49
fun strip_type ty = (binder_types ty, body_type ty);
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    50
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
    51
fun strip_prod_type (Type("*", [ty1,ty2])) =
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
    52
	strip_prod_type ty1 @ strip_prod_type ty2
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
    53
  | strip_prod_type ty = [ty];
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    54
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    55
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    56
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    57
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    58
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    59
 *                              Terms 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    60
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    61
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    62
nonfix aconv;
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
    63
val aconv = curry (op aconv);
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    64
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    65
fun free_vars tm = add_term_frees(tm,[]);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    66
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    67
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    68
(* Free variables, in order of occurrence, from left to right in the 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    69
 * syntax tree. *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    70
fun free_vars_lr tm = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    71
  let fun memb x = let fun m[] = false | m(y::rst) = (x=y)orelse m rst in m end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    72
      fun add (t, frees) = case t of
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    73
            Free   _ => if (memb t frees) then frees else t::frees
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    74
          | Abs (_,_,body) => add(body,frees)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    75
          | f$t =>  add(t, add(f, frees))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    76
          | _ => frees
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    77
  in rev(add(tm,[]))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    78
  end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    79
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    80
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    81
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    82
fun free_varsl L = Utils.mk_set aconv
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
    83
                      (Utils.rev_itlist (curry op @ o free_vars) L []);
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    84
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    85
val type_vars_in_term = map mk_prim_vartype o term_tvars;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    86
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    87
(* Can't really be very exact in Isabelle *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    88
fun all_vars tm = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    89
  let fun memb x = let fun m[] = false | m(y::rst) = (x=y)orelse m rst in m end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    90
      fun add (t, A) = case t of
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    91
            Free   _ => if (memb t A) then A else t::A
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    92
          | Abs (s,ty,body) => add(body, add(Free(s,ty),A))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    93
          | f$t =>  add(t, add(f, A))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    94
          | _ => A
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    95
  in rev(add(tm,[]))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    96
  end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    97
fun all_varsl L = Utils.mk_set aconv
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
    98
                      (Utils.rev_itlist (curry op @ o all_vars) L []);
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    99
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   100
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   101
(* Prelogic *)
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   102
fun dest_tybinding (v,ty) = (#1(dest_vtype v),ty)
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   103
fun inst theta = subst_vars (map dest_tybinding theta,[])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   104
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   105
fun beta_conv((t1 as Abs _ ) $ t2) = betapply(t1,t2)
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   106
  | beta_conv _ = raise USYN_ERR{func = "beta_conv", mesg = "Not a beta-redex"};
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   107
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   108
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   109
(* Construction routines *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   110
fun mk_var{Name,Ty} = Free(Name,Ty);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   111
val mk_prim_var = Var;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   113
val string_variant = variant;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   114
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   115
local fun var_name(Var((Name,_),_)) = Name
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   116
        | var_name(Free(s,_)) = s
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   117
        | var_name _ = raise USYN_ERR{func = "variant",
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   118
                                 mesg = "list elem. is not a variable"}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   119
in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   120
fun variant [] v = v
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   121
  | variant vlist (Var((Name,i),ty)) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   122
       Var((string_variant (map var_name vlist) Name,i),ty)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   123
  | variant vlist (Free(Name,ty)) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   124
       Free(string_variant (map var_name vlist) Name,ty)
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   125
  | variant _ _ = raise USYN_ERR{func = "variant",
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   126
                            mesg = "2nd arg. should be a variable"}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   127
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   128
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   129
fun mk_const{Name,Ty} = Const(Name,Ty)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   130
fun mk_comb{Rator,Rand} = Rator $ Rand;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   131
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   132
fun mk_abs{Bvar as Var((s,_),ty),Body}  = Abs(s,ty,abstract_over(Bvar,Body))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   133
  | mk_abs{Bvar as Free(s,ty),Body}  = Abs(s,ty,abstract_over(Bvar,Body))
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   134
  | mk_abs _ = raise USYN_ERR{func = "mk_abs", mesg = "Bvar is not a variable"};
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   135
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   136
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   137
fun mk_imp{ant,conseq} = 
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   138
   let val c = mk_const{Name = "op -->", Ty = HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT}
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   139
   in list_comb(c,[ant,conseq])
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   140
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   141
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   142
fun mk_select (r as {Bvar,Body}) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   143
  let val ty = type_of Bvar
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   144
      val c = mk_const{Name = "Eps", Ty = (ty --> HOLogic.boolT) --> ty}
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   145
  in list_comb(c,[mk_abs r])
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   146
  end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   147
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   148
fun mk_forall (r as {Bvar,Body}) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   149
  let val ty = type_of Bvar
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   150
      val c = mk_const{Name = "All", Ty = (ty --> HOLogic.boolT) --> HOLogic.boolT}
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   151
  in list_comb(c,[mk_abs r])
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   152
  end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   153
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   154
fun mk_exists (r as {Bvar,Body}) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   155
  let val ty = type_of Bvar 
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   156
      val c = mk_const{Name = "Ex", Ty = (ty --> HOLogic.boolT) --> HOLogic.boolT}
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   157
  in list_comb(c,[mk_abs r])
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   158
  end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   159
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   160
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   161
fun mk_conj{conj1,conj2} =
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   162
   let val c = mk_const{Name = "op &", Ty = HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT}
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   163
   in list_comb(c,[conj1,conj2])
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   164
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   165
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   166
fun mk_disj{disj1,disj2} =
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   167
   let val c = mk_const{Name = "op |", Ty = HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT}
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   168
   in list_comb(c,[disj1,disj2])
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   169
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   170
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   171
fun prod_ty ty1 ty2 = Type("*", [ty1,ty2]);
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   172
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   173
local
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   174
fun mk_uncurry(xt,yt,zt) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   175
    mk_const{Name = "split", Ty = (xt --> yt --> zt) --> prod_ty xt yt --> zt}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   176
fun dest_pair(Const("Pair",_) $ M $ N) = {fst=M, snd=N}
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   177
  | dest_pair _ = raise USYN_ERR{func = "dest_pair", mesg = "not a pair"}
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   178
fun is_var(Var(_)) = true | is_var (Free _) = true | is_var _ = false
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   179
in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   180
fun mk_pabs{varstruct,body} = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   181
 let fun mpa(varstruct,body) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   182
       if (is_var varstruct)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   183
       then mk_abs{Bvar = varstruct, Body = body}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   184
       else let val {fst,snd} = dest_pair varstruct
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   185
            in mk_comb{Rator= mk_uncurry(type_of fst,type_of snd,type_of body),
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   186
                       Rand = mpa(fst,mpa(snd,body))}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   187
            end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   188
 in mpa(varstruct,body)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   189
 end
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   190
 handle _ => raise USYN_ERR{func = "mk_pabs", mesg = ""};
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   191
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   192
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   193
(* Destruction routines *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   194
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   195
datatype lambda = VAR   of {Name : string, Ty : typ}
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   196
                | CONST of {Name : string, Ty : typ}
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   197
                | COMB  of {Rator: term, Rand : term}
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   198
                | LAMB  of {Bvar : term, Body : term};
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   199
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   200
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   201
fun dest_term(Var((s,i),ty)) = VAR{Name = s, Ty = ty}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   202
  | dest_term(Free(s,ty))    = VAR{Name = s, Ty = ty}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   203
  | dest_term(Const(s,ty))   = CONST{Name = s, Ty = ty}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   204
  | dest_term(M$N)           = COMB{Rator=M,Rand=N}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   205
  | dest_term(Abs(s,ty,M))   = let  val v = mk_var{Name = s, Ty = ty}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   206
                               in LAMB{Bvar = v, Body = betapply (M,v)}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   207
                               end
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   208
  | dest_term(Bound _)       = raise USYN_ERR{func = "dest_term",mesg = "Bound"};
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   209
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   210
fun dest_const(Const(s,ty)) = {Name = s, Ty = ty}
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   211
  | dest_const _ = raise USYN_ERR{func = "dest_const", mesg = "not a constant"};
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   212
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   213
fun dest_comb(t1 $ t2) = {Rator = t1, Rand = t2}
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   214
  | dest_comb _ =  raise USYN_ERR{func = "dest_comb", mesg = "not a comb"};
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   215
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   216
fun dest_abs(a as Abs(s,ty,M)) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   217
     let val v = mk_var{Name = s, Ty = ty}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   218
     in {Bvar = v, Body = betapply (a,v)}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   219
     end
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   220
  | dest_abs _ =  raise USYN_ERR{func = "dest_abs", mesg = "not an abstraction"};
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   221
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   222
fun dest_eq(Const("op =",_) $ M $ N) = {lhs=M, rhs=N}
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   223
  | dest_eq _ = raise USYN_ERR{func = "dest_eq", mesg = "not an equality"};
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   224
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   225
fun dest_imp(Const("op -->",_) $ M $ N) = {ant=M, conseq=N}
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   226
  | dest_imp _ = raise USYN_ERR{func = "dest_imp", mesg = "not an implication"};
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   227
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   228
fun dest_select(Const("Eps",_) $ (a as Abs _)) = dest_abs a
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   229
  | dest_select _ = raise USYN_ERR{func = "dest_select", mesg = "not a select"};
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   230
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   231
fun dest_forall(Const("All",_) $ (a as Abs _)) = dest_abs a
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   232
  | dest_forall _ = raise USYN_ERR{func = "dest_forall", mesg = "not a forall"};
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   233
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   234
fun dest_exists(Const("Ex",_) $ (a as Abs _)) = dest_abs a
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   235
  | dest_exists _ = raise USYN_ERR{func = "dest_exists", mesg="not an existential"};
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   236
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   237
fun dest_neg(Const("not",_) $ M) = M
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   238
  | dest_neg _ = raise USYN_ERR{func = "dest_neg", mesg = "not a negation"};
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   239
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   240
fun dest_conj(Const("op &",_) $ M $ N) = {conj1=M, conj2=N}
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   241
  | dest_conj _ = raise USYN_ERR{func = "dest_conj", mesg = "not a conjunction"};
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   242
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   243
fun dest_disj(Const("op |",_) $ M $ N) = {disj1=M, disj2=N}
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   244
  | dest_disj _ = raise USYN_ERR{func = "dest_disj", mesg = "not a disjunction"};
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   245
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   246
fun mk_pair{fst,snd} = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   247
   let val ty1 = type_of fst
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   248
       val ty2 = type_of snd
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   249
       val c = mk_const{Name = "Pair", Ty = ty1 --> ty2 --> prod_ty ty1 ty2}
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   250
   in list_comb(c,[fst,snd])
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   251
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   252
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   253
fun dest_pair(Const("Pair",_) $ M $ N) = {fst=M, snd=N}
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   254
  | dest_pair _ = raise USYN_ERR{func = "dest_pair", mesg = "not a pair"};
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   255
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   256
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   257
local  fun ucheck t = (if #Name(dest_const t) = "split" then t
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   258
                       else raise Match)
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   259
in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   260
fun dest_pabs tm =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   261
   let val {Bvar,Body} = dest_abs tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   262
   in {varstruct = Bvar, body = Body}
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   263
   end 
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   264
    handle 
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   265
     _ => let val {Rator,Rand} = dest_comb tm
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   266
              val _ = ucheck Rator
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   267
              val {varstruct = lv,body} = dest_pabs Rand
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   268
              val {varstruct = rv,body} = dest_pabs body
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   269
          in {varstruct = mk_pair{fst = lv, snd = rv}, body = body}
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   270
          end
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   271
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   272
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   273
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   274
(* Garbage - ought to be dropped *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   275
val lhs   = #lhs o dest_eq
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   276
val rhs   = #rhs o dest_eq
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   277
val rator = #Rator o dest_comb
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   278
val rand  = #Rand o dest_comb
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   279
val bvar  = #Bvar o dest_abs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   280
val body  = #Body o dest_abs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   281
  
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   282
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   283
(* Query routines *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   284
val is_comb   = can dest_comb
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   285
val is_abs    = can dest_abs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   286
val is_eq     = can dest_eq
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   287
val is_imp    = can dest_imp
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   288
val is_forall = can dest_forall
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   289
val is_exists = can dest_exists
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   290
val is_neg    = can dest_neg
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   291
val is_conj   = can dest_conj
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   292
val is_disj   = can dest_disj
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   293
val is_pair   = can dest_pair
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   294
val is_pabs   = can dest_pabs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   295
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   296
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   297
(* Construction of a cterm from a list of Terms *)
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   298
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   299
fun list_mk_abs(L,tm) = itlist (fn v => fn M => mk_abs{Bvar=v, Body=M}) L tm;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   300
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   301
(* These others are almost never used *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   302
fun list_mk_imp(A,c) = itlist(fn a => fn tm => mk_imp{ant=a,conseq=tm}) A c;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   303
fun list_mk_exists(V,t) = itlist(fn v => fn b => mk_exists{Bvar=v, Body=b})V t;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   304
fun list_mk_forall(V,t) = itlist(fn v => fn b => mk_forall{Bvar=v, Body=b})V t;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   305
val list_mk_conj = end_itlist(fn c1 => fn tm => mk_conj{conj1=c1, conj2=tm})
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   306
val list_mk_disj = end_itlist(fn d1 => fn tm => mk_disj{disj1=d1, disj2=tm})
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   307
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   308
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   309
(* Need to reverse? *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   310
fun gen_all tm = list_mk_forall(free_vars tm, tm);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   311
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   312
(* Destructing a cterm to a list of Terms *)
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   313
fun strip_comb tm = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   314
   let fun dest(M$N, A) = dest(M, N::A)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   315
         | dest x = x
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   316
   in dest(tm,[])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   317
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   318
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   319
fun strip_abs(tm as Abs _) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   320
       let val {Bvar,Body} = dest_abs tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   321
           val (bvs, core) = strip_abs Body
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   322
       in (Bvar::bvs, core)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   323
       end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   324
  | strip_abs M = ([],M);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   325
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   326
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   327
fun strip_imp fm =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   328
   if (is_imp fm)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   329
   then let val {ant,conseq} = dest_imp fm
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   330
            val (was,wb) = strip_imp conseq
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   331
        in ((ant::was), wb)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   332
        end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   333
   else ([],fm);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   334
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   335
fun strip_forall fm =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   336
   if (is_forall fm)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   337
   then let val {Bvar,Body} = dest_forall fm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   338
            val (bvs,core) = strip_forall Body
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   339
        in ((Bvar::bvs), core)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   340
        end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   341
   else ([],fm);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   342
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   343
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   344
fun strip_exists fm =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   345
   if (is_exists fm)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   346
   then let val {Bvar, Body} = dest_exists fm 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   347
            val (bvs,core) = strip_exists Body
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   348
        in (Bvar::bvs, core)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   349
        end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   350
   else ([],fm);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   351
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   352
fun strip_conj w = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   353
   if (is_conj w)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   354
   then let val {conj1,conj2} = dest_conj w
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   355
        in (strip_conj conj1@strip_conj conj2)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   356
        end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   357
   else [w];
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   358
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   359
fun strip_disj w =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   360
   if (is_disj w)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   361
   then let val {disj1,disj2} = dest_disj w 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   362
        in (strip_disj disj1@strip_disj disj2)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   363
        end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   364
   else [w];
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   365
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   366
fun strip_pair tm = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   367
   if (is_pair tm) 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   368
   then let val {fst,snd} = dest_pair tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   369
            fun dtuple t =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   370
               if (is_pair t)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   371
               then let val{fst,snd} = dest_pair t
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   372
                    in (fst :: dtuple snd)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   373
                    end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   374
               else [t]
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   375
        in fst::dtuple snd
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   376
        end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   377
   else [tm];
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   378
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   379
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   380
fun mk_preterm tm = #t(rep_cterm tm);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   381
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   382
(* Miscellaneous *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   383
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   384
fun mk_vstruct ty V =
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   385
  let fun follow_prod_type (Type("*",[ty1,ty2])) vs =
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   386
	      let val (ltm,vs1) = follow_prod_type ty1 vs
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   387
		  val (rtm,vs2) = follow_prod_type ty2 vs1
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   388
	      in (mk_pair{fst=ltm, snd=rtm}, vs2) end
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   389
	| follow_prod_type _ (v::vs) = (v,vs)
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   390
  in #1 (follow_prod_type ty V)  end;
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   391
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   392
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   393
(* Search a term for a sub-term satisfying the predicate p. *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   394
fun find_term p =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   395
   let fun find tm =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   396
      if (p tm)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   397
      then tm 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   398
      else if (is_abs tm)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   399
           then find (#Body(dest_abs tm))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   400
           else let val {Rator,Rand} = dest_comb tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   401
                in find Rator handle _ => find Rand
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   402
                end handle _ => raise USYN_ERR{func = "find_term",mesg = ""}
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   403
   in find
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   404
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   405
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   406
(*******************************************************************
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   407
 * find_terms: (term -> HOLogic.boolT) -> term -> term list
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   408
 * 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   409
 *  Find all subterms in a term that satisfy a given predicate p.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   410
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   411
 *******************************************************************)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   412
fun find_terms p =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   413
   let fun accum tl tm =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   414
      let val tl' = if (p tm) then (tm::tl) else tl 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   415
      in if (is_abs tm)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   416
         then accum tl' (#Body(dest_abs tm))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   417
         else let val {Rator,Rand} = dest_comb tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   418
              in accum (accum tl' Rator) Rand
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   419
              end handle _ => tl'
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   420
      end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   421
   in accum []
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   422
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   423
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   424
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   425
fun dest_relation tm =
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   426
   if (type_of tm = HOLogic.boolT)
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   427
   then let val (Const("op :",_) $ (Const("Pair",_)$y$x) $ R) = tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   428
        in (R,y,x)
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   429
        end handle _ => raise USYN_ERR{func="dest_relation",
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   430
                                  mesg="unexpected term structure"}
3353
9112a2efb9a3 Removal of module Mask and datatype binding with its constructor |->
paulson
parents: 3330
diff changeset
   431
   else raise USYN_ERR{func="dest_relation",mesg="not a boolean term"};
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   432
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   433
fun is_WFR tm = (#Name(dest_const(rator tm)) = "wf") handle _ => false;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   434
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   435
fun ARB ty = mk_select{Bvar=mk_var{Name="v",Ty=ty},
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 2112
diff changeset
   436
                       Body=mk_const{Name="True",Ty=HOLogic.boolT}};
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   437
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   438
end; (* Syntax *)