src/CCL/ex/Stream.thy
author wenzelm
Sun Nov 02 18:21:45 2014 +0100 (2014-11-02)
changeset 58889 5b7a9633cfa8
parent 47966 b8a94ed1646e
child 58971 8c9a319821b3
permissions -rw-r--r--
modernized header uniformly as section;
wenzelm@17456
     1
(*  Title:      CCL/ex/Stream.thy
clasohm@1474
     2
    Author:     Martin Coen, Cambridge University Computer Laboratory
clasohm@0
     3
    Copyright   1993  University of Cambridge
clasohm@0
     4
*)
clasohm@0
     5
wenzelm@58889
     6
section {* Programs defined over streams *}
wenzelm@17456
     7
wenzelm@17456
     8
theory Stream
wenzelm@17456
     9
imports List
wenzelm@17456
    10
begin
clasohm@0
    11
wenzelm@42155
    12
definition iter1 :: "[i=>i,i]=>i"
wenzelm@42155
    13
  where "iter1(f,a) == letrec iter x be x$iter(f(x)) in iter(a)"
clasohm@0
    14
wenzelm@42155
    15
definition iter2 :: "[i=>i,i]=>i"
wenzelm@42155
    16
  where "iter2(f,a) == letrec iter x be x$map(f,iter(x)) in iter(a)"
wenzelm@20140
    17
wenzelm@20140
    18
(*
wenzelm@20140
    19
Proving properties about infinite lists using coinduction:
wenzelm@20140
    20
    Lists(A)  is the set of all finite and infinite lists of elements of A.
wenzelm@20140
    21
    ILists(A) is the set of infinite lists of elements of A.
wenzelm@20140
    22
*)
wenzelm@20140
    23
wenzelm@20140
    24
wenzelm@20140
    25
subsection {* Map of composition is composition of maps *}
wenzelm@20140
    26
wenzelm@20140
    27
lemma map_comp:
wenzelm@20140
    28
  assumes 1: "l:Lists(A)"
wenzelm@42155
    29
  shows "map(f \<circ> g,l) = map(f,map(g,l))"
wenzelm@27208
    30
  apply (tactic {* eq_coinduct3_tac @{context}
wenzelm@42155
    31
    "{p. EX x y. p=<x,y> & (EX l:Lists (A) .x=map (f \<circ> g,l) & y=map (f,map (g,l)))}" 1 *})
wenzelm@20140
    32
   apply (blast intro: 1)
wenzelm@20140
    33
  apply safe
wenzelm@20140
    34
  apply (drule ListsXH [THEN iffD1])
wenzelm@23894
    35
  apply (tactic "EQgen_tac @{context} [] 1")
wenzelm@47966
    36
   apply fastforce
wenzelm@20140
    37
  done
wenzelm@20140
    38
wenzelm@20140
    39
(*** Mapping the identity function leaves a list unchanged ***)
wenzelm@20140
    40
wenzelm@20140
    41
lemma map_id:
wenzelm@20140
    42
  assumes 1: "l:Lists(A)"
wenzelm@20140
    43
  shows "map(%x. x,l) = l"
wenzelm@27208
    44
  apply (tactic {* eq_coinduct3_tac @{context}
wenzelm@20140
    45
    "{p. EX x y. p=<x,y> & (EX l:Lists (A) .x=map (%x. x,l) & y=l) }" 1 *})
wenzelm@20140
    46
  apply (blast intro: 1)
wenzelm@20140
    47
  apply safe
wenzelm@20140
    48
  apply (drule ListsXH [THEN iffD1])
wenzelm@23894
    49
  apply (tactic "EQgen_tac @{context} [] 1")
wenzelm@20140
    50
  apply blast
wenzelm@20140
    51
  done
wenzelm@20140
    52
wenzelm@20140
    53
wenzelm@20140
    54
subsection {* Mapping distributes over append *}
wenzelm@20140
    55
wenzelm@20140
    56
lemma map_append:
wenzelm@20140
    57
  assumes "l:Lists(A)"
wenzelm@20140
    58
    and "m:Lists(A)"
wenzelm@20140
    59
  shows "map(f,l@m) = map(f,l) @ map(f,m)"
wenzelm@27208
    60
  apply (tactic {* eq_coinduct3_tac @{context}
wenzelm@20140
    61
    "{p. EX x y. p=<x,y> & (EX l:Lists (A). EX m:Lists (A). x=map (f,l@m) & y=map (f,l) @ map (f,m))}" 1 *})
wenzelm@41526
    62
  apply (blast intro: assms)
wenzelm@20140
    63
  apply safe
wenzelm@20140
    64
  apply (drule ListsXH [THEN iffD1])
wenzelm@23894
    65
  apply (tactic "EQgen_tac @{context} [] 1")
wenzelm@20140
    66
  apply (drule ListsXH [THEN iffD1])
wenzelm@23894
    67
  apply (tactic "EQgen_tac @{context} [] 1")
wenzelm@20140
    68
  apply blast
wenzelm@20140
    69
  done
wenzelm@20140
    70
wenzelm@20140
    71
wenzelm@20140
    72
