src/HOL/UNITY/Lift.thy
author paulson
Thu Oct 01 18:28:18 1998 +0200 (1998-10-01)
changeset 5596 b29d18d8c4d2
parent 5584 aad639e56d4e
child 5931 325300576da7
permissions -rw-r--r--
abstype of programs
paulson@5277
     1
(*  Title:      HOL/UNITY/Lift.thy
paulson@5277
     2
    ID:         $Id$
paulson@5277
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@5277
     4
    Copyright   1998  University of Cambridge
paulson@5277
     5
paulson@5277
     6
The Lift-Control Example
paulson@5277
     7
*)
paulson@5277
     8
paulson@5277
     9
Lift = SubstAx +
paulson@5277
    10
paulson@5277
    11
record state =
paulson@5563
    12
  floor :: int		(*current position of the lift*)
paulson@5277
    13
  open  :: bool		(*whether the door is open at floor*)
paulson@5277
    14
  stop  :: bool		(*whether the lift is stopped at floor*)
paulson@5563
    15
  req   :: int set	(*for each floor, whether the lift is requested*)
paulson@5277
    16
  up    :: bool		(*current direction of movement*)
paulson@5277
    17
  move  :: bool		(*whether moving takes precedence over opening*)
paulson@5277
    18
paulson@5277
    19
consts
paulson@5563
    20
  Min, Max :: int       (*least and greatest floors*)
paulson@5277
    21
paulson@5277
    22
rules
paulson@5357
    23
  Min_le_Max  "Min <= Max"
paulson@5277
    24
  
paulson@5277
    25
constdefs
paulson@5277
    26
  
paulson@5277
    27
  (** Abbreviations: the "always" part **)
paulson@5277
    28
  
paulson@5320
    29
  above :: state set
paulson@5357
    30
    "above == {s. EX i. floor s < i & i <= Max & i : req s}"
paulson@5277
    31
paulson@5320
    32
  below :: state set
paulson@5357
    33
    "below == {s. EX i. Min <= i & i < floor s & i : req s}"
paulson@5277
    34
paulson@5320
    35
  queueing :: state set
paulson@5277
    36
    "queueing == above Un below"
paulson@5277
    37
paulson@5320
    38
  goingup :: state set
paulson@5340
    39
    "goingup   == above Int  ({s. up s}  Un Compl below)"
paulson@5277
    40
paulson@5320
    41
  goingdown :: state set
paulson@5277
    42
    "goingdown == below Int ({s. ~ up s} Un Compl above)"
paulson@5277
    43
paulson@5320
    44
  ready :: state set
paulson@5277
    45
    "ready == {s. stop s & ~ open s & move s}"
paulson@5277
    46
paulson@5320
    47
 
paulson@5320
    48
  (** Further abbreviations **)
paulson@5277
    49
paulson@5320
    50
  moving :: state set
paulson@5320
    51
    "moving ==  {s. ~ stop s & ~ open s}"
paulson@5320
    52
paulson@5320
    53
  stopped :: state set
paulson@5340
    54
    "stopped == {s. stop s  & ~ open s & ~ move s}"
paulson@5320
    55
paulson@5320
    56
  opened :: state set
paulson@5320
    57
    "opened ==  {s. stop s  &  open s  &  move s}"
paulson@5320
    58
paulson@5340
    59
  closed :: state set  (*but this is the same as ready!!*)
paulson@5320
    60
    "closed ==  {s. stop s  & ~ open s &  move s}"
paulson@5320
    61
paulson@5563
    62
  atFloor :: int => state set
paulson@5320
    63
    "atFloor n ==  {s. floor s = n}"
paulson@5320
    64
paulson@5563
    65
  Req :: int => state set
paulson@5340
    66
    "Req n ==  {s. n : req s}"
paulson@5340
    67
paulson@5320
    68
paulson@5320
    69
  
paulson@5277
    70
  (** The program **)
paulson@5277
    71
  
paulson@5277
    72
  request_act :: "(state*state) set"
paulson@5277
    73
    "request_act == {(s,s'). s' = s (|stop:=True, move:=False|)
paulson@5277
    74
		                  & ~ stop s & floor s : req s}"
paulson@5277
    75
paulson@5277
    76
  open_act :: "(state*state) set"
paulson@5277
    77
    "open_act ==
