src/HOL/Library/ExecutableSet.thy
changeset 20597 65fe827aa595
parent 20523 36a59e5d0039
child 20840 5e92606245b6
equal deleted inserted replaced
20596:3950e65f48f8 20597:65fe827aa595
     7 
     7 
     8 theory ExecutableSet
     8 theory ExecutableSet
     9 imports Main
     9 imports Main
    10 begin
    10 begin
    11 
    11 
    12 section {* Definitional rewrites *}
    12 section {* Definitional equality rewrites *}
       
    13 
       
    14 instance set :: (eq) eq ..
    13 
    15 
    14 lemma [code target: Set]:
    16 lemma [code target: Set]:
    15   "(A = B) = (A \<subseteq> B \<and> B \<subseteq> A)"
    17   "(A = B) = (A \<subseteq> B \<and> B \<subseteq> A)"
    16   by blast
    18   by blast
    17 
    19 
       
    20 lemma [code func]:
       
    21   "OperationalEquality.eq A B = (A \<subseteq> B \<and> B \<subseteq> A)"
       
    22   unfolding eq_def by blast
       
    23 
    18 declare bex_triv_one_point1 [symmetric, standard, code]
    24 declare bex_triv_one_point1 [symmetric, standard, code]
       
    25 
    19 
    26 
    20 section {* HOL definitions *}
    27 section {* HOL definitions *}
    21 
    28 
    22 subsection {* Basic definitions *}
    29 subsection {* Basic definitions *}
    23 
    30 
    24 definition
    31 definition
    25   flip :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> 'c"
    32   flip :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> 'c"
    26   "flip f a b = f b a"
    33   "flip f a b = f b a"
    27   member :: "'a list \<Rightarrow> 'a \<Rightarrow> bool"
    34   member :: "'a list \<Rightarrow> 'a\<Colon>eq \<Rightarrow> bool"
    28   "member xs x = (x \<in> set xs)"
    35   "member xs x = (x \<in> set xs)"
    29   insertl :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list"
    36   insertl :: "'a\<Colon>eq\<Rightarrow> 'a list \<Rightarrow> 'a list"
    30   "insertl x xs = (if member xs x then xs else x#xs)"
    37   "insertl x xs = (if member xs x then xs else x#xs)"
    31 
    38 
    32 lemma
    39 lemma
    33   [code target: List]: "member [] y = False"
    40   [code target: List]: "member [] y = False"
    34   and [code target: List]: "member (x#xs) y = (y = x \<or> member xs y)"
    41   and [code target: List]: "member (x#xs) y = (y = x \<or> member xs y)"
    42   "drop_first f (x#xs) = (if f x then xs else x # drop_first f xs)"
    49   "drop_first f (x#xs) = (if f x then xs else x # drop_first f xs)"
    43 declare drop_first.simps [code del]
    50 declare drop_first.simps [code del]
    44 declare drop_first.simps [code target: List]
    51 declare drop_first.simps [code target: List]
    45 
    52 
    46 declare remove1.simps [code del]
    53 declare remove1.simps [code del]
       
    54 ML {* reset CodegenData.strict_functyp *}
    47 lemma [code target: List]:
    55 lemma [code target: List]:
    48   "remove1 x xs = (if member xs x then drop_first (\<lambda>y. y = x) xs else xs)"
    56   "remove1 x xs = (if member xs x then drop_first (\<lambda>y. y = x) xs else xs)"
    49 proof (cases "member xs x")
    57 proof (cases "member xs x")
    50   case False thus ?thesis unfolding member_def by (induct xs) auto
    58   case False thus ?thesis unfolding member_def by (induct xs) auto
    51 next
    59 next
    52   case True
    60   case True
    53   have "remove1 x xs = drop_first (\<lambda>y. y = x) xs" by (induct xs) simp_all
    61   have "remove1 x xs = drop_first (\<lambda>y. y = x) xs" by (induct xs) simp_all
    54   with True show ?thesis by simp
    62   with True show ?thesis by simp
    55 qed
    63 qed
       
    64 ML {* set CodegenData.strict_functyp *}
    56 
    65 
    57 lemma member_nil [simp]:
    66 lemma member_nil [simp]:
    58   "member [] = (\<lambda>x. False)"
    67   "member [] = (\<lambda>x. False)"
    59 proof
    68 proof
    60   fix x
    69   fix x
    82   by (induct xs) simp_all
    91   by (induct xs) simp_all
    83 
    92 
    84 
    93 
    85 subsection {* Derived definitions *}
    94 subsection {* Derived definitions *}
    86 
    95 
    87 
    96 function unionl :: "'a\<Colon>eq list \<Rightarrow> 'a list \<Rightarrow> 'a list"
    88 function unionl :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
       
    89 where
    97 where
    90   "unionl [] ys = ys"
    98   "unionl [] ys = ys"
    91 | "unionl xs ys = foldr insertl xs ys"
    99 | "unionl xs ys = foldr insertl xs ys"
    92 by pat_completeness auto
   100 by pat_completeness auto
    93 termination unionl by (auto_term "{}")
   101 termination unionl by (auto_term "{}")
    94 lemmas unionl_def = unionl.simps(2)
   102 lemmas unionl_def = unionl.simps(2)
    95 declare unionl.simps[code]
   103 declare unionl.simps[code]
    96 
   104 
    97 function intersect :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
   105 function intersect :: "'a\<Colon>eq list \<Rightarrow> 'a list \<Rightarrow> 'a list"
    98 where
   106 where
    99   "intersect [] ys = []"
   107   "intersect [] ys = []"
   100 | "intersect xs [] = []"
   108 | "intersect xs [] = []"
   101 | "intersect xs ys = filter (member xs) ys"
   109 | "intersect xs ys = filter (member xs) ys"
   102   by pat_completeness auto
   110   by pat_completeness auto
   103 termination intersect by (auto_term "{}")
   111 termination intersect by (auto_term "{}")
   104 lemmas intersect_def = intersect.simps(3)
   112 lemmas intersect_def = intersect.simps(3)
   105 declare intersect.simps[code]
   113 declare intersect.simps[code]
   106 
   114 
   107 function subtract :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
   115 function subtract :: "'a\<Colon>eq list \<Rightarrow> 'a list \<Rightarrow> 'a list"
   108 where
   116 where
   109   "subtract [] ys = ys"
   117   "subtract [] ys = ys"
   110 | "subtract xs [] = []"
   118 | "subtract xs [] = []"
   111 | "subtract xs ys = foldr remove1 xs ys"
   119 | "subtract xs ys = foldr remove1 xs ys"
   112   by pat_completeness auto
   120   by pat_completeness auto
   113 termination subtract by (auto_term "{}")
   121 termination subtract by (auto_term "{}")
   114 lemmas subtract_def = subtract.simps(3)
   122 lemmas subtract_def = subtract.simps(3)
   115 declare subtract.simps[code]
   123 declare subtract.simps[code]
   116 
   124 
   117 function map_distinct :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list"
   125 function map_distinct :: "('a \<Rightarrow> 'b\<Colon>eq) \<Rightarrow> 'a list \<Rightarrow> 'b list"
   118 where
   126 where
   119   "map_distinct f [] = []"
   127   "map_distinct f [] = []"
   120 | "map_distinct f xs = foldr (insertl o f) xs []"
   128 | "map_distinct f xs = foldr (insertl o f) xs []"
   121   by pat_completeness auto
   129   by pat_completeness auto
   122 termination map_distinct by (auto_term "{}")
   130 termination map_distinct by (auto_term "{}")
   123 lemmas map_distinct_def = map_distinct.simps(2)
   131 lemmas map_distinct_def = map_distinct.simps(2)
   124 declare map_distinct.simps[code]
   132 declare map_distinct.simps[code]
   125 
   133 
   126 function unions :: "'a list list \<Rightarrow> 'a list"
   134 function unions :: "'a\<Colon>eq list list \<Rightarrow> 'a list"
   127 where
   135 where
   128   "unions [] = []"
   136   "unions [] = []"
   129   "unions xs = foldr unionl xs []"
   137   "unions xs = foldr unionl xs []"
   130   by pat_completeness auto
   138   by pat_completeness auto
   131 termination unions by (auto_term "{}")
   139 termination unions by (auto_term "{}")
   132 lemmas unions_def = unions.simps(2)
   140 lemmas unions_def = unions.simps(2)
   133 declare unions.simps[code]
   141 declare unions.simps[code]
   134 
   142 
   135 consts intersects :: "'a list list \<Rightarrow> 'a list"
   143 consts intersects :: "'a\<Colon>eq list list \<Rightarrow> 'a list"
   136 primrec
   144 primrec
   137   "intersects (x#xs) = foldr intersect xs x"
   145   "intersects (x#xs) = foldr intersect xs x"
   138 
   146 
   139 definition
   147 definition
   140   map_union :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b list) \<Rightarrow> 'b list"
   148   map_union :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b\<Colon>eq list) \<Rightarrow> 'b list"
   141   "map_union xs f = unions (map f xs)"
   149   "map_union xs f = unions (map f xs)"
   142   map_inter :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b list) \<Rightarrow> 'b list"
   150   map_inter :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b\<Colon>eq list) \<Rightarrow> 'b list"
   143   "map_inter xs f = intersects (map f xs)"
   151   "map_inter xs f = intersects (map f xs)"
   144 
   152 
   145 
   153 
   146 section {* Isomorphism proofs *}
   154 section {* Isomorphism proofs *}
   147 
   155 
   264 code_constname
   272 code_constname
   265   "ExecutableSet.member" "List.member"
   273   "ExecutableSet.member" "List.member"
   266   "ExecutableSet.insertl" "List.insertl"
   274   "ExecutableSet.insertl" "List.insertl"
   267   "ExecutableSet.drop_first" "List.drop_first"
   275   "ExecutableSet.drop_first" "List.drop_first"
   268 
   276 
   269 code_gen
       
   270   insertl unionl intersect flip subtract map_distinct
       
   271   unions intersects map_union map_inter Blall Blex
       
   272   (SML) (Haskell) 
       
   273 
       
   274 code_const "{}"
   277 code_const "{}"
   275   (SML target_atom "[]")
   278   (SML target_atom "[]")
   276   (Haskell target_atom "[]")
   279   (Haskell target_atom "[]")
   277 
   280 
   278 code_const insert
   281 code_const insert
   317 
   320 
   318 code_const Bex
   321 code_const Bex
   319   (SML "{*Blex*}")
   322   (SML "{*Blex*}")
   320   (Haskell "{*Blex*}")
   323   (Haskell "{*Blex*}")
   321 
   324 
       
   325 code_gen (SML -) (SML _)
       
   326 
   322 end
   327 end