TFL/dcterm.ML
author paulson
Wed Jul 25 13:13:01 2001 +0200 (2001-07-25)
changeset 11451 8abfb4f7bd02
parent 10769 70b9b0cfe05f
child 13182 21851696dbf0
permissions -rw-r--r--
partial restructuring to reduce dependence on Axiom of Choice
wenzelm@10769
     1
(*  Title:      TFL/dcterm.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
wenzelm@10769
     7
(*---------------------------------------------------------------------------
wenzelm@10769
     8
 * Derived efficient cterm destructors.
wenzelm@10769
     9
 *---------------------------------------------------------------------------*)
wenzelm@10769
    10
wenzelm@10769
    11
signature DCTERM =
wenzelm@10769
    12
sig
wenzelm@10769
    13
  val dest_comb: cterm -> cterm * cterm
wenzelm@10769
    14
  val dest_abs: string option -> cterm -> cterm * cterm
wenzelm@10769
    15
  val capply: cterm -> cterm -> cterm
wenzelm@10769
    16
  val cabs: cterm -> cterm -> cterm
wenzelm@10769
    17
  val mk_conj: cterm * cterm -> cterm
wenzelm@10769
    18
  val mk_disj: cterm * cterm -> cterm
wenzelm@10769
    19
  val mk_exists: cterm * cterm -> cterm
wenzelm@10769
    20
  val dest_conj: cterm -> cterm * cterm
wenzelm@10769
    21
  val dest_const: cterm -> {Name: string, Ty: typ}
wenzelm@10769
    22
  val dest_disj: cterm -> cterm * cterm
wenzelm@10769
    23
  val dest_eq: cterm -> cterm * cterm
wenzelm@10769
    24
  val dest_exists: cterm -> cterm * cterm
wenzelm@10769
    25
  val dest_forall: cterm -> cterm * cterm
wenzelm@10769
    26
  val dest_imp: cterm -> cterm * cterm
wenzelm@10769
    27
  val dest_let: cterm -> cterm * cterm
wenzelm@10769
    28
  val dest_neg: cterm -> cterm
wenzelm@10769
    29
  val dest_pair: cterm -> cterm * cterm
wenzelm@10769
    30
  val dest_var: cterm -> {Name:string, Ty:typ}
wenzelm@10769
    31
  val is_conj: cterm -> bool
wenzelm@10769
    32
  val is_cons: cterm -> bool
wenzelm@10769
    33
  val is_disj: cterm -> bool
wenzelm@10769
    34
  val is_eq: cterm -> bool
wenzelm@10769
    35
  val is_exists: cterm -> bool
wenzelm@10769
    36
  val is_forall: cterm -> bool
wenzelm@10769
    37
  val is_imp: cterm -> bool
wenzelm@10769
    38
  val is_let: cterm -> bool
wenzelm@10769
    39
  val is_neg: cterm -> bool
wenzelm@10769
    40
  val is_pair: cterm -> bool
wenzelm@10769
    41
  val list_mk_disj: cterm list -> cterm
wenzelm@10769
    42
  val strip_abs: cterm -> cterm list * cterm
wenzelm@10769
    43
  val strip_comb: cterm -> cterm * cterm list
wenzelm@10769
    44
  val strip_disj: cterm -> cterm list
wenzelm@10769
    45
  val strip_exists: cterm -> cterm list * cterm
wenzelm@10769
    46
  val strip_forall: cterm -> cterm list * cterm
wenzelm@10769
    47
  val strip_imp: cterm -> cterm list * cterm
wenzelm@10769
    48
  val drop_prop: cterm -> cterm
wenzelm@10769
    49
  val mk_prop: cterm -> cterm
wenzelm@10769
    50
end;
wenzelm@10769
    51
wenzelm@10769
    52
structure Dcterm: DCTERM =
wenzelm@10769
    53
struct
wenzelm@10769
    54
wenzelm@10769
    55
structure U = Utils;
wenzelm@10769
    56
wenzelm@10769
    57
fun ERR func mesg = U.ERR {module = "Dcterm", func = func, mesg = mesg};
wenzelm@10769
    58
wenzelm@10769
    59
wenzelm@10769
    60
fun dest_comb t = Thm.dest_comb t
wenzelm@10769
    61
  handle CTERM msg => raise ERR "dest_comb" msg;
wenzelm@10769
    62
wenzelm@10769
    63
fun dest_abs a t = Thm.dest_abs a t
wenzelm@10769
    64
  handle CTERM msg => raise ERR "dest_abs" msg;
wenzelm@10769
    65
wenzelm@10769
    66
fun capply t u = Thm.capply t u
wenzelm@10769
    67
  handle CTERM msg => raise ERR "capply" msg;
wenzelm@10769
    68
wenzelm@10769
    69
fun cabs a t = Thm.cabs a t
wenzelm@10769
    70
  handle CTERM msg => raise ERR "cabs" msg;
wenzelm@10769
    71
wenzelm@10769
    72
wenzelm@10769
    73
(*---------------------------------------------------------------------------
wenzelm@10769
    74
 * Some simple constructor functions.
wenzelm@10769
    75
 *---------------------------------------------------------------------------*)
wenzelm@10769
    76
wenzelm@10769
    77
val mk_hol_const = Thm.cterm_of (Theory.sign_of HOL.thy) o Const;
wenzelm@10769
    78
wenzelm@10769
    79
fun mk_exists (r as (Bvar, Body)) =
wenzelm@10769
    80
  let val ty = #T(rep_cterm Bvar)
wenzelm@10769
    81
      val c = mk_hol_const("Ex", (ty --> HOLogic.boolT) --> HOLogic.boolT)
wenzelm@10769
    82
  in capply c (uncurry cabs r) end;
wenzelm@10769
    83
wenzelm@10769
    84
wenzelm@10769
    85
local val c = mk_hol_const("op &", HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT)
wenzelm@10769
    86
in fun mk_conj(conj1,conj2) = capply (capply c conj1) conj2
wenzelm@10769
    87
end;
wenzelm@10769
    88
wenzelm@10769
    89
local val c = mk_hol_const("op |", HOLogic.boolT --> HOLogic.boolT --> HOLogic.boolT)
wenzelm@10769
    90
in fun mk_disj(disj1,disj2) = capply (capply c disj1) disj2
wenzelm@10769
    91
end;
wenzelm@10769
    92
wenzelm@10769
    93
wenzelm@10769
    94
(*---------------------------------------------------------------------------
wenzelm@10769
    95
 * The primitives.
wenzelm@10769
    96
 *---------------------------------------------------------------------------*)
wenzelm@10769
    97
fun dest_const ctm =
wenzelm@10769
    98
   (case #t(rep_cterm ctm)
wenzelm@10769
    99
      of Const(s,ty) => {Name = s, Ty = ty}
wenzelm@10769
   100
       | _ => raise ERR "dest_const" "not a constant");
wenzelm@10769
   101
wenzelm@10769
   102
fun dest_var ctm =
wenzelm@10769
   103
   (case #t(rep_cterm ctm)
wenzelm@10769
   104
      of Var((s,i),ty) => {Name=s, Ty=ty}
wenzelm@10769
   105
       | Free(s,ty)    => {Name=s, Ty=ty}
wenzelm@10769
   106
       |             _ => raise ERR "dest_var" "not a variable");
wenzelm@10769
   107
wenzelm@10769
   108
wenzelm@10769
   109
(*---------------------------------------------------------------------------
wenzelm@10769
   110
 * Derived destructor operations.
wenzelm@10769
   111
 *---------------------------------------------------------------------------*)
wenzelm@10769
   112
wenzelm@10769
   113
fun dest_monop expected tm =
wenzelm@10769
   114
 let
wenzelm@10769
   115
   fun err () = raise ERR "dest_monop" ("Not a(n) " ^ quote expected);
wenzelm@10769
   116
   val (c, N) = dest_comb tm handle U.ERR _ => err ();
wenzelm@10769
   117
   val name = #Name (dest_const c handle U.ERR _ => err ());
wenzelm@10769
   118
 in if name = expected then N else err () end;
wenzelm@10769
   119
wenzelm@10769
   120
fun dest_binop expected tm =
wenzelm@10769
   121
 let
wenzelm@10769
   122
   fun err () = raise ERR "dest_binop" ("Not a(n) " ^ quote expected);
wenzelm@10769
   123
   val (M, N) = dest_comb tm handle U.ERR _ => err ()
wenzelm@10769
   124
 in (dest_monop expected M, N) handle U.ERR _ => err () end;
wenzelm@10769
   125
wenzelm@10769
   126
fun dest_binder expected tm =
wenzelm@10769
   127
  dest_abs None (dest_monop expected tm)
wenzelm@10769
   128
  handle U.ERR _ => raise ERR "dest_binder" ("Not a(n) " ^ quote expected);
wenzelm@10769
   129
wenzelm@10769
   130
wenzelm@10769
   131
val dest_neg    = dest_monop "not"
wenzelm@10769
   132
val dest_pair   = dest_binop "Pair";
wenzelm@10769
   133
val dest_eq     = dest_binop "op ="
wenzelm@10769
   134
val dest_imp    = dest_binop "op -->"
wenzelm@10769
   135
val dest_conj   = dest_binop "op &"
wenzelm@10769
   136
val dest_disj   = dest_binop "op |"
wenzelm@10769
   137
val dest_cons   = dest_binop "Cons"
wenzelm@10769
   138
val dest_let    = Library.swap o dest_binop "Let";
wenzelm@10769
   139
val dest_select = dest_binder "Eps"
wenzelm@10769
   140
val dest_exists = dest_binder "Ex"
wenzelm@10769
   141
val dest_forall = dest_binder "All"
wenzelm@10769
   142
wenzelm@10769
   143
(* Query routines *)
wenzelm@10769
   144
wenzelm@10769
   145
val is_eq     = can dest_eq
wenzelm@10769
   146
val is_imp    = can dest_imp
wenzelm@10769
   147
val is_select = can dest_select
wenzelm@10769
   148
val is_forall = can dest_forall
wenzelm@10769
   149
val is_exists = can dest_exists
wenzelm@10769
   150
val is_neg    = can dest_neg
wenzelm@10769
   151
val is_conj   = can dest_conj
wenzelm@10769
   152
val is_disj   = can dest_disj
wenzelm@10769
   153
val is_pair   = can dest_pair
wenzelm@10769
   154
val is_let    = can dest_let
wenzelm@10769
   155
val is_cons   = can dest_cons
wenzelm@10769
   156
wenzelm@10769
   157
wenzelm@10769
   158
(*---------------------------------------------------------------------------
wenzelm@10769
   159
 * Iterated creation.
wenzelm@10769
   160
 *---------------------------------------------------------------------------*)
wenzelm@10769
   161
val list_mk_disj = U.end_itlist (fn d1 => fn tm => mk_disj (d1, tm));
wenzelm@10769
   162
wenzelm@10769
   163
(*---------------------------------------------------------------------------
wenzelm@10769
   164
 * Iterated destruction. (To the "right" in a term.)
wenzelm@10769
   165
 *---------------------------------------------------------------------------*)
wenzelm@10769
   166
fun strip break tm =
wenzelm@10769
   167
  let fun dest (p as (ctm,accum)) =
wenzelm@10769
   168
        let val (M,N) = break ctm
wenzelm@10769
   169
        in dest (N, M::accum)
wenzelm@10769
   170
        end handle U.ERR _ => p
wenzelm@10769
   171
  in dest (tm,[])
wenzelm@10769
   172
  end;
wenzelm@10769
   173
wenzelm@10769
   174
fun rev2swap (x,l) = (rev l, x);
wenzelm@10769
   175
wenzelm@10769
   176
val strip_comb   = strip (Library.swap o dest_comb)  (* Goes to the "left" *)
wenzelm@10769
   177
val strip_imp    = rev2swap o strip dest_imp
wenzelm@10769
   178
val strip_abs    = rev2swap o strip (dest_abs None)
wenzelm@10769
   179
val strip_forall = rev2swap o strip dest_forall
wenzelm@10769
   180
val strip_exists = rev2swap o strip dest_exists
wenzelm@10769
   181
wenzelm@10769
   182
val strip_disj   = rev o (op::) o strip dest_disj
wenzelm@10769
   183
wenzelm@10769
   184
wenzelm@10769
   185
(*---------------------------------------------------------------------------
wenzelm@10769
   186
 * Going into and out of prop
wenzelm@10769
   187
 *---------------------------------------------------------------------------*)
wenzelm@10769
   188
wenzelm@10769
   189
fun mk_prop ctm =
wenzelm@10769
   190
  let val {t, sign, ...} = Thm.rep_cterm ctm in
wenzelm@10769
   191
    if can HOLogic.dest_Trueprop t then ctm
wenzelm@10769
   192
    else Thm.cterm_of sign (HOLogic.mk_Trueprop t)
wenzelm@10769
   193
  end
wenzelm@10769
   194
  handle TYPE (msg, _, _) => raise ERR "mk_prop" msg
wenzelm@10769
   195
    | TERM (msg, _) => raise ERR "mk_prop" msg;
wenzelm@10769
   196
wenzelm@10769
   197
fun drop_prop ctm = dest_monop "Trueprop" ctm handle U.ERR _ => ctm;
wenzelm@10769
   198
wenzelm@10769
   199
wenzelm@10769
   200
end;