src/CCL/ex/Stream.thy
author blanchet
Mon, 15 Sep 2014 10:49:07 +0200
changeset 58335 a5a3b576fcfb
parent 47966 b8a94ed1646e
child 58889 5b7a9633cfa8
permissions -rw-r--r--
generate 'code' attribute only if 'code' plugin is enabled
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
42155
ffe99b07c9c0 modernized specifications -- some attempts to avoid wild axiomatizations;
wenzelm
parents: 41526
diff changeset
    12
definition iter1 :: "[i=>i,i]=>i"
ffe99b07c9c0 modernized specifications -- some attempts to avoid wild axiomatizations;
wenzelm
parents: 41526
diff changeset
    13
  where "iter1(f,a) == letrec iter x be x$iter(f(x)) in iter(a)"
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    14
42155
ffe99b07c9c0 modernized specifications -- some attempts to avoid wild axiomatizations;
wenzelm
parents: 41526
diff changeset
    15
definition iter2 :: "[i=>i,i]=>i"
ffe99b07c9c0 modernized specifications -- some attempts to avoid wild axiomatizations;
wenzelm
parents: 41526
diff changeset
    16
  where "iter2(f,a) == letrec iter x be x$map(f,iter(x)) in iter(a)"
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    17
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    18
(*
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    19
Proving properties about infinite lists using coinduction:
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    20
    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
    21
    ILists(A) is the set of infinite lists of elements of A.
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    22
*)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    23
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    24
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    25
subsection {* Map of composition is composition of maps *}
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    26
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    27
lemma map_comp:
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    28
  assumes 1: "l:Lists(A)"
42155
ffe99b07c9c0 modernized specifications -- some attempts to avoid wild axiomatizations;
wenzelm
parents: 41526
diff changeset
    29
  shows "map(f \<circ> g,l) = map(f,map(g,l))"
27208
5fe899199f85 proper context for tactics derived from res_inst_tac;
wenzelm
parents: 23894
diff changeset
    30
  apply (tactic {* eq_coinduct3_tac @{context}
42155
ffe99b07c9c0 modernized specifications -- some attempts to avoid wild axiomatizations;
wenzelm
parents: 41526
diff changeset
    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 *})
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    32
   apply (blast intro: 1)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    33
  apply safe
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    34
  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
    35
  apply (tactic "EQgen_tac @{context} [] 1")
47966
b8a94ed1646e eliminated obsolete fastsimp;
wenzelm
parents: 42155
diff changeset
    36
   apply fastforce
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    37
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    38
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    39
(*** Mapping the identity function leaves a list unchanged ***)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    40
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    41
lemma map_id:
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    42
  assumes 1: "l:Lists(A)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    43
  shows "map(%x. x,l) = l"
27208
5fe899199f85 proper context for tactics derived from res_inst_tac;
wenzelm
parents: 23894
diff changeset
    44
  apply (tactic {* eq_coinduct3_tac @{context}
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    45
    "{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
    46
  apply (blast intro: 1)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    47
  apply safe
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    48
  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
    49
  apply (tactic "EQgen_tac @{context} [] 1")
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    50
  apply blast
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    51
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    52
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    53
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    54
subsection {* Mapping distributes over append *}
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    55
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    56
lemma map_append:
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    57
  assumes "l:Lists(A)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    58
    and "m:Lists(A)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    59
  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
    60
  apply (tactic {* eq_coinduct3_tac @{context}
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    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 *})
41526
54b4686704af eliminated global prems;
wenzelm
parents: 39159
diff changeset
    62
  apply (blast intro: assms)
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    63
  apply safe
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    64
  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
    65
  apply (tactic "EQgen_tac @{context} [] 1")
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    66
  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
    67
  apply (tactic "EQgen_tac @{context} [] 1")
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    68
  apply blast
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    69
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    70
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    71
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    72
subsection {* Append is associative *}
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    73
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    74
lemma append_assoc:
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    75
  assumes "k:Lists(A)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    76
    and "l:Lists(A)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    77
    and "m:Lists(A)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    78
  shows "k @ l @ m = (k @ l) @ m"
27208
5fe899199f85 proper context for tactics derived from res_inst_tac;
wenzelm
parents: 23894
diff changeset
    79
  apply (tactic {* eq_coinduct3_tac @{context}
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    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*})
41526
54b4686704af eliminated global prems;
wenzelm
parents: 39159
diff changeset
    81
  apply (blast intro: assms)
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    82
  apply safe
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    83
  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
    84
  apply (tactic "EQgen_tac @{context} [] 1")
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    85
   prefer 2
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    86
   apply blast
39159
0dec18004e75 more antiquotations;
wenzelm
parents: 35762
diff changeset
    87
  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
    88
    THEN EQgen_tac @{context} [] 1) *})
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    89
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    90
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    91
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    92
subsection {* Appending anything to an infinite list doesn't alter it *}
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    93
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    94
lemma ilist_append:
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    95
  assumes "l:ILists(A)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    96
  shows "l @ m = l"
27208
5fe899199f85 proper context for tactics derived from res_inst_tac;
wenzelm
parents: 23894
diff changeset
    97
  apply (tactic {* eq_coinduct3_tac @{context}
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    98
    "{p. EX x y. p=<x,y> & (EX l:ILists (A) .EX m. x=l@m & y=l)}" 1 *})
41526
54b4686704af eliminated global prems;
wenzelm
parents: 39159
diff changeset
    99
  apply (blast intro: assms)
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   100
  apply safe
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   101
  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
   102
  apply (tactic "EQgen_tac @{context} [] 1")
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   103
  apply blast
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   104
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   105
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   106
(*** The equivalance of two versions of an iteration function       ***)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   107
(*                                                                    *)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   108
(*        fun iter1(f,a) = a$iter1(f,f(a))                            *)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   109
(*        fun iter2(f,a) = a$map(f,iter2(f,a))                        *)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   110
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   111
lemma iter1B: "iter1(f,a) = a$iter1(f,f(a))"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   112
  apply (unfold iter1_def)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   113
  apply (rule letrecB [THEN trans])
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   114
  apply simp
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   115
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   116
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   117
lemma iter2B: "iter2(f,a) = a $ map(f,iter2(f,a))"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   118
  apply (unfold iter2_def)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   119
  apply (rule letrecB [THEN trans])
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   120
  apply (rule refl)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   121
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   122
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   123
lemma iter2Blemma:
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   124
  "n:Nat ==>  
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   125
    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
   126
  apply (rule_tac P = "%x. ?lhs (x) = ?rhs" in iter2B [THEN ssubst])
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   127
  apply (simp add: nmapBcons)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   128
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   129
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   130
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
   131
  apply (tactic {* eq_coinduct3_tac @{context}
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   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*})
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   133
  apply (fast intro!: napplyBzero [symmetric] napplyBzero [symmetric, THEN arg_cong])
39159
0dec18004e75 more antiquotations;
wenzelm
parents: 35762
diff changeset
   134
  apply (tactic {* EQgen_tac @{context} [@{thm iter1B}, @{thm iter2Blemma}] 1 *})
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   135
  apply (subst napply_f, assumption)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   136
  apply (rule_tac f1 = f in napplyBsucc [THEN subst])
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   137
  apply blast
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   138
  done
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   139
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   140
end