src/HOLCF/Fixrec.thy
author huffman
Sat Nov 27 12:26:18 2010 -0800 (2010-11-27)
changeset 40767 a3e505b236e7
parent 40735 6f65843e78f3
child 40768 50a80cf4b7ef
permissions -rw-r--r--
rename function 'strict' to 'seq', which is its name in Haskell
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@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@37108
    39
by (simp add: succeed_def cont_Abs_match Abs_match_defined)
huffman@18293
    40
huffman@18293
    41
lemma fail_defined [simp]: "fail \<noteq> \<bottom>"
huffman@37108
    42
by (simp add: fail_def Abs_match_defined)
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@40735
    83
lemmas cont2cont_Rep_match = cont_Rep_match [THEN cont_compose]
huffman@40735
    84
huffman@16221
    85
lemma mplus_strict [simp]: "\<bottom> +++ m = \<bottom>"
huffman@40735
    86
unfolding mplus_def
huffman@40735
    87
by (simp add: cont2cont_Rep_match Rep_match_strict)
huffman@16221
    88
huffman@16221
    89
lemma mplus_fail [simp]: "fail +++ m = m"
huffman@40735
    90
unfolding mplus_def fail_def
huffman@40735
    91
by (simp add: cont2cont_Rep_match Abs_match_inverse)
huffman@16221
    92
huffman@37108
    93
lemma mplus_succeed [simp]: "succeed\<cdot>x +++ m = succeed\<cdot>x"
huffman@40735
    94
unfolding mplus_def succeed_def
huffman@40735
    95
by (simp add: cont2cont_Rep_match cont_Abs_match Abs_match_inverse)
huffman@16221
    96
huffman@16460
    97
lemma mplus_fail2 [simp]: "m +++ fail = m"
huffman@37108
    98
by (cases m, simp_all)
huffman@16460
    99
huffman@16221
   100
lemma mplus_assoc: "(x +++ y) +++ z = x +++ (y +++ z)"
huffman@37108
   101
by (cases x, simp_all)
huffman@16221
   102
huffman@16221
   103
subsection {* Match functions for built-in types *}
huffman@16221
   104
wenzelm@36452
   105
default_sort pcpo
huffman@16776
   106
wenzelm@25131
   107
definition
huffman@37108
   108
  match_UU :: "'a \<rightarrow> 'c match \<rightarrow> 'c match"
huffman@30912
   109
where
huffman@40767
   110
  "match_UU = (\<Lambda> x k. seq\<cdot>x\<cdot>fail)"
wenzelm@25131
   111
wenzelm@25131
   112
definition
huffman@39807
   113
  match_Pair :: "'a::cpo \<times> 'b::cpo \<rightarrow> ('a \<rightarrow> 'b \<rightarrow> 'c match) \<rightarrow> 'c match"
huffman@30912
   114
where
huffman@39807
   115
  "match_Pair = (\<Lambda> x k. csplit\<cdot>k\<cdot>x)"
huffman@16776
   116
wenzelm@25131
   117
definition
huffman@37108
   118
  match_spair :: "'a \<otimes> 'b \<rightarrow> ('a \<rightarrow> 'b \<rightarrow> 'c match) \<rightarrow> 'c match"
huffman@30912
   119
where
huffman@30912
   120
  "match_spair = (\<Lambda> x k. ssplit\<cdot>k\<cdot>x)"
huffman@16221
   121
wenzelm@25131
   122
definition
huffman@37108
   123
  match_sinl :: "'a \<oplus> 'b \<rightarrow> ('a \<rightarrow> 'c match) \<rightarrow> 'c match"
huffman@30912
   124
where
huffman@30912
   125
  "match_sinl = (\<Lambda> x k. sscase\<cdot>k\<cdot>(\<Lambda> b. fail)\<cdot>x)"
huffman@16551
   126
wenzelm@25131
   127
definition
huffman@37108
   128
  match_sinr :: "'a \<oplus> 'b \<rightarrow> ('b \<rightarrow> 'c match) \<rightarrow> 'c match"
huffman@30912
   129
where
huffman@30912
   130
  "match_sinr = (\<Lambda> x k. sscase\<cdot>(\<Lambda> a. fail)\<cdot>k\<cdot>x)"
huffman@16551
   131
wenzelm@25131
   132
definition
huffman@37108
   133
  match_up :: "'a::cpo u \<rightarrow> ('a \<rightarrow> 'c match) \<rightarrow> 'c match"
huffman@30912
   134
where
huffman@30912
   135
  "match_up = (\<Lambda> x k. fup\<cdot>k\<cdot>x)"
huffman@16221
   136
wenzelm@25131
   137
definition
huffman@37108
   138
  match_ONE :: "one \<rightarrow> 'c match \<rightarrow> 'c match"
huffman@30912
   139
where
huffman@30912
   140
  "match_ONE = (\<Lambda> ONE k. k)"
huffman@30912
   141
huffman@30912
   142
definition
huffman@37108
   143
  match_TT :: "tr \<rightarrow> 'c match \<rightarrow> 'c match"
huffman@30912
   144
where
huffman@40322
   145
  "match_TT = (\<Lambda> x k. If x then k else fail)"
huffman@18094
   146
 
wenzelm@25131
   147
definition
huffman@37108
   148
  match_FF :: "tr \<rightarrow> 'c match \<rightarrow> 'c match"
huffman@30912
   149
where
huffman@40322
   150
  "match_FF = (\<Lambda> x k. If x then fail else k)"
huffman@16460
   151
huffman@16776
   152
