src/HOL/List.thy
 author haftmann Fri Apr 20 11:21:42 2007 +0200 (2007-04-20) changeset 22744 5cbe966d67a2 parent 22633 a47e4fd7ebc1 child 22793 dc13dfd588b2 permissions -rw-r--r--
Isar definitions are now added explicitly to code theorem table
     1 (*  Title:      HOL/List.thy

     2     ID:         $Id$

     3     Author:     Tobias Nipkow

     4 *)

     5

     6 header {* The datatype of finite lists *}

     7

     8 theory List

     9 imports PreList

    10 uses "Tools/string_syntax.ML"

    11 begin

    12

    13 datatype 'a list =

    14     Nil    ("[]")

    15   | Cons 'a  "'a list"    (infixr "#" 65)

    16

    17 subsection{*Basic list processing functions*}

    18

    19 consts

    20   "@" :: "'a list => 'a list => 'a list"    (infixr 65)

    21   filter:: "('a => bool) => 'a list => 'a list"

    22   concat:: "'a list list => 'a list"

    23   foldl :: "('b => 'a => 'b) => 'b => 'a list => 'b"

    24   foldr :: "('a => 'b => 'b) => 'a list => 'b => 'b"

    25   hd:: "'a list => 'a"

    26   tl:: "'a list => 'a list"

    27   last:: "'a list => 'a"

    28   butlast :: "'a list => 'a list"

    29   set :: "'a list => 'a set"

    30   map :: "('a=>'b) => ('a list => 'b list)"

    31   nth :: "'a list => nat => 'a"    (infixl "!" 100)

    32   list_update :: "'a list => nat => 'a => 'a list"

    33   take:: "nat => 'a list => 'a list"

    34   drop:: "nat => 'a list => 'a list"

    35   takeWhile :: "('a => bool) => 'a list => 'a list"

    36   dropWhile :: "('a => bool) => 'a list => 'a list"

    37   rev :: "'a list => 'a list"

    38   zip :: "'a list => 'b list => ('a * 'b) list"

    39   upt :: "nat => nat => nat list" ("(1[_..</_'])")

    40   remdups :: "'a list => 'a list"

    41   remove1 :: "'a => 'a list => 'a list"

    42   "distinct":: "'a list => bool"

    43   replicate :: "nat => 'a => 'a list"

    44   splice :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"

    45

    46 abbreviation

    47   upto:: "nat => nat => nat list"  ("(1[_../_])") where

    48   "[i..j] == [i..<(Suc j)]"

    49

    50

    51 nonterminals lupdbinds lupdbind

    52

    53 syntax

    54   -- {* list Enumeration *}

    55   "@list" :: "args => 'a list"    ("[(_)]")

    56

    57   -- {* Special syntax for filter *}

    58   "@filter" :: "[pttrn, 'a list, bool] => 'a list"    ("(1[_:_./ _])")

    59

    60   -- {* list update *}

    61   "_lupdbind":: "['a, 'a] => lupdbind"    ("(2_ :=/ _)")

    62   "" :: "lupdbind => lupdbinds"    ("_")

    63   "_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds"    ("_,/ _")

    64   "_LUpdate" :: "['a, lupdbinds] => 'a"    ("_/[(_)]" [900,0] 900)

    65

    66 translations

    67   "[x, xs]" == "x#[xs]"

    68   "[x]" == "x#[]"

    69   "[x:xs . P]"== "filter (%x. P) xs"

    70

    71   "_LUpdate xs (_lupdbinds b bs)"== "_LUpdate (_LUpdate xs b) bs"

    72   "xs[i:=x]" == "list_update xs i x"

    73

    74

    75 syntax (xsymbols)

    76   "@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<in>_ ./ _])")

    77 syntax (HTML output)

    78   "@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<in>_ ./ _])")

    79

    80

    81 text {*

    82   Function @{text size} is overloaded for all datatypes. Users may

    83   refer to the list version as @{text length}. *}

    84

    85 abbreviation

    86   length :: "'a list => nat" where

    87   "length == size"

    88

    89 primrec

    90   "hd(x#xs) = x"

    91

    92 primrec

    93   "tl([]) = []"

    94   "tl(x#xs) = xs"

    95

    96 primrec

    97   "last(x#xs) = (if xs=[] then x else last xs)"

    98

    99 primrec

   100   "butlast []= []"

   101   "butlast(x#xs) = (if xs=[] then [] else x#butlast xs)"

   102

   103 primrec

   104   "set [] = {}"

   105   "set (x#xs) = insert x (set xs)"

   106

   107 primrec

   108   "map f [] = []"

   109   "map f (x#xs) = f(x)#map f xs"

   110

   111 primrec

   112   append_Nil: "[]@ys = ys"

   113   append_Cons: "(x#xs)@ys = x#(xs@ys)"

   114

   115 primrec

   116   "rev([]) = []"

   117   "rev(x#xs) = rev(xs) @ [x]"

   118

   119 primrec

   120   "filter P [] = []"

   121   "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"

   122

   123 primrec

   124   foldl_Nil:"foldl f a [] = a"

   125   foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs"

   126

   127 primrec

   128   "foldr f [] a = a"

   129   "foldr f (x#xs) a = f x (foldr f xs a)"

   130

   131 primrec

   132   "concat([]) = []"

   133   "concat(x#xs) = x @ concat(xs)"

   134

   135 primrec

   136   drop_Nil:"drop n [] = []"

   137   drop_Cons: "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"

   138   -- {*Warning: simpset does not contain this definition, but separate

   139        theorems for @{text "n = 0"} and @{text "n = Suc k"} *}

   140

   141 primrec

   142   take_Nil:"take n [] = []"

   143   take_Cons: "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"

   144   -- {*Warning: simpset does not contain this definition, but separate

   145        theorems for @{text "n = 0"} and @{text "n = Suc k"} *}

   146

   147 primrec

   148   nth_Cons:"(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)"

   149   -- {*Warning: simpset does not contain this definition, but separate

   150        theorems for @{text "n = 0"} and @{text "n = Suc k"} *}

   151

   152 primrec

   153   "[][i:=v] = []"

   154   "(x#xs)[i:=v] = (case i of 0 => v # xs | Suc j => x # xs[j:=v])"

   155

   156 primrec

   157   "takeWhile P [] = []"

   158   "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])"

   159

   160 primrec

   161   "dropWhile P [] = []"

   162   "dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)"

   163

   164 primrec

   165   "zip xs [] = []"

   166   zip_Cons: "zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)"

   167   -- {*Warning: simpset does not contain this definition, but separate

   168        theorems for @{text "xs = []"} and @{text "xs = z # zs"} *}

   169

   170 primrec

   171   upt_0: "[i..<0] = []"

   172   upt_Suc: "[i..<(Suc j)] = (if i <= j then [i..<j] @ [j] else [])"

   173

   174 primrec

   175   "distinct [] = True"

   176   "distinct (x#xs) = (x ~: set xs \<and> distinct xs)"

   177

   178 primrec

   179   "remdups [] = []"

   180   "remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)"

   181

   182 primrec

   183   "remove1 x [] = []"

   184   "remove1 x (y#xs) = (if x=y then xs else y # remove1 x xs)"

   185

   186 primrec

   187   replicate_0: "replicate 0 x = []"

   188   replicate_Suc: "replicate (Suc n) x = x # replicate n x"

   189

   190 definition

   191   rotate1 :: "'a list \<Rightarrow> 'a list" where

   192   "rotate1 xs = (case xs of [] \<Rightarrow> [] | x#xs \<Rightarrow> xs @ [x])"

   193

   194 definition

   195   rotate :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where

   196   "rotate n = rotate1 ^ n"

   197

   198 definition

   199   list_all2 :: "('a => 'b => bool) => 'a list => 'b list => bool" where

   200   "list_all2 P xs ys =

   201     (length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y))"

   202

   203 definition

   204   sublist :: "'a list => nat set => 'a list" where

   205   "sublist xs A = map fst (filter (\<lambda>p. snd p \<in> A) (zip xs [0..<size xs]))"

   206

   207 primrec

   208   "splice [] ys = ys"

   209   "splice (x#xs) ys = (if ys=[] then x#xs else x # hd ys # splice xs (tl ys))"

   210     -- {*Warning: simpset does not contain the second eqn but a derived one. *}

   211

   212

   213 subsubsection {* @{const Nil} and @{const Cons} *}

   214

   215 lemma not_Cons_self [simp]:

   216   "xs \<noteq> x # xs"

   217 by (induct xs) auto

   218

   219 lemmas not_Cons_self2 [simp] = not_Cons_self [symmetric]

   220

   221 lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)"

   222 by (induct xs) auto

   223

   224 lemma length_induct:

   225   "(\<And>xs. \<forall>ys. length ys < length xs \<longrightarrow> P ys \<Longrightarrow> P xs) \<Longrightarrow> P xs"

   226 by (rule measure_induct [of length]) iprover

   227

   228

   229 subsubsection {* @{const length} *}

   230

   231 text {*

   232   Needs to come before @{text "@"} because of theorem @{text

   233   append_eq_append_conv}.

   234 *}

   235

   236 lemma length_append [simp]: "length (xs @ ys) = length xs + length ys"

   237 by (induct xs) auto

   238

   239 lemma length_map [simp]: "length (map f xs) = length xs"

   240 by (induct xs) auto

   241

   242 lemma length_rev [simp]: "length (rev xs) = length xs"

   243 by (induct xs) auto

   244

   245 lemma length_tl [simp]: "length (tl xs) = length xs - 1"

   246 by (cases xs) auto

   247

   248 lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])"

   249 by (induct xs) auto

   250

   251 lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])"

   252 by (induct xs) auto

   253

   254 lemma length_Suc_conv:

   255 "(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"

   256 by (induct xs) auto

   257

   258 lemma Suc_length_conv:

   259 "(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"

   260 apply (induct xs, simp, simp)

   261 apply blast

   262 done

   263

   264 lemma impossible_Cons [rule_format]:

   265   "length xs <= length ys --> xs = x # ys = False"

   266 apply (induct xs)

   267 apply auto

   268 done

   269

   270 lemma list_induct2[consumes 1]: "\<And>ys.

   271  \<lbrakk> length xs = length ys;

   272    P [] [];

   273    \<And>x xs y ys. \<lbrakk> length xs = length ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys) \<rbrakk>

   274  \<Longrightarrow> P xs ys"

   275 apply(induct xs)

   276  apply simp

   277 apply(case_tac ys)

   278  apply simp

   279 apply(simp)

   280 done

   281

   282 lemma list_induct2':

   283   "\<lbrakk> P [] [];

   284   \<And>x xs. P (x#xs) [];

   285   \<And>y ys. P [] (y#ys);

   286    \<And>x xs y ys. P xs ys  \<Longrightarrow> P (x#xs) (y#ys) \<rbrakk>

   287  \<Longrightarrow> P xs ys"

   288 by (induct xs arbitrary: ys) (case_tac x, auto)+

   289

   290 lemma neq_if_length_neq: "length xs \<noteq> length ys \<Longrightarrow> (xs = ys) == False"

   291 apply(rule Eq_FalseI)

   292 by auto

   293

   294 (*

   295 Reduces xs=ys to False if xs and ys cannot be of the same length.

   296 This is the case if the atomic sublists of one are a submultiset

   297 of those of the other list and there are fewer Cons's in one than the other.

   298 *)

   299 ML_setup {*

   300 local

   301

   302 fun len (Const("List.list.Nil",_)) acc = acc

   303   | len (Const("List.list.Cons",_) $_$ xs) (ts,n) = len xs (ts,n+1)

   304   | len (Const("List.op @",_) $xs$ ys) acc = len xs (len ys acc)

   305   | len (Const("List.rev",_) $xs) acc = len xs acc   306 | len (Const("List.map",_)$ _ $xs) acc = len xs acc   307 | len t (ts,n) = (t::ts,n);   308   309 fun list_eq ss (Const(_,eqT)$ lhs $rhs) =   310 let   311 val (ls,m) = len lhs ([],0) and (rs,n) = len rhs ([],0);   312 fun prove_neq() =   313 let   314 val Type(_,listT::_) = eqT;   315 val size = Const("Nat.size", listT --> HOLogic.natT);   316 val eq_len = HOLogic.mk_eq (size$ lhs, size $rhs);   317 val neq_len = HOLogic.mk_Trueprop (HOLogic.Not$ eq_len);

   318         val thm = Goal.prove (Simplifier.the_context ss) [] [] neq_len

   319           (K (simp_tac (Simplifier.inherit_context ss @{simpset}) 1));

   320       in SOME (thm RS @{thm neq_if_length_neq}) end

   321   in

   322     if m < n andalso gen_submultiset (op aconv) (ls,rs) orelse

   323        n < m andalso gen_submultiset (op aconv) (rs,ls)

   324     then prove_neq() else NONE

   325   end;

   326

   327 in

   328

   329 val list_neq_simproc =

   330   Simplifier.simproc @{theory} "list_neq" ["(xs::'a list) = ys"] (K list_eq);

   331

   332 end;

   333

   334 Addsimprocs [list_neq_simproc];

   335 *}

   336

   337

   338 subsubsection {* @{text "@"} -- append *}

   339

   340 lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)"

   341 by (induct xs) auto

   342

   343 lemma append_Nil2 [simp]: "xs @ [] = xs"

   344 by (induct xs) auto

   345

   346 lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])"

   347 by (induct xs) auto

   348

   349 lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])"

   350 by (induct xs) auto

   351

   352 lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])"

   353 by (induct xs) auto

   354

   355 lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])"

   356 by (induct xs) auto

   357

   358 lemma append_eq_append_conv [simp]:

   359  "!!ys. length xs = length ys \<or> length us = length vs

   360  ==> (xs@us = ys@vs) = (xs=ys \<and> us=vs)"

   361 apply (induct xs)

   362  apply (case_tac ys, simp, force)

   363 apply (case_tac ys, force, simp)

   364 done

   365

   366 lemma append_eq_append_conv2: "!!ys zs ts.

   367  (xs @ ys = zs @ ts) =

   368  (EX us. xs = zs @ us & us @ ys = ts | xs @ us = zs & ys = us@ ts)"

   369 apply (induct xs)

   370  apply fastsimp

   371 apply(case_tac zs)

   372  apply simp

   373 apply fastsimp

   374 done

   375

   376 lemma same_append_eq [iff]: "(xs @ ys = xs @ zs) = (ys = zs)"

   377 by simp

   378

   379 lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)"

   380 by simp

   381

   382 lemma append_same_eq [iff]: "(ys @ xs = zs @ xs) = (ys = zs)"

   383 by simp

   384

   385 lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])"

   386 using append_same_eq [of _ _ "[]"] by auto

   387

   388 lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])"

   389 using append_same_eq [of "[]"] by auto

   390

   391 lemma hd_Cons_tl [simp]: "xs \<noteq> [] ==> hd xs # tl xs = xs"

   392 by (induct xs) auto

   393

   394 lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)"

   395 by (induct xs) auto

   396

   397 lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs"

   398 by (simp add: hd_append split: list.split)

   399

   400 lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)"

   401 by (simp split: list.split)

   402

   403 lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys"

   404 by (simp add: tl_append split: list.split)

   405

   406

   407 lemma Cons_eq_append_conv: "x#xs = ys@zs =

   408  (ys = [] & x#xs = zs | (EX ys'. x#ys' = ys & xs = ys'@zs))"

   409 by(cases ys) auto

   410

   411 lemma append_eq_Cons_conv: "(ys@zs = x#xs) =

   412  (ys = [] & zs = x#xs | (EX ys'. ys = x#ys' & ys'@zs = xs))"

   413 by(cases ys) auto

   414

   415

   416 text {* Trivial rules for solving @{text "@"}-equations automatically. *}

   417

   418 lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys"

   419 by simp

   420

   421 lemma Cons_eq_appendI:

   422 "[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs"

   423 by (drule sym) simp

   424

   425 lemma append_eq_appendI:

   426 "[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us"

   427 by (drule sym) simp

   428

   429

   430 text {*

   431 Simplification procedure for all list equalities.

   432 Currently only tries to rearrange @{text "@"} to see if

   433 - both lists end in a singleton list,

   434 - or both lists end in the same list.

   435 *}

   436

   437 ML_setup {*

   438 local

   439

   440 fun last (cons as Const("List.list.Cons",_) $_$ xs) =

   441   (case xs of Const("List.list.Nil",_) => cons | _ => last xs)

   442   | last (Const("List.op @",_) $_$ ys) = last ys

   443   | last t = t;

   444

   445 fun list1 (Const("List.list.Cons",_) $_$ Const("List.list.Nil",_)) = true

   446   | list1 _ = false;

   447

   448 fun butlast ((cons as Const("List.list.Cons",_) $x)$ xs) =

   449   (case xs of Const("List.list.Nil",_) => xs | _ => cons $butlast xs)   450 | butlast ((app as Const("List.op @",_)$ xs) $ys) = app$ butlast ys

   451   | butlast xs = Const("List.list.Nil",fastype_of xs);

   452

   453 val rearr_ss = HOL_basic_ss addsimps [@{thm append_assoc},

   454   @{thm append_Nil}, @{thm append_Cons}];

   455

   456 fun list_eq ss (F as (eq as Const(_,eqT)) $lhs$ rhs) =

   457   let

   458     val lastl = last lhs and lastr = last rhs;

   459     fun rearr conv =

   460       let

   461         val lhs1 = butlast lhs and rhs1 = butlast rhs;

   462         val Type(_,listT::_) = eqT

   463         val appT = [listT,listT] ---> listT

   464         val app = Const("List.op @",appT)

   465         val F2 = eq $(app$lhs1$lastl)$ (app$rhs1$lastr)

   466         val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2));

   467         val thm = Goal.prove (Simplifier.the_context ss) [] [] eq

   468           (K (simp_tac (Simplifier.inherit_context ss rearr_ss) 1));

   469       in SOME ((conv RS (thm RS trans)) RS eq_reflection) end;

   470

   471   in

   472     if list1 lastl andalso list1 lastr then rearr @{thm append1_eq_conv}

   473     else if lastl aconv lastr then rearr @{thm append_same_eq}

   474     else NONE

   475   end;

   476

   477 in

   478

   479 val list_eq_simproc =

   480   Simplifier.simproc @{theory} "list_eq" ["(xs::'a list) = ys"] (K list_eq);

   481

   482 end;

   483

   484 Addsimprocs [list_eq_simproc];

   485 *}

   486

   487

   488 subsubsection {* @{text map} *}

   489

   490 lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs"

   491 by (induct xs) simp_all

   492

   493 lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)"

   494 by (rule ext, induct_tac xs) auto

   495

   496 lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys"

   497 by (induct xs) auto

   498

   499 lemma map_compose: "map (f o g) xs = map f (map g xs)"

   500 by (induct xs) (auto simp add: o_def)

   501

   502 lemma rev_map: "rev (map f xs) = map f (rev xs)"

   503 by (induct xs) auto

   504

   505 lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)"

   506 by (induct xs) auto

   507

   508 lemma map_cong [fundef_cong, recdef_cong]:

   509 "xs = ys ==> (!!x. x : set ys ==> f x = g x) ==> map f xs = map g ys"

   510 -- {* a congruence rule for @{text map} *}

   511 by simp

   512

   513 lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])"

   514 by (cases xs) auto

   515

   516 lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])"

   517 by (cases xs) auto

   518

   519 lemma map_eq_Cons_conv:

   520  "(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)"

   521 by (cases xs) auto

   522

   523 lemma Cons_eq_map_conv:

   524  "(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)"

   525 by (cases ys) auto

   526

   527 lemmas map_eq_Cons_D = map_eq_Cons_conv [THEN iffD1]

   528 lemmas Cons_eq_map_D = Cons_eq_map_conv [THEN iffD1]

   529 declare map_eq_Cons_D [dest!]  Cons_eq_map_D [dest!]

   530

   531 lemma ex_map_conv:

   532   "(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)"

   533 by(induct ys, auto simp add: Cons_eq_map_conv)

   534

   535 lemma map_eq_imp_length_eq:

   536   "!!xs. map f xs = map f ys ==> length xs = length ys"

   537 apply (induct ys)

   538  apply simp

   539 apply(simp (no_asm_use))

   540 apply clarify

   541 apply(simp (no_asm_use))

   542 apply fast

   543 done

   544

   545 lemma map_inj_on:

   546  "[| map f xs = map f ys; inj_on f (set xs Un set ys) |]

   547   ==> xs = ys"

   548 apply(frule map_eq_imp_length_eq)

   549 apply(rotate_tac -1)

   550 apply(induct rule:list_induct2)

   551  apply simp

   552 apply(simp)

   553 apply (blast intro:sym)

   554 done

   555

   556 lemma inj_on_map_eq_map:

   557  "inj_on f (set xs Un set ys) \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"

   558 by(blast dest:map_inj_on)

   559

   560 lemma map_injective:

   561  "!!xs. map f xs = map f ys ==> inj f ==> xs = ys"

   562 by (induct ys) (auto dest!:injD)

   563

   564 lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"

   565 by(blast dest:map_injective)

   566

   567 lemma inj_mapI: "inj f ==> inj (map f)"

   568 by (iprover dest: map_injective injD intro: inj_onI)

   569

   570 lemma inj_mapD: "inj (map f) ==> inj f"

   571 apply (unfold inj_on_def, clarify)

   572 apply (erule_tac x = "[x]" in ballE)

   573  apply (erule_tac x = "[y]" in ballE, simp, blast)

   574 apply blast

   575 done

   576

   577 lemma inj_map[iff]: "inj (map f) = inj f"

   578 by (blast dest: inj_mapD intro: inj_mapI)

   579

   580 lemma inj_on_mapI: "inj_on f (\<Union>(set  A)) \<Longrightarrow> inj_on (map f) A"

   581 apply(rule inj_onI)

   582 apply(erule map_inj_on)

   583 apply(blast intro:inj_onI dest:inj_onD)

   584 done

   585

   586 lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs"

   587 by (induct xs, auto)

   588

   589 lemma map_fun_upd [simp]: "y \<notin> set xs \<Longrightarrow> map (f(y:=v)) xs = map f xs"

   590 by (induct xs) auto

   591

   592 lemma map_fst_zip[simp]:

   593   "length xs = length ys \<Longrightarrow> map fst (zip xs ys) = xs"

   594 by (induct rule:list_induct2, simp_all)

   595

   596 lemma map_snd_zip[simp]:

   597   "length xs = length ys \<Longrightarrow> map snd (zip xs ys) = ys"

   598 by (induct rule:list_induct2, simp_all)

   599

   600

   601 subsubsection {* @{text rev} *}

   602

   603 lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs"

   604 by (induct xs) auto

   605

   606 lemma rev_rev_ident [simp]: "rev (rev xs) = xs"

   607 by (induct xs) auto

   608

   609 lemma rev_swap: "(rev xs = ys) = (xs = rev ys)"

   610 by auto

   611

   612 lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])"

   613 by (induct xs) auto

   614

   615 lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])"

   616 by (induct xs) auto

   617

   618 lemma rev_singleton_conv [simp]: "(rev xs = [x]) = (xs = [x])"

   619 by (cases xs) auto

   620

   621 lemma singleton_rev_conv [simp]: "([x] = rev xs) = (xs = [x])"

   622 by (cases xs) auto

   623

   624 lemma rev_is_rev_conv [iff]: "(rev xs = rev ys) = (xs = ys)"

   625 apply (induct xs arbitrary: ys, force)

   626 apply (case_tac ys, simp, force)

   627 done

   628

   629 lemma inj_on_rev[iff]: "inj_on rev A"

   630 by(simp add:inj_on_def)

   631

   632 lemma rev_induct [case_names Nil snoc]:

   633   "[| P []; !!x xs. P xs ==> P (xs @ [x]) |] ==> P xs"

   634 apply(simplesubst rev_rev_ident[symmetric])

   635 apply(rule_tac list = "rev xs" in list.induct, simp_all)

   636 done

   637

   638 ML {* val rev_induct_tac = induct_thm_tac (thm "rev_induct") *}-- "compatibility"

   639

   640 lemma rev_exhaust [case_names Nil snoc]:

   641   "(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P"

   642 by (induct xs rule: rev_induct) auto

   643

   644 lemmas rev_cases = rev_exhaust

   645

   646 lemma rev_eq_Cons_iff[iff]: "(rev xs = y#ys) = (xs = rev ys @ [y])"

   647 by(rule rev_cases[of xs]) auto

   648

   649

   650 subsubsection {* @{text set} *}

   651

   652 lemma finite_set [iff]: "finite (set xs)"

   653 by (induct xs) auto

   654

   655 lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)"

   656 by (induct xs) auto

   657

   658 lemma hd_in_set[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs : set xs"

   659 by(cases xs) auto

   660

   661 lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)"

   662 by auto

   663

   664 lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs"

   665 by auto

   666

   667 lemma set_empty [iff]: "(set xs = {}) = (xs = [])"

   668 by (induct xs) auto

   669

   670 lemma set_empty2[iff]: "({} = set xs) = (xs = [])"

   671 by(induct xs) auto

   672

   673 lemma set_rev [simp]: "set (rev xs) = set xs"

   674 by (induct xs) auto

   675

   676 lemma set_map [simp]: "set (map f xs) = f(set xs)"

   677 by (induct xs) auto

   678

   679 lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}"

   680 by (induct xs) auto

   681

   682 lemma set_upt [simp]: "set[i..<j] = {k. i \<le> k \<and> k < j}"

   683 apply (induct j, simp_all)

   684 apply (erule ssubst, auto)

   685 done

   686

   687 lemma in_set_conv_decomp: "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs)"

   688 proof (induct xs)

   689   case Nil show ?case by simp

   690   case (Cons a xs)

   691   show ?case

   692   proof

   693     assume "x \<in> set (a # xs)"

   694     with prems show "\<exists>ys zs. a # xs = ys @ x # zs"

   695       by (simp, blast intro: Cons_eq_appendI)

   696   next

   697     assume "\<exists>ys zs. a # xs = ys @ x # zs"

   698     then obtain ys zs where eq: "a # xs = ys @ x # zs" by blast

   699     show "x \<in> set (a # xs)"

   700       by (cases ys, auto simp add: eq)

   701   qed

   702 qed

   703

   704 lemma in_set_conv_decomp_first:

   705  "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys)"

   706 proof (induct xs)

   707   case Nil show ?case by simp

   708 next

   709   case (Cons a xs)

   710   show ?case

   711   proof cases

   712     assume "x = a" thus ?case using Cons by force

   713   next

   714     assume "x \<noteq> a"

   715     show ?case

   716     proof

   717       assume "x \<in> set (a # xs)"

   718       from prems show "\<exists>ys zs. a # xs = ys @ x # zs \<and> x \<notin> set ys"

   719 	by(fastsimp intro!: Cons_eq_appendI)

   720     next

   721       assume "\<exists>ys zs. a # xs = ys @ x # zs \<and> x \<notin> set ys"

   722       then obtain ys zs where eq: "a # xs = ys @ x # zs" by blast

   723       show "x \<in> set (a # xs)" by (cases ys, auto simp add: eq)

   724     qed

   725   qed

   726 qed

   727

   728 lemmas split_list       = in_set_conv_decomp[THEN iffD1, standard]

   729 lemmas split_list_first = in_set_conv_decomp_first[THEN iffD1, standard]

   730

   731

   732 lemma finite_list: "finite A ==> EX l. set l = A"

   733 apply (erule finite_induct, auto)

   734 apply (rule_tac x="x#l" in exI, auto)

   735 done

   736

   737 lemma card_length: "card (set xs) \<le> length xs"

   738 by (induct xs) (auto simp add: card_insert_if)

   739

   740

   741 subsubsection {* @{text filter} *}

   742

   743 lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys"

   744 by (induct xs) auto

   745

   746 lemma rev_filter: "rev (filter P xs) = filter P (rev xs)"

   747 by (induct xs) simp_all

   748

   749 lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs"

   750 by (induct xs) auto

   751

   752 lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs"

   753 by (induct xs) (auto simp add: le_SucI)

   754

   755 lemma sum_length_filter_compl:

   756   "length(filter P xs) + length(filter (%x. ~P x) xs) = length xs"

   757 by(induct xs) simp_all

   758

   759 lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs"

   760 by (induct xs) auto

   761

   762 lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []"

   763 by (induct xs) auto

   764

   765 lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)"

   766   by (induct xs) simp_all

   767

   768 lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)"

   769 apply (induct xs)

   770  apply auto

   771 apply(cut_tac P=P and xs=xs in length_filter_le)

   772 apply simp

   773 done

   774

   775 lemma filter_map:

   776   "filter P (map f xs) = map f (filter (P o f) xs)"

   777 by (induct xs) simp_all

   778

   779 lemma length_filter_map[simp]:

   780   "length (filter P (map f xs)) = length(filter (P o f) xs)"

   781 by (simp add:filter_map)

   782

   783 lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs"

   784 by auto

   785

   786 lemma length_filter_less:

   787   "\<lbrakk> x : set xs; ~ P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs"

   788 proof (induct xs)

   789   case Nil thus ?case by simp

   790 next

   791   case (Cons x xs) thus ?case

   792     apply (auto split:split_if_asm)

   793     using length_filter_le[of P xs] apply arith

   794   done

   795 qed

   796

   797 lemma length_filter_conv_card:

   798  "length(filter p xs) = card{i. i < length xs & p(xs!i)}"

   799 proof (induct xs)

   800   case Nil thus ?case by simp

   801 next

   802   case (Cons x xs)

   803   let ?S = "{i. i < length xs & p(xs!i)}"

   804   have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite)

   805   show ?case (is "?l = card ?S'")

   806   proof (cases)

   807     assume "p x"

   808     hence eq: "?S' = insert 0 (Suc  ?S)"

   809       by(auto simp add: nth_Cons image_def split:nat.split elim:lessE)

   810     have "length (filter p (x # xs)) = Suc(card ?S)"

   811       using Cons by simp

   812     also have "\<dots> = Suc(card(Suc  ?S))" using fin

   813       by (simp add: card_image inj_Suc)

   814     also have "\<dots> = card ?S'" using eq fin

   815       by (simp add:card_insert_if) (simp add:image_def)

   816     finally show ?thesis .

   817   next

   818     assume "\<not> p x"

   819     hence eq: "?S' = Suc  ?S"

   820       by(auto simp add: nth_Cons image_def split:nat.split elim:lessE)

   821     have "length (filter p (x # xs)) = card ?S"

   822       using Cons by simp

   823     also have "\<dots> = card(Suc  ?S)" using fin

   824       by (simp add: card_image inj_Suc)

   825     also have "\<dots> = card ?S'" using eq fin

   826       by (simp add:card_insert_if)

   827     finally show ?thesis .

   828   qed

   829 qed

   830

   831 lemma Cons_eq_filterD:

   832  "x#xs = filter P ys \<Longrightarrow>

   833   \<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs"

   834   (is "_ \<Longrightarrow> \<exists>us vs. ?P ys us vs")

   835 proof(induct ys)

   836   case Nil thus ?case by simp

   837 next

   838   case (Cons y ys)

   839   show ?case (is "\<exists>x. ?Q x")

   840   proof cases

   841     assume Py: "P y"

   842     show ?thesis

   843     proof cases

   844       assume xy: "x = y"

   845       show ?thesis

   846       proof from Py xy Cons(2) show "?Q []" by simp qed

   847     next

   848       assume "x \<noteq> y" with Py Cons(2) show ?thesis by simp

   849     qed

   850   next

   851     assume Py: "\<not> P y"

   852     with Cons obtain us vs where 1 : "?P (y#ys) (y#us) vs" by fastsimp

   853     show ?thesis (is "? us. ?Q us")

   854     proof show "?Q (y#us)" using 1 by simp qed

   855   qed

   856 qed

   857

   858 lemma filter_eq_ConsD:

   859  "filter P ys = x#xs \<Longrightarrow>

   860   \<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs"

   861 by(rule Cons_eq_filterD) simp

   862

   863 lemma filter_eq_Cons_iff:

   864  "(filter P ys = x#xs) =

   865   (\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)"

   866 by(auto dest:filter_eq_ConsD)

   867

   868 lemma Cons_eq_filter_iff:

   869  "(x#xs = filter P ys) =

   870   (\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)"

   871 by(auto dest:Cons_eq_filterD)

   872

   873 lemma filter_cong[fundef_cong, recdef_cong]:

   874  "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys"

   875 apply simp

   876 apply(erule thin_rl)

   877 by (induct ys) simp_all

   878

   879

   880 subsubsection {* @{text concat} *}

   881

   882 lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys"

   883 by (induct xs) auto

   884

   885 lemma concat_eq_Nil_conv [simp]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])"

   886 by (induct xss) auto

   887

   888 lemma Nil_eq_concat_conv [simp]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])"

   889 by (induct xss) auto

   890

   891 lemma set_concat [simp]: "set (concat xs) = \<Union>(set  set xs)"

   892 by (induct xs) auto

   893

   894 lemma map_concat: "map f (concat xs) = concat (map (map f) xs)"

   895 by (induct xs) auto

   896

   897 lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)"

   898 by (induct xs) auto

   899

   900 lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))"

   901 by (induct xs) auto

   902

   903

   904 subsubsection {* @{text nth} *}

   905

   906 lemma nth_Cons_0 [simp]: "(x # xs)!0 = x"

   907 by auto

   908

   909 lemma nth_Cons_Suc [simp]: "(x # xs)!(Suc n) = xs!n"

   910 by auto

   911

   912 declare nth.simps [simp del]

   913

   914 lemma nth_append:

   915 "!!n. (xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"

   916 apply (induct "xs", simp)

   917 apply (case_tac n, auto)

   918 done

   919

   920 lemma nth_append_length [simp]: "(xs @ x # ys) ! length xs = x"

   921 by (induct "xs") auto

   922

   923 lemma nth_append_length_plus[simp]: "(xs @ ys) ! (length xs + n) = ys ! n"

   924 by (induct "xs") auto

   925

   926 lemma nth_map [simp]: "!!n. n < length xs ==> (map f xs)!n = f(xs!n)"

   927 apply (induct xs, simp)

   928 apply (case_tac n, auto)

   929 done

   930

   931 lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0"

   932 by(cases xs) simp_all

   933

   934

   935 lemma list_eq_iff_nth_eq:

   936  "!!ys. (xs = ys) = (length xs = length ys \<and> (ALL i<length xs. xs!i = ys!i))"

   937 apply(induct xs)

   938  apply simp apply blast

   939 apply(case_tac ys)

   940  apply simp

   941 apply(simp add:nth_Cons split:nat.split)apply blast

   942 done

   943

   944 lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}"

   945 apply (induct xs, simp, simp)

   946 apply safe

   947 apply (rule_tac x = 0 in exI, simp)

   948  apply (rule_tac x = "Suc i" in exI, simp)

   949 apply (case_tac i, simp)

   950 apply (rename_tac j)

   951 apply (rule_tac x = j in exI, simp)

   952 done

   953

   954 lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)"

   955 by(auto simp:set_conv_nth)

   956

   957 lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)"

   958 by (auto simp add: set_conv_nth)

   959

   960 lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs"

   961 by (auto simp add: set_conv_nth)

   962

   963 lemma all_nth_imp_all_set:

   964 "[| !i < length xs. P(xs!i); x : set xs|] ==> P x"

   965 by (auto simp add: set_conv_nth)

   966

   967 lemma all_set_conv_all_nth:

   968 "(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))"

   969 by (auto simp add: set_conv_nth)

   970

   971

   972 subsubsection {* @{text list_update} *}

   973

   974 lemma length_list_update [simp]: "!!i. length(xs[i:=x]) = length xs"

   975 by (induct xs) (auto split: nat.split)

   976

   977 lemma nth_list_update:

   978 "!!i j. i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)"

   979 by (induct xs) (auto simp add: nth_Cons split: nat.split)

   980

   981 lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x"

   982 by (simp add: nth_list_update)

   983

   984 lemma nth_list_update_neq [simp]: "!!i j. i \<noteq> j ==> xs[i:=x]!j = xs!j"

   985 by (induct xs) (auto simp add: nth_Cons split: nat.split)

   986

   987 lemma list_update_overwrite [simp]:

   988 "!!i. i < size xs ==> xs[i:=x, i:=y] = xs[i:=y]"

   989 by (induct xs) (auto split: nat.split)

   990

   991 lemma list_update_id[simp]: "!!i. i < length xs ==> xs[i := xs!i] = xs"

   992 apply (induct xs, simp)

   993 apply(simp split:nat.splits)

   994 done

   995

   996 lemma list_update_beyond[simp]: "\<And>i. length xs \<le> i \<Longrightarrow> xs[i:=x] = xs"

   997 apply (induct xs)

   998  apply simp

   999 apply (case_tac i)

  1000 apply simp_all

  1001 done

  1002

  1003 lemma list_update_same_conv:

  1004 "!!i. i < length xs ==> (xs[i := x] = xs) = (xs!i = x)"

  1005 by (induct xs) (auto split: nat.split)

  1006

  1007 lemma list_update_append1:

  1008  "!!i. i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys"

  1009 apply (induct xs, simp)

  1010 apply(simp split:nat.split)

  1011 done

  1012

  1013 lemma list_update_append:

  1014   "!!n. (xs @ ys) [n:= x] =

  1015   (if n < length xs then xs[n:= x] @ ys else xs @ (ys [n-length xs:= x]))"

  1016 by (induct xs) (auto split:nat.splits)

  1017

  1018 lemma list_update_length [simp]:

  1019  "(xs @ x # ys)[length xs := y] = (xs @ y # ys)"

  1020 by (induct xs, auto)

  1021

  1022 lemma update_zip:

  1023 "!!i xy xs. length xs = length ys ==>

  1024 (zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])"

  1025 by (induct ys) (auto, case_tac xs, auto split: nat.split)

  1026

  1027 lemma set_update_subset_insert: "!!i. set(xs[i:=x]) <= insert x (set xs)"

  1028 by (induct xs) (auto split: nat.split)

  1029

  1030 lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A"

  1031 by (blast dest!: set_update_subset_insert [THEN subsetD])

  1032

  1033 lemma set_update_memI: "!!n. n < length xs \<Longrightarrow> x \<in> set (xs[n := x])"

  1034 by (induct xs) (auto split:nat.splits)

  1035

  1036

  1037 subsubsection {* @{text last} and @{text butlast} *}

  1038

  1039 lemma last_snoc [simp]: "last (xs @ [x]) = x"

  1040 by (induct xs) auto

  1041

  1042 lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs"

  1043 by (induct xs) auto

  1044

  1045 lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x"

  1046 by(simp add:last.simps)

  1047

  1048 lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs"

  1049 by(simp add:last.simps)

  1050

  1051 lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)"

  1052 by (induct xs) (auto)

  1053

  1054 lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs"

  1055 by(simp add:last_append)

  1056

  1057 lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys"

  1058 by(simp add:last_append)

  1059

  1060 lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs"

  1061 by(rule rev_exhaust[of xs]) simp_all

  1062

  1063 lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs"

  1064 by(cases xs) simp_all

  1065

  1066 lemma last_in_set[simp]: "as \<noteq> [] \<Longrightarrow> last as \<in> set as"

  1067 by (induct as) auto

  1068

  1069 lemma length_butlast [simp]: "length (butlast xs) = length xs - 1"

  1070 by (induct xs rule: rev_induct) auto

  1071

  1072 lemma butlast_append:

  1073 "!!ys. butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)"

  1074 by (induct xs) auto

  1075

  1076 lemma append_butlast_last_id [simp]:

  1077 "xs \<noteq> [] ==> butlast xs @ [last xs] = xs"

  1078 by (induct xs) auto

  1079

  1080 lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs"

  1081 by (induct xs) (auto split: split_if_asm)

  1082

  1083 lemma in_set_butlast_appendI:

  1084 "x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))"

  1085 by (auto dest: in_set_butlastD simp add: butlast_append)

  1086

  1087 lemma last_drop[simp]: "!!n. n < length xs \<Longrightarrow> last (drop n xs) = last xs"

  1088 apply (induct xs)

  1089  apply simp

  1090 apply (auto split:nat.split)

  1091 done

  1092

  1093 lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)"

  1094 by(induct xs)(auto simp:neq_Nil_conv)

  1095

  1096 subsubsection {* @{text take} and @{text drop} *}

  1097

  1098 lemma take_0 [simp]: "take 0 xs = []"

  1099 by (induct xs) auto

  1100

  1101 lemma drop_0 [simp]: "drop 0 xs = xs"

  1102 by (induct xs) auto

  1103

  1104 lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs"

  1105 by simp

  1106

  1107 lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs"

  1108 by simp

  1109

  1110 declare take_Cons [simp del] and drop_Cons [simp del]

  1111

  1112 lemma take_Suc: "xs ~= [] ==> take (Suc n) xs = hd xs # take n (tl xs)"

  1113 by(clarsimp simp add:neq_Nil_conv)

  1114

  1115 lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)"

  1116 by(cases xs, simp_all)

  1117

  1118 lemma drop_tl: "!!n. drop n (tl xs) = tl(drop n xs)"

  1119 by(induct xs, simp_all add:drop_Cons drop_Suc split:nat.split)

  1120

  1121 lemma nth_via_drop: "!!n. drop n xs = y#ys \<Longrightarrow> xs!n = y"

  1122 apply (induct xs, simp)

  1123 apply(simp add:drop_Cons nth_Cons split:nat.splits)

  1124 done

  1125

  1126 lemma take_Suc_conv_app_nth:

  1127  "!!i. i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]"

  1128 apply (induct xs, simp)

  1129 apply (case_tac i, auto)

  1130 done

  1131

  1132 lemma drop_Suc_conv_tl:

  1133   "!!i. i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs"

  1134 apply (induct xs, simp)

  1135 apply (case_tac i, auto)

  1136 done

  1137

  1138 lemma length_take [simp]: "!!xs. length (take n xs) = min (length xs) n"

  1139 by (induct n) (auto, case_tac xs, auto)

  1140

  1141 lemma length_drop [simp]: "!!xs. length (drop n xs) = (length xs - n)"

  1142 by (induct n) (auto, case_tac xs, auto)

  1143

  1144 lemma take_all [simp]: "!!xs. length xs <= n ==> take n xs = xs"

  1145 by (induct n) (auto, case_tac xs, auto)

  1146

  1147 lemma drop_all [simp]: "!!xs. length xs <= n ==> drop n xs = []"

  1148 by (induct n) (auto, case_tac xs, auto)

  1149

  1150 lemma take_append [simp]:

  1151 "!!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"

  1152 by (induct n) (auto, case_tac xs, auto)

  1153

  1154 lemma drop_append [simp]:

  1155 "!!xs. drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys"

  1156 by (induct n) (auto, case_tac xs, auto)

  1157

  1158 lemma take_take [simp]: "!!xs n. take n (take m xs) = take (min n m) xs"

  1159 apply (induct m, auto)

  1160 apply (case_tac xs, auto)

  1161 apply (case_tac n, auto)

  1162 done

  1163

  1164 lemma drop_drop [simp]: "!!xs. drop n (drop m xs) = drop (n + m) xs"

  1165 apply (induct m, auto)

  1166 apply (case_tac xs, auto)

  1167 done

  1168

  1169 lemma take_drop: "!!xs n. take n (drop m xs) = drop m (take (n + m) xs)"

  1170 apply (induct m, auto)

  1171 apply (case_tac xs, auto)

  1172 done

  1173

  1174 lemma drop_take: "!!m n. drop n (take m xs) = take (m-n) (drop n xs)"

  1175 apply(induct xs)

  1176  apply simp

  1177 apply(simp add: take_Cons drop_Cons split:nat.split)

  1178 done

  1179

  1180 lemma append_take_drop_id [simp]: "!!xs. take n xs @ drop n xs = xs"

  1181 apply (induct n, auto)

  1182 apply (case_tac xs, auto)

  1183 done

  1184

  1185 lemma take_eq_Nil[simp]: "!!n. (take n xs = []) = (n = 0 \<or> xs = [])"

  1186 apply(induct xs)

  1187  apply simp

  1188 apply(simp add:take_Cons split:nat.split)

  1189 done

  1190

  1191 lemma drop_eq_Nil[simp]: "!!n. (drop n xs = []) = (length xs <= n)"

  1192 apply(induct xs)

  1193 apply simp

  1194 apply(simp add:drop_Cons split:nat.split)

  1195 done

  1196

  1197 lemma take_map: "!!xs. take n (map f xs) = map f (take n xs)"

  1198 apply (induct n, auto)

  1199 apply (case_tac xs, auto)

  1200 done

  1201

  1202 lemma drop_map: "!!xs. drop n (map f xs) = map f (drop n xs)"

  1203 apply (induct n, auto)

  1204 apply (case_tac xs, auto)

  1205 done

  1206

  1207 lemma rev_take: "!!i. rev (take i xs) = drop (length xs - i) (rev xs)"

  1208 apply (induct xs, auto)

  1209 apply (case_tac i, auto)

  1210 done

  1211

  1212 lemma rev_drop: "!!i. rev (drop i xs) = take (length xs - i) (rev xs)"

  1213 apply (induct xs, auto)

  1214 apply (case_tac i, auto)

  1215 done

  1216

  1217 lemma nth_take [simp]: "!!n i. i < n ==> (take n xs)!i = xs!i"

  1218 apply (induct xs, auto)

  1219 apply (case_tac n, blast)

  1220 apply (case_tac i, auto)

  1221 done

  1222

  1223 lemma nth_drop [simp]:

  1224 "!!xs i. n + i <= length xs ==> (drop n xs)!i = xs!(n + i)"

  1225 apply (induct n, auto)

  1226 apply (case_tac xs, auto)

  1227 done

  1228

  1229 lemma hd_drop_conv_nth: "\<lbrakk> xs \<noteq> []; n < length xs \<rbrakk> \<Longrightarrow> hd(drop n xs) = xs!n"

  1230 by(simp add: hd_conv_nth)

  1231

  1232 lemma set_take_subset: "\<And>n. set(take n xs) \<subseteq> set xs"

  1233 by(induct xs)(auto simp:take_Cons split:nat.split)

  1234

  1235 lemma set_drop_subset: "\<And>n. set(drop n xs) \<subseteq> set xs"

  1236 by(induct xs)(auto simp:drop_Cons split:nat.split)

  1237

  1238 lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs"

  1239 using set_take_subset by fast

  1240

  1241 lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs"

  1242 using set_drop_subset by fast

  1243

  1244 lemma append_eq_conv_conj:

  1245 "!!zs. (xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)"

  1246 apply (induct xs, simp, clarsimp)

  1247 apply (case_tac zs, auto)

  1248 done

  1249

  1250 lemma take_add [rule_format]:

  1251     "\<forall>i. i+j \<le> length(xs) --> take (i+j) xs = take i xs @ take j (drop i xs)"

  1252 apply (induct xs, auto)

  1253 apply (case_tac i, simp_all)

  1254 done

  1255

  1256 lemma append_eq_append_conv_if:

  1257  "!! ys\<^isub>1. (xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>1 @ ys\<^isub>2) =

  1258   (if size xs\<^isub>1 \<le> size ys\<^isub>1

  1259    then xs\<^isub>1 = take (size xs\<^isub>1) ys\<^isub>1 \<and> xs\<^isub>2 = drop (size xs\<^isub>1) ys\<^isub>1 @ ys\<^isub>2

  1260    else take (size ys\<^isub>1) xs\<^isub>1 = ys\<^isub>1 \<and> drop (size ys\<^isub>1) xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>2)"

  1261 apply(induct xs\<^isub>1)

  1262  apply simp

  1263 apply(case_tac ys\<^isub>1)

  1264 apply simp_all

  1265 done

  1266

  1267 lemma take_hd_drop:

  1268   "!!n. n < length xs \<Longrightarrow> take n xs @ [hd (drop n xs)] = take (n+1) xs"

  1269 apply(induct xs)

  1270 apply simp

  1271 apply(simp add:drop_Cons split:nat.split)

  1272 done

  1273

  1274 lemma id_take_nth_drop:

  1275  "i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs"

  1276 proof -

  1277   assume si: "i < length xs"

  1278   hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto

  1279   moreover

  1280   from si have "take (Suc i) xs = take i xs @ [xs!i]"

  1281     apply (rule_tac take_Suc_conv_app_nth) by arith

  1282   ultimately show ?thesis by auto

  1283 qed

  1284

  1285 lemma upd_conv_take_nth_drop:

  1286  "i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs"

  1287 proof -

  1288   assume i: "i < length xs"

  1289   have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]"

  1290     by(rule arg_cong[OF id_take_nth_drop[OF i]])

  1291   also have "\<dots> = take i xs @ a # drop (Suc i) xs"

  1292     using i by (simp add: list_update_append)

  1293   finally show ?thesis .

  1294 qed

  1295

  1296

  1297 subsubsection {* @{text takeWhile} and @{text dropWhile} *}

  1298

  1299 lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs"

  1300 by (induct xs) auto

  1301

  1302 lemma takeWhile_append1 [simp]:

  1303 "[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs"

  1304 by (induct xs) auto

  1305

  1306 lemma takeWhile_append2 [simp]:

  1307 "(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys"

  1308 by (induct xs) auto

  1309

  1310 lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"

  1311 by (induct xs) auto

  1312

  1313 lemma dropWhile_append1 [simp]:

  1314 "[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"

  1315 by (induct xs) auto

  1316

  1317 lemma dropWhile_append2 [simp]:

  1318 "(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"

  1319 by (induct xs) auto

  1320

  1321 lemma set_take_whileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x"

  1322 by (induct xs) (auto split: split_if_asm)

  1323

  1324 lemma takeWhile_eq_all_conv[simp]:

  1325  "(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)"

  1326 by(induct xs, auto)

  1327

  1328 lemma dropWhile_eq_Nil_conv[simp]:

  1329  "(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)"

  1330 by(induct xs, auto)

  1331

  1332 lemma dropWhile_eq_Cons_conv:

  1333  "(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)"

  1334 by(induct xs, auto)

  1335

  1336 text{* The following two lemmmas could be generalized to an arbitrary

  1337 property. *}

  1338

  1339 lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>

  1340  takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))"

  1341 by(induct xs) (auto simp: takeWhile_tail[where l="[]"])

  1342

  1343 lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>

  1344   dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)"

  1345 apply(induct xs)

  1346  apply simp

  1347 apply auto

  1348 apply(subst dropWhile_append2)

  1349 apply auto

  1350 done

  1351

  1352 lemma takeWhile_not_last:

  1353  "\<lbrakk> xs \<noteq> []; distinct xs\<rbrakk> \<Longrightarrow> takeWhile (\<lambda>y. y \<noteq> last xs) xs = butlast xs"

  1354 apply(induct xs)

  1355  apply simp

  1356 apply(case_tac xs)

  1357 apply(auto)

  1358 done

  1359

  1360 lemma takeWhile_cong [fundef_cong, recdef_cong]:

  1361   "[| l = k; !!x. x : set l ==> P x = Q x |]

  1362   ==> takeWhile P l = takeWhile Q k"

  1363   by (induct k arbitrary: l) (simp_all)

  1364

  1365 lemma dropWhile_cong [fundef_cong, recdef_cong]:

  1366   "[| l = k; !!x. x : set l ==> P x = Q x |]

  1367   ==> dropWhile P l = dropWhile Q k"

  1368   by (induct k arbitrary: l, simp_all)

  1369

  1370

  1371 subsubsection {* @{text zip} *}

  1372

  1373 lemma zip_Nil [simp]: "zip [] ys = []"

  1374 by (induct ys) auto

  1375

  1376 lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys"

  1377 by simp

  1378

  1379 declare zip_Cons [simp del]

  1380

  1381 lemma zip_Cons1:

  1382  "zip (x#xs) ys = (case ys of [] \<Rightarrow> [] | y#ys \<Rightarrow> (x,y)#zip xs ys)"

  1383 by(auto split:list.split)

  1384

  1385 lemma length_zip [simp]:

  1386 "length (zip xs ys) = min (length xs) (length ys)"

  1387 by (induct xs ys rule:list_induct2') auto

  1388

  1389 lemma zip_append1:

  1390 "zip (xs @ ys) zs =

  1391 zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"

  1392 by (induct xs zs rule:list_induct2') auto

  1393

  1394 lemma zip_append2:

  1395 "zip xs (ys @ zs) =

  1396 zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"

  1397 by (induct xs ys rule:list_induct2') auto

  1398

  1399 lemma zip_append [simp]:

  1400  "[| length xs = length us; length ys = length vs |] ==>

  1401 zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"

  1402 by (simp add: zip_append1)

  1403

  1404 lemma zip_rev:

  1405 "length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)"

  1406 by (induct rule:list_induct2, simp_all)

  1407

  1408 lemma nth_zip [simp]:

  1409 "!!i xs. [| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)"

  1410 apply (induct ys, simp)

  1411 apply (case_tac xs)

  1412  apply (simp_all add: nth.simps split: nat.split)

  1413 done

  1414

  1415 lemma set_zip:

  1416 "set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"

  1417 by (simp add: set_conv_nth cong: rev_conj_cong)

  1418

  1419 lemma zip_update:

  1420 "length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"

  1421 by (rule sym, simp add: update_zip)

  1422

  1423 lemma zip_replicate [simp]:

  1424 "!!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"

  1425 apply (induct i, auto)

  1426 apply (case_tac j, auto)

  1427 done

  1428

  1429 lemma take_zip:

  1430  "!!xs ys. take n (zip xs ys) = zip (take n xs) (take n ys)"

  1431 apply (induct n)

  1432  apply simp

  1433 apply (case_tac xs, simp)

  1434 apply (case_tac ys, simp_all)

  1435 done

  1436

  1437 lemma drop_zip:

  1438  "!!xs ys. drop n (zip xs ys) = zip (drop n xs) (drop n ys)"

  1439 apply (induct n)

  1440  apply simp

  1441 apply (case_tac xs, simp)

  1442 apply (case_tac ys, simp_all)

  1443 done

  1444

  1445 lemma set_zip_leftD:

  1446   "(x,y)\<in> set (zip xs ys) \<Longrightarrow> x \<in> set xs"

  1447 by (induct xs ys rule:list_induct2') auto

  1448

  1449 lemma set_zip_rightD:

  1450   "(x,y)\<in> set (zip xs ys) \<Longrightarrow> y \<in> set ys"

  1451 by (induct xs ys rule:list_induct2') auto

  1452

  1453 subsubsection {* @{text list_all2} *}

  1454

  1455 lemma list_all2_lengthD [intro?]:

  1456   "list_all2 P xs ys ==> length xs = length ys"

  1457   by (simp add: list_all2_def)

  1458

  1459 lemma list_all2_Nil [iff, code]: "list_all2 P [] ys = (ys = [])"

  1460   by (simp add: list_all2_def)

  1461

  1462 lemma list_all2_Nil2 [iff, code]: "list_all2 P xs [] = (xs = [])"

  1463   by (simp add: list_all2_def)

  1464

  1465 lemma list_all2_Cons [iff, code]:

  1466   "list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)"

  1467   by (auto simp add: list_all2_def)

  1468

  1469 lemma list_all2_Cons1:

  1470 "list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)"

  1471 by (cases ys) auto

  1472

  1473 lemma list_all2_Cons2:

  1474 "list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)"

  1475 by (cases xs) auto

  1476

  1477 lemma list_all2_rev [iff]:

  1478 "list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"

  1479 by (simp add: list_all2_def zip_rev cong: conj_cong)

  1480

  1481 lemma list_all2_rev1:

  1482 "list_all2 P (rev xs) ys = list_all2 P xs (rev ys)"

  1483 by (subst list_all2_rev [symmetric]) simp

  1484

  1485 lemma list_all2_append1:

  1486 "list_all2 P (xs @ ys) zs =

  1487 (EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and>

  1488 list_all2 P xs us \<and> list_all2 P ys vs)"

  1489 apply (simp add: list_all2_def zip_append1)

  1490 apply (rule iffI)

  1491  apply (rule_tac x = "take (length xs) zs" in exI)

  1492  apply (rule_tac x = "drop (length xs) zs" in exI)

  1493  apply (force split: nat_diff_split simp add: min_def, clarify)

  1494 apply (simp add: ball_Un)

  1495 done

  1496

  1497 lemma list_all2_append2:

  1498 "list_all2 P xs (ys @ zs) =

  1499 (EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and>

  1500 list_all2 P us ys \<and> list_all2 P vs zs)"

  1501 apply (simp add: list_all2_def zip_append2)

  1502 apply (rule iffI)

  1503  apply (rule_tac x = "take (length ys) xs" in exI)

  1504  apply (rule_tac x = "drop (length ys) xs" in exI)

  1505  apply (force split: nat_diff_split simp add: min_def, clarify)

  1506 apply (simp add: ball_Un)

  1507 done

  1508

  1509 lemma list_all2_append:

  1510   "length xs = length ys \<Longrightarrow>

  1511   list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)"

  1512 by (induct rule:list_induct2, simp_all)

  1513

  1514 lemma list_all2_appendI [intro?, trans]:

  1515   "\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)"

  1516   by (simp add: list_all2_append list_all2_lengthD)

  1517

  1518 lemma list_all2_conv_all_nth:

  1519 "list_all2 P xs ys =

  1520 (length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))"

  1521 by (force simp add: list_all2_def set_zip)

  1522

  1523 lemma list_all2_trans:

  1524   assumes tr: "!!a b c. P1 a b ==> P2 b c ==> P3 a c"

  1525   shows "!!bs cs. list_all2 P1 as bs ==> list_all2 P2 bs cs ==> list_all2 P3 as cs"

  1526         (is "!!bs cs. PROP ?Q as bs cs")

  1527 proof (induct as)

  1528   fix x xs bs assume I1: "!!bs cs. PROP ?Q xs bs cs"

  1529   show "!!cs. PROP ?Q (x # xs) bs cs"

  1530   proof (induct bs)

  1531     fix y ys cs assume I2: "!!cs. PROP ?Q (x # xs) ys cs"

  1532     show "PROP ?Q (x # xs) (y # ys) cs"

  1533       by (induct cs) (auto intro: tr I1 I2)

  1534   qed simp

  1535 qed simp

  1536

  1537 lemma list_all2_all_nthI [intro?]:

  1538   "length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b"

  1539   by (simp add: list_all2_conv_all_nth)

  1540

  1541 lemma list_all2I:

  1542   "\<forall>x \<in> set (zip a b). split P x \<Longrightarrow> length a = length b \<Longrightarrow> list_all2 P a b"

  1543   by (simp add: list_all2_def)

  1544

  1545 lemma list_all2_nthD:

  1546   "\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"

  1547   by (simp add: list_all2_conv_all_nth)

  1548

  1549 lemma list_all2_nthD2:

  1550   "\<lbrakk>list_all2 P xs ys; p < size ys\<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"

  1551   by (frule list_all2_lengthD) (auto intro: list_all2_nthD)

  1552

  1553 lemma list_all2_map1:

  1554   "list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs"

  1555   by (simp add: list_all2_conv_all_nth)

  1556

  1557 lemma list_all2_map2:

  1558   "list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs"

  1559   by (auto simp add: list_all2_conv_all_nth)

  1560

  1561 lemma list_all2_refl [intro?]:

  1562   "(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs"

  1563   by (simp add: list_all2_conv_all_nth)

  1564

  1565 lemma list_all2_update_cong:

  1566   "\<lbrakk> i<size xs; list_all2 P xs ys; P x y \<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])"

  1567   by (simp add: list_all2_conv_all_nth nth_list_update)

  1568

  1569 lemma list_all2_update_cong2:

  1570   "\<lbrakk>list_all2 P xs ys; P x y; i < length ys\<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])"

  1571   by (simp add: list_all2_lengthD list_all2_update_cong)

  1572

  1573 lemma list_all2_takeI [simp,intro?]:

  1574   "\<And>n ys. list_all2 P xs ys \<Longrightarrow> list_all2 P (take n xs) (take n ys)"

  1575   apply (induct xs)

  1576    apply simp

  1577   apply (clarsimp simp add: list_all2_Cons1)

  1578   apply (case_tac n)

  1579   apply auto

  1580   done

  1581

  1582 lemma list_all2_dropI [simp,intro?]:

  1583   "\<And>n bs. list_all2 P as bs \<Longrightarrow> list_all2 P (drop n as) (drop n bs)"

  1584   apply (induct as, simp)

  1585   apply (clarsimp simp add: list_all2_Cons1)

  1586   apply (case_tac n, simp, simp)

  1587   done

  1588

  1589 lemma list_all2_mono [intro?]:

  1590   "\<And>y. list_all2 P x y \<Longrightarrow> (\<And>x y. P x y \<Longrightarrow> Q x y) \<Longrightarrow> list_all2 Q x y"

  1591   apply (induct x, simp)

  1592   apply (case_tac y, auto)

  1593   done

  1594

  1595 lemma list_all2_eq:

  1596   "xs = ys \<longleftrightarrow> list_all2 (op =) xs ys"

  1597   by (induct xs ys rule: list_induct2') auto

  1598

  1599

  1600 subsubsection {* @{text foldl} and @{text foldr} *}

  1601

  1602 lemma foldl_append [simp]:

  1603 "!!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"

  1604 by (induct xs) auto

  1605

  1606 lemma foldr_append[simp]: "foldr f (xs @ ys) a = foldr f xs (foldr f ys a)"

  1607 by (induct xs) auto

  1608

  1609 lemma foldl_cong [fundef_cong, recdef_cong]:

  1610   "[| a = b; l = k; !!a x. x : set l ==> f a x = g a x |]

  1611   ==> foldl f a l = foldl g b k"

  1612   by (induct k arbitrary: a b l) simp_all

  1613

  1614 lemma foldr_cong [fundef_cong, recdef_cong]:

  1615   "[| a = b; l = k; !!a x. x : set l ==> f x a = g x a |]

  1616   ==> foldr f l a = foldr g k b"

  1617   by (induct k arbitrary: a b l) simp_all

  1618

  1619 lemma foldr_foldl: "foldr f xs a = foldl (%x y. f y x) a (rev xs)"

  1620 by (induct xs) auto

  1621

  1622 lemma foldl_foldr: "foldl f a xs = foldr (%x y. f y x) (rev xs) a"

  1623 by (simp add: foldr_foldl [of "%x y. f y x" "rev xs"])

  1624

  1625 text {*

  1626 Note: @{text "n \<le> foldl (op +) n ns"} looks simpler, but is more

  1627 difficult to use because it requires an additional transitivity step.

  1628 *}

  1629

  1630 lemma start_le_sum: "!!n::nat. m <= n ==> m <= foldl (op +) n ns"

  1631 by (induct ns) auto

  1632

  1633 lemma elem_le_sum: "!!n::nat. n : set ns ==> n <= foldl (op +) 0 ns"

  1634 by (force intro: start_le_sum simp add: in_set_conv_decomp)

  1635

  1636 lemma sum_eq_0_conv [iff]:

  1637 "!!m::nat. (foldl (op +) m ns = 0) = (m = 0 \<and> (\<forall>n \<in> set ns. n = 0))"

  1638 by (induct ns) auto

  1639

  1640

  1641 subsubsection {* @{text upto} *}

  1642

  1643 lemma upt_rec[code]: "[i..<j] = (if i<j then i#[Suc i..<j] else [])"

  1644 -- {* simp does not terminate! *}

  1645 by (induct j) auto

  1646

  1647 lemma upt_conv_Nil [simp]: "j <= i ==> [i..<j] = []"

  1648 by (subst upt_rec) simp

  1649

  1650 lemma upt_eq_Nil_conv[simp]: "([i..<j] = []) = (j = 0 \<or> j <= i)"

  1651 by(induct j)simp_all

  1652

  1653 lemma upt_eq_Cons_conv:

  1654  "!!x xs. ([i..<j] = x#xs) = (i < j & i = x & [i+1..<j] = xs)"

  1655 apply(induct j)

  1656  apply simp

  1657 apply(clarsimp simp add: append_eq_Cons_conv)

  1658 apply arith

  1659 done

  1660

  1661 lemma upt_Suc_append: "i <= j ==> [i..<(Suc j)] = [i..<j]@[j]"

  1662 -- {* Only needed if @{text upt_Suc} is deleted from the simpset. *}

  1663 by simp

  1664

  1665 lemma upt_conv_Cons: "i < j ==> [i..<j] = i # [Suc i..<j]"

  1666 apply(rule trans)

  1667 apply(subst upt_rec)

  1668  prefer 2 apply (rule refl, simp)

  1669 done

  1670

  1671 lemma upt_add_eq_append: "i<=j ==> [i..<j+k] = [i..<j]@[j..<j+k]"

  1672 -- {* LOOPS as a simprule, since @{text "j <= j"}. *}

  1673 by (induct k) auto

  1674

  1675 lemma length_upt [simp]: "length [i..<j] = j - i"

  1676 by (induct j) (auto simp add: Suc_diff_le)

  1677

  1678 lemma nth_upt [simp]: "i + k < j ==> [i..<j] ! k = i + k"

  1679 apply (induct j)

  1680 apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split)

  1681 done

  1682

  1683

  1684 lemma hd_upt[simp]: "i < j \<Longrightarrow> hd[i..<j] = i"

  1685 by(simp add:upt_conv_Cons)

  1686

  1687 lemma last_upt[simp]: "i < j \<Longrightarrow> last[i..<j] = j - 1"

  1688 apply(cases j)

  1689  apply simp

  1690 by(simp add:upt_Suc_append)

  1691

  1692 lemma take_upt [simp]: "!!i. i+m <= n ==> take m [i..<n] = [i..<i+m]"

  1693 apply (induct m, simp)

  1694 apply (subst upt_rec)

  1695 apply (rule sym)

  1696 apply (subst upt_rec)

  1697 apply (simp del: upt.simps)

  1698 done

  1699

  1700 lemma drop_upt[simp]: "drop m [i..<j] = [i+m..<j]"

  1701 apply(induct j)

  1702 apply auto

  1703 done

  1704

  1705 lemma map_Suc_upt: "map Suc [m..<n] = [Suc m..n]"

  1706 by (induct n) auto

  1707

  1708 lemma nth_map_upt: "!!i. i < n-m ==> (map f [m..<n]) ! i = f(m+i)"

  1709 apply (induct n m rule: diff_induct)

  1710 prefer 3 apply (subst map_Suc_upt[symmetric])

  1711 apply (auto simp add: less_diff_conv nth_upt)

  1712 done

  1713

  1714 lemma nth_take_lemma:

  1715   "!!xs ys. k <= length xs ==> k <= length ys ==>

  1716      (!!i. i < k --> xs!i = ys!i) ==> take k xs = take k ys"

  1717 apply (atomize, induct k)

  1718 apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib, clarify)

  1719 txt {* Both lists must be non-empty *}

  1720 apply (case_tac xs, simp)

  1721 apply (case_tac ys, clarify)

  1722  apply (simp (no_asm_use))

  1723 apply clarify

  1724 txt {* prenexing's needed, not miniscoping *}

  1725 apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps)

  1726 apply blast

  1727 done

  1728

  1729 lemma nth_equalityI:

  1730  "[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys"

  1731 apply (frule nth_take_lemma [OF le_refl eq_imp_le])

  1732 apply (simp_all add: take_all)

  1733 done

  1734

  1735 (* needs nth_equalityI *)

  1736 lemma list_all2_antisym:

  1737   "\<lbrakk> (\<And>x y. \<lbrakk>P x y; Q y x\<rbrakk> \<Longrightarrow> x = y); list_all2 P xs ys; list_all2 Q ys xs \<rbrakk>

  1738   \<Longrightarrow> xs = ys"

  1739   apply (simp add: list_all2_conv_all_nth)

  1740   apply (rule nth_equalityI, blast, simp)

  1741   done

  1742

  1743 lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys"

  1744 -- {* The famous take-lemma. *}

  1745 apply (drule_tac x = "max (length xs) (length ys)" in spec)

  1746 apply (simp add: le_max_iff_disj take_all)

  1747 done

  1748

  1749

  1750 lemma take_Cons':

  1751      "take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)"

  1752 by (cases n) simp_all

  1753

  1754 lemma drop_Cons':

  1755      "drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)"

  1756 by (cases n) simp_all

  1757

  1758 lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))"

  1759 by (cases n) simp_all

  1760

  1761 lemmas take_Cons_number_of = take_Cons'[of "number_of v",standard]

  1762 lemmas drop_Cons_number_of = drop_Cons'[of "number_of v",standard]

  1763 lemmas nth_Cons_number_of = nth_Cons'[of _ _ "number_of v",standard]

  1764

  1765 declare take_Cons_number_of [simp]

  1766         drop_Cons_number_of [simp]

  1767         nth_Cons_number_of [simp]

  1768

  1769

  1770 subsubsection {* @{text "distinct"} and @{text remdups} *}

  1771

  1772 lemma distinct_append [simp]:

  1773 "distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})"

  1774 by (induct xs) auto

  1775

  1776 lemma distinct_rev[simp]: "distinct(rev xs) = distinct xs"

  1777 by(induct xs) auto

  1778

  1779 lemma set_remdups [simp]: "set (remdups xs) = set xs"

  1780 by (induct xs) (auto simp add: insert_absorb)

  1781

  1782 lemma distinct_remdups [iff]: "distinct (remdups xs)"

  1783 by (induct xs) auto

  1784

  1785 lemma remdups_eq_nil_iff [simp]: "(remdups x = []) = (x = [])"

  1786   by (induct x, auto)

  1787

  1788 lemma remdups_eq_nil_right_iff [simp]: "([] = remdups x) = (x = [])"

  1789   by (induct x, auto)

  1790

  1791 lemma length_remdups_leq[iff]: "length(remdups xs) <= length xs"

  1792 by (induct xs) auto

  1793

  1794 lemma length_remdups_eq[iff]:

  1795   "(length (remdups xs) = length xs) = (remdups xs = xs)"

  1796 apply(induct xs)

  1797  apply auto

  1798 apply(subgoal_tac "length (remdups xs) <= length xs")

  1799  apply arith

  1800 apply(rule length_remdups_leq)

  1801 done

  1802

  1803

  1804 lemma distinct_map:

  1805   "distinct(map f xs) = (distinct xs & inj_on f (set xs))"

  1806 by (induct xs) auto

  1807

  1808

  1809 lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)"

  1810 by (induct xs) auto

  1811

  1812 lemma distinct_upt[simp]: "distinct[i..<j]"

  1813 by (induct j) auto

  1814

  1815 lemma distinct_take[simp]: "\<And>i. distinct xs \<Longrightarrow> distinct (take i xs)"

  1816 apply(induct xs)

  1817  apply simp

  1818 apply (case_tac i)

  1819  apply simp_all

  1820 apply(blast dest:in_set_takeD)

  1821 done

  1822

  1823 lemma distinct_drop[simp]: "\<And>i. distinct xs \<Longrightarrow> distinct (drop i xs)"

  1824 apply(induct xs)

  1825  apply simp

  1826 apply (case_tac i)

  1827  apply simp_all

  1828 done

  1829

  1830 lemma distinct_list_update:

  1831 assumes d: "distinct xs" and a: "a \<notin> set xs - {xs!i}"

  1832 shows "distinct (xs[i:=a])"

  1833 proof (cases "i < length xs")

  1834   case True

  1835   with a have "a \<notin> set (take i xs @ xs ! i # drop (Suc i) xs) - {xs!i}"

  1836     apply (drule_tac id_take_nth_drop) by simp

  1837   with d True show ?thesis

  1838     apply (simp add: upd_conv_take_nth_drop)

  1839     apply (drule subst [OF id_take_nth_drop]) apply assumption

  1840     apply simp apply (cases "a = xs!i") apply simp by blast

  1841 next

  1842   case False with d show ?thesis by auto

  1843 qed

  1844

  1845

  1846 text {* It is best to avoid this indexed version of distinct, but

  1847 sometimes it is useful. *}

  1848

  1849 lemma distinct_conv_nth:

  1850 "distinct xs = (\<forall>i < size xs. \<forall>j < size xs. i \<noteq> j --> xs!i \<noteq> xs!j)"

  1851 apply (induct xs, simp, simp)

  1852 apply (rule iffI, clarsimp)

  1853  apply (case_tac i)

  1854 apply (case_tac j, simp)

  1855 apply (simp add: set_conv_nth)

  1856  apply (case_tac j)

  1857 apply (clarsimp simp add: set_conv_nth, simp)

  1858 apply (rule conjI)

  1859  apply (clarsimp simp add: set_conv_nth)

  1860  apply (erule_tac x = 0 in allE, simp)

  1861  apply (erule_tac x = "Suc i" in allE, simp, clarsimp)

  1862 apply (erule_tac x = "Suc i" in allE, simp)

  1863 apply (erule_tac x = "Suc j" in allE, simp)

  1864 done

  1865

  1866 lemma nth_eq_iff_index_eq:

  1867  "\<lbrakk> distinct xs; i < length xs; j < length xs \<rbrakk> \<Longrightarrow> (xs!i = xs!j) = (i = j)"

  1868 by(auto simp: distinct_conv_nth)

  1869

  1870 lemma distinct_card: "distinct xs ==> card (set xs) = size xs"

  1871   by (induct xs) auto

  1872

  1873 lemma card_distinct: "card (set xs) = size xs ==> distinct xs"

  1874 proof (induct xs)

  1875   case Nil thus ?case by simp

  1876 next

  1877   case (Cons x xs)

  1878   show ?case

  1879   proof (cases "x \<in> set xs")

  1880     case False with Cons show ?thesis by simp

  1881   next

  1882     case True with Cons.prems

  1883     have "card (set xs) = Suc (length xs)"

  1884       by (simp add: card_insert_if split: split_if_asm)

  1885     moreover have "card (set xs) \<le> length xs" by (rule card_length)

  1886     ultimately have False by simp

  1887     thus ?thesis ..

  1888   qed

  1889 qed

  1890

  1891

  1892 lemma length_remdups_concat:

  1893  "length(remdups(concat xss)) = card(\<Union>xs \<in> set xss. set xs)"

  1894 by(simp add: distinct_card[symmetric])

  1895

  1896

  1897 subsubsection {* @{text remove1} *}

  1898

  1899 lemma remove1_append:

  1900   "remove1 x (xs @ ys) =

  1901   (if x \<in> set xs then remove1 x xs @ ys else xs @ remove1 x ys)"

  1902 by (induct xs) auto

  1903

  1904 lemma set_remove1_subset: "set(remove1 x xs) <= set xs"

  1905 apply(induct xs)

  1906  apply simp

  1907 apply simp

  1908 apply blast

  1909 done

  1910

  1911 lemma set_remove1_eq [simp]: "distinct xs ==> set(remove1 x xs) = set xs - {x}"

  1912 apply(induct xs)

  1913  apply simp

  1914 apply simp

  1915 apply blast

  1916 done

  1917

  1918 lemma remove1_filter_not[simp]:

  1919   "\<not> P x \<Longrightarrow> remove1 x (filter P xs) = filter P xs"

  1920 by(induct xs) auto

  1921

  1922 lemma notin_set_remove1[simp]: "x ~: set xs ==> x ~: set(remove1 y xs)"

  1923 apply(insert set_remove1_subset)

  1924 apply fast

  1925 done

  1926

  1927 lemma distinct_remove1[simp]: "distinct xs ==> distinct(remove1 x xs)"

  1928 by (induct xs) simp_all

  1929

  1930

  1931 subsubsection {* @{text replicate} *}

  1932

  1933 lemma length_replicate [simp]: "length (replicate n x) = n"

  1934 by (induct n) auto

  1935

  1936 lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)"

  1937 by (induct n) auto

  1938

  1939 lemma replicate_app_Cons_same:

  1940 "(replicate n x) @ (x # xs) = x # replicate n x @ xs"

  1941 by (induct n) auto

  1942

  1943 lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x"

  1944 apply (induct n, simp)

  1945 apply (simp add: replicate_app_Cons_same)

  1946 done

  1947

  1948 lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x"

  1949 by (induct n) auto

  1950

  1951 text{* Courtesy of Matthias Daum: *}

  1952 lemma append_replicate_commute:

  1953   "replicate n x @ replicate k x = replicate k x @ replicate n x"

  1954 apply (simp add: replicate_add [THEN sym])

  1955 apply (simp add: add_commute)

  1956 done

  1957

  1958 lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x"

  1959 by (induct n) auto

  1960

  1961 lemma tl_replicate [simp]: "n \<noteq> 0 ==> tl (replicate n x) = replicate (n - 1) x"

  1962 by (induct n) auto

  1963

  1964 lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x"

  1965 by (atomize (full), induct n) auto

  1966

  1967 lemma nth_replicate[simp]: "!!i. i < n ==> (replicate n x)!i = x"

  1968 apply (induct n, simp)

  1969 apply (simp add: nth_Cons split: nat.split)

  1970 done

  1971

  1972 text{* Courtesy of Matthias Daum (2 lemmas): *}

  1973 lemma take_replicate[simp]: "take i (replicate k x) = replicate (min i k) x"

  1974 apply (case_tac "k \<le> i")

  1975  apply  (simp add: min_def)

  1976 apply (drule not_leE)

  1977 apply (simp add: min_def)

  1978 apply (subgoal_tac "replicate k x = replicate i x @ replicate (k - i) x")

  1979  apply  simp

  1980 apply (simp add: replicate_add [symmetric])

  1981 done

  1982

  1983 lemma drop_replicate[simp]: "!!i. drop i (replicate k x) = replicate (k-i) x"

  1984 apply (induct k)

  1985  apply simp

  1986 apply clarsimp

  1987 apply (case_tac i)

  1988  apply simp

  1989 apply clarsimp

  1990 done

  1991

  1992

  1993 lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}"

  1994 by (induct n) auto

  1995

  1996 lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}"

  1997 by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc)

  1998

  1999 lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})"

  2000 by auto

  2001

  2002 lemma in_set_replicateD: "x : set (replicate n y) ==> x = y"

  2003 by (simp add: set_replicate_conv_if split: split_if_asm)

  2004

  2005

  2006 subsubsection{*@{text rotate1} and @{text rotate}*}

  2007

  2008 lemma rotate_simps[simp]: "rotate1 [] = [] \<and> rotate1 (x#xs) = xs @ [x]"

  2009 by(simp add:rotate1_def)

  2010

  2011 lemma rotate0[simp]: "rotate 0 = id"

  2012 by(simp add:rotate_def)

  2013

  2014 lemma rotate_Suc[simp]: "rotate (Suc n) xs = rotate1(rotate n xs)"

  2015 by(simp add:rotate_def)

  2016

  2017 lemma rotate_add:

  2018   "rotate (m+n) = rotate m o rotate n"

  2019 by(simp add:rotate_def funpow_add)

  2020

  2021 lemma rotate_rotate: "rotate m (rotate n xs) = rotate (m+n) xs"

  2022 by(simp add:rotate_add)

  2023

  2024 lemma rotate1_rotate_swap: "rotate1 (rotate n xs) = rotate n (rotate1 xs)"

  2025 by(simp add:rotate_def funpow_swap1)

  2026

  2027 lemma rotate1_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate1 xs = xs"

  2028 by(cases xs) simp_all

  2029

  2030 lemma rotate_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate n xs = xs"

  2031 apply(induct n)

  2032  apply simp

  2033 apply (simp add:rotate_def)

  2034 done

  2035

  2036 lemma rotate1_hd_tl: "xs \<noteq> [] \<Longrightarrow> rotate1 xs = tl xs @ [hd xs]"

  2037 by(simp add:rotate1_def split:list.split)

  2038

  2039 lemma rotate_drop_take:

  2040   "rotate n xs = drop (n mod length xs) xs @ take (n mod length xs) xs"

  2041 apply(induct n)

  2042  apply simp

  2043 apply(simp add:rotate_def)

  2044 apply(cases "xs = []")

  2045  apply (simp)

  2046 apply(case_tac "n mod length xs = 0")

  2047  apply(simp add:mod_Suc)

  2048  apply(simp add: rotate1_hd_tl drop_Suc take_Suc)

  2049 apply(simp add:mod_Suc rotate1_hd_tl drop_Suc[symmetric] drop_tl[symmetric]

  2050                 take_hd_drop linorder_not_le)

  2051 done

  2052

  2053 lemma rotate_conv_mod: "rotate n xs = rotate (n mod length xs) xs"

  2054 by(simp add:rotate_drop_take)

  2055

  2056 lemma rotate_id[simp]: "n mod length xs = 0 \<Longrightarrow> rotate n xs = xs"

  2057 by(simp add:rotate_drop_take)

  2058

  2059 lemma length_rotate1[simp]: "length(rotate1 xs) = length xs"

  2060 by(simp add:rotate1_def split:list.split)

  2061

  2062 lemma length_rotate[simp]: "!!xs. length(rotate n xs) = length xs"

  2063 by (induct n) (simp_all add:rotate_def)

  2064

  2065 lemma distinct1_rotate[simp]: "distinct(rotate1 xs) = distinct xs"

  2066 by(simp add:rotate1_def split:list.split) blast

  2067

  2068 lemma distinct_rotate[simp]: "distinct(rotate n xs) = distinct xs"

  2069 by (induct n) (simp_all add:rotate_def)

  2070

  2071 lemma rotate_map: "rotate n (map f xs) = map f (rotate n xs)"

  2072 by(simp add:rotate_drop_take take_map drop_map)

  2073

  2074 lemma set_rotate1[simp]: "set(rotate1 xs) = set xs"

  2075 by(simp add:rotate1_def split:list.split)

  2076

  2077 lemma set_rotate[simp]: "set(rotate n xs) = set xs"

  2078 by (induct n) (simp_all add:rotate_def)

  2079

  2080 lemma rotate1_is_Nil_conv[simp]: "(rotate1 xs = []) = (xs = [])"

  2081 by(simp add:rotate1_def split:list.split)

  2082

  2083 lemma rotate_is_Nil_conv[simp]: "(rotate n xs = []) = (xs = [])"

  2084 by (induct n) (simp_all add:rotate_def)

  2085

  2086 lemma rotate_rev:

  2087   "rotate n (rev xs) = rev(rotate (length xs - (n mod length xs)) xs)"

  2088 apply(simp add:rotate_drop_take rev_drop rev_take)

  2089 apply(cases "length xs = 0")

  2090  apply simp

  2091 apply(cases "n mod length xs = 0")

  2092  apply simp

  2093 apply(simp add:rotate_drop_take rev_drop rev_take)

  2094 done

  2095

  2096 lemma hd_rotate_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd(rotate n xs) = xs!(n mod length xs)"

  2097 apply(simp add:rotate_drop_take hd_append hd_drop_conv_nth hd_conv_nth)

  2098 apply(subgoal_tac "length xs \<noteq> 0")

  2099  prefer 2 apply simp

  2100 using mod_less_divisor[of "length xs" n] by arith

  2101

  2102

  2103 subsubsection {* @{text sublist} --- a generalization of @{text nth} to sets *}

  2104

  2105 lemma sublist_empty [simp]: "sublist xs {} = []"

  2106 by (auto simp add: sublist_def)

  2107

  2108 lemma sublist_nil [simp]: "sublist [] A = []"

  2109 by (auto simp add: sublist_def)

  2110

  2111 lemma length_sublist:

  2112   "length(sublist xs I) = card{i. i < length xs \<and> i : I}"

  2113 by(simp add: sublist_def length_filter_conv_card cong:conj_cong)

  2114

  2115 lemma sublist_shift_lemma_Suc:

  2116   "!!is. map fst (filter (%p. P(Suc(snd p))) (zip xs is)) =

  2117          map fst (filter (%p. P(snd p)) (zip xs (map Suc is)))"

  2118 apply(induct xs)

  2119  apply simp

  2120 apply (case_tac "is")

  2121  apply simp

  2122 apply simp

  2123 done

  2124

  2125 lemma sublist_shift_lemma:

  2126      "map fst [p:zip xs [i..<i + length xs] . snd p : A] =

  2127       map fst [p:zip xs [0..<length xs] . snd p + i : A]"

  2128 by (induct xs rule: rev_induct) (simp_all add: add_commute)

  2129

  2130 lemma sublist_append:

  2131      "sublist (l @ l') A = sublist l A @ sublist l' {j. j + length l : A}"

  2132 apply (unfold sublist_def)

  2133 apply (induct l' rule: rev_induct, simp)

  2134 apply (simp add: upt_add_eq_append[of 0] zip_append sublist_shift_lemma)

  2135 apply (simp add: add_commute)

  2136 done

  2137

  2138 lemma sublist_Cons:

  2139 "sublist (x # l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"

  2140 apply (induct l rule: rev_induct)

  2141  apply (simp add: sublist_def)

  2142 apply (simp del: append_Cons add: append_Cons[symmetric] sublist_append)

  2143 done

  2144

  2145 lemma set_sublist: "!!I. set(sublist xs I) = {xs!i|i. i<size xs \<and> i \<in> I}"

  2146 apply(induct xs)

  2147  apply simp

  2148 apply(auto simp add:sublist_Cons nth_Cons split:nat.split elim: lessE)

  2149  apply(erule lessE)

  2150   apply auto

  2151 apply(erule lessE)

  2152 apply auto

  2153 done

  2154

  2155 lemma set_sublist_subset: "set(sublist xs I) \<subseteq> set xs"

  2156 by(auto simp add:set_sublist)

  2157

  2158 lemma notin_set_sublistI[simp]: "x \<notin> set xs \<Longrightarrow> x \<notin> set(sublist xs I)"

  2159 by(auto simp add:set_sublist)

  2160

  2161 lemma in_set_sublistD: "x \<in> set(sublist xs I) \<Longrightarrow> x \<in> set xs"

  2162 by(auto simp add:set_sublist)

  2163

  2164 lemma sublist_singleton [simp]: "sublist [x] A = (if 0 : A then [x] else [])"

  2165 by (simp add: sublist_Cons)

  2166

  2167

  2168 lemma distinct_sublistI[simp]: "!!I. distinct xs \<Longrightarrow> distinct(sublist xs I)"

  2169 apply(induct xs)

  2170  apply simp

  2171 apply(auto simp add:sublist_Cons)

  2172 done

  2173

  2174

  2175 lemma sublist_upt_eq_take [simp]: "sublist l {..<n} = take n l"

  2176 apply (induct l rule: rev_induct, simp)

  2177 apply (simp split: nat_diff_split add: sublist_append)

  2178 done

  2179

  2180 lemma filter_in_sublist: "\<And>s. distinct xs \<Longrightarrow>

  2181   filter (%x. x \<in> set(sublist xs s)) xs = sublist xs s"

  2182 proof (induct xs)

  2183   case Nil thus ?case by simp

  2184 next

  2185   case (Cons a xs)

  2186   moreover hence "!x. x: set xs \<longrightarrow> x \<noteq> a" by auto

  2187   ultimately show ?case by(simp add: sublist_Cons cong:filter_cong)

  2188 qed

  2189

  2190

  2191 subsubsection {* @{const splice} *}

  2192

  2193 lemma splice_Nil2 [simp, code]:

  2194  "splice xs [] = xs"

  2195 by (cases xs) simp_all

  2196

  2197 lemma splice_Cons_Cons [simp, code]:

  2198  "splice (x#xs) (y#ys) = x # y # splice xs ys"

  2199 by simp

  2200

  2201 declare splice.simps(2) [simp del, code del]

  2202

  2203 subsubsection{*Sets of Lists*}

  2204

  2205 subsubsection {* @{text lists}: the list-forming operator over sets *}

  2206

  2207 inductive2

  2208   listsp :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool"

  2209   for A :: "'a \<Rightarrow> bool"

  2210 where

  2211     Nil [intro!]: "listsp A []"

  2212   | Cons [intro!]: "[| A a; listsp A l |] ==> listsp A (a # l)"

  2213

  2214 constdefs

  2215   lists :: "'a set => 'a list set"

  2216   "lists A == Collect (listsp (member A))"

  2217

  2218 lemma listsp_lists_eq [pred_set_conv]: "listsp (member A) = member (lists A)"

  2219   by (simp add: lists_def)

  2220

  2221 lemmas lists_intros [intro!] = listsp.intros [to_set]

  2222

  2223 lemmas lists_induct [consumes 1, case_names Nil Cons, induct set: lists] =

  2224   listsp.induct [to_set]

  2225

  2226 inductive_cases2 listspE [elim!]: "listsp A (x # l)"

  2227

  2228 lemmas listsE [elim!] = listspE [to_set]

  2229

  2230 lemma listsp_mono [mono2]: "A \<le> B ==> listsp A \<le> listsp B"

  2231   by (clarify, erule listsp.induct, blast+)

  2232

  2233 lemmas lists_mono [mono] = listsp_mono [to_set]

  2234

  2235 lemma listsp_infI:

  2236   assumes l: "listsp A l" shows "listsp B l ==> listsp (inf A B) l" using l

  2237   by induct blast+

  2238

  2239 lemmas lists_IntI = listsp_infI [to_set]

  2240

  2241 lemma listsp_inf_eq [simp]: "listsp (inf A B) = inf (listsp A) (listsp B)"

  2242 proof (rule mono_inf [where f=listsp, THEN order_antisym])

  2243   show "mono listsp" by (simp add: mono_def listsp_mono)

  2244   show "inf (listsp A) (listsp B) \<le> listsp (inf A B)" by (blast intro: listsp_infI)

  2245 qed

  2246

  2247 lemmas listsp_conj_eq [simp] = listsp_inf_eq [simplified inf_fun_eq inf_bool_eq]

  2248

  2249 lemmas lists_Int_eq [simp] = listsp_inf_eq [to_set]

  2250

  2251 lemma append_in_listsp_conv [iff]:

  2252      "(listsp A (xs @ ys)) = (listsp A xs \<and> listsp A ys)"

  2253 by (induct xs) auto

  2254

  2255 lemmas append_in_lists_conv [iff] = append_in_listsp_conv [to_set]

  2256

  2257 lemma in_listsp_conv_set: "(listsp A xs) = (\<forall>x \<in> set xs. A x)"

  2258 -- {* eliminate @{text listsp} in favour of @{text set} *}

  2259 by (induct xs) auto

  2260

  2261 lemmas in_lists_conv_set = in_listsp_conv_set [to_set]

  2262

  2263 lemma in_listspD [dest!]: "listsp A xs ==> \<forall>x\<in>set xs. A x"

  2264 by (rule in_listsp_conv_set [THEN iffD1])

  2265

  2266 lemmas in_listsD [dest!] = in_listspD [to_set]

  2267

  2268 lemma in_listspI [intro!]: "\<forall>x\<in>set xs. A x ==> listsp A xs"

  2269 by (rule in_listsp_conv_set [THEN iffD2])

  2270

  2271 lemmas in_listsI [intro!] = in_listspI [to_set]

  2272

  2273 lemma lists_UNIV [simp]: "lists UNIV = UNIV"

  2274 by auto

  2275

  2276

  2277

  2278 subsubsection{* Inductive definition for membership *}

  2279

  2280 inductive2 ListMem :: "'a \<Rightarrow> 'a list \<Rightarrow> bool"

  2281 where

  2282     elem:  "ListMem x (x # xs)"

  2283   | insert:  "ListMem x xs \<Longrightarrow> ListMem x (y # xs)"

  2284

  2285 lemma ListMem_iff: "(ListMem x xs) = (x \<in> set xs)"

  2286 apply (rule iffI)

  2287  apply (induct set: ListMem)

  2288   apply auto

  2289 apply (induct xs)

  2290  apply (auto intro: ListMem.intros)

  2291 done

  2292

  2293

  2294

  2295 subsubsection{*Lists as Cartesian products*}

  2296

  2297 text{*@{text"set_Cons A Xs"}: the set of lists with head drawn from

  2298 @{term A} and tail drawn from @{term Xs}.*}

  2299

  2300 constdefs

  2301   set_Cons :: "'a set \<Rightarrow> 'a list set \<Rightarrow> 'a list set"

  2302   "set_Cons A XS == {z. \<exists>x xs. z = x#xs & x \<in> A & xs \<in> XS}"

  2303

  2304 lemma set_Cons_sing_Nil [simp]: "set_Cons A {[]} = (%x. [x])A"

  2305 by (auto simp add: set_Cons_def)

  2306

  2307 text{*Yields the set of lists, all of the same length as the argument and

  2308 with elements drawn from the corresponding element of the argument.*}

  2309

  2310 consts  listset :: "'a set list \<Rightarrow> 'a list set"

  2311 primrec

  2312    "listset []    = {[]}"

  2313    "listset(A#As) = set_Cons A (listset As)"

  2314

  2315

  2316 subsection{*Relations on Lists*}

  2317

  2318 subsubsection {* Length Lexicographic Ordering *}

  2319

  2320 text{*These orderings preserve well-foundedness: shorter lists

  2321   precede longer lists. These ordering are not used in dictionaries.*}

  2322

  2323 consts lexn :: "('a * 'a)set => nat => ('a list * 'a list)set"

  2324         --{*The lexicographic ordering for lists of the specified length*}

  2325 primrec

  2326   "lexn r 0 = {}"

  2327   "lexn r (Suc n) =

  2328     (prod_fun (%(x,xs). x#xs) (%(x,xs). x#xs)  (r <*lex*> lexn r n)) Int

  2329     {(xs,ys). length xs = Suc n \<and> length ys = Suc n}"

  2330

  2331 constdefs

  2332   lex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set"

  2333     "lex r == \<Union>n. lexn r n"

  2334         --{*Holds only between lists of the same length*}

  2335

  2336   lenlex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set"

  2337     "lenlex r == inv_image (less_than <*lex*> lex r) (%xs. (length xs, xs))"

  2338         --{*Compares lists by their length and then lexicographically*}

  2339

  2340

  2341 lemma wf_lexn: "wf r ==> wf (lexn r n)"

  2342 apply (induct n, simp, simp)

  2343 apply(rule wf_subset)

  2344  prefer 2 apply (rule Int_lower1)

  2345 apply(rule wf_prod_fun_image)

  2346  prefer 2 apply (rule inj_onI, auto)

  2347 done

  2348

  2349 lemma lexn_length:

  2350      "!!xs ys. (xs, ys) : lexn r n ==> length xs = n \<and> length ys = n"

  2351 by (induct n) auto

  2352

  2353 lemma wf_lex [intro!]: "wf r ==> wf (lex r)"

  2354 apply (unfold lex_def)

  2355 apply (rule wf_UN)

  2356 apply (blast intro: wf_lexn, clarify)

  2357 apply (rename_tac m n)

  2358 apply (subgoal_tac "m \<noteq> n")

  2359  prefer 2 apply blast

  2360 apply (blast dest: lexn_length not_sym)

  2361 done

  2362

  2363 lemma lexn_conv:

  2364   "lexn r n =

  2365     {(xs,ys). length xs = n \<and> length ys = n \<and>

  2366     (\<exists>xys x y xs' ys'. xs= xys @ x#xs' \<and> ys= xys @ y # ys' \<and> (x, y):r)}"

  2367 apply (induct n, simp)

  2368 apply (simp add: image_Collect lex_prod_def, safe, blast)

  2369  apply (rule_tac x = "ab # xys" in exI, simp)

  2370 apply (case_tac xys, simp_all, blast)

  2371 done

  2372

  2373 lemma lex_conv:

  2374   "lex r =

  2375     {(xs,ys). length xs = length ys \<and>

  2376     (\<exists>xys x y xs' ys'. xs = xys @ x # xs' \<and> ys = xys @ y # ys' \<and> (x, y):r)}"

  2377 by (force simp add: lex_def lexn_conv)

  2378

  2379 lemma wf_lenlex [intro!]: "wf r ==> wf (lenlex r)"

  2380 by (unfold lenlex_def) blast

  2381

  2382 lemma lenlex_conv:

  2383     "lenlex r = {(xs,ys). length xs < length ys |

  2384                  length xs = length ys \<and> (xs, ys) : lex r}"

  2385 by (simp add: lenlex_def diag_def lex_prod_def inv_image_def)

  2386

  2387 lemma Nil_notin_lex [iff]: "([], ys) \<notin> lex r"

  2388 by (simp add: lex_conv)

  2389

  2390 lemma Nil2_notin_lex [iff]: "(xs, []) \<notin> lex r"

  2391 by (simp add:lex_conv)

  2392

  2393 lemma Cons_in_lex [simp]:

  2394     "((x # xs, y # ys) : lex r) =

  2395       ((x, y) : r \<and> length xs = length ys | x = y \<and> (xs, ys) : lex r)"

  2396 apply (simp add: lex_conv)

  2397 apply (rule iffI)

  2398  prefer 2 apply (blast intro: Cons_eq_appendI, clarify)

  2399 apply (case_tac xys, simp, simp)

  2400 apply blast

  2401 done

  2402

  2403

  2404 subsubsection {* Lexicographic Ordering *}

  2405

  2406 text {* Classical lexicographic ordering on lists, ie. "a" < "ab" < "b".

  2407     This ordering does \emph{not} preserve well-foundedness.

  2408      Author: N. Voelker, March 2005. *}

  2409

  2410 constdefs

  2411   lexord :: "('a * 'a)set \<Rightarrow> ('a list * 'a list) set"

  2412   "lexord  r == {(x,y). \<exists> a v. y = x @ a # v \<or>

  2413             (\<exists> u a b v w. (a,b) \<in> r \<and> x = u @ (a # v) \<and> y = u @ (b # w))}"

  2414

  2415 lemma lexord_Nil_left[simp]:  "([],y) \<in> lexord r = (\<exists> a x. y = a # x)"

  2416   by (unfold lexord_def, induct_tac y, auto)

  2417

  2418 lemma lexord_Nil_right[simp]: "(x,[]) \<notin> lexord r"

  2419   by (unfold lexord_def, induct_tac x, auto)

  2420

  2421 lemma lexord_cons_cons[simp]:

  2422      "((a # x, b # y) \<in> lexord r) = ((a,b)\<in> r | (a = b & (x,y)\<in> lexord r))"

  2423   apply (unfold lexord_def, safe, simp_all)

  2424   apply (case_tac u, simp, simp)

  2425   apply (case_tac u, simp, clarsimp, blast, blast, clarsimp)

  2426   apply (erule_tac x="b # u" in allE)

  2427   by force

  2428

  2429 lemmas lexord_simps = lexord_Nil_left lexord_Nil_right lexord_cons_cons

  2430

  2431 lemma lexord_append_rightI: "\<exists> b z. y = b # z \<Longrightarrow> (x, x @ y) \<in> lexord r"

  2432   by (induct_tac x, auto)

  2433

  2434 lemma lexord_append_left_rightI:

  2435      "(a,b) \<in> r \<Longrightarrow> (u @ a # x, u @ b # y) \<in> lexord r"

  2436   by (induct_tac u, auto)

  2437

  2438 lemma lexord_append_leftI: " (u,v) \<in> lexord r \<Longrightarrow> (x @ u, x @ v) \<in> lexord r"

  2439   by (induct x, auto)

  2440

  2441 lemma lexord_append_leftD:

  2442      "\<lbrakk> (x @ u, x @ v) \<in> lexord r; (! a. (a,a) \<notin> r) \<rbrakk> \<Longrightarrow> (u,v) \<in> lexord r"

  2443   by (erule rev_mp, induct_tac x, auto)

  2444

  2445 lemma lexord_take_index_conv:

  2446    "((x,y) : lexord r) =

  2447     ((length x < length y \<and> take (length x) y = x) \<or>

  2448      (\<exists>i. i < min(length x)(length y) & take i x = take i y & (x!i,y!i) \<in> r))"

  2449   apply (unfold lexord_def Let_def, clarsimp)

  2450   apply (rule_tac f = "(% a b. a \<or> b)" in arg_cong2)

  2451   apply auto

  2452   apply (rule_tac x="hd (drop (length x) y)" in exI)

  2453   apply (rule_tac x="tl (drop (length x) y)" in exI)

  2454   apply (erule subst, simp add: min_def)

  2455   apply (rule_tac x ="length u" in exI, simp)

  2456   apply (rule_tac x ="take i x" in exI)

  2457   apply (rule_tac x ="x ! i" in exI)

  2458   apply (rule_tac x ="y ! i" in exI, safe)

  2459   apply (rule_tac x="drop (Suc i) x" in exI)

  2460   apply (drule sym, simp add: drop_Suc_conv_tl)

  2461   apply (rule_tac x="drop (Suc i) y" in exI)

  2462   by (simp add: drop_Suc_conv_tl)

  2463

  2464 -- {* lexord is extension of partial ordering List.lex *}

  2465 lemma lexord_lex: " (x,y) \<in> lex r = ((x,y) \<in> lexord r \<and> length x = length y)"

  2466   apply (rule_tac x = y in spec)

  2467   apply (induct_tac x, clarsimp)

  2468   by (clarify, case_tac x, simp, force)

  2469

  2470 lemma lexord_irreflexive: "(! x. (x,x) \<notin> r) \<Longrightarrow> (y,y) \<notin> lexord r"

  2471   by (induct y, auto)

  2472

  2473 lemma lexord_trans:

  2474     "\<lbrakk> (x, y) \<in> lexord r; (y, z) \<in> lexord r; trans r \<rbrakk> \<Longrightarrow> (x, z) \<in> lexord r"

  2475    apply (erule rev_mp)+

  2476    apply (rule_tac x = x in spec)

  2477   apply (rule_tac x = z in spec)

  2478   apply ( induct_tac y, simp, clarify)

  2479   apply (case_tac xa, erule ssubst)

  2480   apply (erule allE, erule allE) -- {* avoid simp recursion *}

  2481   apply (case_tac x, simp, simp)

  2482   apply (case_tac x, erule allE, erule allE, simp)

  2483   apply (erule_tac x = listb in allE)

  2484   apply (erule_tac x = lista in allE, simp)

  2485   apply (unfold trans_def)

  2486   by blast

  2487

  2488 lemma lexord_transI:  "trans r \<Longrightarrow> trans (lexord r)"

  2489   by (rule transI, drule lexord_trans, blast)

  2490

  2491 lemma lexord_linear: "(! a b. (a,b)\<in> r | a = b | (b,a) \<in> r) \<Longrightarrow> (x,y) : lexord r | x = y | (y,x) : lexord r"

  2492   apply (rule_tac x = y in spec)

  2493   apply (induct_tac x, rule allI)

  2494   apply (case_tac x, simp, simp)

  2495   apply (rule allI, case_tac x, simp, simp)

  2496   by blast

  2497

  2498

  2499 subsection {* Lexicographic combination of measure functions *}

  2500

  2501 text {* These are useful for termination proofs *}

  2502

  2503 definition

  2504   "measures fs = inv_image (lex less_than) (%a. map (%f. f a) fs)"

  2505

  2506 lemma wf_measures[recdef_wf, simp]: "wf (measures fs)"

  2507   unfolding measures_def

  2508   by blast

  2509

  2510 lemma in_measures[simp]:

  2511   "(x, y) \<in> measures [] = False"

  2512   "(x, y) \<in> measures (f # fs)

  2513          = (f x < f y \<or> (f x = f y \<and> (x, y) \<in> measures fs))"

  2514   unfolding measures_def

  2515   by auto

  2516

  2517 lemma measures_less: "f x < f y ==> (x, y) \<in> measures (f#fs)"

  2518   by simp

  2519

  2520 lemma measures_lesseq: "f x <= f y ==> (x, y) \<in> measures fs ==> (x, y) \<in> measures (f#fs)"

  2521   by auto

  2522

  2523 (* install the lexicographic_order method and the "fun" command *)

  2524 use "Tools/function_package/lexicographic_order.ML"

  2525 use "Tools/function_package/fundef_datatype.ML"

  2526 setup LexicographicOrder.setup

  2527 setup FundefDatatype.setup

  2528

  2529

  2530 subsubsection{*Lifting a Relation on List Elements to the Lists*}

  2531

  2532 inductive2

  2533   list_all2' :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> bool"

  2534   for r :: "'a \<Rightarrow> 'b \<Rightarrow> bool"

  2535 where

  2536     Nil:  "list_all2' r [] []"

  2537   | Cons: "[| r x y; list_all2' r xs ys |] ==> list_all2' r (x#xs) (y#ys)"

  2538

  2539 constdefs

  2540   listrel :: "('a * 'b) set => ('a list * 'b list) set"

  2541   "listrel r == Collect2 (list_all2' (member2 r))"

  2542

  2543 lemma list_all2_listrel_eq [pred_set_conv]:

  2544   "list_all2' (member2 r) = member2 (listrel r)"

  2545   by (simp add: listrel_def)

  2546

  2547 lemmas listrel_induct [consumes 1, case_names Nil Cons, induct set: listrel] =

  2548   list_all2'.induct [to_set]

  2549

  2550 lemmas listrel_intros = list_all2'.intros [to_set]

  2551

  2552 inductive_cases2 listrel_Nil1 [to_set, elim!]: "list_all2' r [] xs"

  2553 inductive_cases2 listrel_Nil2 [to_set, elim!]: "list_all2' r xs []"

  2554 inductive_cases2 listrel_Cons1 [to_set, elim!]: "list_all2' r  (y#ys) xs"

  2555 inductive_cases2 listrel_Cons2 [to_set, elim!]: "list_all2' r xs (y#ys)"

  2556

  2557

  2558 lemma listrel_mono: "r \<subseteq> s \<Longrightarrow> listrel r \<subseteq> listrel s"

  2559 apply clarify

  2560 apply (erule listrel_induct)

  2561 apply (blast intro: listrel_intros)+

  2562 done

  2563

  2564 lemma listrel_subset: "r \<subseteq> A \<times> A \<Longrightarrow> listrel r \<subseteq> lists A \<times> lists A"

  2565 apply clarify

  2566 apply (erule listrel_induct, auto)

  2567 done

  2568

  2569 lemma listrel_refl: "refl A r \<Longrightarrow> refl (lists A) (listrel r)"

  2570 apply (simp add: refl_def listrel_subset Ball_def)

  2571 apply (rule allI)

  2572 apply (induct_tac x)

  2573 apply (auto intro: listrel_intros)

  2574 done

  2575

  2576 lemma listrel_sym: "sym r \<Longrightarrow> sym (listrel r)"

  2577 apply (auto simp add: sym_def)

  2578 apply (erule listrel_induct)

  2579 apply (blast intro: listrel_intros)+

  2580 done

  2581

  2582 lemma listrel_trans: "trans r \<Longrightarrow> trans (listrel r)"

  2583 apply (simp add: trans_def)

  2584 apply (intro allI)

  2585 apply (rule impI)

  2586 apply (erule listrel_induct)

  2587 apply (blast intro: listrel_intros)+

  2588 done

  2589

  2590 theorem equiv_listrel: "equiv A r \<Longrightarrow> equiv (lists A) (listrel r)"

  2591 by (simp add: equiv_def listrel_refl listrel_sym listrel_trans)

  2592

  2593 lemma listrel_Nil [simp]: "listrel r  {[]} = {[]}"

  2594 by (blast intro: listrel_intros)

  2595

  2596 lemma listrel_Cons:

  2597      "listrel r  {x#xs} = set_Cons (r{x}) (listrel r  {xs})";

  2598 by (auto simp add: set_Cons_def intro: listrel_intros)

  2599

  2600

  2601 subsection{*Miscellany*}

  2602

  2603 subsubsection {* Characters and strings *}

  2604

  2605 datatype nibble =

  2606     Nibble0 | Nibble1 | Nibble2 | Nibble3 | Nibble4 | Nibble5 | Nibble6 | Nibble7

  2607   | Nibble8 | Nibble9 | NibbleA | NibbleB | NibbleC | NibbleD | NibbleE | NibbleF

  2608

  2609 datatype char = Char nibble nibble

  2610   -- "Note: canonical order of character encoding coincides with standard term ordering"

  2611

  2612 types string = "char list"

  2613

  2614 syntax

  2615   "_Char" :: "xstr => char"    ("CHR _")

  2616   "_String" :: "xstr => string"    ("_")

  2617

  2618 setup StringSyntax.setup

  2619

  2620

  2621 subsection {* Code generator *}

  2622

  2623 subsubsection {* Setup *}

  2624

  2625 types_code

  2626   "list" ("_ list")

  2627 attach (term_of) {*

  2628 fun term_of_list f T = HOLogic.mk_list T o map f;

  2629 *}

  2630 attach (test) {*

  2631 fun gen_list' aG i j = frequency

  2632   [(i, fn () => aG j :: gen_list' aG (i-1) j), (1, fn () => [])] ()

  2633 and gen_list aG i = gen_list' aG i i;

  2634 *}

  2635   "char" ("string")

  2636 attach (term_of) {*

  2637 val term_of_char = HOLogic.mk_char;

  2638 *}

  2639 attach (test) {*

  2640 fun gen_char i = chr (random_range (ord "a") (Int.min (ord "a" + i, ord "z")));

  2641 *}

  2642

  2643 consts_code "Cons" ("(_ ::/ _)")

  2644

  2645 code_type list

  2646   (SML "_ list")

  2647   (OCaml "_ list")

  2648   (Haskell "![_]")

  2649

  2650 code_const Nil

  2651   (SML "[]")

  2652   (OCaml "[]")

  2653   (Haskell "[]")

  2654

  2655 code_type char

  2656   (SML "char")

  2657   (OCaml "char")

  2658   (Haskell "Char")

  2659

  2660 code_const Char and char_rec

  2661     and char_case and "size \<Colon> char \<Rightarrow> nat"

  2662   (Haskell "error/ \"Char\""

  2663     and "error/ \"char_rec\"" and "error/ \"char_case\"" and "error/ \"size_char\"")

  2664

  2665 setup {*

  2666   fold (uncurry (CodegenSerializer.add_undefined "SML")) [

  2667       ("List.char.Char", "(raise Fail \"Char\")"),

  2668       ("List.char.char_rec", "(raise Fail \"char_rec\")"),

  2669       ("List.char.char_case", "(raise Fail \"char_case\")")

  2670     ]

  2671   #> fold (uncurry (CodegenSerializer.add_undefined "OCaml")) [

  2672       ("List.char.Char", "(failwith \"Char\")"),

  2673       ("List.char.char_rec", "(failwith \"char_rec\")"),

  2674       ("List.char.char_case", "(failwith \"char_case\")")

  2675     ]

  2676 *}

  2677

  2678 code_const "size \<Colon> char \<Rightarrow> nat"

  2679   (SML "!(_;/ raise Fail \"size'_char\")")

  2680   (OCaml "!(_;/ failwith \"size'_char\")")

  2681

  2682 code_instance list :: eq and char :: eq

  2683   (Haskell - and -)

  2684

  2685 code_const "op = \<Colon> 'a\<Colon>eq list \<Rightarrow> 'a list \<Rightarrow> bool"

  2686   (Haskell infixl 4 "==")

  2687

  2688 code_const "op = \<Colon> char \<Rightarrow> char \<Rightarrow> bool"

  2689   (SML "!((_ : char) = _)")

  2690   (OCaml "!((_ : char) = _)")

  2691   (Haskell infixl 4 "==")

  2692

  2693 code_reserved SML

  2694   list char nil

  2695

  2696 code_reserved OCaml

  2697   list char

  2698

  2699 setup {*

  2700 let

  2701

  2702 fun list_codegen thy defs gr dep thyname b t =

  2703   let val (gr', ps) = foldl_map (Codegen.invoke_codegen thy defs dep thyname false)

  2704     (gr, HOLogic.dest_list t)

  2705   in SOME (gr', Pretty.list "[" "]" ps) end handle TERM _ => NONE;

  2706

  2707 fun char_codegen thy defs gr dep thyname b t =

  2708   case Option.map chr (try HOLogic.dest_char t) of

  2709       SOME c => SOME (gr, Pretty.quote (Pretty.str (ML_Syntax.print_char c)))

  2710     | NONE => NONE;

  2711

  2712 in

  2713

  2714   Codegen.add_codegen "list_codegen" list_codegen

  2715   #> Codegen.add_codegen "char_codegen" char_codegen

  2716   #> CodegenSerializer.add_pretty_list "SML" "List.list.Nil" "List.list.Cons"

  2717        (Pretty.enum "," "[" "]") NONE (7, "::")

  2718   #> CodegenSerializer.add_pretty_list "OCaml" "List.list.Nil" "List.list.Cons"

  2719        (Pretty.enum ";" "[" "]") NONE (6, "::")

  2720   #> CodegenSerializer.add_pretty_list "Haskell" "List.list.Nil" "List.list.Cons"

  2721        (Pretty.enum "," "[" "]") (SOME (ML_Syntax.print_char, ML_Syntax.print_string)) (5, ":")

  2722   #> CodegenPackage.add_appconst

  2723        ("List.char.Char", CodegenPackage.appgen_char (try HOLogic.dest_char))

  2724

  2725 end;

  2726 *}

  2727

  2728

  2729 subsubsection {* Generation of efficient code *}

  2730

  2731 consts

  2732   memberl :: "'a \<Rightarrow> 'a list \<Rightarrow> bool" (infixl "mem" 55)

  2733   null:: "'a list \<Rightarrow> bool"

  2734   list_inter :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"

  2735   list_ex :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool"

  2736   list_all :: "('a \<Rightarrow> bool) \<Rightarrow> ('a list \<Rightarrow> bool)"

  2737   itrev :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"

  2738   filtermap :: "('a \<Rightarrow> 'b option) \<Rightarrow> 'a list \<Rightarrow> 'b list"

  2739   map_filter :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'b list"

  2740

  2741 primrec

  2742   "x mem [] = False"

  2743   "x mem (y#ys) = (x = y \<or> x mem ys)"

  2744

  2745 primrec

  2746   "null [] = True"

  2747   "null (x#xs) = False"

  2748

  2749 primrec

  2750   "list_inter [] bs = []"

  2751   "list_inter (a#as) bs =

  2752      (if a \<in> set bs then a # list_inter as bs else list_inter as bs)"

  2753

  2754 primrec

  2755   "list_all P [] = True"

  2756   "list_all P (x#xs) = (P x \<and> list_all P xs)"

  2757

  2758 primrec

  2759   "list_ex P [] = False"

  2760   "list_ex P (x#xs) = (P x \<or> list_ex P xs)"

  2761

  2762 primrec

  2763   "filtermap f [] = []"

  2764   "filtermap f (x#xs) =

  2765      (case f x of None \<Rightarrow> filtermap f xs

  2766       | Some y \<Rightarrow> y # filtermap f xs)"

  2767

  2768 primrec

  2769   "map_filter f P [] = []"

  2770   "map_filter f P (x#xs) =

  2771      (if P x then f x # map_filter f P xs else map_filter f P xs)"

  2772

  2773 primrec

  2774   "itrev [] ys = ys"

  2775   "itrev (x#xs) ys = itrev xs (x#ys)"

  2776

  2777 text {*

  2778   Only use @{text mem} for generating executable code.  Otherwise use

  2779   @{prop "x : set xs"} instead --- it is much easier to reason about.

  2780   The same is true for @{const list_all} and @{const list_ex}: write

  2781   @{text "\<forall>x\<in>set xs"} and @{text "\<exists>x\<in>set xs"} instead because the HOL

  2782   quantifiers are aleady known to the automatic provers. In fact, the

  2783   declarations in the code subsection make sure that @{text "\<in>"},

  2784   @{text "\<forall>x\<in>set xs"} and @{text "\<exists>x\<in>set xs"} are implemented

  2785   efficiently.

  2786

  2787   Efficient emptyness check is implemented by @{const null}.

  2788

  2789   The functions @{const itrev}, @{const filtermap} and @{const

  2790   map_filter} are just there to generate efficient code. Do not use

  2791   them for modelling and proving.

  2792 *}

  2793

  2794 lemma mem_iff [normal post]:

  2795   "x mem xs \<longleftrightarrow> x \<in> set xs"

  2796   by (induct xs) auto

  2797

  2798 lemmas in_set_code [code unfold] =

  2799   mem_iff [symmetric, THEN eq_reflection]

  2800

  2801 lemma empty_null [code inline]:

  2802   "xs = [] \<longleftrightarrow> null xs"

  2803   by (cases xs) simp_all

  2804

  2805 lemmas null_empty [normal post] =

  2806   empty_null [symmetric]

  2807

  2808 lemma list_inter_conv:

  2809   "set (list_inter xs ys) = set xs \<inter> set ys"

  2810   by (induct xs) auto

  2811

  2812 lemma list_all_iff [normal post]:

  2813   "list_all P xs \<longleftrightarrow> (\<forall>x \<in> set xs. P x)"

  2814   by (induct xs) auto

  2815

  2816 lemmas list_ball_code [code unfold] =

  2817   list_all_iff [symmetric, THEN eq_reflection]

  2818

  2819 lemma list_all_append [simp]:

  2820   "list_all P (xs @ ys) \<longleftrightarrow> (list_all P xs \<and> list_all P ys)"

  2821   by (induct xs) auto

  2822

  2823 lemma list_all_rev [simp]:

  2824   "list_all P (rev xs) \<longleftrightarrow> list_all P xs"

  2825   by (simp add: list_all_iff)

  2826

  2827 lemma list_all_length:

  2828   "list_all P xs \<longleftrightarrow> (\<forall>n < length xs. P (xs ! n))"

  2829   unfolding list_all_iff by (auto intro: all_nth_imp_all_set)

  2830

  2831 lemma list_ex_iff [normal post]:

  2832   "list_ex P xs \<longleftrightarrow> (\<exists>x \<in> set xs. P x)"

  2833   by (induct xs) simp_all

  2834

  2835 lemmas list_bex_code [code unfold] =

  2836   list_ex_iff [symmetric, THEN eq_reflection]

  2837

  2838 lemma list_ex_length:

  2839   "list_ex P xs \<longleftrightarrow> (\<exists>n < length xs. P (xs ! n))"

  2840   unfolding list_ex_iff set_conv_nth by auto

  2841

  2842 lemma itrev [simp]:

  2843   "itrev xs ys = rev xs @ ys"

  2844   by (induct xs arbitrary: ys) simp_all

  2845

  2846 lemma filtermap_conv:

  2847    "filtermap f xs = map (\<lambda>x. the (f x)) (filter (\<lambda>x. f x \<noteq> None) xs)"

  2848   by (induct xs) (simp_all split: option.split)

  2849

  2850 lemma map_filter_conv [simp]:

  2851   "map_filter f P xs = map f (filter P xs)"

  2852   by (induct xs) auto

  2853

  2854 lemma rev_code [code func, code unfold, code noinline]:

  2855   "rev xs == itrev xs []"

  2856   by simp

  2857

  2858 text {* code for bounded quantification over nats *}

  2859

  2860 lemma atMost_upto [code inline]:

  2861   "{..n} = set [0..n]"

  2862   by auto

  2863 lemmas atMost_upto' [code unfold] = atMost_upto [THEN eq_reflection]

  2864

  2865 lemma atLeast_upt [code inline]:

  2866   "{..<n} = set [0..<n]"

  2867   by auto

  2868 lemmas atLeast_upt' [code unfold] = atLeast_upt [THEN eq_reflection]

  2869

  2870 lemma greaterThanLessThan_upd [code inline]:

  2871   "{n<..<m} = set [Suc n..<m]"

  2872   by auto

  2873 lemmas greaterThanLessThan_upd' [code unfold] = greaterThanLessThan_upd [THEN eq_reflection]

  2874

  2875 lemma atLeastLessThan_upd [code inline]:

  2876   "{n..<m} = set [n..<m]"

  2877   by auto

  2878 lemmas atLeastLessThan_upd' [code unfold] = atLeastLessThan_upd [THEN eq_reflection]

  2879

  2880 lemma greaterThanAtMost_upto [code inline]:

  2881   "{n<..m} = set [Suc n..m]"

  2882   by auto

  2883 lemmas greaterThanAtMost_upto' [code unfold] = greaterThanAtMost_upto [THEN eq_reflection]

  2884

  2885 lemma atLeastAtMost_upto [code inline]:

  2886   "{n..m} = set [n..m]"

  2887   by auto

  2888 lemmas atLeastAtMost_upto' [code unfold] = atLeastAtMost_upto [THEN eq_reflection]

  2889

  2890 lemma all_nat_less_eq [code inline]:

  2891   "(\<forall>m<n\<Colon>nat. P m) \<longleftrightarrow> (\<forall>m \<in> {0..<n}. P m)"

  2892   by auto

  2893 lemmas all_nat_less_eq' [code unfold] = all_nat_less_eq [THEN eq_reflection]

  2894

  2895 lemma ex_nat_less_eq [code inline]:

  2896   "(\<exists>m<n\<Colon>nat. P m) \<longleftrightarrow> (\<exists>m \<in> {0..<n}. P m)"

  2897   by auto

  2898 lemmas ex_nat_less_eq' [code unfold] = ex_nat_less_eq [THEN eq_reflection]

  2899

  2900 lemma all_nat_less [code inline]:

  2901   "(\<forall>m\<le>n\<Colon>nat. P m) \<longleftrightarrow> (\<forall>m \<in> {0..n}. P m)"

  2902   by auto

  2903 lemmas all_nat_less' [code unfold] =  all_nat_less [THEN eq_reflection]

  2904

  2905 lemma ex_nat_less [code inline]:

  2906   "(\<exists>m\<le>n\<Colon>nat. P m) \<longleftrightarrow> (\<exists>m \<in> {0..n}. P m)"

  2907   by auto

  2908 lemmas ex_nat_less' [code unfold] = ex_nat_less [THEN eq_reflection]

  2909

  2910 end
`