src/HOL/HOLCF/Fixrec.thy
author kuncar
Fri Dec 09 18:07:04 2011 +0100 (2011-12-09)
changeset 45802 b16f976db515
parent 42151 4da4fc77664b
child 46950 d0181abdbdac
permissions -rw-r--r--
Quotient_Info stores only relation maps
wenzelm@42151
     1
(*  Title:      HOL/HOLCF/Fixrec.thy
huffman@16221
     2
    Author:     Amber Telfer and Brian Huffman
huffman@16221
     3
*)
huffman@16221
     4
huffman@16221
     5
header "Package for defining recursive functions in HOLCF"
huffman@16221
     6
huffman@16221
     7
theory Fixrec
huffman@40502
     8
imports Plain_HOLCF
huffman@35527
     9
uses
huffman@35527
    10
  ("Tools/holcf_library.ML")
huffman@35527
    11
  ("Tools/fixrec.ML")
huffman@16221
    12
begin
huffman@16221
    13
huffman@37108
    14
subsection {* Pattern-match monad *}
huffman@16221
    15
wenzelm@36452
    16
default_sort cpo
huffman@16776
    17
huffman@37108
    18
pcpodef (open) 'a match = "UNIV::(one ++ 'a u) set"
wenzelm@29063
    19
by simp_all
huffman@16221
    20
huffman@29141
    21
definition
huffman@37108
    22
  fail :: "'a match" where
huffman@37108
    23
  "fail = Abs_match (sinl\<cdot>ONE)"
huffman@16221
    24
huffman@29141
    25
definition
huffman@37108
    26
  succeed :: "'a \<rightarrow> 'a match" where
huffman@37108
    27
  "succeed = (\<Lambda> x. Abs_match (sinr\<cdot>(up\<cdot>x)))"
huffman@19092
    28
huffman@37108
    29
lemma matchE [case_names bottom fail succeed, cases type: match]:
huffman@37108
    30
  "\<lbrakk>p = \<bottom> \<Longrightarrow> Q; p = fail \<Longrightarrow> Q; \<And>x. p = succeed\<cdot>x \<Longrightarrow> Q\<rbrakk> \<Longrightarrow> Q"
huffman@37108
    31
unfolding fail_def succeed_def
huffman@19092
    32
apply (cases p, rename_tac r)
huffman@37108
    33
apply (rule_tac p=r in ssumE, simp add: Abs_match_strict)
huffman@16221
    34
apply (rule_tac p=x in oneE, simp, simp)
huffman@37108
    35
apply (rule_tac p=y in upE, simp, simp add: cont_Abs_match)
huffman@16221
    36
done
huffman@16221
    37
huffman@37108
    38
lemma succeed_defined [simp]: "succeed\<cdot>x \<noteq> \<bottom>"
huffman@41029
    39
by (simp add: succeed_def cont_Abs_match Abs_match_bottom_iff)
huffman@18293
    40
huffman@18293
    41
lemma fail_defined [simp]: "fail \<noteq> \<bottom>"
huffman@41029
    42
by (simp add: fail_def Abs_match_bottom_iff)
huffman@18293
    43
huffman@37108
    44
lemma succeed_eq [simp]: "(succeed\<cdot>x = succeed\<cdot>y) = (x = y)"
huffman@37108
    45
by (simp add: succeed_def cont_Abs_match Abs_match_inject)
huffman@18293
    46
huffman@37108
    47
lemma succeed_neq_fail [simp]:
huffman@37108
    48
  "succeed\<cdot>x \<noteq> fail" "fail \<noteq> succeed\<cdot>x"
huffman@37108
    49
by (simp_all add: succeed_def fail_def cont_Abs_match Abs_match_inject)
huffman@19092
    50
huffman@18097
    51
subsubsection {* Run operator *}
huffman@16221
    52
wenzelm@25131
    53
definition
huffman@37108
    54
  run :: "'a match \<rightarrow> 'a::pcpo" where
huffman@40735
    55
  "run = (\<Lambda> m. sscase\<cdot>\<bottom>\<cdot>(fup\<cdot>ID)\<cdot>(Rep_match m))"
huffman@16221
    56
huffman@16221
    57
text {* rewrite rules for run *}
huffman@16221
    58
huffman@16221
    59
lemma run_strict [simp]: "run\<cdot>\<bottom> = \<bottom>"
huffman@40735
    60
