src/HOL/Predicate_Compile.thy
author wenzelm
Mon Aug 31 21:28:08 2015 +0200 (2015-08-31)
changeset 61070 b72a990adfe2
parent 60758 d8d85a8172b5
child 62390 842917225d56
permissions -rw-r--r--
prefer symbols;
wenzelm@33265
     1
(*  Title:      HOL/Predicate_Compile.thy
wenzelm@33265
     2
    Author:     Stefan Berghofer, Lukas Bulwahn, Florian Haftmann, TU Muenchen
wenzelm@33265
     3
*)
bulwahn@33250
     4
wenzelm@60758
     5
section \<open>A compiler for predicates defined by introduction rules\<close>
bulwahn@33250
     6
bulwahn@33250
     7
theory Predicate_Compile
haftmann@51126
     8
imports Random_Sequence Quickcheck_Exhaustive
wenzelm@46950
     9
keywords "code_pred" :: thy_goal and "values" :: diag
bulwahn@33250
    10
begin
bulwahn@33250
    11
wenzelm@48891
    12
ML_file "Tools/Predicate_Compile/predicate_compile_aux.ML"
wenzelm@48891
    13
ML_file "Tools/Predicate_Compile/predicate_compile_compilations.ML"
wenzelm@48891
    14
ML_file "Tools/Predicate_Compile/core_data.ML"
wenzelm@48891
    15
ML_file "Tools/Predicate_Compile/mode_inference.ML"
wenzelm@48891
    16
ML_file "Tools/Predicate_Compile/predicate_compile_proof.ML"
wenzelm@48891
    17
ML_file "Tools/Predicate_Compile/predicate_compile_core.ML"
wenzelm@48891
    18
ML_file "Tools/Predicate_Compile/predicate_compile_data.ML"
wenzelm@48891
    19
ML_file "Tools/Predicate_Compile/predicate_compile_fun.ML"
wenzelm@48891
    20
ML_file "Tools/Predicate_Compile/predicate_compile_pred.ML"
wenzelm@48891
    21
ML_file "Tools/Predicate_Compile/predicate_compile_specialisation.ML"
wenzelm@48891
    22
ML_file "Tools/Predicate_Compile/predicate_compile.ML"
wenzelm@48891
    23
bulwahn@33250
    24
wenzelm@60758
    25
subsection \<open>Set membership as a generator predicate\<close>
Andreas@52023
    26
wenzelm@60758
    27
text \<open>
Andreas@52023
    28
  Introduce a new constant for membership to allow 
Andreas@52023
    29
  fine-grained control in code equations. 
wenzelm@60758
    30
\<close>
Andreas@52023
    31
Andreas@52023
    32
definition contains :: "'a set => 'a => bool"
Andreas@52023
    33
where "contains A x \<longleftrightarrow> x : A"
Andreas@52023
    34
Andreas@52023
    35
definition contains_pred :: "'a set => 'a => unit Predicate.pred"
Andreas@52023
    36
where "contains_pred A x = (if x : A then Predicate.single () else bot)"
Andreas@52023
    37
Andreas@52023
    38
lemma pred_of_setE:
Andreas@52023
    39
  assumes "Predicate.eval (pred_of_set A) x"
Andreas@52023
    40
  obtains "contains A x"
Andreas@52023
    41
using assms by(simp add: contains_def)
Andreas@52023
    42
Andreas@52023
    43
lemma pred_of_setI: "contains A x ==> Predicate.eval (pred_of_set A) x"
Andreas@52023
    44
by(simp add: contains_def)
Andreas@52023
    45
Andreas@52023
    46
lemma pred_of_set_eq: "pred_of_set \<equiv> \<lambda>A. Predicate.Pred (contains A)"
Andreas@52023
    47
by(simp add: contains_def[abs_def] pred_of_set_def o_def)
Andreas@52023
    48
Andreas@52023
    49
lemma containsI: "x \<in> A ==> contains A x" 
Andreas@52023
    50
by(simp add: contains_def)
Andreas@52023
    51
