src/HOLCF/Fixrec.thy
author huffman
Wed Oct 20 19:40:02 2010 -0700 (2010-10-20)
changeset 40046 ba2e41c8b725
parent 39807 19ddbededdd3
child 40322 707eb30e8a53
permissions -rw-r--r--
introduce function strict :: 'a -> 'b -> 'b, which works like Haskell's seq; use strict instead of strictify in various definitions
huffman@16221
     1
(*  Title:      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@35939
     8
imports Cprod Sprod Ssum Up One Tr Fix
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
wenzelm@25131
    29
definition
huffman@37108
    30
  match_case :: "'b \<rightarrow> ('a \<rightarrow> 'b) \<rightarrow> 'a match \<rightarrow> 'b::pcpo" where
huffman@37108
    31
  "match_case = (\<Lambda> f r m. sscase\<cdot>(\<Lambda> x. f)\<cdot>(fup\<cdot>r)\<cdot>(Rep_match m))"
huffman@16221
    32
huffman@37108
    33
lemma matchE [case_names bottom fail succeed, cases type: match]:
huffman@37108
    34
  "\<lbrakk>p = \<bottom> \<Longrightarrow> Q; p = fail \<Longrightarrow> Q; \<And>x. p = succeed\<cdot>x \<Longrightarrow> Q\<rbrakk> \<Longrightarrow> Q"
huffman@37108
    35
unfolding fail_def succeed_def
huffman@19092
    36
apply (cases p, rename_tac r)
huffman@37108
    37
apply (rule_tac p=r in ssumE, simp add: Abs_match_strict)
huffman@16221
    38
apply (rule_tac p=x in oneE, simp, simp)
huffman@37108
    39
apply (rule_tac p=y in upE, simp, simp add: cont_Abs_match)
huffman@16221
    40
done
huffman@16221
    41
huffman@37108
    42
lemma succeed_defined [simp]: "succeed\<cdot>x \<noteq> \<bottom>"
huffman@37108
    43
by (simp add: succeed_def cont_Abs_match Abs_match_defined)
huffman@18293
    44
huffman@18293
    45
lemma fail_defined [simp]: "fail \<noteq> \<bottom>"
huffman@37108
    46
by (simp add: fail_def Abs_match_defined)
huffman@18293
    47
huffman@37108
    48
lemma succeed_eq [simp]: "(succeed\<cdot>x = succeed\<cdot>y) = (x = y)"
huffman@37108
    49
by (simp add: succeed_def cont_Abs_match Abs_match_inject)
huffman@18293
    50
huffman@37108
    51
lemma succeed_neq_fail [simp]:
huffman@37108
    52
  "succeed\<cdot>x \<noteq> fail" "fail \<noteq> succeed\<cdot>x"
huffman@37108
    53
by (simp_all add: succeed_def fail_def cont_Abs_match Abs_match_inject)
huffman@19092
    54
huffman@37108
    55
lemma match_case_simps [simp]:
huffman@37108
    56
  "match_case\<cdot>f\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@37108
    57
  "match_case\<cdot>f\<cdot>r\<cdot>fail = f"
huffman@37108
    58
  "match_case\<cdot>f\<cdot>r\<cdot>(succeed\<cdot>x) = r\<cdot>x"
huffman@37108
    59
by (simp_all add: succeed_def fail_def match_case_def cont_Rep_match
huffman@29530
    60
                  cont2cont_LAM
huffman@37108
    61
                  cont_Abs_match Abs_match_inverse Rep_match_strict)
huffman@19092
    62
huffman@19092
    63
translations
huffman@37108
    64
  "case m of XCONST fail \<Rightarrow> t1 | XCONST succeed\<cdot>x \<Rightarrow> t2"
huffman@37108
    65
    == "CONST match_case\<cdot>t1\<cdot>(\<Lambda> x. t2)\<cdot>m"
huffman@18293
    66
huffman@18097
    67
subsubsection {* Run operator *}
huffman@16221
    68
wenzelm@25131
    69
definition
huffman@37108
    70
  run :: "'a match \<rightarrow> 'a::pcpo" where
huffman@37108
    71
  "run = match_case\<cdot>\<bottom>\<cdot>ID"
huffman@16221
    72
huffman@16221
    73
text {* rewrite rules for run *}
huffman@16221
    74
huffman@16221
    75
lemma run_strict [simp]: "run\<cdot>\<bottom> = \<bottom>"
huffman@16221
    76
by (simp add: run_def)
huffman@16221
    77
huffman@16221
    78
lemma run_fail [simp]: "run\<cdot>fail = \<bottom>"
huffman@19092
    79
by (simp add: run_def)
huffman@16221
    80
huffman@37108
    81
lemma run_succeed [simp]: "run\<cdot>(succeed\<cdot>x) = x"
huffman@19092
    82
by (simp add: run_def)
huffman@16221
    83
huffman@18097
    84
subsubsection {* Monad plus operator *}
huffman@16221
    85
wenzelm@25131
    86
definition
huffman@37108
    87
  mplus :: "'a match \<rightarrow> 'a match \<rightarrow> 'a match" where
huffman@37108
    88
  "mplus = (\<Lambda> m1 m2. case m1 of fail \<Rightarrow> m2 | succeed\<cdot>x \<Rightarrow> m1)"
huffman@18097
    89
wenzelm@25131
    90
abbreviation
huffman@37108
    91
  mplus_syn :: "['a match, 'a match] \<Rightarrow> 'a match"  (infixr "+++" 65)  where
wenzelm@25131
    92
  "m1 +++ m2 == mplus\<cdot>m1\<cdot>m2"
huffman@16221
    93
huffman@16221
    94
text {* rewrite rules for mplus *}
huffman@16221
    95
huffman@16221
    96
lemma mplus_strict [simp]: "\<bottom> +++ m = \<bottom>"
huffman@16221
    97
by (simp add: mplus_def)
huffman@16221
    98
huffman@16221
    99
lemma mplus_fail [simp]: "fail +++ m = m"
huffman@19092
   100
by (simp add: mplus_def)
huffman@16221
   101
huffman@37108
   102
lemma mplus_succeed [simp]: "succeed\<cdot>x +++ m = succeed\<cdot>x"
huffman@19092
   103
by (simp add: mplus_def)
huffman@16221
   104
huffman@16460
   105
lemma mplus_fail2 [simp]: "m +++ fail = m"
huffman@37108
   106
by (cases m, simp_all)
huffman@16460
   107
huffman@16221
   108
lemma mplus_assoc: "(x +++ y) +++ z = x +++ (y +++ z)"
huffman@37108
   109
by (cases x, simp_all)
huffman@16221
   110
huffman@16221
   111
subsection {* Match functions for built-in types *}
huffman@16221
   112
wenzelm@36452
   113
default_sort pcpo
huffman@16776
   114
wenzelm@25131
   115
definition
huffman@37108
   116
  match_UU :: "'a \<rightarrow> 'c match \<rightarrow> 'c match"
huffman@30912
   117
where
huffman@40046
   118
  "match_UU = (\<Lambda> x k. strict\<cdot>x\<cdot>fail)"
wenzelm@25131
   119
wenzelm@25131
   120
definition
huffman@39807
   121
  match_Pair :: "'a::cpo \<times> 'b::cpo \<rightarrow> ('a \<rightarrow> 'b \<rightarrow> 'c match) \<rightarrow> 'c match"
huffman@30912
   122
where
huffman@39807
   123
  "match_Pair = (\<Lambda> x k. csplit\<cdot>k\<cdot>x)"
huffman@16776
   124
wenzelm@25131
   125
definition
huffman@37108
   126
  match_spair :: "'a \<otimes> 'b \<rightarrow> ('a \<rightarrow> 'b \<rightarrow> 'c match) \<rightarrow> 'c match"
huffman@30912
   127
where
huffman@30912
   128
  "match_spair = (\<Lambda> x k. ssplit\<cdot>k\<cdot>x)"
huffman@16221
   129
wenzelm@25131
   130
definition
huffman@37108
   131
  match_sinl :: "'a \<oplus> 'b \<rightarrow> ('a \<rightarrow> 'c match) \<rightarrow> 'c match"
huffman@30912
   132
where
huffman@30912
   133
  "match_sinl = (\<Lambda> x k. sscase\<cdot>k\<cdot>(\<Lambda> b. fail)\<cdot>x)"
huffman@16551
   134
wenzelm@25131
   135
definition
huffman@37108
   136
  match_sinr :: "'a \<oplus> 'b \<rightarrow> ('b \<rightarrow> 'c match) \<rightarrow> 'c match"
huffman@30912
   137
where
huffman@30912
   138
  "match_sinr = (\<Lambda> x k. sscase\<cdot>(\<Lambda> a. fail)\<cdot>k\<cdot>x)"
huffman@16551
   139
wenzelm@25131
   140
definition
huffman@37108
   141
  match_up :: "'a::cpo u \<rightarrow> ('a \<rightarrow> 'c match) \<rightarrow> 'c match"
huffman@30912
   142
where
huffman@30912
   143
  "match_up = (\<Lambda> x k. fup\<cdot>k\<cdot>x)"
huffman@16221
   144
wenzelm@25131
   145
definition
huffman@37108
   146
  match_ONE :: "one \<rightarrow> 'c match \<rightarrow> 'c match"
huffman@30912
   147
where
huffman@30912
   148
  "match_ONE = (\<Lambda> ONE k. k)"
huffman@30912
   149
huffman@30912
   150
definition
huffman@37108
   151
  match_TT :: "tr \<rightarrow> 'c match \<rightarrow> 'c match"
huffman@30912
   152
where
huffman@30912
   153
  "match_TT = (\<Lambda> x k. If x then k else fail fi)"
huffman@18094
   154
 
wenzelm@25131
   155
definition
huffman@37108
   156
  match_FF :: "tr \<rightarrow> 'c match \<rightarrow> 'c match"
huffman@30912
   157
where
huffman@30912
   158
  "match_FF = (\<Lambda> x k. If x then fail else k fi)"
huffman@16460
   159
huffman@16776
   160
lemma match_UU_simps [simp]:
huffman@31008
   161
  "match_UU\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@31008
   162
  "x \<noteq> \<bottom> \<Longrightarrow> match_UU\<cdot>x\<cdot>k = fail"
huffman@31008
   163
by (simp_all add: match_UU_def)
huffman@16776
   164
huffman@39807
   165
lemma match_Pair_simps [simp]:
huffman@39807
   166
  "match_Pair\<cdot>(x, y)\<cdot>k = k\<cdot>x\<cdot>y"
huffman@39807
   167
by (simp_all add: match_Pair_def)
huffman@16221
   168
huffman@16551
   169
lemma match_spair_simps [simp]:
huffman@30912
   170
  "\<lbrakk>x \<noteq> \<bottom>; y \<noteq> \<bottom>\<rbrakk> \<Longrightarrow> match_spair\<cdot>(:x, y:)\<cdot>k = k\<cdot>x\<cdot>y"
huffman@30912
   171
  "match_spair\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@16551
   172
by (simp_all add: match_spair_def)
huffman@16551
   173
huffman@16551
   174
lemma match_sinl_simps [simp]:
huffman@30912
   175
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinl\<cdot>x)\<cdot>k = k\<cdot>x"
huffman@30914
   176
  "y \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinr\<cdot>y)\<cdot>k = fail"