unfolding run_def
huffman@40735
    61
by (simp add: cont_Rep_match Rep_match_strict)
huffman@16221
    62
huffman@16221
    63
lemma run_fail [simp]: "run\<cdot>fail = \<bottom>"
huffman@40735
    64
unfolding run_def fail_def
huffman@40735
    65
by (simp add: cont_Rep_match Abs_match_inverse)
huffman@16221
    66
huffman@37108
    67
lemma run_succeed [simp]: "run\<cdot>(succeed\<cdot>x) = x"
huffman@40735
    68
unfolding run_def succeed_def
huffman@40735
    69
by (simp add: cont_Rep_match cont_Abs_match Abs_match_inverse)
huffman@16221
    70
huffman@18097
    71
subsubsection {* Monad plus operator *}
huffman@16221
    72
wenzelm@25131
    73
definition
huffman@37108
    74
  mplus :: "'a match \<rightarrow> 'a match \<rightarrow> 'a match" where
huffman@40735
    75
  "mplus = (\<Lambda> m1 m2. sscase\<cdot>(\<Lambda> _. m2)\<cdot>(\<Lambda> _. m1)\<cdot>(Rep_match m1))"
huffman@18097
    76
wenzelm@25131
    77
abbreviation
huffman@37108
    78
  mplus_syn :: "['a match, 'a match] \<Rightarrow> 'a match"  (infixr "+++" 65)  where
wenzelm@25131
    79
  "m1 +++ m2 == mplus\<cdot>m1\<cdot>m2"
huffman@16221
    80
huffman@16221
    81
text {* rewrite rules for mplus *}
huffman@16221
    82
huffman@16221
    83
lemma mplus_strict [simp]: "\<bottom> +++ m = \<bottom>"
huffman@40735
    84
unfolding mplus_def
huffman@40834
    85
by (simp add: cont_Rep_match Rep_match_strict)
huffman@16221
    86
huffman@16221
    87
lemma mplus_fail [simp]: "fail +++ m = m"
huffman@40735
    88
unfolding mplus_def fail_def
huffman@40834
    89
by (simp add: cont_Rep_match Abs_match_inverse)
huffman@16221
    90
huffman@37108
    91
lemma mplus_succeed [simp]: "succeed\<cdot>x +++ m = succeed\<cdot>x"
huffman@40735
    92
unfolding mplus_def succeed_def
huffman@40834
    93
by (simp add: cont_Rep_match cont_Abs_match Abs_match_inverse)
huffman@16221
    94
huffman@16460
    95
lemma mplus_fail2 [simp]: "m +++ fail = m"
huffman@37108
    96
by (cases m, simp_all)
huffman@16460
    97
huffman@16221
    98
lemma mplus_assoc: "(x +++ y) +++ z = x +++ (y +++ z)"
huffman@37108
    99
by (cases x, simp_all)
huffman@16221
   100
huffman@16221
   101
subsection {* Match functions for built-in types *}
huffman@16221
   102
wenzelm@36452
   103
default_sort pcpo
huffman@16776
   104
wenzelm@25131
   105
definition
huffman@40768
   106
  match_bottom :: "'a \<rightarrow> 'c match \<rightarrow> 'c match"
huffman@30912
   107
where
huffman@40768
   108
  "match_bottom = (\<Lambda> x k. seq\<cdot>x\<cdot>fail)"
wenzelm@25131
   109
wenzelm@25131
   110
definition
huffman@39807
   111
  match_Pair :: "'a::cpo \<times> 'b::cpo \<rightarrow> ('a \<rightarrow> 'b \<rightarrow> 'c match) \<rightarrow> 'c match"
huffman@30912
   112
where
huffman@39807
   113
  "match_Pair = (\<Lambda> x k. csplit\<cdot>k\<cdot>x)"
huffman@16776
   114
wenzelm@25131
   115
definition
huffman@37108
   116
  match_spair :: "'a \<otimes> 'b \<rightarrow> ('a \<rightarrow> 'b \<rightarrow> 'c match) \<rightarrow> 'c match"
huffman@30912
   117
where
huffman@30912
   118
  "match_spair = (\<Lambda> x k. ssplit\<cdot>k\<cdot>x)"
huffman@16221
   119
wenzelm@25131
   120
