Theory Limited_Sequence
section ‹Depth-Limited Sequences with failure element›
theory Limited_Sequence
imports Lazy_Sequence
begin
subsection ‹Depth-Limited Sequence›
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 ⇒ None
| Some s ⇒ (map_option ∘ apsnd) (λ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 (λx. f x i pol) xq))"
lemma map_seq_code [code]:
"map_seq f xq i pol = (case Lazy_Sequence.yield xq of
None ⇒ Some Lazy_Sequence.empty
| Some (x, xq') ⇒ (case eval (f x) i pol of
None ⇒ None
| Some yq ⇒ (case map_seq f xq' i pol of
None ⇒ None
| Some zq ⇒ 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 ⇒ None
| Some xq ⇒ 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) ⇒ Some (Lazy_Sequence.append xq yq)
| _ ⇒ 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 ⇒ Some Lazy_Sequence.empty
| Some xq ⇒ (case Lazy_Sequence.yield xq of
None ⇒ Some (Lazy_Sequence.single ())
| Some _ ⇒ Some (Lazy_Sequence.empty)))"
definition map :: "('a ⇒ 'b) ⇒ 'a dseq ⇒ 'b dseq"
where
"map f g = (λi pol. case g i pol of
None ⇒ None
| Some xq ⇒ Some (Lazy_Sequence.map f xq))"
subsection ‹Positive Depth-Limited Sequence›
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)) (λ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))"
subsection ‹Negative Depth-Limited Sequence›
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)) (λ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 ‹Negation›
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 ⇒ Lazy_Sequence.hb_single ()
| Some ((), xq) ⇒ Lazy_Sequence.empty)"
ML ‹
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;
structure Limited_Sequence : LIMITED_SEQUENCE =
struct
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);
end;
›
code_reserved Eval Limited_Sequence
hide_const (open) yield empty single eval map_seq bind union if_seq not_seq map
pos_empty pos_single pos_bind pos_decr_bind pos_union pos_if_seq pos_iterate_upto pos_not_seq pos_map
neg_empty neg_single neg_bind neg_decr_bind neg_union neg_if_seq neg_iterate_upto neg_not_seq neg_map
hide_fact (open) yield_def empty_def single_def eval_def map_seq_def bind_def union_def
if_seq_def not_seq_def map_def
pos_empty_def pos_single_def pos_bind_def pos_union_def pos_if_seq_def pos_iterate_upto_def pos_not_seq_def pos_map_def
neg_empty_def neg_single_def neg_bind_def neg_union_def neg_if_seq_def neg_iterate_upto_def neg_not_seq_def neg_map_def
end