huffman@30912
   177
  "match_sinl\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@16551
   178
by (simp_all add: match_sinl_def)
huffman@16551
   179
huffman@16551
   180
lemma match_sinr_simps [simp]:
huffman@30912
   181
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinl\<cdot>x)\<cdot>k = fail"
huffman@30914
   182
  "y \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinr\<cdot>y)\<cdot>k = k\<cdot>y"
huffman@30912
   183
  "match_sinr\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@16551
   184
by (simp_all add: match_sinr_def)
huffman@16551
   185
huffman@16221
   186
lemma match_up_simps [simp]:
huffman@30912
   187
  "match_up\<cdot>(up\<cdot>x)\<cdot>k = k\<cdot>x"
huffman@30912
   188
  "match_up\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@16221
   189
by (simp_all add: match_up_def)
huffman@16221
   190
huffman@16460
   191
lemma match_ONE_simps [simp]:
huffman@30912
   192
  "match_ONE\<cdot>ONE\<cdot>k = k"
huffman@30912
   193
  "match_ONE\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@18094
   194
by (simp_all add: match_ONE_def)
huffman@16460
   195
huffman@16460
   196
lemma match_TT_simps [simp]:
huffman@30912
   197
  "match_TT\<cdot>TT\<cdot>k = k"
huffman@30912
   198
  "match_TT\<cdot>FF\<cdot>k = fail"