paulson@5277
    78
         {(s,s'). s' = s (|open :=True,
paulson@5277
    79
			   req  := req s - {floor s},
paulson@5277
    80
			   move := True|)
paulson@5277
    81
		       & stop s & ~ open s & floor s : req s
paulson@5277
    82
	               & ~(move s & s: queueing)}"
paulson@5277
    83
paulson@5277
    84
  close_act :: "(state*state) set"
paulson@5277
    85
    "close_act == {(s,s'). s' = s (|open := False|) & open s}"
paulson@5277
    86
paulson@5357
    87
  req_up :: "(state*state) set"
paulson@5357
    88
    "req_up ==
paulson@5277
    89
         {(s,s'). s' = s (|stop  :=False,
paulson@5563
    90
			   floor := floor s + #1,
paulson@5277
    91
			   up    := True|)
paulson@5277
    92
		       & s : (ready Int goingup)}"
paulson@5277
    93
paulson@5357
    94
  req_down :: "(state*state) set"
paulson@5357
    95
    "req_down ==
paulson@5277
    96
         {(s,s'). s' = s (|stop  :=False,
paulson@5563
    97
			   floor := floor s - #1,
paulson@5277
    98
			   up    := False|)
paulson@5277
    99
		       & s : (ready Int goingdown)}"
paulson@5277
   100
paulson@5277
   101
  move_up :: "(state*state) set"
paulson@5277
   102
    "move_up ==
paulson@5563
   103
         {(s,s'). s' = s (|floor := floor s + #1|)
paulson@5277
   104
		       & ~ stop s & up s & floor s ~: req s}"
paulson@5277
   105
paulson@5277
   106
  move_down :: "(state*state) set"
paulson@5277
   107
    "move_down ==
paulson@5563
   108
         {(s,s'). s' = s (|floor := floor s - #1|)
paulson@5277
   109
		       & ~ stop s & ~ up s & floor s ~: req s}"
paulson@5277
   110
paulson@5357
   111
  button_press  :: "(state*state) set"
paulson@5357
   112
    "button_press ==
paulson@5357
   113
         {(s,s'). EX n. s' = s (|req := insert n (req s)|)
paulson@5357
   114
		        & Min <= n & n <= Max}"
paulson@5357
   115
paulson@5357
   116
paulson@5277
   117
  Lprg :: state program
paulson@5357
   118
    (*for the moment, we OMIT button_press*)
paulson@5596
   119
    "Lprg == mk_program ({s. floor s = Min & ~ up s & move s & stop s &
paulson@5277
   120
		          ~ open s & req s = {}},
paulson@5596
   121
			 {request_act, open_act, close_act,
paulson@5596
   122
			  req_up, req_down, move_up, move_down})"
paulson@5277
   123
paulson@5277
   124
paulson@5277
   125
  (** Invariants **)
paulson@5277
   126
paulson@5277
   127
  bounded :: state set
paulson@5357
   128
    "bounded == {s. Min <= floor s & floor s <= Max}"
paulson@5277
   129
paulson@5277
   130
  open_stop :: state set
paulson@5277
   131
    "open_stop == {s. open s --> stop s}"
paulson@5277
   132
  
paulson@5277
   133
  open_move :: state set
paulson@5277
   134
    "open_move == {s. open s --> move s}"
paulson@5277
   135
  
paulson@5277
   136
  stop_floor :: state set
paulson@5340
   137
    "stop_floor == {s. stop s & ~ move s --> floor s : req s}"
paulson@5277
   138
  
paulson@5277
   139
  moving_up :: state set
paulson@5277
   140
    "moving_up == {s. ~ stop s & up s -->
paulson@5357
   141
                   (EX f. floor s <= f & f <= Max & f : req s)}"
paulson@5277
   142
  
paulson@5277
   143
  moving_down :: state set
paulson@5277
   144
    "moving_down == {s. ~ stop s & ~ up s -->
paulson@5357
   145
                     (EX f. Min <= f & f <= floor s & f : req s)}"
paulson@5277
   146
  
paulson@5563
   147
  metric :: [int,state] => int
paulson@5357
   148
    "metric ==
paulson@5563
   149
       %n s. if floor s < n then (if up s then n - floor s
paulson@5563
   150
			          else (floor s - Min) + (n-Min))
paulson@5563
   151
             else
paulson@5563
   152
             if n < floor s then (if up s then (Max - floor s) + (Max-n)
paulson@5563
   153
		                  else floor s - n)
paulson@5563
   154
             else #0"
paulson@5340
   155
paulson@5340
   156
locale floor =
paulson@5340
   157
  fixes 
paulson@5563
   158
    n	:: int
paulson@5340
   159
  assumes
paulson@5357
   160
    Min_le_n    "Min <= n"
paulson@5357
   161
    n_le_Max    "n <= Max"
paulson@5340
   162
  defines
paulson@5277
   163
paulson@5277
   164
end