src/HOL/IOA/ABP/Check.ML
author lcp
Tue, 25 Apr 1995 11:14:03 +0200
changeset 1072 0140ff702b23
parent 1050 0c36c6a52a1d
child 1138 82fd99d5a6ff
permissions -rw-r--r--
updated version
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1050
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
     1
(*  Title:      HOL/IOA/ABP/Check.ML
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
     2
    ID:         $Id$
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
     3
    Author:     Tobias Nipkow & Olaf Mueller
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
     4
    Copyright   1995  TU Muenchen
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
     5
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
     6
Simple ModelChecker prototype checking Spec against 
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
     7
the finite version of the ABP-protocol.
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
     8
*)
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
     9
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    10
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    11
(* model checker function prototype *)
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    12
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    13
fun check(extacts,intacts,string_of_a,startsI,string_of_s,
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    14
          nexts,hom,transA,startsS) =
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    15
  let fun check_s(s,unchecked,checked) =
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    16
        let fun check_sa(unchecked,a) =
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    17
              let fun check_sas(unchecked,t) =
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    18
                    (if a mem extacts then
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    19
                          (if transA(hom s,a,hom t) then ()
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    20
                           else (writeln("Error: Mapping of Externals!");
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    21
                                 string_of_s s; writeln"";
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    22
                                 string_of_a a; writeln"";
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    23
                                 string_of_s t;writeln"";writeln""))
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    24
                     else (if hom(s)=hom(t) then ()
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    25
                           else (writeln("Error: Mapping of Internals!");
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    26
                                 string_of_s s;writeln"";
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    27
                                 string_of_a a;writeln"";
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    28
                                 string_of_s t;writeln"";writeln""));
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    29
                     if t mem checked then unchecked else t ins unchecked)
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    30
              in foldl check_sas (unchecked,nexts s a) end;
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    31
              val unchecked' = foldl check_sa (unchecked,extacts @ intacts)
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    32
        in    (if s mem startsI then 
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    33
                    (if hom(s) mem startsS then ()
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    34
                     else writeln("Error: At start states!"))
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    35
               else ();  
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    36
               checks(unchecked',s::checked)) end
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    37
      and checks([],_) = ()
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    38
        | checks(s::unchecked,checked) = check_s(s,unchecked,checked)
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    39
  in checks(startsI,[]) end;
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    40
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    41
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    42
(* datatypes for ABP example *)
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    43
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    44
datatype msg = m;
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    45
datatype act = Next | S_msg of msg | R_msg of msg
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    46
                    | S_pkt of bool * msg | R_pkt of bool * msg
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    47
                    | S_ack of bool | R_ack of bool;
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    48
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    49
fun transA((u,s),a,(v,t)) = 
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    50
    (case a of 
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    51
       Next       => v andalso t = s |                         
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    52
       S_msg(m)   => u andalso not(v) andalso t = s@[m]   |    
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    53
       R_msg(m)   => u = v andalso s = (m::t)  |                    
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    54
       S_pkt(b,m) => false |                    
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    55
       R_pkt(b,m) => false |                    
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    56
       S_ack(b)   => false |                      
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    57
       R_ack(b)   => false);
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    58
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    59
fun hom((env,p,a,q,b,_,_)) = (env,q@(if (a=b) then tl(p) else p));
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    60
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    61
fun nexts (s as (env,p,a,q,b,ch1,ch2)) action =
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    62
    (case action of
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    63
       Next       => if p=[] then [(true,p,a,q,b,ch1,ch2)] else [] |                         
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    64
       S_msg(m)   => if env then [(false,p@[m],a,q,b,ch1,ch2)] else [] |     
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    65
       R_msg(m)   => if (q<>[] andalso m=hd(q)) 
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    66
                        then [(env,p,a,tl(q),b,ch1,ch2)]
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    67
                        else [] |                    
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    68
       S_pkt(h,m) => if (p<>[] andalso m=hd(p) andalso h=a)
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    69
                        then (if (ch1<>[] andalso hd(rev(ch1))=(h,m))
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    70
                              then [s]
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    71
                              else [s,(env,p,a,q,b,ch1@[(h,m)],ch2)])
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    72
                        else [] |
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    73
       R_pkt(h,m) => if (ch1<>[] andalso hd(ch1)=(h,m))
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    74
                         then (if (h<>b andalso q=[])
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    75
                               then [(env,p,a,q@[m],not(b),ch1,ch2),
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    76
                                     (env,p,a,q@[m],not(b),tl(ch1),ch2)]
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    77
                               else [s,(env,p,a,q,b,tl(ch1),ch2)])
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    78
                          else [] | 
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    79
       S_ack(h)   => if (h=b)
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    80
                        then (if (ch2<>[] andalso h=hd(rev(ch2))) 
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    81
                              then [s]
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    82
                              else [s,(env,p,a,q,b,ch1,ch2@[h])])
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    83
                        else []  |                      
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    84
       R_ack(h)   => if (ch2<>[] andalso hd(ch2)=h)
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    85
                        then (if h=a
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    86
                              then [(env,tl(p),not(a),q,b,ch1,ch2),
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    87
                                    (env,tl(p),not(a),q,b,ch1,tl(ch2))]
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    88
                              else [s,(env,p,a,q,b,ch1,tl(ch2))]) 
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    89
                         else [])
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    90
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    91
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    92
val extactions = [Next,S_msg(m),R_msg(m)];
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    93
val intactions = [S_pkt(true,m),R_pkt(true,m),S_ack(true),R_ack(true),
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    94
                  S_pkt(false,m),R_pkt(false,m),S_ack(false),R_ack(false)];
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    95
 
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    96
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    97
(* Input / Output auxiliary functions *)
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    98
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
    99
fun print_list (lpar, rpar, pre: 'a -> unit) (l : 'a list) =
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   100
  let fun prec x = (prs ","; pre x)
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   101
  in
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   102
    (case l of
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   103
      [] => (prs lpar; prs rpar)
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   104
    | x::l => (prs lpar; pre x; seq prec l; prs rpar))
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   105
   end;
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   106
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   107
fun pr_bool true = output(std_out,"true")
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   108
|   pr_bool false = output(std_out,"false");
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   109
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   110
fun pr_msg m = output(std_out,"m");
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   111
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   112
fun pr_act a = output(std_out, case a of
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   113
      Next => "Next"|                         
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   114
      S_msg(m) => "S_msg(m)"  |
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   115
      R_msg(m) => "R_msg(m)"  |
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   116
      S_pkt(b,m) => "S_pkt(b,m)" |                    
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   117
      R_pkt(b,m) => "R_pkt(b,m)" |                    
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   118
      S_ack(b)   => "S_ack(b)" |                      
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   119
      R_ack(b)   => "R_ack(b)");
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   120
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   121
fun pr_pkt (b,m) = (prs "<"; pr_bool b;prs ", "; pr_msg m; prs ">");
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   122
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   123
val pr_bool_list  = print_list("[","]",pr_bool);
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   124
val pr_msg_list   = print_list("[","]",pr_msg);
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   125
val pr_pkt_list   = print_list("[","]",pr_pkt);
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   126
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   127
fun pr_tuple (env,p,a,q,b,ch1,ch2) = 
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   128
        (prs "{"; pr_bool env; prs ", "; pr_msg_list p;  prs ", ";
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   129
         pr_bool a;  prs ", "; pr_msg_list q; prs ", ";
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   130
         pr_bool b;  prs ", "; pr_pkt_list ch1;  prs ", ";
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   131
         pr_bool_list ch2; prs "}");
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   132
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   133
(* ----------------------------*)
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   134
(* main check function call    *)
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   135
(* ----------------------------*)
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   136
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   137
check(extactions,intactions,pr_act, [(true,[],true,[],false,[],[])], 
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   138
      pr_tuple, nexts, hom, transA, [(true,[])]);
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   139
0c36c6a52a1d ABP: Alternating bit protocol example
nipkow
parents:
diff changeset
   140