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