TFL/dcterm.sml
author wenzelm
Fri, 07 Mar 1997 11:48:46 +0100
changeset 2754 59bd96046ad6
parent 2112 3902e9af752f
child 3245 241838c01caf
permissions -rw-r--r--
moved settings comment to build;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     1
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     2
 * Derived efficient cterm destructors.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     3
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     4
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     5
structure Dcterm :
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     6
sig
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     7
    val caconv : cterm -> cterm -> bool
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     8
    val mk_eq : cterm * cterm -> cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     9
    val mk_imp : cterm * cterm -> cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    10
    val mk_conj : cterm * cterm -> cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    11
    val mk_disj : cterm * cterm -> cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    12
    val mk_select : cterm * cterm -> cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    13
    val mk_forall : cterm * cterm -> cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    14
    val mk_exists : cterm * cterm -> cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    15
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    16
    val dest_conj : cterm -> cterm * cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    17
    val dest_const : cterm -> {Name:string, Ty:typ}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    18
    val dest_disj : cterm -> cterm * cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    19
    val dest_eq : cterm -> cterm * cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    20
    val dest_exists : cterm -> cterm * cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    21
    val dest_forall : cterm -> cterm * cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    22
    val dest_imp : cterm -> cterm * cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    23
    val dest_let : cterm -> cterm * cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    24
    val dest_neg : cterm -> cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    25
    val dest_pair : cterm -> cterm * cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    26
    val dest_select : cterm -> cterm * cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    27
    val dest_var : cterm -> {Name:string, Ty:typ}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    28
    val is_abs : cterm -> bool
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    29
    val is_comb : cterm -> bool
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    30
    val is_conj : cterm -> bool
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    31
    val is_cons : cterm -> bool
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    32
    val is_const : cterm -> bool
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    33
    val is_disj : cterm -> bool
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    34
    val is_eq : cterm -> bool
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    35
    val is_exists : cterm -> bool
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    36
    val is_forall : cterm -> bool
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    37
    val is_imp : cterm -> bool
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    38
    val is_let : cterm -> bool
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    39
    val is_neg : cterm -> bool
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    40
    val is_pair : cterm -> bool
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    41
    val is_select : cterm -> bool
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    42
    val is_var : cterm -> bool
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    43
    val list_mk_comb : cterm * cterm list -> cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    44
    val list_mk_abs : cterm list -> cterm -> cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    45
    val list_mk_imp : cterm list * cterm -> cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    46
    val list_mk_exists : cterm list * cterm -> cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    47
    val list_mk_forall : cterm list * cterm -> cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    48
    val list_mk_conj : cterm list -> cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    49
    val list_mk_disj : cterm list -> cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    50
    val strip_abs : cterm -> cterm list * cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    51
    val strip_comb : cterm -> cterm * cterm list
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    52
    val strip_conj : cterm -> cterm list
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    53
    val strip_disj : cterm -> cterm list
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    54
    val strip_exists : cterm -> cterm list * cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    55
    val strip_forall : cterm -> cterm list * cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    56
    val strip_imp : cterm -> cterm list * cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    57
    val strip_pair : cterm -> cterm list
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    58
    val drop_prop : cterm -> cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    59
    val mk_prop : cterm -> cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    60
  end =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    61
struct
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    62
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    63
fun ERR func mesg = Utils.ERR{module = "Dcterm", func = func, mesg = mesg};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    64
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    65
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    66
 * General support routines
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    67
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    68
val can = Utils.can;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    69
val quote = Utils.quote;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    70
fun swap (x,y) = (y,x);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    71
val bool = Type("bool",[]);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    72
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    73
fun itlist f L base_value =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    74
   let fun it [] = base_value
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    75
         | it (a::rst) = f a (it rst)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    76
   in it L 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    77
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    78
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    79
fun end_itlist f =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    80
let fun endit [] = raise ERR"end_itlist" "list too short"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    81
      | endit alist = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    82
         let val (base::ralist) = rev alist
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    83
         in itlist f (rev ralist) base  end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    84
