src/HOL/ex/MT.thy
author paulson
Fri Jan 21 13:54:09 2005 +0100 (2005-01-21)
changeset 15450 43dfc914d1b8
parent 12338 de0f4a63baa5
child 17289 8608f7a881eb
permissions -rw-r--r--
inserted quotes preparatory to conversion
clasohm@1476
     1
(*  Title:      HOL/ex/mt.thy
clasohm@969
     2
    ID:         $Id$
clasohm@1476
     3
    Author:     Jacob Frost, Cambridge University Computer Laboratory
clasohm@969
     4
    Copyright   1993  University of Cambridge
clasohm@969
     5
clasohm@969
     6
Based upon the article
clasohm@969
     7
    Robin Milner and Mads Tofte,
clasohm@969
     8
    Co-induction in Relational Semantics,
clasohm@969
     9
    Theoretical Computer Science 87 (1991), pages 209-220.
clasohm@969
    10
clasohm@969
    11
Written up as
clasohm@969
    12
    Jacob Frost, A Case Study of Co_induction in Isabelle/HOL
clasohm@969
    13
    Report 308, Computer Lab, University of Cambridge (1993).
clasohm@969
    14
*)
clasohm@969
    15
berghofe@5102
    16
MT = Inductive + 
clasohm@969
    17
clasohm@969
    18
types 
clasohm@969
    19
  Const
clasohm@969
    20
clasohm@969
    21
  ExVar
clasohm@969
    22
  Ex
clasohm@969
    23
clasohm@969
    24
  TyConst
clasohm@969
    25
  Ty
clasohm@969
    26
clasohm@969
    27
  Clos
clasohm@969
    28
  Val
clasohm@969
    29
clasohm@969
    30
  ValEnv
clasohm@969
    31
  TyEnv
clasohm@969
    32
clasohm@969
    33
arities 
wenzelm@12338
    34
  Const :: type
clasohm@969
    35
wenzelm@12338
    36
  ExVar :: type
wenzelm@12338
    37
  Ex :: type
clasohm@969
    38
wenzelm@12338
    39
  TyConst :: type
wenzelm@12338
    40
  Ty :: type
clasohm@969
    41
wenzelm@12338
    42
  Clos :: type
wenzelm@12338
    43
  Val :: type
clasohm@969
    44
wenzelm@12338
    45
  ValEnv :: type
wenzelm@12338
    46
  TyEnv :: type
clasohm@969
    47
clasohm@969
    48
consts
paulson@15450
    49
  c_app :: "[Const, Const] => Const"
clasohm@969
    50
paulson@15450
    51
  e_const :: "Const => Ex"
paulson@15450
    52
  e_var :: "ExVar => Ex"
paulson@15450
    53
  e_fn :: "[ExVar, Ex] => Ex" ("fn _ => _" [0,51] 1000)
paulson@15450
    54
  e_fix :: "[ExVar, ExVar, Ex] => Ex" ("fix _ ( _ ) = _" [0,51,51] 1000)
paulson@15450
    55
  e_app :: "[Ex, Ex] => Ex" ("_ @ _" [51,51] 1000)
paulson@15450
    56
  e_const_fst :: "Ex => Const"
clasohm@969
    57
paulson@15450
    58
  t_const :: "TyConst => Ty"
paulson@15450
    59
  t_fun :: "[Ty, Ty] => Ty" ("_ -> _" [51,51] 1000)
clasohm@969
    60
paulson@15450
    61
  v_const :: "Const => Val"
paulson@15450
    62
  v_clos :: "Clos => Val"
clasohm@969
    63
  
clasohm@1376
    64
  ve_emp :: ValEnv
paulson@15450
    65
  ve_owr :: "[ValEnv, ExVar, Val] => ValEnv" ("_ + { _ |-> _ }" [36,0,0] 50)
paulson@15450
    66
  ve_dom :: "ValEnv => ExVar set"
paulson@15450
    67
  ve_app :: "[ValEnv, ExVar] => Val"
clasohm@969
    68
paulson@15450
    69
  clos_mk :: "[ExVar, Ex, ValEnv] => Clos" ("<| _ , _ , _ |>" [0,0,0] 1000)
clasohm@969
    70
clasohm@1376
    71
  te_emp :: TyEnv
paulson@15450
    72
  te_owr :: "[TyEnv, ExVar, Ty] => TyEnv" ("_ + { _ |=> _ }" [36,0,0] 50)
paulson@15450
    73
  te_app :: "[TyEnv, ExVar] => Ty"
paulson@15450
    74
  te_dom :: "TyEnv => ExVar set"
clasohm@969
    75
clasohm@969
    76
  eval_fun :: "((ValEnv * Ex) * Val) set => ((ValEnv * Ex) * Val) set"
