src/HOL/UNITY/Reach.ML
author paulson
Fri, 31 Jul 1998 18:46:55 +0200
changeset 5232 e5a7cdd07ea5
parent 5196 1dd4ec921f71
child 5240 bbcd79ef7cf2
permissions -rw-r--r--
Tidied; uses records
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
     1
(*  Title:      HOL/UNITY/Reach.thy
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
     2
    ID:         $Id$
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
     4
    Copyright   1998  University of Cambridge
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
     5
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
     6
Reachability in Directed Graphs.  From Chandy and Misra, section 6.4.
4896
4727272f3db6 New syntax for function update; moved to main HOL directory
paulson
parents: 4776
diff changeset
     7
	[ this example took only four days!]
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
     8
*)
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
     9
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    10
(*TO SIMPDATA.ML??  FOR CLASET??  *)
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    11
val major::prems = goal thy 
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    12
    "[| if P then Q else R;    \
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    13
\       [| P;   Q |] ==> S;    \
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    14
\       [| ~ P; R |] ==> S |] ==> S";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    15
by (cut_facts_tac [major] 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    16
by (blast_tac (claset() addSDs [if_bool_eq_disj RS iffD1] addIs prems) 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    17
qed "ifE";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    18
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    19
AddSEs [ifE];
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    20
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    21
5196
1dd4ec921f71 update -> fun_upd
nipkow
parents: 5111
diff changeset
    22
val cmd_defs = [racts_def, asgt_def, fun_upd_def];
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    23
5069
3ea049f7979d isatool fixgoal;
wenzelm
parents: 4896
diff changeset
    24
Goalw [racts_def] "id : racts";
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    25
by (Simp_tac 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    26
qed "id_in_racts";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    27
AddIffs [id_in_racts];
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    28
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    29
(*All vertex sets are finite*)
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    30
AddIffs [[subset_UNIV, finite_graph] MRS finite_subset];
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    31
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    32
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    33
(** Constrains/Ensures tactics: NEED TO BE GENERALIZED OVER ALL PROGRAMS **)
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    34
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    35
(*proves "constrains" properties when the program is specified*)
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    36
val constrains_tac = 
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    37
   SELECT_GOAL
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    38
      (EVERY [rtac constrainsI 1,
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    39
	      rewtac racts_def,
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    40
	      REPEAT_FIRST (eresolve_tac [insertE, emptyE]),
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    41
	      rewrite_goals_tac [racts_def, asgt_def],
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    42
	      ALLGOALS (SELECT_GOAL Auto_tac)]);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    43
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    44
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    45
(*proves "ensures" properties when the program is specified*)
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    46
fun ensures_tac sact = 
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    47
    SELECT_GOAL
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    48
      (EVERY [REPEAT (resolve_tac [LeadsTo_Basis, leadsTo_Basis, ensuresI] 1),
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    49
	      res_inst_tac [("act", sact)] transient_mem 2,
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    50
	      Simp_tac 2,
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    51
	      constrains_tac 1,
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    52
	      rewrite_goals_tac [racts_def, asgt_def],
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    53
	      Auto_tac]);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    54
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    55
5069
3ea049f7979d isatool fixgoal;
wenzelm
parents: 4896
diff changeset
    56
Goalw [stable_def, invariant_def]
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    57
    "stable racts invariant";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    58
by (constrains_tac 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    59
by (blast_tac (claset() addIs [r_into_rtrancl,rtrancl_trans]) 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    60
qed "stable_invariant";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    61
5069
3ea049f7979d isatool fixgoal;
wenzelm
parents: 4896
diff changeset
    62
Goalw [rinit_def, invariant_def] "rinit <= invariant";
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    63
by Auto_tac;
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    64
qed "rinit_invariant";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    65
5111
8f4b72f0c15d Uncurried functions LeadsTo and reach
paulson
parents: 5071
diff changeset
    66
Goal "reachable (rinit,racts) <= invariant";
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    67
by (simp_tac (simpset() addsimps
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    68
	      [strongest_invariant, stable_invariant, rinit_invariant]) 1); 
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    69
qed "reachable_subset_invariant";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    70
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    71
val reachable_subset_invariant' = 
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    72
    rewrite_rule [invariant_def] reachable_subset_invariant;
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    73
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    74
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    75
(*** Fixedpoint ***)
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    76
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    77
(*If it reaches a fixedpoint, it has found a solution*)
5069
3ea049f7979d isatool fixgoal;
wenzelm
parents: 4896
diff changeset
    78
Goalw [fixedpoint_def, invariant_def]
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    79
    "fixedpoint Int invariant = { %v. (init, v) : edges^* }";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    80
by (rtac equalityI 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    81
by (blast_tac (claset() addIs [r_into_rtrancl,rtrancl_trans]) 2);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    82
by (auto_tac (claset() addSIs [ext], simpset()));
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    83
by (etac rtrancl_induct 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    84
by Auto_tac;
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    85
qed "fixedpoint_invariant_correct";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    86
5069
3ea049f7979d isatool fixgoal;
wenzelm
parents: 4896
diff changeset
    87
Goalw (cmd_defs @ [FP_def, fixedpoint_def, stable_def, constrains_def])
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    88
    "FP racts <= fixedpoint";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    89
by Auto_tac;
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    90
by (dtac bspec 1); 
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    91
by (Blast_tac 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    92
by (asm_full_simp_tac (simpset() addsimps [Image_singleton, image_iff]) 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    93
by (dtac fun_cong 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    94
by Auto_tac;
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    95
val lemma1 = result();
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    96
5069
3ea049f7979d isatool fixgoal;
wenzelm
parents: 4896
diff changeset
    97
Goalw (cmd_defs @ [FP_def, fixedpoint_def, stable_def, constrains_def])
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    98
    "fixedpoint <= FP racts";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
    99
by (auto_tac (claset() addIs [ext], simpset()));
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   100
val lemma2 = result();
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   101
5069
3ea049f7979d isatool fixgoal;
wenzelm
parents: 4896
diff changeset
   102
Goal "FP racts = fixedpoint";
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   103
by (rtac ([lemma1,lemma2] MRS equalityI) 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   104
qed "FP_fixedpoint";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   105
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   106
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   107
(*If we haven't reached a fixedpoint then there is some edge for which u but
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   108
  not v holds.  Progress will be proved via an ENSURES assertion that the
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   109
  metric will decrease for each suitable edge.  A union over all edges proves
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   110
  a LEADSTO assertion that the metric decreases if we are not at a fixedpoint.
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   111
  *)
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   112
5069
3ea049f7979d isatool fixgoal;
wenzelm
parents: 4896
diff changeset
   113
Goal "Compl fixedpoint = (UN (u,v): edges. {s. s u & ~ s v})";
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   114
by (simp_tac (simpset() addsimps
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   115
	      ([Compl_FP, UN_UN_flatten, FP_fixedpoint RS sym, 
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   116
		racts_def, asgt_def])) 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   117
by Safe_tac;
5196
1dd4ec921f71 update -> fun_upd
nipkow
parents: 5111
diff changeset
   118
by (rtac fun_upd_idem 1);
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   119
by (Blast_tac 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   120
by (Full_simp_tac 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   121
by (REPEAT (dtac bspec 1 THEN Simp_tac 1));
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   122
by (dtac subsetD 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   123
by (Simp_tac 1);
5196
1dd4ec921f71 update -> fun_upd
nipkow
parents: 5111
diff changeset
   124
by (asm_full_simp_tac (simpset() addsimps [fun_upd_idem_iff]) 1);
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   125
qed "Compl_fixedpoint";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   126
5069
3ea049f7979d isatool fixgoal;
wenzelm
parents: 4896
diff changeset
   127
Goal "A - fixedpoint = (UN (u,v): edges. A Int {s. s u & ~ s v})";
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   128
by (simp_tac (simpset() addsimps [Diff_eq, Compl_fixedpoint]) 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   129
by (Blast_tac 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   130
qed "Diff_fixedpoint";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   131
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   132
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   133
(*** Progress ***)
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   134
5111
8f4b72f0c15d Uncurried functions LeadsTo and reach
paulson
parents: 5071
diff changeset
   135
Goalw [metric_def] "~ s x ==> Suc (metric (s(x:=True))) = metric s";
5071
548f398d770b Consequences of the change from [ := ] to ( := ) in theory Update.
nipkow
parents: 5069
diff changeset
   136
by (subgoal_tac "{v. ~ (s(x:=True)) v} = {v. ~ s v} - {x}" 1);
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   137
by Auto_tac;
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   138
by (asm_simp_tac (simpset() addsimps [card_Suc_Diff]) 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   139
qed "Suc_metric";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   140
5111
8f4b72f0c15d Uncurried functions LeadsTo and reach
paulson
parents: 5071
diff changeset
   141
Goal "~ s x ==> metric (s(x:=True)) < metric s";
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   142
by (etac (Suc_metric RS subst) 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   143
by (Blast_tac 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   144
qed "metric_less";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   145
AddSIs [metric_less];
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   146
5071
548f398d770b Consequences of the change from [ := ] to ( := ) in theory Update.
nipkow
parents: 5069
diff changeset
   147
Goal "metric (s(y:=s x | s y)) <= metric s";
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   148
by (case_tac "s x --> s y" 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   149
by (auto_tac (claset() addIs [less_imp_le],
5196
1dd4ec921f71 update -> fun_upd
nipkow
parents: 5111
diff changeset
   150
	      simpset() addsimps [fun_upd_idem]));
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   151
qed "metric_le";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   152
5111
8f4b72f0c15d Uncurried functions LeadsTo and reach
paulson
parents: 5071
diff changeset
   153
Goal "(u,v): edges ==> \
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   154
\              ensures racts ((metric-``{m}) Int {s. s u & ~ s v})  \
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   155
\                            (metric-``(lessThan m))";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   156
by (ensures_tac "asgt u v" 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   157
by (cut_facts_tac [metric_le] 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   158
by (fast_tac (claset() addSDs [le_imp_less_or_eq]) 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   159
qed "edges_ensures";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   160
5069
3ea049f7979d isatool fixgoal;
wenzelm
parents: 4896
diff changeset
   161
Goal "leadsTo racts ((metric-``{m}) - fixedpoint) (metric-``(lessThan m))";
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   162
by (simp_tac (simpset() addsimps [Diff_fixedpoint]) 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   163
by (rtac leadsTo_UN 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   164
by (split_all_tac 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   165
by (asm_simp_tac (simpset() addsimps [edges_ensures RS leadsTo_Basis]) 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   166
qed "leadsTo_Diff_fixedpoint";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   167
5069
3ea049f7979d isatool fixgoal;
wenzelm
parents: 4896
diff changeset
   168
Goal "leadsTo racts (metric-``{m}) (metric-``(lessThan m) Un fixedpoint)";
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   169
by (rtac (leadsTo_Diff_fixedpoint RS leadsTo_weaken_R RS leadsTo_Diff) 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   170
by (ALLGOALS (blast_tac (claset() addIs [subset_imp_leadsTo])));
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   171
qed "leadsTo_Un_fixedpoint";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   172
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   173
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   174
(*Execution in any state leads to a fixedpoint (i.e. can terminate)*)
5069
3ea049f7979d isatool fixgoal;
wenzelm
parents: 4896
diff changeset
   175
Goal "leadsTo racts UNIV fixedpoint";
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   176
by (rtac lessThan_induct 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   177
by Auto_tac;
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   178
by (rtac leadsTo_Un_fixedpoint 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   179
qed "leadsTo_fixedpoint";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   180
5111
8f4b72f0c15d Uncurried functions LeadsTo and reach
paulson
parents: 5071
diff changeset
   181
Goal "LeadsTo(rinit,racts) UNIV { %v. (init, v) : edges^* }";
4776
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   182
by (stac (fixedpoint_invariant_correct RS sym) 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   183
by (rtac (leadsTo_fixedpoint RS leadsTo_imp_LeadsTo RS LeadsTo_weaken_R) 1); 
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   184
by (cut_facts_tac [reachable_subset_invariant] 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   185
by (Blast_tac 1);
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   186
qed "LeadsTo_correct";
1f9362e769c1 New UNITY theory
paulson
parents:
diff changeset
   187