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