src/HOL/HOLCF/ex/Pattern_Match.thy
author huffman
Tue Nov 30 15:56:19 2010 -0800 (2010-11-30)
changeset 40834 a1249aeff5b6
parent 40774 0437dbc127b3
child 41229 d797baa3d57c
permissions -rw-r--r--
change cpodef-generated cont_Rep rules to cont2cont format
huffman@37109
     1
(*  Title:      HOLCF/ex/Pattern_Match.thy
huffman@37109
     2
    Author:     Brian Huffman
huffman@37109
     3
*)
huffman@37109
     4
huffman@37109
     5
header {* An experimental pattern-matching notation *}
huffman@37109
     6
huffman@37109
     7
theory Pattern_Match
huffman@37109
     8
imports HOLCF
huffman@37109
     9
begin
huffman@37109
    10
huffman@40329
    11
default_sort pcpo
huffman@40329
    12
huffman@37109
    13
text {* FIXME: Find a proper way to un-hide constants. *}
huffman@37109
    14
huffman@37109
    15
abbreviation fail :: "'a match"
huffman@37109
    16
where "fail \<equiv> Fixrec.fail"
huffman@37109
    17
huffman@37109
    18
abbreviation succeed :: "'a \<rightarrow> 'a match"
huffman@37109
    19
where "succeed \<equiv> Fixrec.succeed"
huffman@37109
    20
huffman@37109
    21
abbreviation run :: "'a match \<rightarrow> 'a"
huffman@37109
    22
where "run \<equiv> Fixrec.run"
huffman@37109
    23
huffman@37109
    24
subsection {* Fatbar combinator *}
huffman@37109
    25
huffman@37109
    26
definition
huffman@37109
    27
  fatbar :: "('a \<rightarrow> 'b match) \<rightarrow> ('a \<rightarrow> 'b match) \<rightarrow> ('a \<rightarrow> 'b match)" where
huffman@37109
    28
  "fatbar = (\<Lambda> a b x. a\<cdot>x +++ b\<cdot>x)"
huffman@37109
    29
huffman@37109
    30
abbreviation
huffman@37109
    31
  fatbar_syn :: "['a \<rightarrow> 'b match, 'a \<rightarrow> 'b match] \<Rightarrow> 'a \<rightarrow> 'b match" (infixr "\<parallel>" 60)  where
huffman@37109
    32
  "m1 \<parallel> m2 == fatbar\<cdot>m1\<cdot>m2"
huffman@37109
    33
huffman@37109
    34
lemma fatbar1: "m\<cdot>x = \<bottom> \<Longrightarrow> (m \<parallel> ms)\<cdot>x = \<bottom>"
huffman@37109
    35
by (simp add: fatbar_def)
huffman@37109
    36
huffman@37109
    37
lemma fatbar2: "m\<cdot>x = fail \<Longrightarrow> (m \<parallel> ms)\<cdot>x = ms\<cdot>x"
huffman@37109
    38
by (simp add: fatbar_def)
huffman@37109
    39
huffman@37109
    40
lemma fatbar3: "m\<cdot>x = succeed\<cdot>y \<Longrightarrow> (m \<parallel> ms)\<cdot>x = succeed\<cdot>y"
huffman@37109
    41
by (simp add: fatbar_def)
huffman@37109
    42
huffman@37109
    43
lemmas fatbar_simps = fatbar1 fatbar2 fatbar3
huffman@37109
    44
huffman@37109
    45
lemma run_fatbar1: "m\<cdot>x = \<bottom> \<Longrightarrow> run\<cdot>((m \<parallel> ms)\<cdot>x) = \<bottom>"
huffman@37109
    46
by (simp add: fatbar_def)
huffman@37109
    47
huffman@37109
    48
lemma run_fatbar2: "m\<cdot>x = fail \<Longrightarrow> run\<cdot>((m \<parallel> ms)\<cdot>x) = run\<cdot>(ms\<cdot>x)"
huffman@37109
    49
by (simp add: fatbar_def)
huffman@37109
    50
huffman@37109
    51
lemma run_fatbar3: "m\<cdot>x = succeed\<cdot>y \<Longrightarrow> run\<cdot>((m \<parallel> ms)\<cdot>x) = y"
huffman@37109
    52
by (simp add: fatbar_def)
huffman@37109
    53
huffman@37109
    54
lemmas run_fatbar_simps [simp] = run_fatbar1 run_fatbar2 run_fatbar3
huffman@37109
    55
huffman@40735
    56
subsection {* Bind operator for match monad *}
huffman@40735
    57
huffman@40735
    58
definition match_bind :: "'a match \<rightarrow> ('a \<rightarrow> 'b match) \<rightarrow> 'b match" where
huffman@40735
    59
  "match_bind = (\<Lambda> m k. sscase\<cdot>(\<Lambda> _. fail)\<cdot>(fup\<cdot>k)\<cdot>(Rep_match m))"
huffman@40735
    60
huffman@40735
    61
lemma match_bind_simps [simp]:
huffman@40735
    62
  "match_bind\<cdot>\<bottom>\<cdot>k = \<bottom>"
huffman@40735
    63
  "match_bind\<cdot>fail\<cdot>k = fail"
huffman@40735
    64
  "match_bind\<cdot>(succeed\<cdot>x)\<cdot>k = k\<cdot>x"
huffman@40735
    65
unfolding match_bind_def fail_def succeed_def
huffman@40834
    66
by (simp_all add: cont_Rep_match cont_Abs_match
huffman@40735
    67
  Rep_match_strict Abs_match_inverse)
huffman@40735
    68
huffman@37109
    69
subsection {* Case branch combinator *}
huffman@37109
    70
huffman@37109
    71