clasohm@969
    77
  eval_rel :: "((ValEnv * Ex) * Val) set"
paulson@15450
    78
  eval :: "[ValEnv, Ex, Val] => bool" ("_ |- _ ---> _" [36,0,36] 50)
clasohm@969
    79
clasohm@969
    80
  elab_fun :: "((TyEnv * Ex) * Ty) set => ((TyEnv * Ex) * Ty) set"
clasohm@969
    81
  elab_rel :: "((TyEnv * Ex) * Ty) set"
paulson@15450
    82
  elab :: "[TyEnv, Ex, Ty] => bool" ("_ |- _ ===> _" [36,0,36] 50)
clasohm@969
    83
  
paulson@15450
    84
  isof :: "[Const, Ty] => bool" ("_ isof _" [36,36] 50)
paulson@15450
    85
  isof_env :: "[ValEnv,TyEnv] => bool" ("_ isofenv _")
clasohm@969
    86
clasohm@969
    87
  hasty_fun :: "(Val * Ty) set => (Val * Ty) set"
clasohm@969
    88
  hasty_rel :: "(Val * Ty) set"
paulson@15450
    89
  hasty :: "[Val, Ty] => bool" ("_ hasty _" [36,36] 50)
paulson@15450
    90
  hasty_env :: "[ValEnv,TyEnv] => bool" ("_ hastyenv _ " [36,36] 35)
clasohm@969
    91
clasohm@969
    92
rules
clasohm@969
    93
clasohm@969
    94
(* 
clasohm@969
    95
  Expression constructors must be injective, distinct and it must be possible
clasohm@969
    96
  to do induction over expressions.
clasohm@969
    97
*)
clasohm@969
    98
clasohm@969
    99
(* All the constructors are injective *)
clasohm@969
   100
clasohm@969
   101
  e_const_inj "e_const(c1) = e_const(c2) ==> c1 = c2"
clasohm@969
   102
  e_var_inj "e_var(ev1) = e_var(ev2) ==> ev1 = ev2"
clasohm@969
   103
  e_fn_inj "fn ev1 => e1 = fn ev2 => e2 ==> ev1 = ev2 & e1 = e2"
clasohm@969
   104
  e_fix_inj 
clasohm@1151
   105
    " fix ev11e(v12) = e1 = fix ev21(ev22) = e2 ==> 
clasohm@1151
   106
     ev11 = ev21 & ev12 = ev22 & e1 = e2 
clasohm@1151
   107
   "
clasohm@969
   108
  e_app_inj "e11 @ e12 = e21 @ e22 ==> e11 = e21 & e12 = e22"
clasohm@969
   109
clasohm@969
   110
(* All constructors are distinct *)
clasohm@969
   111
clasohm@969
   112
  e_disj_const_var "~e_const(c) = e_var(ev)"
clasohm@969
   113
  e_disj_const_fn "~e_const(c) = fn ev => e"
clasohm@969
   114
  e_disj_const_fix "~e_const(c) = fix ev1(ev2) = e"
clasohm@969
   115
  e_disj_const_app "~e_const(c) = e1 @ e2"
clasohm@969
   116
  e_disj_var_fn "~e_var(ev1) = fn ev2 => e"
clasohm@969
   117
  e_disj_var_fix "~e_var(ev) = fix ev1(ev2) = e"
clasohm@969
   118
  e_disj_var_app "~e_var(ev) = e1 @ e2"
clasohm@969
   119
  e_disj_fn_fix "~fn ev1 => e1 = fix ev21(ev22) = e2"
clasohm@969
   120
  e_disj_fn_app "~fn ev1 => e1 = e21 @ e22"
clasohm@969
   121
  e_disj_fix_app "~fix ev11(ev12) = e1 = e21 @ e22"
clasohm@969
   122
clasohm@969
   123
(* Strong elimination, induction on expressions  *)
clasohm@969
   124
clasohm@969
   125
  e_ind 
clasohm@1151
   126
    " [|  !!ev. P(e_var(ev)); 
clasohm@1151
   127
         !!c. P(e_const(c)); 
clasohm@1151
   128
         !!ev e. P(e) ==> P(fn ev => e); 
clasohm@1151
   129
         !!ev1 ev2 e. P(e) ==> P(fix ev1(ev2) = e); 
clasohm@1151
   130
         !!e1 e2. P(e1) ==> P(e2) ==> P(e1 @ e2) 
clasohm@1151
   131
     |] ==> 
clasohm@1151
   132
   P(e) 
clasohm@1151
   133
   "
clasohm@969
   134
clasohm@969
   135
(* Types - same scheme as for expressions *)
clasohm@969
   136
clasohm@969
   137
(* All constructors are injective *) 
clasohm@969
   138