definition
huffman@37108
   121
  match_sinl :: "'a \<oplus> 'b \<rightarrow> ('a \<rightarrow> 'c match) \<rightarrow> 'c match"
huffman@30912
   122
where
huffman@30912
   123
  "match_sinl = (\<Lambda> x k. sscase\<cdot>k\<cdot>(\<Lambda> b. fail)\<cdot>x)"
huffman@16551
   124
wenzelm@25131
   125
definition
huffman@37108
   126
  match_sinr :: "'a \<oplus> 'b \<rightarrow> ('b \<rightarrow> 'c match) \<rightarrow> 'c match"
huffman@30912
   127
where
huffman@30912
   128
  "match_sinr = (\<Lambda> x k. sscase\<cdot>(\<Lambda> a. fail)\<cdot>k\<cdot>x)"
huffman@16551
   129
wenzelm@25131
   130
definition
huffman@37108
   131
  match_up :: "'a::cpo u \<rightarrow> ('a \<rightarrow> 'c match) \<rightarrow> 'c match"
huffman@30912
   132
where
huffman@30912
   133
  "match_up = (\<Lambda> x k. fup\<cdot>k\<cdot>x)"
huffman@16221
   134
wenzelm@25131
   135
definition
huffman@37108
   136
  match_ONE :: "one \<rightarrow> 'c match \<rightarrow> 'c match"
huffman@30912
   137
where
huffman@30912
   138
  "match_ONE = (\<Lambda> ONE k. k)"
huffman@30912
   139
huffman@30912
   140
definition
huffman@37108
   141
  match_TT :: "tr \<rightarrow> 'c match \<rightarrow> 'c match"
huffman@30912
   142
where
huffman@40322
   143
  "match_TT = (\<Lambda> x k. If x then k else fail)"
huffman@18094
   144
 
wenzelm@25131
   145
definition
huffman@37108
   146
  match_FF :: "tr \<rightarrow> 'c match \<rightarrow> 'c match"
huffman@30912
   147
where
huffman@40322
   148
  "match_FF = (\<Lambda> x k. If x then fail else k)"
huffman@16460
   149
huffman@40768
   150
lemma match_bottom_simps [simp]:
huffman@40795
   151
  "match_bottom\<cdot>x\<cdot>k = (if x = \<bottom> then \<bottom> else fail)"
huffman@40795
   152
by (simp add: match_bottom_def)
huffman@16776
   153
huffman@39807
   154
lemma match_Pair_simps [simp]:
huffman@39807
   155
  "match_Pair\<cdot>(x, y)\<cdot>k = k\<cdot>x\<cdot>y"
huffman@39807
   156
by (simp_all add: match_Pair_def)
huffman@16221
   157
huffman@16551
   158
lemma match_spair_simps [simp]:
huffman@30912
   159
  "\<lbrakk>x \<noteq> \<bottom>; y \<noteq> \<bottom>\<rbrakk> \<Longrightarrow> match_spair\<cdot>(:x, y:)\<cdot>k = k\<cdot>x\<cdot>y"
huffman@30912
   160
  "match_spair\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@16551
   161
by (simp_all add: match_spair_def)
huffman@16551
   162
huffman@16551
   163
lemma match_sinl_simps [simp]:
huffman@30912
   164
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinl\<cdot>x)\<cdot>k = k\<cdot>x"
huffman@30914
   165
  "y \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinr\<cdot>y)\<cdot>k = fail"
huffman@30912
   166
  "match_sinl\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@16551
   167
by (simp_all add: match_sinl_def)
huffman@16551
   168
huffman@16551
   169
lemma match_sinr_simps [simp]:
huffman@30912
   170
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinl\<cdot>x)\<cdot>k = fail"
huffman@30914
   171
  "y \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinr\<cdot>y)\<cdot>k = k\<cdot>y"
huffman@30912
   172
  "match_sinr\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@16551
   173
by (simp_all add: match_sinr_def)
huffman@16551
   174
huffman@16221
   175
lemma match_up_simps [simp]:
huffman@30912
   176
  "match_up\<cdot>(up\<cdot>x)\<cdot>k = k\<cdot>x"
huffman@30912
   177
  "match_up\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@16221
   178
by (simp_all add: match_up_def)
huffman@16221
   179
huffman@16460
   180
lemma match_ONE_simps [simp]:
huffman@30912
   181
  "match_ONE\<cdot>ONE\<cdot>k = k"
