src/HOL/ex/Set_Comprehension_Pointfree_Examples.thy
author haftmann
Fri Oct 10 19:55:32 2014 +0200 (2014-10-10)
changeset 58646 cd63a4b12a33
parent 55663 12448c179851
child 58889 5b7a9633cfa8
permissions -rw-r--r--
specialized specification: avoid trivial instances
     1 (*  Title:      HOL/ex/Set_Comprehension_Pointfree_Examples.thy
     2     Author:     Lukas Bulwahn, Rafal Kolanski
     3     Copyright   2012 TU Muenchen
     4 *)
     5 
     6 header {* Examples for the set comprehension to pointfree simproc *}
     7 
     8 theory Set_Comprehension_Pointfree_Examples
     9 imports Main
    10 begin
    11 
    12 declare [[simproc add: finite_Collect]]
    13 
    14 lemma
    15   "finite (UNIV::'a set) ==> finite {p. EX x::'a. p = (x, x)}"
    16   by simp
    17 
    18 lemma
    19   "finite A ==> finite B ==> finite {f a b| a b. a : A \<and> b : B}"
    20   by simp
    21   
    22 lemma
    23   "finite B ==> finite A' ==> finite {f a b| a b. a : A \<and> a : A' \<and> b : B}"
    24   by simp
    25 
    26 lemma
    27   "finite A ==> finite B ==> finite {f a b| a b. a : A \<and> b : B \<and> b : B'}"
    28   by simp
    29 
    30 lemma
    31   "finite A ==> finite B ==> finite C ==> finite {f a b c| a b c. a : A \<and> b : B \<and> c : C}"
    32   by simp
    33 
    34 lemma
    35   "finite A ==> finite B ==> finite C ==> finite D ==>
    36      finite {f a b c d| a b c d. a : A \<and> b : B \<and> c : C \<and> d : D}"
    37   by simp
    38 
    39 lemma
    40   "finite A ==> finite B ==> finite C ==> finite D ==> finite E ==>
    41     finite {f a b c d e | a b c d e. a : A \<and> b : B \<and> c : C \<and> d : D \<and> e : E}"
    42   by simp
    43 
    44 lemma
    45   "finite A ==> finite B ==> finite C ==> finite D ==> finite E \<Longrightarrow>
    46     finite {f a d c b e | e b c d a. b : B \<and> a : A \<and> e : E' \<and> c : C \<and> d : D \<and> e : E \<and> b : B'}"
    47   by simp
    48 
    49 lemma
    50   "\<lbrakk> finite A ; finite B ; finite C ; finite D \<rbrakk>
    51   \<Longrightarrow> finite ({f a b c d| a b c d. a : A \<and> b : B \<and> c : C \<and> d : D})"
    52   by simp
    53 
    54 lemma
    55   "finite ((\<lambda>(a,b,c,d). f a b c d) ` (A \<times> B \<times> C \<times> D))
    56   \<Longrightarrow> finite ({f a b c d| a b c d. a : A \<and> b : B \<and> c : C \<and> d : D})"
    57   by simp
    58 
    59 lemma
    60   "finite S ==> finite {s'. EX s:S. s' = f a e s}"
    61   by simp
    62 
    63 lemma
    64   "finite A ==> finite B ==> finite {f a b| a b. a : A \<and> b : B \<and> a \<notin> Z}"
    65   by simp
    66 
    67 lemma
    68   "finite A ==> finite B ==> finite R ==> finite {f a b x y| a b x y. a : A \<and> b : B \<and> (x,y) \<in> R}"
    69 by simp
    70 
    71 lemma
    72   "finite A ==> finite B ==> finite R ==> finite {f a b x y| a b x y. a : A \<and> (x,y) \<in> R \<and> b : B}"
    73 by simp
    74 
    75 lemma
    76   "finite A ==> finite B ==> finite R ==> finite {f a (x, b) y| y b x a. a : A \<and> (x,y) \<in> R \<and> b : B}"
    77 by simp
    78 
    79 lemma
    80   "finite A ==> finite AA ==> finite B ==> finite {f a b| a b. (a : A \<or> a : AA) \<and> b : B \<and> a \<notin> Z}"
    81 by simp
    82 
    83 lemma
    84   "finite A1 ==> finite A2 ==> finite A3 ==> finite A4 ==> finite A5 ==> finite B ==>
    85      finite {f a b c | a b c. ((a : A1 \<and> a : A2) \<or> (a : A3 \<and> (a : A4 \<or> a : A5))) \<and> b : B \<and> a \<notin> Z}"
    86 apply simp
    87 oops
    88 
    89 lemma "finite B ==> finite {c. EX x. x : B & c = a * x}"
    90 by simp
    91 
    92 lemma
    93   "finite A ==> finite B ==> finite {f a * g b |a b. a : A & b : B}"
    94 by simp
    95 
    96 lemma
    97   "finite S ==> inj (%(x, y). g x y) ==> finite {f x y| x y. g x y : S}"
    98   by (auto intro: finite_vimageI)
    99 
   100 lemma
   101   "finite A ==> finite S ==> inj (%(x, y). g x y) ==> finite {f x y z | x y z. g x y : S & z : A}"
   102   by (auto intro: finite_vimageI)
   103 
   104 lemma
   105   "finite S ==> finite A ==> inj (%(x, y). g x y) ==> inj (%(x, y). h x y)
   106     ==> finite {f a b c d | a b c d. g a c : S & h b d : A}"
   107   by (auto intro: finite_vimageI)
   108 
   109 lemma
   110   assumes "finite S" shows "finite {(a,b,c,d). ([a, b], [c, d]) : S}"
   111 using assms by (auto intro!: finite_vimageI simp add: inj_on_def)
   112   (* injectivity to be automated with further rules and automation *)
   113 
   114 schematic_lemma (* check interaction with schematics *)
   115   "finite {x :: ?'A \<Rightarrow> ?'B \<Rightarrow> bool. \<exists>a b. x = Pair_Rep a b}
   116    = finite ((\<lambda>(b :: ?'B, a:: ?'A). Pair_Rep a b) ` (UNIV \<times> UNIV))"
   117   by simp
   118 
   119 declare [[simproc del: finite_Collect]]
   120 
   121 
   122 section {* Testing simproc in code generation *}
   123 
   124 definition union :: "nat set => nat set => nat set"
   125 where
   126   "union A B = {x. x : A \<or> x : B}"
   127 
   128 definition common_subsets :: "nat set => nat set => nat set set"
   129 where
   130   "common_subsets S1 S2 = {S. S \<subseteq> S1 \<and> S \<subseteq> S2}"
   131 
   132 definition products :: "nat set => nat set => nat set"
   133 where
   134   "products A B = {c. EX a b. a : A & b : B & c = a * b}"
   135 
   136 export_code products in Haskell
   137 
   138 export_code union common_subsets products in Haskell
   139 
   140 end