clasohm@969
   139
  t_const_inj "t_const(c1) = t_const(c2) ==> c1 = c2"
clasohm@969
   140
  t_fun_inj "t11 -> t12 = t21 -> t22 ==> t11 = t21 & t12 = t22"
clasohm@969
   141
clasohm@969
   142
(* All constructors are distinct, not needed so far ... *)
clasohm@969
   143
clasohm@969
   144
(* Strong elimination, induction on types *)
clasohm@969
   145
clasohm@969
   146
 t_ind 
clasohm@1151
   147
    "[| !!p. P(t_const p); !!t1 t2. P(t1) ==> P(t2) ==> P(t_fun t1 t2) |] 
clasohm@1151
   148
    ==> P(t)"
clasohm@969
   149
clasohm@969
   150
clasohm@969
   151
(* Values - same scheme again *)
clasohm@969
   152
clasohm@969
   153
(* All constructors are injective *) 
clasohm@969
   154
clasohm@969
   155
  v_const_inj "v_const(c1) = v_const(c2) ==> c1 = c2"
clasohm@969
   156
  v_clos_inj 
clasohm@1151
   157
    " v_clos(<|ev1,e1,ve1|>) = v_clos(<|ev2,e2,ve2|>) ==> 
clasohm@1151
   158
     ev1 = ev2 & e1 = e2 & ve1 = ve2"
clasohm@969
   159
  
clasohm@969
   160
(* All constructors are distinct *)
clasohm@969
   161
clasohm@969
   162
  v_disj_const_clos "~v_const(c) = v_clos(cl)"
clasohm@969
   163
paulson@15450
   164
(* No induction on values: they are a codatatype! ... *)
clasohm@969
   165
clasohm@969
   166
clasohm@969
   167
(* 
clasohm@969
   168
  Value environments bind variables to values. Only the following trivial
clasohm@969
   169
  properties are needed.
clasohm@969
   170
*)
clasohm@969
   171
clasohm@969
   172
  ve_dom_owr "ve_dom(ve + {ev |-> v}) = ve_dom(ve) Un {ev}"
clasohm@969
   173
 
clasohm@969
   174
  ve_app_owr1 "ve_app (ve + {ev |-> v}) ev=v"
clasohm@969
   175
  ve_app_owr2 "~ev1=ev2 ==> ve_app (ve+{ev1 |-> v}) ev2=ve_app ve ev2"
clasohm@969
   176
clasohm@969
   177
clasohm@969
   178
(* Type Environments bind variables to types. The following trivial
clasohm@969
   179
properties are needed.  *)
clasohm@969
   180
clasohm@969
   181
  te_dom_owr "te_dom(te + {ev |=> t}) = te_dom(te) Un {ev}"
clasohm@969
   182
 
clasohm@969
   183
  te_app_owr1 "te_app (te + {ev |=> t}) ev=t"
clasohm@969
   184
  te_app_owr2 "~ev1=ev2 ==> te_app (te+{ev1 |=> t}) ev2=te_app te ev2"
clasohm@969
   185
clasohm@969
   186
clasohm@969
   187
(* The dynamic semantics is defined inductively by a set of inference
clasohm@969
   188
rules.  These inference rules allows one to draw conclusions of the form ve
clasohm@969
   189
|- e ---> v, read the expression e evaluates to the value v in the value
clasohm@969
   190
environment ve.  Therefore the relation _ |- _ ---> _ is defined in Isabelle
clasohm@969
   191
as the least fixpoint of the functor eval_fun below.  From this definition
clasohm@969
   192
introduction rules and a strong elimination (induction) rule can be
clasohm@969
   193
derived.  
clasohm@969
   194
*)
clasohm@969
   195
clasohm@969
   196
  eval_fun_def 
clasohm@1151
   197
    " eval_fun(s) == 
clasohm@1151
   198
     { pp. 
clasohm@1151
   199
       (? ve c. pp=((ve,e_const(c)),v_const(c))) | 
clasohm@1151
   200
       (? ve x. pp=((ve,e_var(x)),ve_app ve x) & x:ve_dom(ve)) |
clasohm@1151
   201
       (? ve e x. pp=((ve,fn x => e),v_clos(<|x,e,ve|>)))| 
clasohm@1151
   202
       ( ? ve e x f cl. 
clasohm@1151
   203
           pp=((ve,fix f(x) = e),v_clos(cl)) & 
clasohm@1151
   204
           cl=<|x, e, ve+{f |-> v_clos(cl)} |>  
clasohm@1151
   205
       ) | 
clasohm@1151
   206
       ( ? ve e1 e2 c1 c2. 
clasohm@1151
   207
           pp=((ve,e1 @ e2),v_const(c_app c1 c2)) & 
clasohm@1151
   208
           ((ve,e1),v_const(c1)):s & ((ve,e2),v_const(c2)):s 
clasohm@1151
   209
       ) | 
clasohm@1151
   210
       ( ? ve vem e1 e2 em xm v v2. 
clasohm@1151
   211
           pp=((ve,e1 @ e2),v) & 
clasohm@1151
   212
           ((ve,e1),v_clos(<|xm,em,vem|>)):s & 
clasohm@1151
   213
           ((ve,e2),v2):s & 
clasohm@1151
   214
           ((vem+{xm |-> v2},em),v):s 
clasohm@1151
   215
       ) 
clasohm@1151
   216
     }"