huffman@30912
   182
  "match_ONE\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@18094
   183
by (simp_all add: match_ONE_def)
huffman@16460
   184
huffman@16460
   185
lemma match_TT_simps [simp]:
huffman@30912
   186
  "match_TT\<cdot>TT\<cdot>k = k"
huffman@30912
   187
  "match_TT\<cdot>FF\<cdot>k = fail"
huffman@30912
   188
  "match_TT\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@18094
   189
by (simp_all add: match_TT_def)
huffman@16460
   190
huffman@16460
   191
lemma match_FF_simps [simp]:
huffman@30912
   192
  "match_FF\<cdot>FF\<cdot>k = k"
huffman@30912
   193
  "match_FF\<cdot>TT\<cdot>k = fail"
huffman@30912
   194
  "match_FF\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@18094
   195
by (simp_all add: match_FF_def)
huffman@16460
   196
huffman@16401
   197
subsection {* Mutual recursion *}
huffman@16401
   198
huffman@16401
   199
text {*
huffman@16401
   200
  The following rules are used to prove unfolding theorems from
huffman@16401
   201
  fixed-point definitions of mutually recursive functions.
huffman@16401
   202
*}
huffman@16401
   203
huffman@31095
   204
lemma Pair_equalI: "\<lbrakk>x \<equiv> fst p; y \<equiv> snd p\<rbrakk> \<Longrightarrow> (x, y) \<equiv> p"
huffman@31095
   205
by simp
huffman@16401
   206
huffman@31095
   207
lemma Pair_eqD1: "(x, y) = (x', y') \<Longrightarrow> x = x'"
huffman@16401
   208
by simp
huffman@16401
   209
huffman@31095
   210
lemma Pair_eqD2: "(x, y) = (x', y') \<Longrightarrow> y = y'"
huffman@16401
   211
by simp
huffman@16401
   212
huffman@31095
   213
lemma def_cont_fix_eq:
huffman@40327
   214
  "\<lbrakk>f \<equiv> fix\<cdot>(Abs_cfun F); cont F\<rbrakk> \<Longrightarrow> f = F f"
huffman@31095
   215
by (simp, subst fix_eq, simp)
huffman@31095
   216
huffman@31095
   217
lemma def_cont_fix_ind:
huffman@40327
   218
  "\<lbrakk>f \<equiv> fix\<cdot>(Abs_cfun F); cont F; adm P; P \<bottom>; \<And>x. P x \<Longrightarrow> P (F x)\<rbrakk> \<Longrightarrow> P f"
huffman@31095
   219
by (simp add: fix_ind)
huffman@31095
   220
huffman@16463
   221
text {* lemma for proving rewrite rules *}
huffman@16463
   222
huffman@16463
   223
lemma ssubst_lhs: "\<lbrakk>t = s; P s = Q\<rbrakk> \<Longrightarrow> P t = Q"
huffman@16463
   224
by simp
huffman@16463
   225
huffman@16221
   226
huffman@16758
   227
subsection {* Initializing the fixrec package *}
huffman@16221
   228
huffman@35527
   229
use "Tools/holcf_library.ML"
haftmann@31738
   230
use "Tools/fixrec.ML"
huffman@16221
   231
haftmann@31738
   232
setup {* Fixrec.setup *}
huffman@30131
   233
huffman@30131
   234
setup {*
haftmann@31738
   235
  Fixrec.add_matchers
huffman@30131
   236
    [ (@{const_name up}, @{const_name match_up}),
huffman@30131
   237
      (@{const_name sinl}, @{const_name match_sinl}),
huffman@30131
   238
      (@{const_name sinr}, @{const_name match_sinr}),
huffman@30131
   239
      (@{const_name spair}, @{const_name match_spair}),
huffman@39807
   240
      (@{const_name Pair}, @{const_name match_Pair}),
huffman@30131
   241
      (@{const_name ONE}, @{const_name match_ONE}),
huffman@30131
   242
      (@{const_name TT}, @{const_name match_TT}),
huffman@31008
   243
      (@{const_name FF}, @{const_name match_FF}),
huffman@41429
   244
      (@{const_name bottom}, @{const_name match_bottom}) ]
huffman@30131
   245
*}
huffman@30131
   246
huffman@37109
   247
hide_const (open) succeed fail run
huffman@19104
   248
huffman@16221
   249
end