src/HOL/Bali/Basis.thy
changeset 37956 ee939247b2fb
parent 36176 3fe7e97ccca8
child 44011 f67c93f52d13
equal deleted inserted replaced
37955:f87d1105e181 37956:ee939247b2fb
   178 
   178 
   179 hide_const In0 In1
   179 hide_const In0 In1
   180 
   180 
   181 notation sum_case  (infixr "'(+')"80)
   181 notation sum_case  (infixr "'(+')"80)
   182 
   182 
   183 consts    the_Inl  :: "'a + 'b \<Rightarrow> 'a"
   183 primrec the_Inl  :: "'a + 'b \<Rightarrow> 'a"
   184           the_Inr  :: "'a + 'b \<Rightarrow> 'b"
   184   where "the_Inl (Inl a) = a"
   185 primrec  "the_Inl (Inl a) = a"
   185 
   186 primrec  "the_Inr (Inr b) = b"
   186 primrec the_Inr  :: "'a + 'b \<Rightarrow> 'b"
       
   187   where "the_Inr (Inr b) = b"
   187 
   188 
   188 datatype ('a, 'b, 'c) sum3 = In1 'a | In2 'b | In3 'c
   189 datatype ('a, 'b, 'c) sum3 = In1 'a | In2 'b | In3 'c
   189 
   190 
   190 consts    the_In1  :: "('a, 'b, 'c) sum3 \<Rightarrow> 'a"
   191 primrec the_In1  :: "('a, 'b, 'c) sum3 \<Rightarrow> 'a"
   191           the_In2  :: "('a, 'b, 'c) sum3 \<Rightarrow> 'b"
   192   where "the_In1 (In1 a) = a"
   192           the_In3  :: "('a, 'b, 'c) sum3 \<Rightarrow> 'c"
   193 
   193 primrec  "the_In1 (In1 a) = a"
   194 primrec the_In2  :: "('a, 'b, 'c) sum3 \<Rightarrow> 'b"
   194 primrec  "the_In2 (In2 b) = b"
   195   where "the_In2 (In2 b) = b"
   195 primrec  "the_In3 (In3 c) = c"
   196 
       
   197 primrec the_In3  :: "('a, 'b, 'c) sum3 \<Rightarrow> 'c"
       
   198   where "the_In3 (In3 c) = c"
   196 
   199 
   197 abbreviation In1l   :: "'al \<Rightarrow> ('al + 'ar, 'b, 'c) sum3"
   200 abbreviation In1l   :: "'al \<Rightarrow> ('al + 'ar, 'b, 'c) sum3"
   198       where "In1l e == In1 (Inl e)"
   201   where "In1l e == In1 (Inl e)"
   199 
   202 
   200 abbreviation In1r   :: "'ar \<Rightarrow> ('al + 'ar, 'b, 'c) sum3"
   203 abbreviation In1r   :: "'ar \<Rightarrow> ('al + 'ar, 'b, 'c) sum3"
   201       where "In1r c == In1 (Inr c)"
   204   where "In1r c == In1 (Inr c)"
   202 
   205 
   203 abbreviation the_In1l :: "('al + 'ar, 'b, 'c) sum3 \<Rightarrow> 'al"
   206 abbreviation the_In1l :: "('al + 'ar, 'b, 'c) sum3 \<Rightarrow> 'al"
   204       where "the_In1l == the_Inl \<circ> the_In1"
   207   where "the_In1l == the_Inl \<circ> the_In1"
   205 
   208 
   206 abbreviation the_In1r :: "('al + 'ar, 'b, 'c) sum3 \<Rightarrow> 'ar"
   209 abbreviation the_In1r :: "('al + 'ar, 'b, 'c) sum3 \<Rightarrow> 'ar"
   207       where "the_In1r == the_Inr \<circ> the_In1"
   210   where "the_In1r == the_Inr \<circ> the_In1"
   208 
   211 
   209 ML {*
   212 ML {*
   210 fun sum3_instantiate ctxt thm = map (fn s =>
   213 fun sum3_instantiate ctxt thm = map (fn s =>
   211   simplify (simpset_of ctxt delsimps[@{thm not_None_eq}])
   214   simplify (simpset_of ctxt delsimps[@{thm not_None_eq}])
   212     (read_instantiate ctxt [(("t", 0), "In" ^ s ^ " ?x")] thm)) ["1l","2","3","1r"]
   215     (read_instantiate ctxt [(("t", 0), "In" ^ s ^ " ?x")] thm)) ["1l","2","3","1r"]
   230 
   233 
   231 section "Special map update"
   234 section "Special map update"
   232 
   235 
   233 text{* Deemed too special for theory Map. *}
   236 text{* Deemed too special for theory Map. *}
   234 
   237 
   235 definition chg_map :: "('b => 'b) => 'a => ('a ~=> 'b) => ('a ~=> 'b)" where
   238 definition
   236  "chg_map f a m == case m a of None => m | Some b => m(a|->f b)"
   239   chg_map :: "('b => 'b) => 'a => ('a ~=> 'b) => ('a ~=> 'b)"
       
   240   where "chg_map f a m = (case m a of None => m | Some b => m(a|->f b))"
   237 
   241 
   238 lemma chg_map_new[simp]: "m a = None   ==> chg_map f a m = m"
   242 lemma chg_map_new[simp]: "m a = None   ==> chg_map f a m = m"
   239 by (unfold chg_map_def, auto)
   243 by (unfold chg_map_def, auto)
   240 
   244 
   241 lemma chg_map_upd[simp]: "m a = Some b ==> chg_map f a m = m(a|->f b)"
   245 lemma chg_map_upd[simp]: "m a = Some b ==> chg_map f a m = m(a|->f b)"
   245 by (auto simp: chg_map_def split add: option.split)
   249 by (auto simp: chg_map_def split add: option.split)
   246 
   250 
   247 
   251 
   248 section "unique association lists"
   252 section "unique association lists"
   249 
   253 
   250 definition unique :: "('a \<times> 'b) list \<Rightarrow> bool" where
   254 definition
   251  "unique \<equiv> distinct \<circ> map fst"
   255   unique :: "('a \<times> 'b) list \<Rightarrow> bool"
       
   256   where "unique = distinct \<circ> map fst"
   252 
   257 
   253 lemma uniqueD [rule_format (no_asm)]: 
   258 lemma uniqueD [rule_format (no_asm)]: 
   254 "unique l--> (!x y. (x,y):set l --> (!x' y'. (x',y'):set l --> x=x'-->  y=y'))"
   259 "unique l--> (!x y. (x,y):set l --> (!x' y'. (x',y'):set l --> x=x'-->  y=y'))"
   255 apply (unfold unique_def o_def)
   260 apply (unfold unique_def o_def)
   256 apply (induct_tac "l")
   261 apply (induct_tac "l")
   294 done
   299 done
   295 
   300 
   296 
   301 
   297 section "list patterns"
   302 section "list patterns"
   298 
   303 
   299 consts
   304 definition
   300   lsplit         :: "[['a, 'a list] => 'b, 'a list] => 'b"
   305   lsplit :: "[['a, 'a list] => 'b, 'a list] => 'b" where
   301 defs
   306   "lsplit = (\<lambda>f l. f (hd l) (tl l))"
   302   lsplit_def:    "lsplit == %f l. f (hd l) (tl l)"
   307 
   303 (*  list patterns -- extends pre-defined type "pttrn" used in abstractions *)
   308 text {* list patterns -- extends pre-defined type "pttrn" used in abstractions *}
   304 syntax
   309 syntax
   305   "_lpttrn"    :: "[pttrn,pttrn] => pttrn"     ("_#/_" [901,900] 900)
   310   "_lpttrn"    :: "[pttrn,pttrn] => pttrn"     ("_#/_" [901,900] 900)
   306 translations
   311 translations
   307   "%y#x#xs. b"  == "CONST lsplit (%y x#xs. b)"
   312   "%y#x#xs. b"  == "CONST lsplit (%y x#xs. b)"
   308   "%x#xs  . b"  == "CONST lsplit (%x xs  . b)"
   313   "%x#xs  . b"  == "CONST lsplit (%x xs  . b)"