Andreas@52023
    52
lemma containsE: assumes "contains A x"
Andreas@52023
    53
  obtains A' x' where "A = A'" "x = x'" "x : A"
Andreas@52023
    54
using assms by(simp add: contains_def)
Andreas@52023
    55
Andreas@52023
    56
lemma contains_predI: "contains A x ==> Predicate.eval (contains_pred A x) ()"
Andreas@52023
    57
by(simp add: contains_pred_def contains_def)
Andreas@52023
    58
Andreas@52023
    59
lemma contains_predE: 
Andreas@52023
    60
  assumes "Predicate.eval (contains_pred A x) y"
Andreas@52023
    61
  obtains "contains A x"
Andreas@52023
    62
using assms by(simp add: contains_pred_def contains_def split: split_if_asm)
Andreas@52023
    63
Andreas@52023
    64
lemma contains_pred_eq: "contains_pred \<equiv> \<lambda>A x. Predicate.Pred (\<lambda>y. contains A x)"
Andreas@52023
    65
by(rule eq_reflection)(auto simp add: contains_pred_def fun_eq_iff contains_def intro: pred_eqI)
Andreas@52023
    66
Andreas@52023
    67
lemma contains_pred_notI:
Andreas@52023
    68
   "\<not> contains A x ==> Predicate.eval (Predicate.not_pred (contains_pred A x)) ()"
Andreas@52023
    69
by(simp add: contains_pred_def contains_def not_pred_eq)
Andreas@52023
    70
wenzelm@60758
    71
setup \<open>
Andreas@52023
    72
let
Andreas@52023
    73
  val Fun = Predicate_Compile_Aux.Fun
Andreas@52023
    74
  val Input = Predicate_Compile_Aux.Input
Andreas@52023
    75
  val Output = Predicate_Compile_Aux.Output
Andreas@52023
    76
  val Bool = Predicate_Compile_Aux.Bool
Andreas@52023
    77
  val io = Fun (Input, Fun (Output, Bool))
Andreas@52023
    78
  val ii = Fun (Input, Fun (Input, Bool))
Andreas@52023
    79
in
Andreas@52023
    80
  Core_Data.PredData.map (Graph.new_node 
Andreas@52023
    81
    (@{const_name contains}, 
Andreas@52023
    82
     Core_Data.PredData {
wenzelm@55543
    83
       pos = Position.thread_data (),
Andreas@52023
    84
       intros = [(NONE, @{thm containsI})], 
Andreas@52023
    85
       elim = SOME @{thm containsE}, 
Andreas@52023
    86
       preprocessed = true,
Andreas@52023
    87
       function_names = [(Predicate_Compile_Aux.Pred, 
Andreas@52023
    88
         [(io, @{const_name pred_of_set}), (ii, @{const_name contains_pred})])], 
Andreas@52023
    89
       predfun_data = [
Andreas@52023
    90
         (io, Core_Data.PredfunData {
Andreas@52023
    91
            elim = @{thm pred_of_setE}, intro = @{thm pred_of_setI},
Andreas@52023
    92
            neg_intro = NONE, definition = @{thm pred_of_set_eq}
Andreas@52023
    93
          }),
Andreas@52023
    94
         (ii, Core_Data.PredfunData {
Andreas@52023
    95
            elim = @{thm contains_predE}, intro = @{thm contains_predI}, 
Andreas@52023
    96
            neg_intro = SOME @{thm contains_pred_notI}, definition = @{thm contains_pred_eq}
Andreas@52023
    97
          })],
Andreas@52023
    98
       needs_random = []}))
bulwahn@34948
    99
end
wenzelm@60758
   100
\<close>
Andreas@52023
   101
Andreas@52023
   102
hide_const (open) contains contains_pred
Andreas@52023
   103
hide_fact (open) pred_of_setE pred_of_setI pred_of_set_eq 
Andreas@52023
   104
  containsI containsE contains_predI contains_predE contains_pred_eq contains_pred_notI
Andreas@52023
   105
Andreas@52023
   106
end