src/HOL/DSequence.thy
author huffman
Fri Mar 30 12:32:35 2012 +0200 (2012-03-30)
changeset 47220 52426c62b5d0
parent 42163 392fd6c4669c
child 50055 94041d602ecb
permissions -rw-r--r--
replace lemmas eval_nat_numeral with a simpler reformulation
bulwahn@34948
     1
bulwahn@34948
     2
(* Author: Lukas Bulwahn, TU Muenchen *)
bulwahn@34948
     3
bulwahn@34948
     4
header {* Depth-Limited Sequences with failure element *}
bulwahn@34948
     5
bulwahn@34948
     6
theory DSequence
bulwahn@34948
     7
imports Lazy_Sequence Code_Numeral
bulwahn@34948
     8
begin
bulwahn@34948
     9
bulwahn@42163
    10
type_synonym 'a dseq = "code_numeral => bool => 'a Lazy_Sequence.lazy_sequence option"
bulwahn@34948
    11
bulwahn@34948
    12
definition empty :: "'a dseq"
bulwahn@34948
    13
where
bulwahn@34948
    14
  "empty = (%i pol. Some Lazy_Sequence.empty)"
bulwahn@34948
    15
bulwahn@34948
    16
definition single :: "'a => 'a dseq"
bulwahn@34948
    17
where
bulwahn@34948
    18
  "single x = (%i pol. Some (Lazy_Sequence.single x))"
bulwahn@34948
    19
bulwahn@34948
    20
fun eval :: "'a dseq => code_numeral => bool => 'a Lazy_Sequence.lazy_sequence option"
bulwahn@34948
    21
where
bulwahn@34948
    22
  "eval f i pol = f i pol"
bulwahn@34948
    23
bulwahn@34948
    24
definition yield :: "'a dseq => code_numeral => bool => ('a * 'a dseq) option" 
bulwahn@34948
    25
where
bulwahn@34948
    26
  "yield dseq i pol = (case eval dseq i pol of
bulwahn@34948
    27
    None => None
bulwahn@34948
    28
  | Some s => Option.map (apsnd (%r i pol. Some r)) (Lazy_Sequence.yield s))"
bulwahn@34948
    29
bulwahn@34948
    30
fun map_seq :: "('a => 'b dseq) => 'a Lazy_Sequence.lazy_sequence => 'b dseq"
bulwahn@34948
    31
where
bulwahn@34948
    32
  "map_seq f xq i pol = (case Lazy_Sequence.yield xq of
bulwahn@34948
    33
    None => Some Lazy_Sequence.empty
bulwahn@34948
    34
  | Some (x, xq') => (case eval (f x) i pol of
bulwahn@34948
    35
      None => None
bulwahn@34948
    36
    | Some yq => (case map_seq f xq' i pol of
bulwahn@34948
    37
        None => None
bulwahn@34948
    38
      | Some zq => Some (Lazy_Sequence.append yq zq))))"
bulwahn@34948
    39
bulwahn@34953
    40
definition bind :: "'a dseq => ('a => 'b dseq) => 'b dseq"
bulwahn@34948
    41
where
bulwahn@34948
    42
  "bind x f = (%i pol. 
bulwahn@34948
    43
     if i = 0 then
bulwahn@34948
    44
       (if pol then Some Lazy_Sequence.empty else None)
bulwahn@34948
    45
     else
bulwahn@34948
    46
       (case x (i - 1) pol of
bulwahn@34948
    47
         None => None
bulwahn@34948
    48
       | Some xq => map_seq f xq i pol))"
bulwahn@34948
    49
bulwahn@34953
    50
definition union :: "'a dseq => 'a dseq => 'a dseq"
bulwahn@34948
    51
where
bulwahn@34948
    52
  "union x y = (%i pol. case (x i pol, y i pol) of
bulwahn@34948
    53
      (Some xq, Some yq) => Some (Lazy_Sequence.append xq yq)
bulwahn@34948
    54
    | _ => None)"
bulwahn@34948
    55
bulwahn@34948
    56
definition if_seq :: "bool => unit dseq"
bulwahn@34948
    57
where
bulwahn@34948
    58
  "if_seq b = (if b then single () else empty)"
bulwahn@34948
    59
bulwahn@34953
    60
definition not_seq :: "unit dseq => unit dseq"
bulwahn@34948
    61
where
bulwahn@34948
    62
  "not_seq x = (%i pol. case x i (\<not>pol) of
bulwahn@34948
    63
    None => Some Lazy_Sequence.empty
bulwahn@34948
    64
  | Some xq => (case Lazy_Sequence.yield xq of
bulwahn@34948
    65
      None => Some (Lazy_Sequence.single ())
bulwahn@34948
    66
    | Some _ => Some (Lazy_Sequence.empty)))"
bulwahn@34948
    67
bulwahn@34953
    68
definition map :: "('a => 'b) => 'a dseq => 'b dseq"
bulwahn@34948
    69
where
bulwahn@34948
    70
  "map f g = (%i pol. case g i pol of
bulwahn@34948
    71
     None => None
bulwahn@34948
    72
   | Some xq => Some (Lazy_Sequence.map f xq))"
bulwahn@34948
    73
bulwahn@34948
    74
ML {*
bulwahn@34948
    75
signature DSEQUENCE =
bulwahn@34948
    76
sig
bulwahn@34948
    77
  type 'a dseq = int -> bool -> 'a Lazy_Sequence.lazy_sequence option
bulwahn@34948
    78
  val yield : 'a dseq -> int -> bool -> ('a * 'a dseq) option
bulwahn@34948
    79
  val yieldn : int -> 'a dseq -> int -> bool -> 'a list * 'a dseq
bulwahn@34948
    80
  val map : ('a -> 'b) -> 'a dseq -> 'b dseq
bulwahn@34948
    81
end;
bulwahn@34948
    82
bulwahn@34948
    83
structure DSequence : DSEQUENCE =
bulwahn@34948
    84
struct
bulwahn@34948
    85
bulwahn@34948
    86
type 'a dseq = int -> bool -> 'a Lazy_Sequence.lazy_sequence option
bulwahn@34948
    87
bulwahn@34948
    88
val yield = @{code yield}
bulwahn@36024
    89
fun yieldn n s d pol = case s d pol of  
bulwahn@36024
    90
    NONE => ([], fn d => fn pol => NONE)
bulwahn@36024
    91
  | SOME s => let val (xs, s') = Lazy_Sequence.yieldn n s in (xs, fn d => fn pol => SOME s') end
bulwahn@36024
    92
bulwahn@34948
    93
val map = @{code map}
bulwahn@34948
    94
bulwahn@34948
    95
end;
bulwahn@34948
    96
*}
bulwahn@34948
    97
bulwahn@34948
    98
code_reserved Eval DSequence
bulwahn@34948
    99
(*
wenzelm@36176
   100
hide_type Sequence.seq
bulwahn@34948
   101
wenzelm@36176
   102
hide_const Sequence.Seq Sequence.yield Sequence.yieldn Sequence.empty Sequence.single
bulwahn@34948
   103
  Sequence.append Sequence.flat Sequence.map Sequence.bind Sequence.ifpred Sequence.not_seq
bulwahn@34948
   104
*)
wenzelm@36176
   105
hide_const (open) empty single eval map_seq bind union if_seq not_seq map
wenzelm@36176
   106
hide_fact (open) empty_def single_def eval.simps map_seq.simps bind_def union_def
bulwahn@34953
   107
  if_seq_def not_seq_def map_def
bulwahn@34948
   108
bulwahn@34948
   109
end