subsection {* Append is associative *}
wenzelm@20140
    73
wenzelm@20140
    74
lemma append_assoc:
wenzelm@20140
    75
  assumes "k:Lists(A)"
wenzelm@20140
    76
    and "l:Lists(A)"
wenzelm@20140
    77
    and "m:Lists(A)"
wenzelm@20140
    78
  shows "k @ l @ m = (k @ l) @ m"
wenzelm@27208
    79
  apply (tactic {* eq_coinduct3_tac @{context}
wenzelm@20140
    80
    "{p. EX x y. p=<x,y> & (EX k:Lists (A). EX l:Lists (A). EX m:Lists (A). x=k @ l @ m & y= (k @ l) @ m) }" 1*})
wenzelm@41526
    81
  apply (blast intro: assms)
wenzelm@20140
    82
  apply safe
wenzelm@20140
    83
  apply (drule ListsXH [THEN iffD1])
wenzelm@23894
    84
  apply (tactic "EQgen_tac @{context} [] 1")
wenzelm@20140
    85
   prefer 2
wenzelm@20140
    86
   apply blast
wenzelm@39159
    87
  apply (tactic {* DEPTH_SOLVE (etac (XH_to_E @{thm ListsXH}) 1
wenzelm@23894
    88
    THEN EQgen_tac @{context} [] 1) *})
wenzelm@20140
    89
  done
wenzelm@20140
    90
wenzelm@20140
    91
wenzelm@20140
    92
subsection {* Appending anything to an infinite list doesn't alter it *}
wenzelm@20140
    93
wenzelm@20140
    94
lemma ilist_append:
wenzelm@20140
    95
  assumes "l:ILists(A)"
wenzelm@20140
    96
  shows "l @ m = l"
wenzelm@27208
    97
  apply (tactic {* eq_coinduct3_tac @{context}
wenzelm@20140
    98
    "{p. EX x y. p=<x,y> & (EX l:ILists (A) .EX m. x=l@m & y=l)}" 1 *})
wenzelm@41526
    99
  apply (blast intro: assms)
wenzelm@20140
   100
  apply safe
wenzelm@20140
   101
  apply (drule IListsXH [THEN iffD1])
wenzelm@23894
   102
  apply (tactic "EQgen_tac @{context} [] 1")
wenzelm@20140
   103
  apply blast
wenzelm@20140
   104
  done
wenzelm@20140
   105
wenzelm@20140
   106
(*** The equivalance of two versions of an iteration function       ***)
wenzelm@20140
   107
(*                                                                    *)
wenzelm@20140
   108
(*        fun iter1(f,a) = a$iter1(f,f(a))                            *)
wenzelm@20140
   109
(*        fun iter2(f,a) = a$map(f,iter2(f,a))                        *)
wenzelm@20140
   110
wenzelm@20140
   111
lemma iter1B: "iter1(f,a) = a$iter1(f,f(a))"
wenzelm@20140
   112
  apply (unfold iter1_def)
wenzelm@20140
   113
  apply (rule letrecB [THEN trans])
wenzelm@20140
   114
  apply simp
wenzelm@20140
   115
  done
wenzelm@20140
   116
wenzelm@20140
   117
lemma iter2B: "iter2(f,a) = a $ map(f,iter2(f,a))"
wenzelm@20140
   118
  apply (unfold iter2_def)
wenzelm@20140
   119
  apply (rule letrecB [THEN trans])
wenzelm@20140
   120
  apply (rule refl)
wenzelm@20140
   121
  done
wenzelm@20140
   122
wenzelm@20140
   123
lemma iter2Blemma:
wenzelm@20140
   124
  "n:Nat ==>  
wenzelm@20140
   125
    map(f) ^ n ` iter2(f,a) = (f ^ n ` a) $ (map(f) ^ n ` map(f,iter2(f,a)))"
wenzelm@20140
   126
  apply (rule_tac P = "%x. ?lhs (x) = ?rhs" in iter2B [THEN ssubst])
wenzelm@20140
   127
  apply (simp add: nmapBcons)
wenzelm@20140
   128
  done
wenzelm@20140
   129
wenzelm@20140
   130
lemma iter1_iter2_eq: "iter1(f,a) = iter2(f,a)"
wenzelm@27208
   131
  apply (tactic {* eq_coinduct3_tac @{context}
wenzelm@20140
   132
    "{p. EX x y. p=<x,y> & (EX n:Nat. x=iter1 (f,f^n`a) & y=map (f) ^n`iter2 (f,a))}" 1*})
wenzelm@20140
   133
  apply (fast intro!: napplyBzero [symmetric] napplyBzero [symmetric, THEN arg_cong])
wenzelm@39159
   134
  apply (tactic {* EQgen_tac @{context} [@{thm iter1B}, @{thm iter2Blemma}] 1 *})
wenzelm@20140
   135
  apply (subst napply_f, assumption)
wenzelm@20140
   136
  apply (rule_tac f1 = f in napplyBsucc [THEN subst])
wenzelm@20140
   137
  apply blast
wenzelm@20140
   138
  done
clasohm@0
   139
clasohm@0
   140
end