src/HOL/Tools/choice_specification.ML
author boehmes
Sat Mar 27 02:10:00 2010 +0100 (2010-03-27)
changeset 35983 27e2fa7d4ce7
parent 35897 8758895ea413
child 36323 655e2d74de3a
permissions -rw-r--r--
slightly more general simproc (avoids errors of linarith)
haftmann@31723
     1
(*  Title:      HOL/Tools/choice_specification.ML
skalberg@14115
     2
    Author:     Sebastian Skalberg, TU Muenchen
skalberg@14115
     3
skalberg@14115
     4
Package for defining constants by specification.
skalberg@14115
     5
*)
skalberg@14115
     6
haftmann@31723
     7
signature CHOICE_SPECIFICATION =
skalberg@14115
     8
sig
blanchet@35807
     9
  val close_form : term -> term
wenzelm@18729
    10
  val add_specification: string option -> (bstring * xstring * bool) list ->
wenzelm@18729
    11
    theory * thm -> theory * thm
skalberg@14115
    12
end
skalberg@14115
    13
haftmann@31723
    14
structure Choice_Specification: CHOICE_SPECIFICATION =
skalberg@14115
    15
struct
skalberg@14115
    16
wenzelm@26478
    17
(* actual code *)
skalberg@14115
    18
blanchet@35807
    19
fun close_form t =
blanchet@35807
    20
    fold_rev (fn (s, T) => fn t => HOLogic.mk_all (s, T, t))
blanchet@35807
    21
             (map dest_Free (OldTerm.term_frees t)) t
blanchet@35807
    22
skalberg@14222
    23
local
skalberg@14222
    24
    fun mk_definitional [] arg = arg
skalberg@14222
    25
      | mk_definitional ((thname,cname,covld)::cos) (thy,thm) =
wenzelm@17336
    26
        case HOLogic.dest_Trueprop (concl_of thm) of
wenzelm@17336
    27
            Const("Ex",_) $ P =>
wenzelm@17336
    28
            let
wenzelm@17336
    29
                val ctype = domain_type (type_of P)
wenzelm@22578
    30
                val cname_full = Sign.intern_const thy cname
wenzelm@17336
    31
                val cdefname = if thname = ""
wenzelm@30364
    32
                               then Thm.def_name (Long_Name.base_name cname)
wenzelm@17336
    33
                               else thname
wenzelm@17336
    34
                val def_eq = Logic.mk_equals (Const(cname_full,ctype),
wenzelm@17336
    35
                                              HOLogic.choice_const ctype $  P)
