src/HOL/Lazy_Sequence.thy
author blanchet
Sun Sep 14 22:59:30 2014 +0200 (2014-09-14)
changeset 58334 7553a1bcecb7
parent 58310 91ea607a34d8
child 58350 919149921e46
permissions -rw-r--r--
disable datatype 'plugins' for internal types
     1 (* Author: Lukas Bulwahn, TU Muenchen *)
     2 
     3 header {* Lazy sequences *}
     4 
     5 theory Lazy_Sequence
     6 imports Predicate
     7 begin
     8 
     9 subsection {* Type of lazy sequences *}
    10 
    11 datatype (plugins only: code) (dead 'a) lazy_sequence = lazy_sequence_of_list "'a list"
    12 
    13 primrec list_of_lazy_sequence :: "'a lazy_sequence \<Rightarrow> 'a list"
    14 where
    15   "list_of_lazy_sequence (lazy_sequence_of_list xs) = xs"
    16 
    17 lemma lazy_sequence_of_list_of_lazy_sequence [simp]:
    18   "lazy_sequence_of_list (list_of_lazy_sequence xq) = xq"
    19   by (cases xq) simp_all
    20 
    21 lemma lazy_sequence_eqI:
    22   "list_of_lazy_sequence xq = list_of_lazy_sequence yq \<Longrightarrow> xq = yq"
    23   by (cases xq, cases yq) simp
    24 
    25 lemma lazy_sequence_eq_iff:
    26   "xq = yq \<longleftrightarrow> list_of_lazy_sequence xq = list_of_lazy_sequence yq"
    27   by (auto intro: lazy_sequence_eqI)
    28 
    29 lemma case_lazy_sequence [simp]:
    30   "case_lazy_sequence f xq = f (list_of_lazy_sequence xq)"
    31   by (cases xq) auto
    32 
    33 lemma rec_lazy_sequence [simp]:
    34   "rec_lazy_sequence f xq = f (list_of_lazy_sequence xq)"
    35   by (cases xq) auto
    36 
    37 definition Lazy_Sequence :: "(unit \<Rightarrow> ('a \<times> 'a lazy_sequence) option) \<Rightarrow> 'a lazy_sequence"
    38 where
    39   "Lazy_Sequence f = lazy_sequence_of_list (case f () of
    40     None \<Rightarrow> []
    41   | Some (x, xq) \<Rightarrow> x # list_of_lazy_sequence xq)"
    42 
    43 code_datatype Lazy_Sequence
    44 
    45 declare list_of_lazy_sequence.simps [code del]
    46 declare lazy_sequence.case [code del]
    47 declare lazy_sequence.rec [code del]
    48 
    49 lemma list_of_Lazy_Sequence [simp]:
    50   "list_of_lazy_sequence (Lazy_Sequence f) = (case f () of
    51     None \<Rightarrow> []
    52   | Some (x, xq) \<Rightarrow> x # list_of_lazy_sequence xq)"
    53   by (simp add: Lazy_Sequence_def)
    54 
    55 definition yield :: "'a lazy_sequence \<Rightarrow> ('a \<times> 'a lazy_sequence) option"
    56 where
    57   "yield xq = (case list_of_lazy_sequence xq of
    58     [] \<Rightarrow> None
    59   | x # xs \<Rightarrow> Some (x, lazy_sequence_of_list xs))" 
    60 
    61 lemma yield_Seq [simp, code]:
    62   "yield (Lazy_Sequence f) = f ()"
    63   by (cases "f ()") (simp_all add: yield_def split_def)
    64 
    65 lemma case_yield_eq [simp]: "case_option g h (yield xq) =
    66   case_list g (\<lambda>x. curry h x \<circ> lazy_sequence_of_list) (list_of_lazy_sequence xq)"
    67   by (cases "list_of_lazy_sequence xq") (simp_all add: yield_def)
    68 
    69 lemma equal_lazy_sequence_code [code]:
    70   "HOL.equal xq yq = (case (yield xq, yield yq) of
    71     (None, None) \<Rightarrow> True
    72   | (Some (x, xq'), Some (y, yq')) \<Rightarrow> HOL.equal x y \<and> HOL.equal xq yq
    73   | _ \<Rightarrow> False)"
    74   by (simp_all add: lazy_sequence_eq_iff equal_eq split: list.splits)
    75 
    76 lemma [code nbe]:
    77   "HOL.equal (x :: 'a lazy_sequence) x \<longleftrightarrow> True"
    78   by (fact equal_refl)
    79 
    80 definition empty :: "'a lazy_sequence"
    81 where
    82   "empty = lazy_sequence_of_list []"
    83 
    84 lemma list_of_lazy_sequence_empty [simp]:
    85   "list_of_lazy_sequence empty = []"
    86   by (simp add: empty_def)
    87 
    88 lemma empty_code [code]:
    89   "empty = Lazy_Sequence (\<lambda>_. None)"
    90   by (simp add: lazy_sequence_eq_iff)
    91 
    92 definition single :: "'a \<Rightarrow> 'a lazy_sequence"
    93 where
    94   "single x = lazy_sequence_of_list [x]"
    95 
    96 lemma list_of_lazy_sequence_single [simp]:
    97   "list_of_lazy_sequence (single x) = [x]"
    98   by (simp add: single_def)
    99 
   100 lemma single_code [code]:
   101   "single x = Lazy_Sequence (\<lambda>_. Some (x, empty))"
   102   by (simp add: lazy_sequence_eq_iff)
   103 
   104 definition append :: "'a lazy_sequence \<Rightarrow> 'a lazy_sequence \<Rightarrow> 'a lazy_sequence"
   105 where
   106   "append xq yq = lazy_sequence_of_list (list_of_lazy_sequence xq @ list_of_lazy_sequence yq)"
   107 
   108 lemma list_of_lazy_sequence_append [simp]:
   109   "list_of_lazy_sequence (append xq yq) = list_of_lazy_sequence xq @ list_of_lazy_sequence yq"
   110   by (simp add: append_def)
   111 
   112 lemma append_code [code]:
   113   "append xq yq = Lazy_Sequence (\<lambda>_. case yield xq of
   114     None \<Rightarrow> yield yq
   115   | Some (x, xq') \<Rightarrow> Some (x, append xq' yq))"
   116   by (simp_all add: lazy_sequence_eq_iff split: list.splits)
   117 
   118 definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a lazy_sequence \<Rightarrow> 'b lazy_sequence"
   119 where
   120   "map f xq = lazy_sequence_of_list (List.map f (list_of_lazy_sequence xq))"
   121 
   122 lemma list_of_lazy_sequence_map [simp]:
   123   "list_of_lazy_sequence (map f xq) = List.map f (list_of_lazy_sequence xq)"
   124   by (simp add: map_def)
   125 
   126 lemma map_code [code]:
   127   "map f xq =
   128     Lazy_Sequence (\<lambda>_. map_option (\<lambda>(x, xq'). (f x, map f xq')) (yield xq))"
   129   by (simp_all add: lazy_sequence_eq_iff split: list.splits)
   130 
   131 definition flat :: "'a lazy_sequence lazy_sequence \<Rightarrow> 'a lazy_sequence"
   132 where
   133   "flat xqq = lazy_sequence_of_list (concat (List.map list_of_lazy_sequence (list_of_lazy_sequence xqq)))"
   134 
   135 lemma list_of_lazy_sequence_flat [simp]:
   136   "list_of_lazy_sequence (flat xqq) = concat (List.map list_of_lazy_sequence (list_of_lazy_sequence xqq))"
   137   by (simp add: flat_def)
   138 
   139 lemma flat_code [code]:
   140   "flat xqq = Lazy_Sequence (\<lambda>_. case yield xqq of
   141     None \<Rightarrow> None
   142   | Some (xq, xqq') \<Rightarrow> yield (append xq (flat xqq')))"
   143   by (simp add: lazy_sequence_eq_iff split: list.splits)
   144 
   145 definition bind :: "'a lazy_sequence \<Rightarrow> ('a \<Rightarrow> 'b lazy_sequence) \<Rightarrow> 'b lazy_sequence"
   146 where
   147   "bind xq f = flat (map f xq)"
   148 
   149 definition if_seq :: "bool \<Rightarrow> unit lazy_sequence"
   150 where
   151   "if_seq b = (if b then single () else empty)"
   152 
   153 definition those :: "'a option lazy_sequence \<Rightarrow> 'a lazy_sequence option"
   154 where
   155   "those xq = map_option lazy_sequence_of_list (List.those (list_of_lazy_sequence xq))"
   156   
   157 function iterate_upto :: "(natural \<Rightarrow> 'a) \<Rightarrow> natural \<Rightarrow> natural \<Rightarrow> 'a lazy_sequence"
   158 where
   159   "iterate_upto f n m =
   160     Lazy_Sequence (\<lambda>_. if n > m then None else Some (f n, iterate_upto f (n + 1) m))"
   161   by pat_completeness auto
   162 
   163 termination by (relation "measure (\<lambda>(f, n, m). nat_of_natural (m + 1 - n))")
   164   (auto simp add: less_natural_def)
   165 
   166 definition not_seq :: "unit lazy_sequence \<Rightarrow> unit lazy_sequence"
   167 where
   168   "not_seq xq = (case yield xq of
   169     None \<Rightarrow> single ()
   170   | Some ((), xq) \<Rightarrow> empty)"
   171 
   172   
   173 subsection {* Code setup *}
   174 
   175 code_reflect Lazy_Sequence
   176   datatypes lazy_sequence = Lazy_Sequence
   177 
   178 ML {*
   179 signature LAZY_SEQUENCE =
   180 sig
   181   datatype 'a lazy_sequence = Lazy_Sequence of (unit -> ('a * 'a Lazy_Sequence.lazy_sequence) option)
   182   val map: ('a -> 'b) -> 'a lazy_sequence -> 'b lazy_sequence
   183   val yield: 'a lazy_sequence -> ('a * 'a lazy_sequence) option
   184   val yieldn: int -> 'a lazy_sequence -> 'a list * 'a lazy_sequence
   185 end;
   186 
   187 structure Lazy_Sequence : LAZY_SEQUENCE =
   188 struct
   189 
   190 datatype lazy_sequence = datatype Lazy_Sequence.lazy_sequence;
   191 
   192 fun map f = @{code Lazy_Sequence.map} f;
   193 
   194 fun yield P = @{code Lazy_Sequence.yield} P;
   195 
   196 fun yieldn k = Predicate.anamorph yield k;
   197 
   198 end;
   199 *}
   200 
   201 
   202 subsection {* Generator Sequences *}
   203 
   204 subsubsection {* General lazy sequence operation *}
   205 
   206 definition product :: "'a lazy_sequence \<Rightarrow> 'b lazy_sequence \<Rightarrow> ('a \<times> 'b) lazy_sequence"
   207 where
   208   "product s1 s2 = bind s1 (\<lambda>a. bind s2 (\<lambda>b. single (a, b)))"
   209 
   210 
   211 subsubsection {* Small lazy typeclasses *}
   212 
   213 class small_lazy =
   214   fixes small_lazy :: "natural \<Rightarrow> 'a lazy_sequence"
   215 
   216 instantiation unit :: small_lazy
   217 begin
   218 
   219 definition "small_lazy d = single ()"
   220 
   221 instance ..
   222 
   223 end
   224 
   225 instantiation int :: small_lazy
   226 begin
   227 
   228 text {* maybe optimise this expression -> append (single x) xs == cons x xs 
   229 Performance difference? *}
   230 
   231 function small_lazy' :: "int \<Rightarrow> int \<Rightarrow> int lazy_sequence"
   232 where
   233   "small_lazy' d i = (if d < i then empty
   234     else append (single i) (small_lazy' d (i + 1)))"
   235     by pat_completeness auto
   236 
   237 termination 
   238   by (relation "measure (%(d, i). nat (d + 1 - i))") auto
   239 
   240 definition
   241   "small_lazy d = small_lazy' (int (nat_of_natural d)) (- (int (nat_of_natural d)))"
   242 
   243 instance ..
   244 
   245 end
   246 
   247 instantiation prod :: (small_lazy, small_lazy) small_lazy
   248 begin
   249 
   250 definition
   251   "small_lazy d = product (small_lazy d) (small_lazy d)"
   252 
   253 instance ..
   254 
   255 end
   256 
   257 instantiation list :: (small_lazy) small_lazy
   258 begin
   259 
   260 fun small_lazy_list :: "natural \<Rightarrow> 'a list lazy_sequence"
   261 where
   262   "small_lazy_list d = append (single [])
   263     (if d > 0 then bind (product (small_lazy (d - 1))
   264       (small_lazy (d - 1))) (\<lambda>(x, xs). single (x # xs)) else empty)"
   265 
   266 instance ..
   267 
   268 end
   269 
   270 subsection {* With Hit Bound Value *}
   271 text {* assuming in negative context *}
   272 
   273 type_synonym 'a hit_bound_lazy_sequence = "'a option lazy_sequence"
   274 
   275 definition hit_bound :: "'a hit_bound_lazy_sequence"
   276 where
   277   "hit_bound = Lazy_Sequence (\<lambda>_. Some (None, empty))"
   278 
   279 lemma list_of_lazy_sequence_hit_bound [simp]:
   280   "list_of_lazy_sequence hit_bound = [None]"
   281   by (simp add: hit_bound_def)
   282   
   283 definition hb_single :: "'a \<Rightarrow> 'a hit_bound_lazy_sequence"
   284 where
   285   "hb_single x = Lazy_Sequence (\<lambda>_. Some (Some x, empty))"
   286 
   287 definition hb_map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a hit_bound_lazy_sequence \<Rightarrow> 'b hit_bound_lazy_sequence"
   288 where
   289   "hb_map f xq = map (map_option f) xq"
   290 
   291 lemma hb_map_code [code]:
   292   "hb_map f xq =
   293     Lazy_Sequence (\<lambda>_. map_option (\<lambda>(x, xq'). (map_option f x, hb_map f xq')) (yield xq))"
   294   using map_code [of "map_option f" xq] by (simp add: hb_map_def)
   295 
   296 definition hb_flat :: "'a hit_bound_lazy_sequence hit_bound_lazy_sequence \<Rightarrow> 'a hit_bound_lazy_sequence"
   297 where
   298   "hb_flat xqq = lazy_sequence_of_list (concat
   299     (List.map ((\<lambda>x. case x of None \<Rightarrow> [None] | Some xs \<Rightarrow> xs) \<circ> map_option list_of_lazy_sequence) (list_of_lazy_sequence xqq)))"
   300 
   301 lemma list_of_lazy_sequence_hb_flat [simp]:
   302   "list_of_lazy_sequence (hb_flat xqq) =
   303     concat (List.map ((\<lambda>x. case x of None \<Rightarrow> [None] | Some xs \<Rightarrow> xs) \<circ> map_option list_of_lazy_sequence) (list_of_lazy_sequence xqq))"
   304   by (simp add: hb_flat_def)
   305 
   306 lemma hb_flat_code [code]:
   307   "hb_flat xqq = Lazy_Sequence (\<lambda>_. case yield xqq of
   308     None \<Rightarrow> None
   309   | Some (xq, xqq') \<Rightarrow> yield
   310      (append (case xq of None \<Rightarrow> hit_bound | Some xq \<Rightarrow> xq) (hb_flat xqq')))"
   311   by (simp add: lazy_sequence_eq_iff split: list.splits option.splits)
   312 
   313 definition hb_bind :: "'a hit_bound_lazy_sequence \<Rightarrow> ('a \<Rightarrow> 'b hit_bound_lazy_sequence) \<Rightarrow> 'b hit_bound_lazy_sequence"
   314 where
   315   "hb_bind xq f = hb_flat (hb_map f xq)"
   316 
   317 definition hb_if_seq :: "bool \<Rightarrow> unit hit_bound_lazy_sequence"
   318 where
   319   "hb_if_seq b = (if b then hb_single () else empty)"
   320 
   321 definition hb_not_seq :: "unit hit_bound_lazy_sequence \<Rightarrow> unit lazy_sequence"
   322 where
   323   "hb_not_seq xq = (case yield xq of
   324     None \<Rightarrow> single ()
   325   | Some (x, xq) \<Rightarrow> empty)"
   326 
   327 hide_const (open) yield empty single append flat map bind
   328   if_seq those iterate_upto not_seq product
   329 
   330 hide_fact (open) yield_def empty_def single_def append_def flat_def map_def bind_def
   331   if_seq_def those_def not_seq_def product_def 
   332 
   333 end