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