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