src/CCL/ex/Stream.thy
author haftmann
Thu, 08 Jul 2010 16:19:24 +0200
changeset 37744 3daaf23b9ab4
parent 35762 af3ff2ba4c54
child 39159 0dec18004e75
permissions -rw-r--r--
tuned titles
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 1474
diff changeset
     1
(*  Title:      CCL/ex/Stream.thy
1474
3f7d67927fe2 expanded tabs
clasohm
parents: 290
diff changeset
     2
    Author:     Martin Coen, Cambridge University Computer Laboratory
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     3
    Copyright   1993  University of Cambridge
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 1474
diff changeset
     6
header {* Programs defined over streams *}
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 1474
diff changeset
     7
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 1474
diff changeset
     8
theory Stream
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 1474
diff changeset
     9
imports List
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 1474
diff changeset
    10
begin
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    12
consts
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 1474
diff changeset
    13
  iter1   ::  "[i=>i,i]=>i"
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 1474
diff changeset
    14
  iter2   ::  "[i=>i,i]=>i"
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    15
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 1474
diff changeset
    16
defs
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    17
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 1474
diff changeset
    18
  iter1_def:   "iter1(f,a) == letrec iter x be x$iter(f(x)) in iter(a)"
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 1474
diff changeset
    19
  iter2_def:   "iter2(f,a) == letrec iter x be x$map(f,iter(x)) in iter(a)"
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    20
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    21
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    22
(*
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    23
Proving properties about infinite lists using coinduction:
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    24
    Lists(A)  is the set of all finite and infinite lists of elements of A.
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    25
    ILists(A) is the set of infinite lists of elements of A.
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    26
*)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    27
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    28
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    29
subsection {* Map of composition is composition of maps *}
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    30
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    31
lemma map_comp:
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    32
  assumes 1: "l:Lists(A)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    33
  shows "map(f o g,l) = map(f,map(g,l))"
27208
5fe899199f85 proper context for tactics derived from res_inst_tac;
wenzelm
parents: 23894
diff changeset
    34
  apply (tactic {* eq_coinduct3_tac @{context}
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    35
    "{p. EX x y. p=<x,y> & (EX l:Lists (A) .x=map (f o g,l) & y=map (f,map (g,l)))}" 1 *})
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    36
   apply (blast intro: 1)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    37
  apply safe
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    38
  apply (drule ListsXH [THEN iffD1])
23894
1a4167d761ac tactics: avoid dynamic reference to accidental theory context (via ML_Context.the_context etc.);
wenzelm
parents: 20140
diff changeset
    39
  apply (tactic "EQgen_tac @{context} [] 1")
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    40
   apply fastsimp
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    41
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    42
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    43
(*** Mapping the identity function leaves a list unchanged ***)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    44
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    45
lemma map_id:
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    46
  assumes 1: "l:Lists(A)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    47
  shows "map(%x. x,l) = l"
27208
5fe899199f85 proper context for tactics derived from res_inst_tac;
wenzelm
parents: 23894
diff changeset
    48
  apply (tactic {* eq_coinduct3_tac @{context}
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    49
    "{p. EX x y. p=<x,y> & (EX l:Lists (A) .x=map (%x. x,l) & y=l) }" 1 *})
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    50
  apply (blast intro: 1)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    51
  apply safe
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    52
  apply (drule ListsXH [THEN iffD1])
23894
1a4167d761ac tactics: avoid dynamic reference to accidental theory context (via ML_Context.the_context etc.);
wenzelm
parents: 20140
diff changeset
    53
  apply (tactic "EQgen_tac @{context} [] 1")
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    54
  apply blast
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    55
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    56
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    57
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    58
subsection {* Mapping distributes over append *}
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    59
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    60
lemma map_append:
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    61
  assumes "l:Lists(A)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    62
    and "m:Lists(A)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    63
  shows "map(f,l@m) = map(f,l) @ map(f,m)"
27208
5fe899199f85 proper context for tactics derived from res_inst_tac;
wenzelm
parents: 23894
diff changeset
    64
  apply (tactic {* eq_coinduct3_tac @{context}
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    65
    "{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 *})
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    66
  apply (blast intro: prems)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    67
  apply safe
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    68
  apply (drule ListsXH [THEN iffD1])
23894
1a4167d761ac tactics: avoid dynamic reference to accidental theory context (via ML_Context.the_context etc.);
wenzelm
parents: 20140
diff changeset
    69
  apply (tactic "EQgen_tac @{context} [] 1")
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    70
  apply (drule ListsXH [THEN iffD1])
23894
1a4167d761ac tactics: avoid dynamic reference to accidental theory context (via ML_Context.the_context etc.);
wenzelm
parents: 20140
diff changeset
    71
  apply (tactic "EQgen_tac @{context} [] 1")
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    72
  apply blast
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    73
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    74
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    75
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    76
subsection {* Append is associative *}
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    77
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    78
lemma append_assoc:
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    79
  assumes "k:Lists(A)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    80
    and "l:Lists(A)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    81
    and "m:Lists(A)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    82
  shows "k @ l @ m = (k @ l) @ m"
27208
5fe899199f85 proper context for tactics derived from res_inst_tac;
wenzelm
parents: 23894
diff changeset
    83
  apply (tactic {* eq_coinduct3_tac @{context}
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    84
    "{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*})
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    85
  apply (blast intro: prems)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    86
  apply safe
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    87
  apply (drule ListsXH [THEN iffD1])
23894
1a4167d761ac tactics: avoid dynamic reference to accidental theory context (via ML_Context.the_context etc.);
wenzelm
parents: 20140
diff changeset
    88
  apply (tactic "EQgen_tac @{context} [] 1")
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    89
   prefer 2
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    90
   apply blast
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    91
  apply (tactic {* DEPTH_SOLVE (etac (XH_to_E (thm "ListsXH")) 1
23894
1a4167d761ac tactics: avoid dynamic reference to accidental theory context (via ML_Context.the_context etc.);
wenzelm
parents: 20140
diff changeset
    92
    THEN EQgen_tac @{context} [] 1) *})
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    93
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    94
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    95
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    96
subsection {* Appending anything to an infinite list doesn't alter it *}
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    97
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    98
lemma ilist_append:
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    99
  assumes "l:ILists(A)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   100
  shows "l @ m = l"
27208
5fe899199f85 proper context for tactics derived from res_inst_tac;
wenzelm
parents: 23894
diff changeset
   101
  apply (tactic {* eq_coinduct3_tac @{context}
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   102
    "{p. EX x y. p=<x,y> & (EX l:ILists (A) .EX m. x=l@m & y=l)}" 1 *})
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   103
  apply (blast intro: prems)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   104
  apply safe
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   105
  apply (drule IListsXH [THEN iffD1])
23894
1a4167d761ac tactics: avoid dynamic reference to accidental theory context (via ML_Context.the_context etc.);
wenzelm
parents: 20140
diff changeset
   106
  apply (tactic "EQgen_tac @{context} [] 1")
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   107
  apply blast
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   108
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   109
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   110
(*** The equivalance of two versions of an iteration function       ***)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   111
(*                                                                    *)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   112
(*        fun iter1(f,a) = a$iter1(f,f(a))                            *)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   113
(*        fun iter2(f,a) = a$map(f,iter2(f,a))                        *)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   114
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   115
lemma iter1B: "iter1(f,a) = a$iter1(f,f(a))"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   116
  apply (unfold iter1_def)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   117
  apply (rule letrecB [THEN trans])
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   118
  apply simp
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   119
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   120
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   121
lemma iter2B: "iter2(f,a) = a $ map(f,iter2(f,a))"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   122
  apply (unfold iter2_def)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   123
  apply (rule letrecB [THEN trans])
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   124
  apply (rule refl)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   125
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   126
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   127
lemma iter2Blemma:
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   128
  "n:Nat ==>  
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   129
    map(f) ^ n ` iter2(f,a) = (f ^ n ` a) $ (map(f) ^ n ` map(f,iter2(f,a)))"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   130
  apply (rule_tac P = "%x. ?lhs (x) = ?rhs" in iter2B [THEN ssubst])
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   131
  apply (simp add: nmapBcons)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   132
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   133
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   134
lemma iter1_iter2_eq: "iter1(f,a) = iter2(f,a)"
27208
5fe899199f85 proper context for tactics derived from res_inst_tac;
wenzelm
parents: 23894
diff changeset
   135
  apply (tactic {* eq_coinduct3_tac @{context}
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   136
    "{p. EX x y. p=<x,y> & (EX n:Nat. x=iter1 (f,f^n`a) & y=map (f) ^n`iter2 (f,a))}" 1*})
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   137
  apply (fast intro!: napplyBzero [symmetric] napplyBzero [symmetric, THEN arg_cong])
23894
1a4167d761ac tactics: avoid dynamic reference to accidental theory context (via ML_Context.the_context etc.);
wenzelm
parents: 20140
diff changeset
   138
  apply (tactic {* EQgen_tac @{context} [thm "iter1B", thm "iter2Blemma"] 1 *})
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   139
  apply (subst napply_f, assumption)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   140
  apply (rule_tac f1 = f in napplyBsucc [THEN subst])
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   141
  apply blast
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   142
  done
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   143
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   144
end