clasohm@969
   217
clasohm@969
   218
  eval_rel_def "eval_rel == lfp(eval_fun)"
clasohm@972
   219
  eval_def "ve |- e ---> v == ((ve,e),v):eval_rel"
clasohm@969
   220
clasohm@969
   221
(* The static semantics is defined in the same way as the dynamic
clasohm@969
   222
semantics.  The relation te |- e ===> t express the expression e has the
clasohm@969
   223
type t in the type environment te.
clasohm@969
   224
*)
clasohm@969
   225
clasohm@969
   226
  elab_fun_def 
clasohm@1151
   227
  "elab_fun(s) == 
clasohm@1151
   228
  { pp. 
clasohm@1151
   229
    (? te c t. pp=((te,e_const(c)),t) & c isof t) | 
clasohm@1151
   230
    (? te x. pp=((te,e_var(x)),te_app te x) & x:te_dom(te)) | 
clasohm@1151
   231
    (? te x e t1 t2. pp=((te,fn x => e),t1->t2) & ((te+{x |=> t1},e),t2):s) | 
clasohm@1151
   232
    (? te f x e t1 t2. 
clasohm@1151
   233
       pp=((te,fix f(x)=e),t1->t2) & ((te+{f |=> t1->t2}+{x |=> t1},e),t2):s 
clasohm@1151
   234
    ) | 
clasohm@1151
   235
    (? te e1 e2 t1 t2. 
clasohm@1151
   236
       pp=((te,e1 @ e2),t2) & ((te,e1),t1->t2):s & ((te,e2),t1):s 
clasohm@1151
   237
    ) 
clasohm@1151
   238
  }"
clasohm@969
   239
clasohm@969
   240
  elab_rel_def "elab_rel == lfp(elab_fun)"
clasohm@972
   241
  elab_def "te |- e ===> t == ((te,e),t):elab_rel"
clasohm@969
   242
clasohm@969
   243
(* The original correspondence relation *)
clasohm@969
   244
clasohm@969
   245
  isof_env_def 
clasohm@1151
   246
    " ve isofenv te == 
clasohm@1151
   247
     ve_dom(ve) = te_dom(te) & 
clasohm@1151
   248
     ( ! x. 
clasohm@1151
   249
         x:ve_dom(ve) --> 
wenzelm@3842
   250
         (? c. ve_app ve x = v_const(c) & c isof te_app te x) 
clasohm@1151
   251
     ) 
clasohm@1151
   252
   "
clasohm@969
   253
clasohm@969
   254
  isof_app "[| c1 isof t1->t2; c2 isof t1 |] ==> c_app c1 c2 isof t2"
clasohm@969
   255
clasohm@969
   256
(* The extented correspondence relation *)
clasohm@969
   257
clasohm@969
   258
  hasty_fun_def
clasohm@1151
   259
    " hasty_fun(r) == 
clasohm@1151
   260
     { p. 
clasohm@1151
   261
       ( ? c t. p = (v_const(c),t) & c isof t) | 
clasohm@1151
   262
       ( ? ev e ve t te. 
clasohm@1151
   263
           p = (v_clos(<|ev,e,ve|>),t) & 
clasohm@1151
   264
           te |- fn ev => e ===> t & 
clasohm@1151
   265
           ve_dom(ve) = te_dom(te) & 
wenzelm@3842
   266
           (! ev1. ev1:ve_dom(ve) --> (ve_app ve ev1,te_app te ev1) : r) 
clasohm@1151
   267
       ) 
clasohm@1151
   268
     } 
clasohm@1151
   269
   "
clasohm@969
   270
clasohm@969
   271
  hasty_rel_def "hasty_rel == gfp(hasty_fun)"
clasohm@972
   272
  hasty_def "v hasty t == (v,t) : hasty_rel"
clasohm@969
   273
  hasty_env_def 
clasohm@1151
   274
    " ve hastyenv te == 
clasohm@1151
   275
     ve_dom(ve) = te_dom(te) & 
clasohm@1151
   276
     (! x. x: ve_dom(ve) --> ve_app ve x hasty te_app te x)"
clasohm@969
   277
clasohm@969
   278
end