section \<open>Depth-Limited Sequences with failure element\<close>
theory Limited_Sequence imports Lazy_Sequence begin
subsection \<open>Depth-Limited Sequence\<close>
type_synonym'a dseq = "natural \ bool \ 'a lazy_sequence option"
definition empty :: "'a dseq" where "empty = (\_ _. Some Lazy_Sequence.empty)"
definition single :: "'a \ 'a dseq" where "single x = (\_ _. Some (Lazy_Sequence.single x))"
definition eval :: "'a dseq \ natural \ bool \ 'a lazy_sequence option" where
[simp]: "eval f i pol = f i pol"
definition yield :: "'a dseq \ natural \ bool \ ('a \ 'a dseq) option" where "yield f i pol = (case eval f i pol of
None \<Rightarrow> None
| Some s \<Rightarrow> (map_option \<circ> apsnd) (\<lambda>r _ _. Some r) (Lazy_Sequence.yield s))"
definition map_seq :: "('a \ 'b dseq) \ 'a lazy_sequence \ 'b dseq" where "map_seq f xq i pol = map_option Lazy_Sequence.flat
(Lazy_Sequence.those (Lazy_Sequence.map (\<lambda>x. f x i pol) xq))"
lemma map_seq_code [code]: "map_seq f xq i pol = (case Lazy_Sequence.yield xq of
None \<Rightarrow> Some Lazy_Sequence.empty
| Some (x, xq') \ (case eval (f x) i pol of
None \<Rightarrow> None
| Some yq \<Rightarrow> (case map_seq f xq' i pol of
None \<Rightarrow> None
| Some zq \<Rightarrow> Some (Lazy_Sequence.append yq zq))))" by (cases xq)
(auto simp add: map_seq_def Lazy_Sequence.those_def lazy_sequence_eq_iff split: list.splits option.splits)
definition bind :: "'a dseq \ ('a \ 'b dseq) \ 'b dseq" where "bind x f = (\i pol. if i = 0 then
(if pol then Some Lazy_Sequence.empty else None)
else
(case x (i - 1) pol of
None \<Rightarrow> None
| Some xq \<Rightarrow> map_seq f xq i pol))"
definition union :: "'a dseq \ 'a dseq \ 'a dseq" where "union x y = (\i pol. case (x i pol, y i pol) of
(Some xq, Some yq) \<Rightarrow> Some (Lazy_Sequence.append xq yq)
| _ \<Rightarrow> None)"
definition if_seq :: "bool \ unit dseq" where "if_seq b = (if b then single () else empty)"
definition not_seq :: "unit dseq \ unit dseq" where "not_seq x = (\i pol. case x i (\ pol) of
None \<Rightarrow> Some Lazy_Sequence.empty
| Some xq \<Rightarrow> (case Lazy_Sequence.yield xq of
None \<Rightarrow> Some (Lazy_Sequence.single ())
| Some _ \<Rightarrow> Some (Lazy_Sequence.empty)))"
definition map :: "('a \ 'b) \ 'a dseq \ 'b dseq" where "map f g = (\i pol. case g i pol of
None \<Rightarrow> None
| Some xq \<Rightarrow> Some (Lazy_Sequence.map f xq))"
type_synonym'a pos_dseq = "natural \ 'a Lazy_Sequence.lazy_sequence"
definition pos_empty :: "'a pos_dseq" where "pos_empty = (\i. Lazy_Sequence.empty)"
definition pos_single :: "'a \ 'a pos_dseq" where "pos_single x = (\i. Lazy_Sequence.single x)"
definition pos_bind :: "'a pos_dseq \ ('a \ 'b pos_dseq) \ 'b pos_dseq" where "pos_bind x f = (\i. Lazy_Sequence.bind (x i) (\a. f a i))"
definition pos_decr_bind :: "'a pos_dseq \ ('a \ 'b pos_dseq) \ 'b pos_dseq" where "pos_decr_bind x f = (\i. if i = 0 then
Lazy_Sequence.empty
else
Lazy_Sequence.bind (x (i - 1)) (\<lambda>a. f a i))"
definition pos_union :: "'a pos_dseq \ 'a pos_dseq \ 'a pos_dseq" where "pos_union xq yq = (\i. Lazy_Sequence.append (xq i) (yq i))"
definition pos_if_seq :: "bool \ unit pos_dseq" where "pos_if_seq b = (if b then pos_single () else pos_empty)"
definition pos_iterate_upto :: "(natural \ 'a) \ natural \ natural \ 'a pos_dseq" where "pos_iterate_upto f n m = (\i. Lazy_Sequence.iterate_upto f n m)"
definition pos_map :: "('a \ 'b) \ 'a pos_dseq \ 'b pos_dseq" where "pos_map f xq = (\i. Lazy_Sequence.map f (xq i))"
type_synonym'a neg_dseq = "natural \ 'a Lazy_Sequence.hit_bound_lazy_sequence"
definition neg_empty :: "'a neg_dseq" where "neg_empty = (\i. Lazy_Sequence.empty)"
definition neg_single :: "'a \ 'a neg_dseq" where "neg_single x = (\i. Lazy_Sequence.hb_single x)"
definition neg_bind :: "'a neg_dseq \ ('a \ 'b neg_dseq) \ 'b neg_dseq" where "neg_bind x f = (\i. hb_bind (x i) (\a. f a i))"
definition neg_decr_bind :: "'a neg_dseq \ ('a \ 'b neg_dseq) \ 'b neg_dseq" where "neg_decr_bind x f = (\i. if i = 0 then
Lazy_Sequence.hit_bound
else
hb_bind (x (i - 1)) (\<lambda>a. f a i))"
definition neg_union :: "'a neg_dseq \ 'a neg_dseq \ 'a neg_dseq" where "neg_union x y = (\i. Lazy_Sequence.append (x i) (y i))"
definition neg_if_seq :: "bool \ unit neg_dseq" where "neg_if_seq b = (if b then neg_single () else neg_empty)"
definition neg_iterate_upto where "neg_iterate_upto f n m = (\i. Lazy_Sequence.iterate_upto (\i. Some (f i)) n m)"
definition neg_map :: "('a \ 'b) \ 'a neg_dseq \ 'b neg_dseq" where "neg_map f xq = (\i. Lazy_Sequence.hb_map f (xq i))"
subsection \<open>Negation\<close>
definition pos_not_seq :: "unit neg_dseq \ unit pos_dseq" where "pos_not_seq xq = (\i. Lazy_Sequence.hb_not_seq (xq (3 * i)))"
definition neg_not_seq :: "unit pos_dseq \ unit neg_dseq" where "neg_not_seq x = (\i. case Lazy_Sequence.yield (x i) of
None \<Rightarrow> Lazy_Sequence.hb_single ()
| Some ((), xq) \<Rightarrow> Lazy_Sequence.empty)"
ML \<open> signature LIMITED_SEQUENCE =
sig
type 'a dseq = Code_Numeral.natural -> bool -> 'a Lazy_Sequence.lazy_sequence option
val map : ('a -> 'b) -> 'a dseq -> 'b dseq
val yield : 'a dseq -> Code_Numeral.natural -> bool -> ('a * 'a dseq) option
val yieldn : int -> 'a dseq -> Code_Numeral.natural -> bool -> 'a list * 'a dseq end;
type 'a dseq = Code_Numeral.natural -> bool -> 'a Lazy_Sequence.lazy_sequence option
fun map f = @{code Limited_Sequence.map} f;
fun yield f = @{code Limited_Sequence.yield} f;
fun yieldn n f i pol = (case f i pol of
NONE => ([], fn _ => fn _ => NONE)
| SOME s => let val (xs, s') = Lazy_Sequence.yieldn n s in (xs, fn _ => fn _ => SOME s') end);