huffman@30912
   199
  "match_TT\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@18094
   200
by (simp_all add: match_TT_def)
huffman@16460
   201
huffman@16460
   202
lemma match_FF_simps [simp]:
huffman@30912
   203
  "match_FF\<cdot>FF\<cdot>k = k"
huffman@30912
   204
  "match_FF\<cdot>TT\<cdot>k = fail"
huffman@30912
   205
  "match_FF\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@18094
   206
by (simp_all add: match_FF_def)
huffman@16460
   207
huffman@16401
   208
subsection {* Mutual recursion *}
huffman@16401
   209
huffman@16401
   210
text {*
huffman@16401
   211
  The following rules are used to prove unfolding theorems from
huffman@16401
   212
  fixed-point definitions of mutually recursive functions.
huffman@16401
   213
*}
huffman@16401
   214
huffman@31095
   215
lemma Pair_equalI: "\<lbrakk>x \<equiv> fst p; y \<equiv> snd p\<rbrakk> \<Longrightarrow> (x, y) \<equiv> p"
huffman@31095
   216
by simp
huffman@16401
   217
huffman@31095
   218
lemma Pair_eqD1: "(x, y) = (x', y') \<Longrightarrow> x = x'"
huffman@16401
   219
by simp
huffman@16401
   220
huffman@31095
   221
