src/HOL/Tools/Predicate_Compile/pred_compile_pred.ML
author wenzelm
Sat, 17 Oct 2009 16:58:03 +0200
changeset 32970 fbd2bb2489a8
parent 32966 5b21661fe618
child 33149 2c8f1c450b47
permissions -rw-r--r--
operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     1
(* Author: Lukas Bulwahn, TU Muenchen
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     2
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     3
Preprocessing definitions of predicates to introduction rules
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     4
*)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     5
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     6
signature PREDICATE_COMPILE_PRED =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     7
sig
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     8
  (* preprocesses an equation to a set of intro rules; defines new constants *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     9
  (*
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    10
  val preprocess_pred_equation : thm -> theory -> thm list * theory
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    11
  *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    12
  val preprocess : string -> theory -> (thm list list * theory) 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    13
  (* output is the term list of clauses of an unknown predicate *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    14
  val preprocess_term : term -> theory -> (term list * theory)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    15
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    16
  (*val rewrite : thm -> thm*)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    17
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    18
end;
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    19
(* : PREDICATE_COMPILE_PREPROC_PRED *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    20
structure Predicate_Compile_Pred =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    21
struct
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    22
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    23
open Predicate_Compile_Aux
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    24
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    25
fun is_compound ((Const ("Not", _)) $ t) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    26
    error "is_compound: Negation should not occur; preprocessing is defect"
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    27
  | is_compound ((Const ("Ex", _)) $ _) = true
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    28
  | is_compound ((Const (@{const_name "op |"}, _)) $ _ $ _) = true
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    29
  | is_compound ((Const ("op &", _)) $ _ $ _) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    30
    error "is_compound: Conjunction should not occur; preprocessing is defect"
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    31
  | is_compound _ = false
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    32
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    33
fun flatten constname atom (defs, thy) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    34
  if is_compound atom then
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    35
    let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    36
      val constname = Name.variant (map (Long_Name.base_name o fst) defs)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    37
        ((Long_Name.base_name constname) ^ "_aux")
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    38
      val full_constname = Sign.full_bname thy constname
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    39
      val (params, args) = List.partition (is_predT o fastype_of)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    40
        (map Free (Term.add_frees atom []))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    41
      val constT = map fastype_of (params @ args) ---> HOLogic.boolT
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    42
      val lhs = list_comb (Const (full_constname, constT), params @ args)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    43
      val def = Logic.mk_equals (lhs, atom)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    44
      val ([definition], thy') = thy
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    45
        |> Sign.add_consts_i [(Binding.name constname, constT, NoSyn)]
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    46
        |> PureThy.add_defs false [((Binding.name (constname ^ "_def"), def), [])]
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    47
    in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    48
      (lhs, ((full_constname, [definition]) :: defs, thy'))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    49
    end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    50
  else
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    51
    (atom, (defs, thy))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    52
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    53
fun flatten_intros constname intros thy =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    54
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    55
    val ctxt = ProofContext.init thy
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    56
    val ((_, intros), ctxt') = Variable.import true intros ctxt
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    57
    val (intros', (local_defs, thy')) = (fold_map o fold_map_atoms)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    58
      (flatten constname) (map prop_of intros) ([], thy)
32970
fbd2bb2489a8 operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
wenzelm
parents: 32966
diff changeset
    59
    val tac = fn _ => Skip_Proof.cheat_tac thy'
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    60
    val intros'' = map (fn t => Goal.prove ctxt' [] [] t tac) intros'
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    61
      |> Variable.export ctxt' ctxt
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    62
  in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    63
    (intros'', (local_defs, thy'))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    64
  end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    65
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    66
(* TODO: same function occurs in inductive package *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    67
fun select_disj 1 1 = []
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    68
  | select_disj _ 1 = [rtac @{thm disjI1}]
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    69
  | select_disj n i = (rtac @{thm disjI2})::(select_disj (n - 1) (i - 1));
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    70
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    71
fun introrulify thy ths = 
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    72
  let
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    73
    val ctxt = ProofContext.init thy
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    74
    val ((_, ths'), ctxt') = Variable.import true ths ctxt
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    75
    fun introrulify' th =
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    76
      let
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    77
        val (lhs, rhs) = Logic.dest_equals (prop_of th)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    78
        val frees = Term.add_free_names rhs []
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    79
        val disjuncts = HOLogic.dest_disj rhs
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    80
        val nctxt = Name.make_context frees
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    81
        fun mk_introrule t =
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    82
          let
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    83
            val ((ps, t'), nctxt') = focus_ex t nctxt
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    84
            val prems = map HOLogic.mk_Trueprop (HOLogic.dest_conj t')
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    85
          in
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    86
            (ps, Logic.list_implies (prems, HOLogic.mk_Trueprop lhs))
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    87
          end
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    88
        val x = ((cterm_of thy) o the_single o snd o strip_comb o HOLogic.dest_Trueprop o fst o
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    89
          Logic.dest_implies o prop_of) @{thm exI}
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    90
        fun prove_introrule (index, (ps, introrule)) =
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    91
          let
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    92
            val tac = Simplifier.simp_tac (HOL_basic_ss addsimps [th]) 1
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    93
              THEN EVERY1 (select_disj (length disjuncts) (index + 1)) 
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    94
              THEN (EVERY (map (fn y =>
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    95
                rtac (Drule.cterm_instantiate [(x, cterm_of thy (Free y))] @{thm exI}) 1) ps))
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    96
              THEN REPEAT_DETERM (rtac @{thm conjI} 1 THEN atac 1)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    97
              THEN TRY (atac 1)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    98
          in
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
    99
            Goal.prove ctxt' (map fst ps) [] introrule (fn {...} => tac)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
   100
          end
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   101
      in
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
   102
        map_index prove_introrule (map mk_introrule disjuncts)
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   103
      end
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
   104
  in maps introrulify' ths' |> Variable.export ctxt' ctxt end
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   105
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   106
val rewrite =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   107
  Simplifier.simplify (HOL_basic_ss addsimps [@{thm Ball_def}, @{thm Bex_def}])
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   108
  #> Simplifier.simplify (HOL_basic_ss addsimps [@{thm all_not_ex}]) 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   109
  #> Conv.fconv_rule nnf_conv 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   110
  #> Simplifier.simplify (HOL_basic_ss addsimps [@{thm ex_disj_distrib}])
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
   111
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
   112
val rewrite_intros =
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
   113
  Simplifier.simplify (HOL_basic_ss addsimps @{thms HOL.simp_thms(9)})
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
   114
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   115
fun preprocess (constname, specs) thy =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   116
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   117
    val ctxt = ProofContext.init thy
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   118
      val intros =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   119
      if forall is_pred_equation specs then 
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
   120
        introrulify thy (map rewrite specs)
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   121
      else if forall (is_intro constname) specs then
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
   122
        map rewrite_intros specs
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   123
      else
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   124
        error ("unexpected specification for constant " ^ quote constname ^ ":\n"
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   125
          ^ commas (map (quote o Display.string_of_thm_global thy) specs))
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
   126
    val _ = tracing ("Introduction rules of definitions before flattening: "
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
   127
      ^ commas (map (Display.string_of_thm ctxt) intros))
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32669
diff changeset
   128
    val _ = tracing "Defining local predicates and their intro rules..."
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   129
    val (intros', (local_defs, thy')) = flatten_intros constname intros thy
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   130
    val (intross, thy'') = fold_map preprocess local_defs thy'
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   131
  in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   132
    (intros' :: flat intross,thy'')
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   133
  end;
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   134
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   135
fun preprocess_term t thy = error "preprocess_pred_term: to implement" 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   136
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   137
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   138
end;