src/HOL/New_Random_Sequence.thy
author wenzelm
Fri Dec 17 17:43:54 2010 +0100 (2010-12-17)
changeset 41229 d797baa3d57c
parent 40049 75d9f57123d6
child 42163 392fd6c4669c
permissions -rw-r--r--
replaced command 'nonterminals' by slightly modernized version 'nonterminal';
     1 
     2 (* Author: Lukas Bulwahn, TU Muenchen *)
     3 
     4 theory New_Random_Sequence
     5 imports Quickcheck New_DSequence
     6 begin
     7 
     8 types 'a pos_random_dseq = "code_numeral \<Rightarrow> code_numeral \<Rightarrow> Random.seed \<Rightarrow> 'a New_DSequence.pos_dseq"
     9 types 'a neg_random_dseq = "code_numeral \<Rightarrow> code_numeral \<Rightarrow> Random.seed \<Rightarrow> 'a New_DSequence.neg_dseq"
    10 
    11 definition pos_empty :: "'a pos_random_dseq"
    12 where
    13   "pos_empty = (%nrandom size seed. New_DSequence.pos_empty)"
    14 
    15 definition pos_single :: "'a => 'a pos_random_dseq"
    16 where
    17   "pos_single x = (%nrandom size seed. New_DSequence.pos_single x)"
    18 
    19 definition pos_bind :: "'a pos_random_dseq => ('a \<Rightarrow> 'b pos_random_dseq) \<Rightarrow> 'b pos_random_dseq"
    20 where
    21   "pos_bind R f = (\<lambda>nrandom size seed. New_DSequence.pos_bind (R nrandom size seed) (%a. f a nrandom size seed))"
    22 
    23 definition pos_decr_bind :: "'a pos_random_dseq => ('a \<Rightarrow> 'b pos_random_dseq) \<Rightarrow> 'b pos_random_dseq"
    24 where
    25   "pos_decr_bind R f = (\<lambda>nrandom size seed. New_DSequence.pos_decr_bind (R nrandom size seed) (%a. f a nrandom size seed))"
    26 
    27 definition pos_union :: "'a pos_random_dseq => 'a pos_random_dseq => 'a pos_random_dseq"
    28 where
    29   "pos_union R1 R2 = (\<lambda>nrandom size seed. New_DSequence.pos_union (R1 nrandom size seed) (R2 nrandom size seed))"
    30 
    31 definition pos_if_random_dseq :: "bool => unit pos_random_dseq"
    32 where
    33   "pos_if_random_dseq b = (if b then pos_single () else pos_empty)"
    34 
    35 definition pos_iterate_upto :: "(code_numeral => 'a) => code_numeral => code_numeral => 'a pos_random_dseq"
    36 where
    37   "pos_iterate_upto f n m = (\<lambda>nrandom size seed. New_DSequence.pos_iterate_upto f n m)"
    38 
    39 definition pos_not_random_dseq :: "unit neg_random_dseq => unit pos_random_dseq"
    40 where
    41   "pos_not_random_dseq R = (\<lambda>nrandom size seed. New_DSequence.pos_not_seq (R nrandom size seed))"
    42 
    43 fun iter :: "(code_numeral * code_numeral => ('a * (unit => term)) * code_numeral * code_numeral) => code_numeral => code_numeral * code_numeral => 'a Lazy_Sequence.lazy_sequence"
    44 where
    45   "iter random nrandom seed =
    46     (if nrandom = 0 then Lazy_Sequence.empty else Lazy_Sequence.Lazy_Sequence (%u. let ((x, _), seed') = random seed in Some (x, iter random (nrandom - 1) seed')))"
    47 
    48 definition Random :: "(code_numeral \<Rightarrow> Random.seed \<Rightarrow> (('a \<times> (unit \<Rightarrow> term)) \<times> Random.seed)) \<Rightarrow> 'a pos_random_dseq"
    49 where
    50   "Random g = (%nrandom size seed depth. iter (g size) nrandom seed)"
    51 
    52 definition pos_map :: "('a => 'b) => 'a pos_random_dseq => 'b pos_random_dseq"
    53 where
    54   "pos_map f P = pos_bind P (pos_single o f)"
    55 
    56 
    57 definition neg_empty :: "'a neg_random_dseq"
    58 where
    59   "neg_empty = (%nrandom size seed. New_DSequence.neg_empty)"
    60 
    61 definition neg_single :: "'a => 'a neg_random_dseq"
    62 where
    63   "neg_single x = (%nrandom size seed. New_DSequence.neg_single x)"
    64 
    65 definition neg_bind :: "'a neg_random_dseq => ('a \<Rightarrow> 'b neg_random_dseq) \<Rightarrow> 'b neg_random_dseq"
    66 where
    67   "neg_bind R f = (\<lambda>nrandom size seed. New_DSequence.neg_bind (R nrandom size seed) (%a. f a nrandom size seed))"
    68 
    69 definition neg_decr_bind :: "'a neg_random_dseq => ('a \<Rightarrow> 'b neg_random_dseq) \<Rightarrow> 'b neg_random_dseq"
    70 where
    71   "neg_decr_bind R f = (\<lambda>nrandom size seed. New_DSequence.neg_decr_bind (R nrandom size seed) (%a. f a nrandom size seed))"
    72 
    73 definition neg_union :: "'a neg_random_dseq => 'a neg_random_dseq => 'a neg_random_dseq"
    74 where
    75   "neg_union R1 R2 = (\<lambda>nrandom size seed. New_DSequence.neg_union (R1 nrandom size seed) (R2 nrandom size seed))"
    76 
    77 definition neg_if_random_dseq :: "bool => unit neg_random_dseq"
    78 where
    79   "neg_if_random_dseq b = (if b then neg_single () else neg_empty)"
    80 
    81 definition neg_iterate_upto :: "(code_numeral => 'a) => code_numeral => code_numeral => 'a neg_random_dseq"
    82 where
    83   "neg_iterate_upto f n m = (\<lambda>nrandom size seed. New_DSequence.neg_iterate_upto f n m)"
    84 
    85 definition neg_not_random_dseq :: "unit pos_random_dseq => unit neg_random_dseq"
    86 where
    87   "neg_not_random_dseq R = (\<lambda>nrandom size seed. New_DSequence.neg_not_seq (R nrandom size seed))"
    88 (*
    89 fun iter :: "(code_numeral * code_numeral => ('a * (unit => term)) * code_numeral * code_numeral) => code_numeral => code_numeral * code_numeral => 'a Lazy_Sequence.lazy_sequence"
    90 where
    91   "iter random nrandom seed =
    92     (if nrandom = 0 then Lazy_Sequence.empty else Lazy_Sequence.Lazy_Sequence (%u. let ((x, _), seed') = random seed in Some (x, iter random (nrandom - 1) seed')))"
    93 
    94 definition Random :: "(code_numeral \<Rightarrow> Random.seed \<Rightarrow> (('a \<times> (unit \<Rightarrow> term)) \<times> Random.seed)) \<Rightarrow> 'a pos_random_dseq"
    95 where
    96   "Random g = (%nrandom size seed depth. iter (g size) nrandom seed)"
    97 *)
    98 definition neg_map :: "('a => 'b) => 'a neg_random_dseq => 'b neg_random_dseq"
    99 where
   100   "neg_map f P = neg_bind P (neg_single o f)"
   101 (*
   102 hide_const DSequence.empty DSequence.single DSequence.eval
   103   DSequence.map_seq DSequence.bind DSequence.union DSequence.if_seq DSequence.not_seq
   104   DSequence.map
   105 *)
   106 
   107 hide_const (open)
   108   pos_empty pos_single pos_bind pos_decr_bind pos_union pos_if_random_dseq pos_iterate_upto pos_not_random_dseq iter Random pos_map
   109   neg_empty neg_single neg_bind neg_decr_bind neg_union neg_if_random_dseq neg_iterate_upto neg_not_random_dseq neg_map
   110 hide_type New_DSequence.pos_dseq New_DSequence.neg_dseq pos_random_dseq neg_random_dseq
   111 (* FIXME: hide facts *)
   112 (*hide_fact (open) empty_def single_def bind_def union_def if_random_dseq_def not_random_dseq_def Random.simps map_def*)
   113 
   114 end