definition
huffman@37109
    72
  branch :: "('a \<rightarrow> 'b match) \<Rightarrow> ('b \<rightarrow> 'c) \<rightarrow> ('a \<rightarrow> 'c match)" where
huffman@40735
    73
  "branch p \<equiv> \<Lambda> r x. match_bind\<cdot>(p\<cdot>x)\<cdot>(\<Lambda> y. succeed\<cdot>(r\<cdot>y))"
huffman@37109
    74
huffman@37109
    75
lemma branch_simps:
huffman@37109
    76
  "p\<cdot>x = \<bottom> \<Longrightarrow> branch p\<cdot>r\<cdot>x = \<bottom>"
huffman@37109
    77
  "p\<cdot>x = fail \<Longrightarrow> branch p\<cdot>r\<cdot>x = fail"
huffman@37109
    78
  "p\<cdot>x = succeed\<cdot>y \<Longrightarrow> branch p\<cdot>r\<cdot>x = succeed\<cdot>(r\<cdot>y)"
huffman@37109
    79
by (simp_all add: branch_def)
huffman@37109
    80
huffman@37109
    81
lemma branch_succeed [simp]: "branch succeed\<cdot>r\<cdot>x = succeed\<cdot>(r\<cdot>x)"
huffman@37109
    82
by (simp add: branch_def)
huffman@37109
    83
huffman@37109
    84
subsection {* Cases operator *}
huffman@37109
    85
huffman@37109
    86
definition
huffman@37109
    87
  cases :: "'a match \<rightarrow> 'a::pcpo" where
huffman@40735
    88
  "cases = Fixrec.run"
huffman@37109
    89
huffman@37109
    90
text {* rewrite rules for cases *}
huffman@37109
    91
huffman@37109
    92
lemma cases_strict [simp]: "cases\<cdot>\<bottom> = \<bottom>"
huffman@37109
    93
by (simp add: cases_def)
huffman@37109
    94
huffman@37109
    95
lemma cases_fail [simp]: "cases\<cdot>fail = \<bottom>"
huffman@37109
    96
by (simp add: cases_def)
huffman@37109
    97
huffman@37109
    98
lemma cases_succeed [simp]: "cases\<cdot>(succeed\<cdot>x) = x"
huffman@37109
    99
by (simp add: cases_def)
huffman@37109
   100
huffman@37109
   101
subsection {* Case syntax *}
huffman@37109
   102
huffman@37109
   103
nonterminals
huffman@37109
   104
  Case_syn  Cases_syn
huffman@37109
   105
huffman@37109
   106
syntax
huffman@37109
   107
  "_Case_syntax":: "['a, Cases_syn] => 'b"               ("(Case _ of/ _)" 10)
huffman@37109
   108
  "_Case1"      :: "['a, 'b] => Case_syn"                ("(2_ =>/ _)" 10)
huffman@37109
   109
  ""            :: "Case_syn => Cases_syn"               ("_")
huffman@37109
   110
  "_Case2"      :: "[Case_syn, Cases_syn] => Cases_syn"  ("_/ | _")
huffman@37109
   111
huffman@37109
   112
syntax (xsymbols)
huffman@37109
   113
  "_Case1"      :: "['a, 'b] => Case_syn"                ("(2_ \<Rightarrow>/ _)" 10)
huffman@37109
   114
huffman@37109
   115
translations
huffman@37109
   116
  "_Case_syntax x ms" == "CONST cases\<cdot>(ms\<cdot>x)"
huffman@37109
   117
  "_Case2 m ms" == "m \<parallel> ms"
huffman@37109
   118
huffman@37109
   119
text {* Parsing Case expressions *}
huffman@37109
   120
huffman@37109
   121
syntax
huffman@37109
   122
  "_pat" :: "'a"
huffman@37109
   123
  "_variable" :: "'a"
huffman@37109
   124
  "_noargs" :: "'a"
huffman@37109
   125
huffman@37109
   126
translations
huffman@37109
   127
  "_Case1 p r" => "CONST branch (_pat p)\<cdot>(_variable p r)"
huffman@37109
   128
  "_variable (_args x y) r" => "CONST csplit\<cdot>(_variable x (_variable y r))"
huffman@37109
   129
  "_variable _noargs r" => "CONST unit_when\<cdot>r"
huffman@37109
   130
huffman@37109
   131
parse_translation {*
huffman@37109
   132
(* rewrite (_pat x) => (succeed) *)
huffman@40327
   133
(* rewrite (_variable x t) => (Abs_cfun (%x. t)) *)
huffman@37109
   134
 [(@{syntax_const "_pat"}, fn _ => Syntax.const @{const_syntax Fixrec.succeed}),
huffman@40327
   135
  mk_binder_tr (@{syntax_const "_variable"}, @{const_syntax Abs_cfun})];
huffman@37109
   136
*}
huffman@37109
   137
huffman@37109
   138
text {* Printing Case expressions *}
huffman@37109
   139
huffman@37109
   140
syntax
huffman@37109
   141
  "_match" :: "'a"
huffman@37109
   142
huffman@37109
   143