haftmann@29579
    36
                val (thms, thy') = PureThy.add_defs covld [((Binding.name cdefname, def_eq),[])] thy
wenzelm@26478
    37
                val thm' = [thm,hd thms] MRS @{thm exE_some}
wenzelm@17336
    38
            in
wenzelm@17336
    39
                mk_definitional cos (thy',thm')
wenzelm@17336
    40
            end
wenzelm@17336
    41
          | _ => raise THM ("Internal error: Bad specification theorem",0,[thm])
skalberg@14222
    42
skalberg@14222
    43
    fun mk_axiomatic axname cos arg =
wenzelm@17336
    44
        let
wenzelm@17336
    45
            fun process [] (thy,tm) =
wenzelm@17336
    46
                let
wenzelm@35897
    47
                    val (thm, thy') =
wenzelm@35897
    48
                      Specification.axiom ((Binding.name axname, []), HOLogic.mk_Trueprop tm) thy
wenzelm@17336
    49
                in
wenzelm@35897
    50
                    (thy', Drule.export_without_context thm)
wenzelm@17336
    51
                end
wenzelm@17336
    52
              | process ((thname,cname,covld)::cos) (thy,tm) =
wenzelm@17336
    53
                case tm of
wenzelm@17336
    54
                    Const("Ex",_) $ P =>
wenzelm@17336
    55
                    let
wenzelm@17336
    56
                        val ctype = domain_type (type_of P)
wenzelm@22578
    57
                        val cname_full = Sign.intern_const thy cname
wenzelm@17336
    58
                        val cdefname = if thname = ""
wenzelm@30364
    59
                                       then Thm.def_name (Long_Name.base_name cname)
wenzelm@17336
    60
                                       else thname
wenzelm@17336
    61
                        val co = Const(cname_full,ctype)
wenzelm@17336
    62
                        val thy' = Theory.add_finals_i covld [co] thy
wenzelm@17336
    63
                        val tm' = case P of
wenzelm@17336
    64
                                      Abs(_, _, bodt) => subst_bound (co, bodt)
wenzelm@17336
    65
                                    | _ => P $ co
wenzelm@17336
    66
                    in
wenzelm@17336
    67
                        process cos (thy',tm')
wenzelm@17336
    68
                    end
wenzelm@17336
    69
                  | _ => raise TERM ("Internal error: Bad specification theorem",[tm])
wenzelm@17336
    70
        in
wenzelm@17336
    71
            process cos arg
wenzelm@17336
    72
        end
skalberg@14115
    73
skalberg@14222
    74
in
skalberg@14222
    75
fun proc_exprop axiomatic cos arg =
skalberg@14222
    76
    case axiomatic of
wenzelm@17336
    77
        SOME axname => mk_axiomatic axname cos (apsnd (HOLogic.dest_Trueprop o concl_of) arg)
skalberg@15531
    78
      | NONE => mk_definitional cos arg
skalberg@14222
    79
end
skalberg@14222
    80
wenzelm@18729
    81
fun add_specification axiomatic cos arg =
wenzelm@19876
    82
    arg |> apsnd Thm.freezeT
wenzelm@17336
    83
        |> proc_exprop axiomatic cos
wenzelm@35021
    84
        |> apsnd Drule.export_without_context
skalberg@14115
    85
wenzelm@18729
    86
skalberg@14115
    87
(* Collect all intances of constants in term *)
skalberg@14115
    88
skalberg@14115
    89
fun collect_consts (        t $ u,tms) = collect_consts (u,collect_consts (t,tms))
skalberg@14115
    90
  | collect_consts (   Abs(_,_,t),tms) = collect_consts (t,tms)
wenzelm@18921
    91
  | collect_consts (tm as Const _,tms) = insert (op aconv) tm tms
skalberg@14115
    92
  | collect_consts (            _,tms) = tms
skalberg@14115
    93
wenzelm@35845
    94
(* Complementing Type.varify_global... *)
skalberg@14115
    95
wenzelm@35845
    96
fun unvarify_global t fmap =
skalberg@14115
    97
    let
wenzelm@17336
    98
        val fmap' = map Library.swap fmap
haftmann@17377
    99
        fun unthaw (f as (a, S)) =
haftmann@17377
   100
            (case AList.lookup (op =) fmap' a of
wenzelm@17336
   101
                 NONE => TVar f
haftmann@17377
   102
               | SOME (b, _) => TFree (b, S))
skalberg@14115
   103
    in
wenzelm@20548
   104
        map_types (map_type_tvar unthaw) t
skalberg@14115
   105
    end
skalberg@14115
   106
skalberg@14115
   107
(* The syntactic meddling needed to setup add_specification for work *)
skalberg@14115
   108
wenzelm@17336
   109
fun process_spec axiomatic cos alt_props thy =
skalberg@14115
   110
    let
wenzelm@17336
   111
        fun zip3 [] [] [] = []
wenzelm@17336
   112
          | zip3 (x::xs) (y::ys) (z::zs) = (x,y,z)::zip3 xs ys zs
haftmann@31723
   113
          | zip3 _ _ _ = error "Choice_Specification.process_spec internal error"
skalberg@14167
   114
wenzelm@17336
   115
        fun myfoldr f [x] = x
wenzelm@17336
   116
          | myfoldr f (x::xs) = f (x,myfoldr f xs)
haftmann@31723
   117
          | myfoldr f [] = error "Choice_Specification.process_spec internal error"
skalberg@14164
   118
wenzelm@17895
   119
        val rew_imps = alt_props |>
wenzelm@35625
   120
          map (Object_Logic.atomize o Thm.cterm_of thy o Syntax.read_prop_global thy o snd)
wenzelm@17895
   121
        val props' = rew_imps |>
wenzelm@22902
   122
          map (HOLogic.dest_Trueprop o term_of o snd o Thm.dest_equals o cprop_of)
skalberg@14166
   123
wenzelm@17336
   124
        fun proc_single prop =
wenzelm@17336
   125
            let
wenzelm@29265
   126
                val frees = OldTerm.term_frees prop
wenzelm@21858
   127
                val _ = forall (fn v => Sign.of_sort thy (type_of v,HOLogic.typeS)) frees
wenzelm@21858
   128
                  orelse error "Specificaton: Only free variables of sort 'type' allowed"
blanchet@35807
   129
                val prop_closed = close_form prop
wenzelm@17336
   130
            in
wenzelm@17336
   131
                (prop_closed,frees)
wenzelm@17336
   132
            end
skalberg@14166
   133
wenzelm@17336
   134
        val props'' = map proc_single props'
wenzelm@17336
   135
        val frees = map snd props''
wenzelm@17336
   136
        val prop  = myfoldr HOLogic.mk_conj (map fst props'')
wenzelm@17895
   137
        val cprop = cterm_of thy (HOLogic.mk_Trueprop prop)
skalberg@14164
   138
wenzelm@35845
   139
        val (vmap, prop_thawed) = Type.varify_global [] prop
wenzelm@17336
   140
        val thawed_prop_consts = collect_consts (prop_thawed,[])
wenzelm@17336
   141
        val (altcos,overloaded) = Library.split_list cos
wenzelm@17336
   142
        val (names,sconsts) = Library.split_list altcos
wenzelm@24707
   143
        val consts = map (Syntax.read_term_global thy) sconsts
wenzelm@21858
   144
        val _ = not (Library.exists (not o Term.is_Const) consts)
wenzelm@21858
   145
          orelse error "Specification: Non-constant found as parameter"
skalberg@14166
   146
wenzelm@17336
   147
        fun proc_const c =
wenzelm@17336
   148
            let
wenzelm@35845
   149
                val (_, c') = Type.varify_global [] c
wenzelm@17336
   150
                val (cname,ctyp) = dest_Const c'
wenzelm@17336
   151
            in
wenzelm@33317
   152
                case filter (fn t => let val (name,typ) = dest_Const t
wenzelm@19414
   153
                                     in name = cname andalso Sign.typ_equiv thy (typ, ctyp)
wenzelm@17336
   154
                                     end) thawed_prop_consts of
wenzelm@26939
   155
                    [] => error ("Specification: No suitable instances of constant \"" ^ Syntax.string_of_term_global thy c ^ "\" found")
wenzelm@35845
   156
                  | [cf] => unvarify_global cf vmap
wenzelm@26939
   157
                  | _ => error ("Specification: Several variations of \"" ^ Syntax.string_of_term_global thy c ^ "\" found (try applying explicit type constraints)")
wenzelm@17336
   158
            end
wenzelm@17336
   159
        val proc_consts = map proc_const consts
wenzelm@33339
   160
        fun mk_exist c prop =
wenzelm@17336
   161
            let
wenzelm@17336
   162
                val T = type_of c
wenzelm@30364
   163
                val cname = Long_Name.base_name (fst (dest_Const c))
wenzelm@17336
   164
                val vname = if Syntax.is_identifier cname
wenzelm@17336
   165
                            then cname
wenzelm@17336
   166
                            else "x"
wenzelm@17336
   167
            in
wenzelm@17336
   168
                HOLogic.exists_const T $ Abs(vname,T,Term.abstract_over (c,prop))
wenzelm@17336
   169
            end
wenzelm@33339
   170
        val ex_prop = fold_rev mk_exist proc_consts prop
wenzelm@17336
   171
        val cnames = map (fst o dest_Const) proc_consts
wenzelm@17336
   172
        fun post_process (arg as (thy,thm)) =
wenzelm@17336
   173
            let
wenzelm@33245
   174
                fun inst_all thy v thm =
wenzelm@17336
   175
                    let
wenzelm@17895
   176
                        val cv = cterm_of thy v
wenzelm@17336
   177
                        val cT = ctyp_of_term cv
wenzelm@17336
   178
                        val spec' = instantiate' [SOME cT] [NONE,SOME cv] spec
wenzelm@17336
   179
                    in
wenzelm@17336
   180
                        thm RS spec'
wenzelm@17336
   181
                    end
wenzelm@22578
   182
                fun remove_alls frees thm =
wenzelm@33245
   183
                    fold (inst_all (Thm.theory_of_thm thm)) frees thm
wenzelm@17336
   184
                fun process_single ((name,atts),rew_imp,frees) args =
wenzelm@17336
   185
                    let
wenzelm@17336
   186
                        fun undo_imps thm =
wenzelm@17336
   187
                            equal_elim (symmetric rew_imp) thm
skalberg@14164
   188
haftmann@18358
   189
                        fun add_final (arg as (thy, thm)) =
wenzelm@17336
   190
                            if name = ""
haftmann@18358
   191
                            then arg |> Library.swap
wenzelm@32091
   192
                            else (writeln ("  " ^ name ^ ": " ^ Display.string_of_thm_global thy thm);
haftmann@29579
   193
                                  PureThy.store_thm (Binding.name name, thm) thy)
wenzelm@17336
   194
                    in
wenzelm@17336
   195
                        args |> apsnd (remove_alls frees)
wenzelm@17336
   196
                             |> apsnd undo_imps
wenzelm@35021
   197
                             |> apsnd Drule.export_without_context
blanchet@35807
   198
                             |> Thm.theory_attributes (map (Attrib.attribute thy)
blanchet@35807
   199
                                                           (Attrib.internal (K Nitpick_Choice_Specs.add) :: atts))
wenzelm@17336
   200
                             |> add_final
haftmann@18358
   201
                             |> Library.swap
wenzelm@17336
   202
                    end
skalberg@14164
   203
wenzelm@17336
   204
                fun process_all [proc_arg] args =
wenzelm@17336
   205
                    process_single proc_arg args
wenzelm@17336
   206
                  | process_all (proc_arg::rest) (thy,thm) =
wenzelm@17336
   207
                    let
wenzelm@17336
   208
                        val single_th = thm RS conjunct1
wenzelm@17336
   209
                        val rest_th   = thm RS conjunct2
wenzelm@17336
   210
                        val (thy',_)  = process_single proc_arg (thy,single_th)
wenzelm@17336
   211
                    in
wenzelm@17336
   212
                        process_all rest (thy',rest_th)
wenzelm@17336
   213
                    end
haftmann@31723
   214
                  | process_all [] _ = error "Choice_Specification.process_spec internal error"
wenzelm@17336
   215
                val alt_names = map fst alt_props
wenzelm@17336
   216
                val _ = if exists (fn(name,_) => not (name = "")) alt_names
wenzelm@17336
   217
                        then writeln "specification"
wenzelm@17336
   218
                        else ()
wenzelm@17336
   219
            in
wenzelm@19876
   220
                arg |> apsnd Thm.freezeT
wenzelm@17336
   221
                    |> process_all (zip3 alt_names rew_imps frees)
wenzelm@17336
   222
            end
wenzelm@20903
   223
wenzelm@20903
   224
      fun after_qed [[thm]] = ProofContext.theory (fn thy =>
wenzelm@20903
   225
        #1 (post_process (add_specification axiomatic (zip3 names cnames overloaded) (thy, thm))));
skalberg@14115
   226
    in
wenzelm@20903
   227
      thy
wenzelm@20903
   228
      |> ProofContext.init
wenzelm@21434
   229
      |> Proof.theorem_i NONE after_qed [[(HOLogic.mk_Trueprop ex_prop, [])]]
wenzelm@20903
   230
    end;
skalberg@14115
   231
wenzelm@17336
   232
skalberg@14115
   233
(* outer syntax *)
skalberg@14115
   234
wenzelm@17057
   235
local structure P = OuterParse and K = OuterKeyword in
skalberg@14115
   236
skalberg@14116
   237
val opt_name = Scan.optional (P.name --| P.$$$ ":") ""
wenzelm@17336
   238
val opt_overloaded = P.opt_keyword "overloaded";
skalberg@14116
   239
skalberg@14115
   240
val specification_decl =
wenzelm@17336
   241
  P.$$$ "(" |-- Scan.repeat1 (opt_name -- P.term -- opt_overloaded) --| P.$$$ ")" --
wenzelm@30223
   242
          Scan.repeat1 ((SpecParse.opt_thm_name ":" >> apfst Binding.name_of) -- P.prop)
skalberg@14115
   243
wenzelm@24867
   244
val _ =
skalberg@14115
   245
  OuterSyntax.command "specification" "define constants by specification" K.thy_goal
skalberg@14164
   246
    (specification_decl >> (fn (cos,alt_props) =>
wenzelm@17336
   247
                               Toplevel.print o (Toplevel.theory_to_proof
wenzelm@17336
   248
                                                     (process_spec NONE cos alt_props))))
skalberg@14222
   249
skalberg@14222
   250
val ax_specification_decl =
skalberg@14222
   251
    P.name --
skalberg@14222
   252
    (P.$$$ "(" |-- Scan.repeat1 (opt_name -- P.term -- opt_overloaded) --| P.$$$ ")" --
wenzelm@30223
   253
           Scan.repeat1 ((SpecParse.opt_thm_name ":" >> apfst Binding.name_of) -- P.prop))
skalberg@14115
   254
wenzelm@24867
   255
val _ =
skalberg@14222
   256
  OuterSyntax.command "ax_specification" "define constants by specification" K.thy_goal
skalberg@14222
   257
    (ax_specification_decl >> (fn (axname,(cos,alt_props)) =>
wenzelm@17336
   258
                               Toplevel.print o (Toplevel.theory_to_proof
wenzelm@17336
   259
                                                     (process_spec (SOME axname) cos alt_props))))
skalberg@14222
   260
skalberg@14115
   261
end
skalberg@14115
   262
skalberg@14115
   263
skalberg@14115
   264
end