lemma Pair_eqD2: "(x, y) = (x', y') \<Longrightarrow> y = y'"
huffman@16401
   222
by simp
huffman@16401
   223
huffman@31095
   224
lemma def_cont_fix_eq:
huffman@31095
   225
  "\<lbrakk>f \<equiv> fix\<cdot>(Abs_CFun F); cont F\<rbrakk> \<Longrightarrow> f = F f"
huffman@31095
   226
by (simp, subst fix_eq, simp)
huffman@31095
   227
huffman@31095
   228
lemma def_cont_fix_ind:
huffman@31095
   229
  "\<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
   230
by (simp add: fix_ind)
huffman@31095
   231
huffman@16463
   232
text {* lemma for proving rewrite rules *}
huffman@16463
   233
huffman@16463
   234
lemma ssubst_lhs: "\<lbrakk>t = s; P s = Q\<rbrakk> \<Longrightarrow> P t = Q"
huffman@16463
   235
by simp
huffman@16463
   236
huffman@16221
   237
huffman@16758
   238
subsection {* Initializing the fixrec package *}
huffman@16221
   239
huffman@35527
   240
use "Tools/holcf_library.ML"
haftmann@31738
   241
use "Tools/fixrec.ML"
huffman@16221
   242
haftmann@31738
   243
setup {* Fixrec.setup *}
huffman@30131
   244
huffman@30131
   245
setup {*
haftmann@31738
   246
  Fixrec.add_matchers
huffman@30131
   247
    [ (@{const_name up}, @{const_name match_up}),
huffman@30131
   248
      (@{const_name sinl}, @{const_name match_sinl}),
huffman@30131
   249
      (@{const_name sinr}, @{const_name match_sinr}),
huffman@30131
   250
      (@{const_name spair}, @{const_name match_spair}),
huffman@39807
   251
      (@{const_name Pair}, @{const_name match_Pair}),
huffman@30131
   252
      (@{const_name ONE}, @{const_name match_ONE}),
huffman@30131
   253
      (@{const_name TT}, @{const_name match_TT}),
huffman@31008
   254
      (@{const_name FF}, @{const_name match_FF}),
huffman@31008
   255
      (@{const_name UU}, @{const_name match_UU}) ]
huffman@30131
   256
*}
huffman@30131
   257
huffman@37109
   258
hide_const (open) succeed fail run
huffman@19104
   259
huffman@16221
   260
end