lemma match_UU_simps [simp]:
huffman@31008
   153
  "match_UU\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@31008
   154
  "x \<noteq> \<bottom> \<Longrightarrow> match_UU\<cdot>x\<cdot>k = fail"
huffman@31008
   155
by (simp_all add: match_UU_def)
huffman@16776
   156
huffman@39807
   157
lemma match_Pair_simps [simp]:
huffman@39807
   158
  "match_Pair\<cdot>(x, y)\<cdot>k = k\<cdot>x\<cdot>y"
huffman@39807
   159
by (simp_all add: match_Pair_def)
huffman@16221
   160
huffman@16551
   161
lemma match_spair_simps [simp]:
huffman@30912
   162
  "\<lbrakk>x \<noteq> \<bottom>; y \<noteq> \<bottom>\<rbrakk> \<Longrightarrow> match_spair\<cdot>(:x, y:)\<cdot>k = k\<cdot>x\<cdot>y"
huffman@30912
   163
  "match_spair\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@16551
   164
by (simp_all add: match_spair_def)
huffman@16551
   165
huffman@16551
   166
lemma match_sinl_simps [simp]:
huffman@30912
   167
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinl\<cdot>x)\<cdot>k = k\<cdot>x"
huffman@30914
   168
  "y \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinr\<cdot>y)\<cdot>k = fail"
huffman@30912
   169
  "match_sinl\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@16551
   170
by (simp_all add: match_sinl_def)
huffman@16551
   171
huffman@16551
   172
lemma match_sinr_simps [simp]:
huffman@30912
   173
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinl\<cdot>x)\<cdot>k = fail"
huffman@30914
   174
  "y \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinr\<cdot>y)\<cdot>k = k\<cdot>y"
huffman@30912
   175
  "match_sinr\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@16551
   176
by (simp_all add: match_sinr_def)
huffman@16551
   177
huffman@16221
   178
lemma match_up_simps [simp]:
huffman@30912
   179
  "match_up\<cdot>(up\<cdot>x)\<cdot>k = k\<cdot>x"
huffman@30912
   180
  "match_up\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@16221
   181
by (simp_all add: match_up_def)
huffman@16221
   182
huffman@16460
   183
lemma match_ONE_simps [simp]:
huffman@30912
   184
  "match_ONE\<cdot>ONE\<cdot>k = k"
huffman@30912
   185
  "match_ONE\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@18094
   186
by (simp_all add: match_ONE_def)
huffman@16460
   187
huffman@16460
   188
lemma match_TT_simps [simp]:
huffman@30912
   189
  "match_TT\<cdot>TT\<cdot>k = k"
huffman@30912
   190
  "match_TT\<cdot>FF\<cdot>k = fail"
huffman@30912
   191
  "match_TT\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@18094
   192
by (simp_all add: match_TT_def)
huffman@16460
   193
huffman@16460
   194
lemma match_FF_simps [simp]:
huffman@30912
   195
  "match_FF\<cdot>FF\<cdot>k = k"
huffman@30912
   196
  "match_FF\<cdot>TT\<cdot>k = fail"
huffman@30912
   197
  "match_FF\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@18094
   198
by (simp_all add: match_FF_def)
huffman@16460
   199
huffman@16401
   200
subsection {* Mutual recursion *}
huffman@16401
   201
huffman@16401
   202
text {*
huffman@16401
   203
  The following rules are used to prove unfolding theorems from
huffman@16401
   204
  fixed-point definitions of mutually recursive functions.
huffman@16401
   205
*}
huffman@16401
   206
huffman@31095
   207
lemma Pair_equalI: "\<lbrakk>x \<equiv> fst p; y \<equiv> snd p\<rbrakk> \<Longrightarrow> (x, y) \<equiv> p"
huffman@31095
   208
by simp
huffman@16401
   209
huffman@31095
   210
lemma Pair_eqD1: "(x, y) = (x', y') \<Longrightarrow> x = x'"
huffman@16401
   211
by simp
huffman@16401
   212
huffman@31095
   213
lemma Pair_eqD2: "(x, y) = (x', y') \<Longrightarrow> y = y'"
huffman@16401
   214
by simp
huffman@16401
   215
huffman@31095
   216
lemma def_cont_fix_eq:
huffman@40327
   217
  "\<lbrakk>f \<equiv> fix\<cdot>(Abs_cfun F); cont F\<rbrakk> \<Longrightarrow> f = F f"
huffman@31095
   218
by (simp, subst fix_eq, simp)
huffman@31095
   219
huffman@31095
   220
lemma def_cont_fix_ind:
huffman@40327
   221
  "\<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
   222
by (simp add: fix_ind)
huffman@31095
   223
huffman@16463
   224
text {* lemma for proving rewrite rules *}
huffman@16463
   225
huffman@16463
   226
lemma ssubst_lhs: "\<lbrakk>t = s; P s = Q\<rbrakk> \<Longrightarrow> P t = Q"
huffman@16463
   227
by simp
huffman@16463
   228
huffman@16221
   229
huffman@16758
   230
subsection {* Initializing the fixrec package *}
huffman@16221
   231
huffman@35527
   232
use "Tools/holcf_library.ML"
haftmann@31738
   233
use "Tools/fixrec.ML"
huffman@16221
   234
haftmann@31738
   235
setup {* Fixrec.setup *}
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@31008
   247
      (@{const_name UU}, @{const_name match_UU}) ]
huffman@30131
   248
*}
huffman@30131
   249
huffman@37109
   250
hide_const (open) succeed fail run
huffman@19104
   251
huffman@16221
   252
end