in endit
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    85
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    86
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    87
fun rev_itlist f =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    88
   let fun rev_it [] base = base
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    89
         | rev_it (a::rst) base = rev_it rst (f a base)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    90
   in rev_it
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    91
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    92
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    93
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    94
 * Alpha conversion.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    95
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    96
fun caconv ctm1 ctm2 = Term.aconv(#t(rep_cterm ctm1),#t(rep_cterm ctm2));
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    97
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    98
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    99
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   100
 * Some simple constructor functions.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   101
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   102
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   103
fun mk_const sign pr = cterm_of sign (Const pr);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   104
val mk_hol_const = mk_const (sign_of HOL.thy);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   105
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   106
fun list_mk_comb (h,[]) = h
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   107
  | list_mk_comb (h,L) = rev_itlist (fn t1 => fn t2 => capply t2 t1) L h;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   108
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   109
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   110
fun mk_eq(lhs,rhs) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   111
   let val ty = #T(rep_cterm lhs)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   112
       val c = mk_hol_const("op =", ty --> ty --> bool)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   113
   in list_mk_comb(c,[lhs,rhs])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   114
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   115
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   116
local val c = mk_hol_const("op -->", bool --> bool --> bool)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   117
in fun mk_imp(ant,conseq) = list_mk_comb(c,[ant,conseq])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   118
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   119
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   120
fun mk_select (r as (Bvar,Body)) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   121
  let val ty = #T(rep_cterm Bvar)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   122
      val c = mk_hol_const("Eps", (ty --> bool) --> ty)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   123
  in capply c (uncurry cabs r)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   124
  end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   125
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   126
fun mk_forall (r as (Bvar,Body)) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   127
  let val ty = #T(rep_cterm Bvar)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   128
      val c = mk_hol_const("All", (ty --> bool) --> bool)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   129
  in capply c (uncurry cabs r)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   130
  end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   131
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   132
fun mk_exists (r as (Bvar,Body)) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   133
  let val ty = #T(rep_cterm Bvar)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   134
      val c = mk_hol_const("Ex", (ty --> bool) --> bool)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   135
  in capply c (uncurry cabs r)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   136
  end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   137
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   138
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   139
local val c = mk_hol_const("op &", bool --> bool --> bool)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   140
in fun mk_conj(conj1,conj2) = capply (capply c conj1) conj2
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   141
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   142
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   143
local val c = mk_hol_const("op |", bool --> bool --> bool)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   144
in fun mk_disj(disj1,disj2) = capply (capply c disj1) disj2
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   145
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   146
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   147
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   148
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   149
 * The primitives.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   150
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   151
fun dest_const ctm = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   152
   (case #t(rep_cterm ctm)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   153
      of Const(s,ty) => {Name = s, Ty = ty}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   154
       | _ => raise ERR "dest_const" "not a constant");
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   155
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   156
fun dest_var ctm = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   157
   (case #t(rep_cterm ctm)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   158
      of Var((s,i),ty) => {Name=s, Ty=ty}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   159
       | Free(s,ty)    => {Name=s, Ty=ty}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   160
       |             _ => raise ERR "dest_var" "not a variable");
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   161
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   162
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   163
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   164
 * Derived destructor operations. 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   165
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   166
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   167
fun dest_monop expected tm = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   168
 let exception Fail
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   169
     val (c,N) = dest_comb tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   170
 in if (#Name(dest_const c) = expected) then N else raise Fail
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   171
 end handle e => raise ERR "dest_monop" ("Not a(n) "^quote expected);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   172
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   173
fun dest_binop expected tm =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   174
 let val (M,N) = dest_comb tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   175
 in (dest_monop expected M, N)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   176
 end handle e => raise ERR "dest_binop" ("Not a(n) "^quote expected);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   177
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   178
(* For "if-then-else" 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   179
fun dest_triop expected tm =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   180
 let val (MN,P) = dest_comb tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   181
     val (M,N) = dest_binop expected MN
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   182
 in (M,N,P)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   183
 end handle e => raise ERR "dest_triop" ("Not a(n) "^quote expected);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   184
*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   185
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   186
fun dest_binder expected tm = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   187
  dest_abs(dest_monop expected tm)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   188
  handle e => raise ERR "dest_binder" ("Not a(n) "^quote expected);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   189
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   190
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   191
val dest_neg    = dest_monop "not"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   192
val dest_pair   = dest_binop "Pair";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   193
val dest_eq     = dest_binop "op ="
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   194
val dest_imp    = dest_binop "op -->"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   195
val dest_conj   = dest_binop "op &"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   196
val dest_disj   = dest_binop "op |"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   197
val dest_cons   = dest_binop "op #"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   198
val dest_let    = swap o dest_binop "Let";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   199
(* val dest_cond   = dest_triop "if" *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   200
val dest_select = dest_binder "Eps"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   201
val dest_exists = dest_binder "Ex"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   202
val dest_forall = dest_binder "All"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   203
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   204
(* Query routines *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   205
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   206
val is_var    = can dest_var
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   207
val is_const  = can dest_const
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   208
val is_comb   = can dest_comb
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   209
val is_abs    = can dest_abs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   210
val is_eq     = can dest_eq
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   211
val is_imp    = can dest_imp
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   212
val is_select = can dest_select
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   213
val is_forall = can dest_forall
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   214
val is_exists = can dest_exists
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   215
val is_neg    = can dest_neg
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   216
val is_conj   = can dest_conj
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   217
val is_disj   = can dest_disj
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   218
(* val is_cond   = can dest_cond *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   219
val is_pair   = can dest_pair
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   220
val is_let    = can dest_let
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   221
val is_cons   = can dest_cons
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   222
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   223
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   224
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   225
 * Iterated creation.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   226
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   227
val list_mk_abs = itlist cabs;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   228
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   229
fun list_mk_imp(A,c) = itlist(fn a => fn tm => mk_imp(a,tm)) A c;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   230
fun list_mk_exists(V,t) = itlist(fn v => fn b => mk_exists(v, b)) V t;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   231
fun list_mk_forall(V,t) = itlist(fn v => fn b => mk_forall(v, b)) V t;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   232
val list_mk_conj = end_itlist(fn c1 => fn tm => mk_conj(c1, tm))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   233
val list_mk_disj = end_itlist(fn d1 => fn tm => mk_disj(d1, tm))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   234
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   235
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   236
 * Iterated destruction. (To the "right" in a term.)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   237
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   238
fun strip break tm = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   239
  let fun dest (p as (ctm,accum)) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   240
        let val (M,N) = break ctm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   241
        in dest (N, M::accum)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   242
        end handle _ => p
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   243
  in dest (tm,[])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   244
  end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   245
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   246
fun rev2swap (x,l) = (rev l, x);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   247
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   248
val strip_comb   = strip (swap o dest_comb)  (* Goes to the "left" *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   249
val strip_imp    = rev2swap o strip dest_imp
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   250
val strip_abs    = rev2swap o strip dest_abs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   251
val strip_forall = rev2swap o strip dest_forall
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   252
val strip_exists = rev2swap o strip dest_exists
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   253
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   254
val strip_conj   = rev o (op::) o strip dest_conj
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   255
val strip_disj   = rev o (op::) o strip dest_disj
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   256
val strip_pair   = rev o (op::) o strip dest_pair;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   257
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   258
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   259
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   260
 * Going into and out of prop
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   261
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   262
local val prop = cterm_of (sign_of HOL.thy) (read"Trueprop")
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   263
in fun mk_prop ctm =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   264
     case (#t(rep_cterm ctm))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   265
       of (Const("Trueprop",_)$_) => ctm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   266
        |  _ => capply prop ctm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   267
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   268
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   269
fun drop_prop ctm = dest_monop "Trueprop" ctm handle _ => ctm;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   270
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   271
end;