print_translation {*
huffman@37109
   144
  let
huffman@40327
   145
    fun dest_LAM (Const (@{const_syntax Rep_cfun},_) $ Const (@{const_syntax unit_when},_) $ t) =
huffman@37109
   146
          (Syntax.const @{syntax_const "_noargs"}, t)
huffman@40327
   147
    |   dest_LAM (Const (@{const_syntax Rep_cfun},_) $ Const (@{const_syntax csplit},_) $ t) =
huffman@37109
   148
          let
huffman@37109
   149
            val (v1, t1) = dest_LAM t;
huffman@37109
   150
            val (v2, t2) = dest_LAM t1;
huffman@37109
   151
          in (Syntax.const @{syntax_const "_args"} $ v1 $ v2, t2) end
huffman@40327
   152
    |   dest_LAM (Const (@{const_syntax Abs_cfun},_) $ t) =
huffman@37109
   153
          let
huffman@37109
   154
            val abs =
huffman@37109
   155
              case t of Abs abs => abs
huffman@37109
   156
                | _ => ("x", dummyT, incr_boundvars 1 t $ Bound 0);
huffman@37109
   157
            val (x, t') = atomic_abs_tr' abs;
huffman@37109
   158
          in (Syntax.const @{syntax_const "_variable"} $ x, t') end
huffman@37109
   159
    |   dest_LAM _ = raise Match; (* too few vars: abort translation *)
huffman@37109
   160
huffman@37109
   161
    fun Case1_tr' [Const(@{const_syntax branch},_) $ p, r] =
huffman@37109
   162
          let val (v, t) = dest_LAM r in
huffman@37109
   163
            Syntax.const @{syntax_const "_Case1"} $
huffman@37109
   164
              (Syntax.const @{syntax_const "_match"} $ p $ v) $ t
huffman@37109
   165
          end;
huffman@37109
   166
huffman@40327
   167
  in [(@{const_syntax Rep_cfun}, Case1_tr')] end;
huffman@37109
   168
*}
huffman@37109
   169
huffman@37109
   170
translations
huffman@37109
   171
  "x" <= "_match (CONST succeed) (_variable x)"
huffman@37109
   172
huffman@37109
   173
huffman@37109
   174
subsection {* Pattern combinators for data constructors *}
huffman@37109
   175
huffman@37109
   176
types ('a, 'b) pat = "'a \<rightarrow> 'b match"
huffman@37109
   177
huffman@37109
   178
definition
huffman@37109
   179
  cpair_pat :: "('a, 'c) pat \<Rightarrow> ('b, 'd) pat \<Rightarrow> ('a \<times> 'b, 'c \<times> 'd) pat" where
huffman@37109
   180
  "cpair_pat p1 p2 = (\<Lambda>(x, y).
huffman@40735
   181
    match_bind\<cdot>(p1\<cdot>x)\<cdot>(\<Lambda> a. match_bind\<cdot>(p2\<cdot>y)\<cdot>(\<Lambda> b. succeed\<cdot>(a, b))))"
huffman@37109
   182
huffman@37109
   183
definition
huffman@37109
   184
  spair_pat ::
huffman@37109
   185
  "('a, 'c) pat \<Rightarrow> ('b, 'd) pat \<Rightarrow> ('a::pcpo \<otimes> 'b::pcpo, 'c \<times> 'd) pat" where
huffman@37109
   186
  "spair_pat p1 p2 = (\<Lambda>(:x, y:). cpair_pat p1 p2\<cdot>(x, y))"
huffman@37109
   187
huffman@37109
   188
definition
huffman@37109
   189
  sinl_pat :: "('a, 'c) pat \<Rightarrow> ('a::pcpo \<oplus> 'b::pcpo, 'c) pat" where
huffman@37109
   190
  "sinl_pat p = sscase\<cdot>p\<cdot>(\<Lambda> x. fail)"
huffman@37109
   191
huffman@37109
   192
definition
huffman@37109
   193
  sinr_pat :: "('b, 'c) pat \<Rightarrow> ('a::pcpo \<oplus> 'b::pcpo, 'c) pat" where
huffman@37109
   194
  "sinr_pat p = sscase\<cdot>(\<Lambda> x. fail)\<cdot>p"
huffman@37109
   195
huffman@37109
   196
definition
huffman@37109
   197
  up_pat :: "('a, 'b) pat \<Rightarrow> ('a u, 'b) pat" where
huffman@37109
   198
  "up_pat p = fup\<cdot>p"
huffman@37109
   199
huffman@37109
   200
definition
huffman@37109
   201
  TT_pat :: "(tr, unit) pat" where
huffman@40322
   202
  "TT_pat = (\<Lambda> b. If b then succeed\<cdot>() else fail)"
huffman@37109
   203
huffman@37109
   204
definition
huffman@37109
   205
  FF_pat :: "(tr, unit) pat" where
huffman@40322
   206
  "FF_pat = (\<Lambda> b. If b then fail else succeed\<cdot>())"
huffman@37109
   207
huffman@37109
   208
definition
huffman@37109
   209
  ONE_pat :: "(one, unit) pat" where
huffman@37109
   210
  "ONE_pat = (\<Lambda> ONE. succeed\<cdot>())"
huffman@37109
   211
huffman@37109
   212
text {* Parse translations (patterns) *}
huffman@37109
   213
translations
huffman@37109
   214
  "_pat (XCONST Pair x y)" => "CONST cpair_pat (_pat x) (_pat y)"
huffman@37109
   215
  "_pat (XCONST spair\<cdot>x\<cdot>y)" => "CONST spair_pat (_pat x) (_pat y)"
huffman@37109
   216
  "_pat (XCONST sinl\<cdot>x)" => "CONST sinl_pat (_pat x)"
huffman@37109
   217
  "_pat (XCONST sinr\<cdot>x)" => "CONST sinr_pat (_pat x)"
huffman@37109
   218
  "_pat (XCONST up\<cdot>x)" => "CONST up_pat (_pat x)"
huffman@37109
   219
  "_pat (XCONST TT)" => "CONST TT_pat"
huffman@37109
   220
  "_pat (XCONST FF)" => "CONST FF_pat"
huffman@37109
   221
  "_pat (XCONST ONE)" => "CONST ONE_pat"
huffman@37109
   222
huffman@37109
   223
text {* CONST version is also needed for constructors with special syntax *}
huffman@37109
   224
translations
huffman@37109
   225
  "_pat (CONST Pair x y)" => "CONST cpair_pat (_pat x) (_pat y)"
huffman@37109
   226
  "_pat (CONST spair\<cdot>x\<cdot>y)" => "CONST spair_pat (_pat x) (_pat y)"
huffman@37109
   227
huffman@37109
   228
text {* Parse translations (variables) *}
huffman@37109
   229
translations
huffman@37109
   230
  "_variable (XCONST Pair x y) r" => "_variable (_args x y) r"
huffman@37109
   231
  "_variable (XCONST spair\<cdot>x\<cdot>y) r" => "_variable (_args x y) r"
huffman@37109
   232
  "_variable (XCONST sinl\<cdot>x) r" => "_variable x r"
huffman@37109
   233
  "_variable (XCONST sinr\<cdot>x) r" => "_variable x r"
huffman@37109
   234
  "_variable (XCONST up\<cdot>x) r" => "_variable x r"
huffman@37109
   235
  "_variable (XCONST TT) r" => "_variable _noargs r"
huffman@37109
   236
  "_variable (XCONST FF) r" => "_variable _noargs r"
huffman@37109
   237
  "_variable (XCONST ONE) r" => "_variable _noargs r"
huffman@37109
   238
huffman@37109
   239
translations
huffman@37109
   240
  "_variable (CONST Pair x y) r" => "_variable (_args x y) r"
huffman@37109
   241
  "_variable (CONST spair\<cdot>x\<cdot>y) r" => "_variable (_args x y) r"
huffman@37109
   242
huffman@37109
   243
text {* Print translations *}
huffman@37109
   244
translations
huffman@37109
   245
  "CONST Pair (_match p1 v1) (_match p2 v2)"
huffman@37109
   246
      <= "_match (CONST cpair_pat p1 p2) (_args v1 v2)"
huffman@37109
   247
  "CONST spair\<cdot>(_match p1 v1)\<cdot>(_match p2 v2)"
huffman@37109
   248
      <= "_match (CONST spair_pat p1 p2) (_args v1 v2)"
huffman@37109
   249
  "CONST sinl\<cdot>(_match p1 v1)" <= "_match (CONST sinl_pat p1) v1"
huffman@37109
   250
  "CONST sinr\<cdot>(_match p1 v1)" <= "_match (CONST sinr_pat p1) v1"
huffman@37109
   251
  "CONST up\<cdot>(_match p1 v1)" <= "_match (CONST up_pat p1) v1"
huffman@37109
   252
  "CONST TT" <= "_match (CONST TT_pat) _noargs"
huffman@37109
   253
  "CONST FF" <= "_match (CONST FF_pat) _noargs"
huffman@37109
   254
  "CONST ONE" <= "_match (CONST ONE_pat) _noargs"
huffman@37109
   255
huffman@37109
   256
lemma cpair_pat1:
huffman@37109
   257
  "branch p\<cdot>r\<cdot>x = \<bottom> \<Longrightarrow> branch (cpair_pat p q)\<cdot>(csplit\<cdot>r)\<cdot>(x, y) = \<bottom>"
huffman@37109
   258
apply (simp add: branch_def cpair_pat_def)
huffman@37109
   259
apply (cases "p\<cdot>x", simp_all)
huffman@37109
   260
done
huffman@37109
   261
huffman@37109
   262
lemma cpair_pat2:
huffman@37109
   263
  "branch p\<cdot>r\<cdot>x = fail \<Longrightarrow> branch (cpair_pat p q)\<cdot>(csplit\<cdot>r)\<cdot>(x, y) = fail"
huffman@37109
   264
apply (simp add: branch_def cpair_pat_def)
huffman@37109
   265
apply (cases "p\<cdot>x", simp_all)
huffman@37109
   266
done
huffman@37109
   267
huffman@37109
   268
lemma cpair_pat3:
huffman@37109
   269
  "branch p\<cdot>r\<cdot>x = succeed\<cdot>s \<Longrightarrow>
huffman@37109
   270
   branch (cpair_pat p q)\<cdot>(csplit\<cdot>r)\<cdot>(x, y) = branch q\<cdot>s\<cdot>y"
huffman@37109
   271
apply (simp add: branch_def cpair_pat_def)
huffman@37109
   272
apply (cases "p\<cdot>x", simp_all)
huffman@37109
   273
apply (cases "q\<cdot>y", simp_all)
huffman@37109
   274
done
huffman@37109
   275
huffman@37109
   276
lemmas cpair_pat [simp] =
huffman@37109
   277
  cpair_pat1 cpair_pat2 cpair_pat3
huffman@37109
   278
huffman@37109
   279
lemma spair_pat [simp]:
huffman@37109
   280
  "branch (spair_pat p1 p2)\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@37109
   281
  "\<lbrakk>x \<noteq> \<bottom>; y \<noteq> \<bottom>\<rbrakk>
huffman@37109
   282
     \<Longrightarrow> branch (spair_pat p1 p2)\<cdot>r\<cdot>(:x, y:) =
huffman@37109
   283
         branch (cpair_pat p1 p2)\<cdot>r\<cdot>(x, y)"
huffman@37109
   284
by (simp_all add: branch_def spair_pat_def)
huffman@37109
   285
huffman@37109
   286
lemma sinl_pat [simp]:
huffman@37109
   287
  "branch (sinl_pat p)\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@37109
   288
  "x \<noteq> \<bottom> \<Longrightarrow> branch (sinl_pat p)\<cdot>r\<cdot>(sinl\<cdot>x) = branch p\<cdot>r\<cdot>x"
huffman@37109
   289
  "y \<noteq> \<bottom> \<Longrightarrow> branch (sinl_pat p)\<cdot>r\<cdot>(sinr\<cdot>y) = fail"
huffman@37109
   290
by (simp_all add: branch_def sinl_pat_def)
huffman@37109
   291
huffman@37109
   292
lemma sinr_pat [simp]:
huffman@37109
   293
  "branch (sinr_pat p)\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@37109
   294
  "x \<noteq> \<bottom> \<Longrightarrow> branch (sinr_pat p)\<cdot>r\<cdot>(sinl\<cdot>x) = fail"
huffman@37109
   295
  "y \<noteq> \<bottom> \<Longrightarrow> branch (sinr_pat p)\<cdot>r\<cdot>(sinr\<cdot>y) = branch p\<cdot>r\<cdot>y"
huffman@37109
   296
by (simp_all add: branch_def sinr_pat_def)
huffman@37109
   297
huffman@37109
   298
lemma up_pat [simp]:
huffman@37109
   299
  "branch (up_pat p)\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@37109
   300
  "branch (up_pat p)\<cdot>r\<cdot>(up\<cdot>x) = branch p\<cdot>r\<cdot>x"
huffman@37109
   301
by (simp_all add: branch_def up_pat_def)
huffman@37109
   302
huffman@37109
   303
lemma TT_pat [simp]:
huffman@37109
   304
  "branch TT_pat\<cdot>(unit_when\<cdot>r)\<cdot>\<bottom> = \<bottom>"
huffman@37109
   305
  "branch TT_pat\<cdot>(unit_when\<cdot>r)\<cdot>TT = succeed\<cdot>r"
huffman@37109
   306
  "branch TT_pat\<cdot>(unit_when\<cdot>r)\<cdot>FF = fail"
huffman@37109
   307
by (simp_all add: branch_def TT_pat_def)
huffman@37109
   308
huffman@37109
   309
lemma FF_pat [simp]:
huffman@37109
   310
  "branch FF_pat\<cdot>(unit_when\<cdot>r)\<cdot>\<bottom> = \<bottom>"
huffman@37109
   311
  "branch FF_pat\<cdot>(unit_when\<cdot>r)\<cdot>TT = fail"
huffman@37109
   312
  "branch FF_pat\<cdot>(unit_when\<cdot>r)\<cdot>FF = succeed\<cdot>r"
huffman@37109
   313
by (simp_all add: branch_def FF_pat_def)
huffman@37109
   314
huffman@37109
   315
lemma ONE_pat [simp]:
huffman@37109
   316
  "branch ONE_pat\<cdot>(unit_when\<cdot>r)\<cdot>\<bottom> = \<bottom>"
huffman@37109
   317
  "branch ONE_pat\<cdot>(unit_when\<cdot>r)\<cdot>ONE = succeed\<cdot>r"
huffman@37109
   318
by (simp_all add: branch_def ONE_pat_def)
huffman@37109
   319
huffman@37109
   320
huffman@37109
   321
subsection {* Wildcards, as-patterns, and lazy patterns *}
huffman@37109
   322
huffman@37109
   323
definition
huffman@37109
   324
  wild_pat :: "'a \<rightarrow> unit match" where
huffman@37109
   325
  "wild_pat = (\<Lambda> x. succeed\<cdot>())"
huffman@37109
   326
huffman@37109
   327
definition
huffman@37109
   328
  as_pat :: "('a \<rightarrow> 'b match) \<Rightarrow> 'a \<rightarrow> ('a \<times> 'b) match" where
huffman@40735
   329
  "as_pat p = (\<Lambda> x. match_bind\<cdot>(p\<cdot>x)\<cdot>(\<Lambda> a. succeed\<cdot>(x, a)))"
huffman@37109
   330
huffman@37109
   331
definition
huffman@37109
   332
  lazy_pat :: "('a \<rightarrow> 'b::pcpo match) \<Rightarrow> ('a \<rightarrow> 'b match)" where
huffman@37109
   333
  "lazy_pat p = (\<Lambda> x. succeed\<cdot>(cases\<cdot>(p\<cdot>x)))"
huffman@37109
   334
huffman@37109
   335
text {* Parse translations (patterns) *}
huffman@37109
   336
translations
huffman@37109
   337
  "_pat _" => "CONST wild_pat"
huffman@37109
   338
huffman@37109
   339
text {* Parse translations (variables) *}
huffman@37109
   340
translations
huffman@37109
   341
  "_variable _ r" => "_variable _noargs r"
huffman@37109
   342
huffman@37109
   343
text {* Print translations *}
huffman@37109
   344
translations
huffman@37109
   345
  "_" <= "_match (CONST wild_pat) _noargs"
huffman@37109
   346
huffman@37109
   347
lemma wild_pat [simp]: "branch wild_pat\<cdot>(unit_when\<cdot>r)\<cdot>x = succeed\<cdot>r"
huffman@37109
   348
by (simp add: branch_def wild_pat_def)
huffman@37109
   349
huffman@37109
   350
lemma as_pat [simp]:
huffman@37109
   351
  "branch (as_pat p)\<cdot>(csplit\<cdot>r)\<cdot>x = branch p\<cdot>(r\<cdot>x)\<cdot>x"
huffman@37109
   352
apply (simp add: branch_def as_pat_def)
huffman@37109
   353
apply (cases "p\<cdot>x", simp_all)
huffman@37109
   354
done
huffman@37109
   355
huffman@37109
   356
lemma lazy_pat [simp]:
huffman@37109
   357
  "branch p\<cdot>r\<cdot>x = \<bottom> \<Longrightarrow> branch (lazy_pat p)\<cdot>r\<cdot>x = succeed\<cdot>(r\<cdot>\<bottom>)"
huffman@37109
   358
  "branch p\<cdot>r\<cdot>x = fail \<Longrightarrow> branch (lazy_pat p)\<cdot>r\<cdot>x = succeed\<cdot>(r\<cdot>\<bottom>)"
huffman@37109
   359
  "branch p\<cdot>r\<cdot>x = succeed\<cdot>s \<Longrightarrow> branch (lazy_pat p)\<cdot>r\<cdot>x = succeed\<cdot>s"
huffman@37109
   360
apply (simp_all add: branch_def lazy_pat_def)
huffman@37109
   361
apply (cases "p\<cdot>x", simp_all)+
huffman@37109
   362
done
huffman@37109
   363
huffman@37109
   364
subsection {* Examples *}
huffman@37109
   365
huffman@37109
   366
term "Case t of (:up\<cdot>(sinl\<cdot>x), sinr\<cdot>y:) \<Rightarrow> (x, y)"
huffman@37109
   367
huffman@37109
   368
term "\<Lambda> t. Case t of up\<cdot>(sinl\<cdot>a) \<Rightarrow> a | up\<cdot>(sinr\<cdot>b) \<Rightarrow> b"
huffman@37109
   369
huffman@37109
   370
term "\<Lambda> t. Case t of (:up\<cdot>(sinl\<cdot>_), sinr\<cdot>x:) \<Rightarrow> x"
huffman@37109
   371
huffman@37109
   372
subsection {* ML code for generating definitions *}
huffman@37109
   373
huffman@37109
   374
ML {*
huffman@37109
   375
local open HOLCF_Library in
huffman@37109
   376
huffman@40026
   377
infixr 6 ->>;
huffman@40026
   378
infix 9 ` ;
huffman@40026
   379
huffman@37109
   380
val beta_rules =
huffman@40326
   381
  @{thms beta_cfun cont_id cont_const cont2cont_APP cont2cont_LAM'} @
huffman@37109
   382
  @{thms cont2cont_fst cont2cont_snd cont2cont_Pair};
huffman@37109
   383
huffman@37109
   384
val beta_ss = HOL_basic_ss addsimps (simp_thms @ beta_rules);
huffman@37109
   385
huffman@37109
   386
fun define_consts
huffman@37109
   387
    (specs : (binding * term * mixfix) list)
huffman@37109
   388
    (thy : theory)
huffman@37109
   389
    : (term list * thm list) * theory =
huffman@37109
   390
  let
huffman@37109
   391
    fun mk_decl (b, t, mx) = (b, fastype_of t, mx);
huffman@37109
   392
    val decls = map mk_decl specs;
huffman@37109
   393
    val thy = Cont_Consts.add_consts decls thy;
huffman@37109
   394
    fun mk_const (b, T, mx) = Const (Sign.full_name thy b, T);
huffman@37109
   395
    val consts = map mk_const decls;
huffman@37109
   396
    fun mk_def c (b, t, mx) =
huffman@37109
   397
      (Binding.suffix_name "_def" b, Logic.mk_equals (c, t));
huffman@37109
   398
    val defs = map2 mk_def consts specs;
huffman@37109
   399
    val (def_thms, thy) =
wenzelm@39557
   400
      Global_Theory.add_defs false (map Thm.no_attributes defs) thy;
huffman@37109
   401
  in
huffman@37109
   402
    ((consts, def_thms), thy)
huffman@37109
   403
  end;
huffman@37109
   404
huffman@37109
   405
fun prove
huffman@37109
   406
    (thy : theory)
huffman@37109
   407
    (defs : thm list)
huffman@37109
   408
    (goal : term)
huffman@37109
   409
    (tacs : {prems: thm list, context: Proof.context} -> tactic list)
huffman@37109
   410
    : thm =
huffman@37109
   411
  let
huffman@37109
   412
    fun tac {prems, context} =
huffman@37109
   413
      rewrite_goals_tac defs THEN
huffman@37109
   414
      EVERY (tacs {prems = map (rewrite_rule defs) prems, context = context})
huffman@37109
   415
  in
huffman@37109
   416
    Goal.prove_global thy [] [] goal tac
huffman@37109
   417
  end;
huffman@37109
   418
huffman@37109
   419
fun get_vars_avoiding
huffman@37109
   420
    (taken : string list)
huffman@37109
   421
    (args : (bool * typ) list)
huffman@37109
   422
    : (term list * term list) =
huffman@37109
   423
  let
huffman@37109
   424
    val Ts = map snd args;
huffman@37109
   425
    val ns = Name.variant_list taken (Datatype_Prop.make_tnames Ts);
huffman@37109
   426
    val vs = map Free (ns ~~ Ts);
huffman@37109
   427
    val nonlazy = map snd (filter_out (fst o fst) (args ~~ vs));
huffman@37109
   428
  in
huffman@37109
   429
    (vs, nonlazy)
huffman@37109
   430
  end;
huffman@37109
   431
huffman@37109
   432
(******************************************************************************)
huffman@37109
   433
(************** definitions and theorems for pattern combinators **************)
huffman@37109
   434
(******************************************************************************)
huffman@37109
   435
huffman@37109
   436
fun add_pattern_combinators
huffman@37109
   437
    (bindings : binding list)
huffman@37109
   438
    (spec : (term * (bool * typ) list) list)
huffman@37109
   439
    (lhsT : typ)
huffman@37109
   440
    (exhaust : thm)
huffman@37109
   441
    (case_const : typ -> term)
huffman@37109
   442
    (case_rews : thm list)
huffman@37109
   443
    (thy : theory) =
huffman@37109
   444
  let
huffman@37109
   445
huffman@37109
   446
    (* utility functions *)
huffman@37109
   447
    fun mk_pair_pat (p1, p2) =
huffman@37109
   448
      let
huffman@37109
   449
        val T1 = fastype_of p1;
huffman@37109
   450
        val T2 = fastype_of p2;
huffman@37109
   451
        val (U1, V1) = apsnd dest_matchT (dest_cfunT T1);
huffman@37109
   452
        val (U2, V2) = apsnd dest_matchT (dest_cfunT T2);
huffman@37109
   453
        val pat_typ = [T1, T2] --->
huffman@37109
   454
            (mk_prodT (U1, U2) ->> mk_matchT (mk_prodT (V1, V2)));
huffman@37109
   455
        val pat_const = Const (@{const_name cpair_pat}, pat_typ);
huffman@37109
   456
      in
huffman@37109
   457
        pat_const $ p1 $ p2
huffman@37109
   458
      end;
huffman@37109
   459
    fun mk_tuple_pat [] = succeed_const HOLogic.unitT
huffman@37109
   460
      | mk_tuple_pat ps = foldr1 mk_pair_pat ps;
huffman@37109
   461
    fun branch_const (T,U,V) = 
huffman@37109
   462
      Const (@{const_name branch},
huffman@37109
   463
        (T ->> mk_matchT U) --> (U ->> V) ->> T ->> mk_matchT V);
huffman@37109
   464
huffman@37109
   465
    (* define pattern combinators *)
huffman@37109
   466
    local
huffman@37109
   467
      val tns = map (fst o dest_TFree) (snd (dest_Type lhsT));
huffman@37109
   468
huffman@37109
   469
      fun pat_eqn (i, (bind, (con, args))) : binding * term * mixfix =
huffman@37109
   470
        let
huffman@37109
   471
          val pat_bind = Binding.suffix_name "_pat" bind;
huffman@37109
   472
          val Ts = map snd args;
huffman@37109
   473
          val Vs =
huffman@37109
   474
              (map (K "'t") args)
huffman@37109
   475
              |> Datatype_Prop.indexify_names
huffman@37109
   476
              |> Name.variant_list tns
huffman@37109
   477
              |> map (fn t => TFree (t, @{sort pcpo}));
huffman@37109
   478
          val patNs = Datatype_Prop.indexify_names (map (K "pat") args);
huffman@37109
   479
          val patTs = map2 (fn T => fn V => T ->> mk_matchT V) Ts Vs;
huffman@37109
   480
          val pats = map Free (patNs ~~ patTs);
huffman@37109
   481
          val fail = mk_fail (mk_tupleT Vs);
huffman@37109
   482
          val (vs, nonlazy) = get_vars_avoiding patNs args;
huffman@37109
   483
          val rhs = big_lambdas vs (mk_tuple_pat pats ` mk_tuple vs);
huffman@37109
   484
          fun one_fun (j, (_, args')) =
huffman@37109
   485
            let
huffman@37109
   486
              val (vs', nonlazy) = get_vars_avoiding patNs args';
huffman@37109
   487
            in if i = j then rhs else big_lambdas vs' fail end;
huffman@37109
   488
          val funs = map_index one_fun spec;
huffman@37109
   489
          val body = list_ccomb (case_const (mk_matchT (mk_tupleT Vs)), funs);
huffman@37109
   490
        in
huffman@37109
   491
          (pat_bind, lambdas pats body, NoSyn)
huffman@37109
   492
        end;
huffman@37109
   493
    in
huffman@37109
   494
      val ((pat_consts, pat_defs), thy) =
huffman@37109
   495
          define_consts (map_index pat_eqn (bindings ~~ spec)) thy
huffman@37109
   496
    end;
huffman@37109
   497
huffman@37109
   498
    (* syntax translations for pattern combinators *)
huffman@37109
   499
    local
huffman@37109
   500
      open Syntax
huffman@37109
   501
      fun syntax c = Syntax.mark_const (fst (dest_Const c));
huffman@37109
   502
      fun app s (l, r) = Syntax.mk_appl (Constant s) [l, r];
huffman@40327
   503
      val capp = app @{const_syntax Rep_cfun};
huffman@37109
   504
      val capps = Library.foldl capp
huffman@37109
   505
huffman@37109
   506
      fun app_var x = Syntax.mk_appl (Constant "_variable") [x, Variable "rhs"];
huffman@37109
   507
      fun app_pat x = Syntax.mk_appl (Constant "_pat") [x];
huffman@37109
   508
      fun args_list [] = Constant "_noargs"
huffman@37109
   509
        | args_list xs = foldr1 (app "_args") xs;
huffman@37109
   510
      fun one_case_trans (pat, (con, args)) =
huffman@37109
   511
        let
huffman@37109
   512
          val cname = Constant (syntax con);
huffman@37109
   513
          val pname = Constant (syntax pat);
huffman@37109
   514
          val ns = 1 upto length args;
huffman@37109
   515
          val xs = map (fn n => Variable ("x"^(string_of_int n))) ns;
huffman@37109
   516
          val ps = map (fn n => Variable ("p"^(string_of_int n))) ns;
huffman@37109
   517
          val vs = map (fn n => Variable ("v"^(string_of_int n))) ns;
huffman@37109
   518
        in
huffman@37109
   519
          [ParseRule (app_pat (capps (cname, xs)),
huffman@37109
   520
                      mk_appl pname (map app_pat xs)),
huffman@37109
   521
           ParseRule (app_var (capps (cname, xs)),
huffman@37109
   522
                      app_var (args_list xs)),
huffman@37109
   523
           PrintRule (capps (cname, ListPair.map (app "_match") (ps,vs)),
huffman@37109
   524
                      app "_match" (mk_appl pname ps, args_list vs))]
huffman@37109
   525
        end;
huffman@37109
   526
      val trans_rules : Syntax.ast Syntax.trrule list =
huffman@37109
   527
          maps one_case_trans (pat_consts ~~ spec);
huffman@37109
   528
    in
huffman@37109
   529
      val thy = Sign.add_trrules_i trans_rules thy;
huffman@37109
   530
    end;
huffman@37109
   531
huffman@37109
   532
    (* prove strictness and reduction rules of pattern combinators *)
huffman@37109
   533
    local
huffman@37109
   534
      val tns = map (fst o dest_TFree) (snd (dest_Type lhsT));
huffman@37109
   535
      val rn = Name.variant tns "'r";
huffman@37109
   536
      val R = TFree (rn, @{sort pcpo});
huffman@37109
   537
      fun pat_lhs (pat, args) =
huffman@37109
   538
        let
huffman@37109
   539
          val Ts = map snd args;
huffman@37109
   540
          val Vs =
huffman@37109
   541
              (map (K "'t") args)
huffman@37109
   542
              |> Datatype_Prop.indexify_names
huffman@37109
   543
              |> Name.variant_list (rn::tns)
huffman@37109
   544
              |> map (fn t => TFree (t, @{sort pcpo}));
huffman@37109
   545
          val patNs = Datatype_Prop.indexify_names (map (K "pat") args);
huffman@37109
   546
          val patTs = map2 (fn T => fn V => T ->> mk_matchT V) Ts Vs;
huffman@37109
   547
          val pats = map Free (patNs ~~ patTs);
huffman@37109
   548
          val k = Free ("rhs", mk_tupleT Vs ->> R);
huffman@37109
   549
          val branch1 = branch_const (lhsT, mk_tupleT Vs, R);
huffman@37109
   550
          val fun1 = (branch1 $ list_comb (pat, pats)) ` k;
huffman@37109
   551
          val branch2 = branch_const (mk_tupleT Ts, mk_tupleT Vs, R);
huffman@37109
   552
          val fun2 = (branch2 $ mk_tuple_pat pats) ` k;
huffman@37109
   553
          val taken = "rhs" :: patNs;
huffman@37109
   554
        in (fun1, fun2, taken) end;
huffman@37109
   555
      fun pat_strict (pat, (con, args)) =
huffman@37109
   556
        let
huffman@37109
   557
          val (fun1, fun2, taken) = pat_lhs (pat, args);
huffman@37109
   558
          val defs = @{thm branch_def} :: pat_defs;
huffman@37109
   559
          val goal = mk_trp (mk_strict fun1);
huffman@40735
   560
          val rules = @{thms match_bind_simps} @ case_rews;
huffman@37109
   561
          val tacs = [simp_tac (beta_ss addsimps rules) 1];
huffman@37109
   562
        in prove thy defs goal (K tacs) end;
huffman@37109
   563
      fun pat_apps (i, (pat, (con, args))) =
huffman@37109
   564
        let
huffman@37109
   565
          val (fun1, fun2, taken) = pat_lhs (pat, args);
huffman@37109
   566
          fun pat_app (j, (con', args')) =
huffman@37109
   567
            let
huffman@37109
   568
              val (vs, nonlazy) = get_vars_avoiding taken args';
huffman@37109
   569
              val con_app = list_ccomb (con', vs);
huffman@37109
   570
              val assms = map (mk_trp o mk_defined) nonlazy;
huffman@37109
   571
              val rhs = if i = j then fun2 ` mk_tuple vs else mk_fail R;
huffman@37109
   572
              val concl = mk_trp (mk_eq (fun1 ` con_app, rhs));
huffman@37109
   573
              val goal = Logic.list_implies (assms, concl);
huffman@37109
   574
              val defs = @{thm branch_def} :: pat_defs;
huffman@40735
   575
              val rules = @{thms match_bind_simps} @ case_rews;
huffman@37109
   576
              val tacs = [asm_simp_tac (beta_ss addsimps rules) 1];
huffman@37109
   577
            in prove thy defs goal (K tacs) end;
huffman@37109
   578
        in map_index pat_app spec end;
huffman@37109
   579
    in
huffman@37109
   580
      val pat_stricts = map pat_strict (pat_consts ~~ spec);
huffman@37109
   581
      val pat_apps = flat (map_index pat_apps (pat_consts ~~ spec));
huffman@37109
   582
    end;
huffman@37109
   583
huffman@37109
   584
  in
huffman@37109
   585
    (pat_stricts @ pat_apps, thy)
huffman@37109
   586
  end
huffman@37109
   587
huffman@37109
   588
end
huffman@37109
   589
*}
huffman@37109
   590
huffman@37109
   591
(*
huffman@37109
   592
Cut from HOLCF/Tools/domain_constructors.ML
huffman@37109
   593
in function add_domain_constructors:
huffman@37109
   594
huffman@37109
   595
    ( * define and prove theorems for pattern combinators * )
huffman@37109
   596
    val (pat_thms : thm list, thy : theory) =
huffman@37109
   597
      let
huffman@37109
   598
        val bindings = map #1 spec;
huffman@37109
   599
        fun prep_arg (lazy, sel, T) = (lazy, T);
huffman@37109
   600
        fun prep_con c (b, args, mx) = (c, map prep_arg args);
huffman@37109
   601
        val pat_spec = map2 prep_con con_consts spec;
huffman@37109
   602
      in
huffman@37109
   603
        add_pattern_combinators bindings pat_spec lhsT
huffman@37109
   604
          exhaust case_const cases thy
huffman@37109
   605
      end
huffman@37109
   606
huffman@37109
   607
*)
huffman@37109
   608
huffman@37109
   609
end