src/HOLCF/Fixrec.thy
author huffman
Wed, 30 Nov 2005 01:01:15 +0100
changeset 18293 4eaa654c92f2
parent 18112 dc1d6f588204
child 19092 e32cf29f01fc
permissions -rw-r--r--
reimplement Case expression pattern matching to support lazy patterns
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
     1
(*  Title:      HOLCF/Fixrec.thy
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
     2
    ID:         $Id$
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
     3
    Author:     Amber Telfer and Brian Huffman
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
     4
*)
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
     5
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
     6
header "Package for defining recursive functions in HOLCF"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
     7
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
     8
theory Fixrec
16551
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
     9
imports Sprod Ssum Up One Tr Fix
16417
9bc16273c2d4 migrated theory headers to new format
haftmann
parents: 16401
diff changeset
    10
uses ("fixrec_package.ML")
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    11
begin
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    12
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    13
subsection {* Maybe monad type *}
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    14
16776
a3899ac14a1c generalized types of monadic operators to class cpo; added match function for UU
huffman
parents: 16758
diff changeset
    15
defaultsort cpo
a3899ac14a1c generalized types of monadic operators to class cpo; added match function for UU
huffman
parents: 16758
diff changeset
    16
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    17
types 'a maybe = "one ++ 'a u"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    18
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    19
constdefs
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    20
  fail :: "'a maybe"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    21
  "fail \<equiv> sinl\<cdot>ONE"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    22
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    23
  return :: "'a \<rightarrow> 'a maybe"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    24
  "return \<equiv> sinr oo up"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    25
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    26
lemma maybeE:
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    27
  "\<lbrakk>p = \<bottom> \<Longrightarrow> Q; p = fail \<Longrightarrow> Q; \<And>x. p = return\<cdot>x \<Longrightarrow> Q\<rbrakk> \<Longrightarrow> Q"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    28
apply (unfold fail_def return_def)
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    29
apply (rule_tac p=p in ssumE, simp)
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    30
apply (rule_tac p=x in oneE, simp, simp)
16754
1b979f8b7e8e renamed upE1 to upE
huffman
parents: 16551
diff changeset
    31
apply (rule_tac p=y in upE, simp, simp)
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    32
done
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    33
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
    34
lemma return_defined [simp]: "return\<cdot>x \<noteq> \<bottom>"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
    35
by (simp add: return_def)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
    36
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
    37
lemma fail_defined [simp]: "fail \<noteq> \<bottom>"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
    38
by (simp add: fail_def)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
    39
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
    40
lemma return_eq [simp]: "(return\<cdot>x = return\<cdot>y) = (x = y)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
    41
by (simp add: return_def)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
    42
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
    43
lemma return_neq_fail [simp]:
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
    44
  "return\<cdot>x \<noteq> fail" "fail \<noteq> return\<cdot>x"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
    45
by (simp_all add: return_def fail_def)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
    46
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
    47
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
    48
subsubsection {* Monadic bind operator *}
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    49
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
    50
constdefs
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
    51
  bind :: "'a maybe \<rightarrow> ('a \<rightarrow> 'b maybe) \<rightarrow> 'b maybe"
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
    52
  "bind \<equiv> \<Lambda> m f. sscase\<cdot>sinl\<cdot>(fup\<cdot>f)\<cdot>m"
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
    53
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
    54
syntax ">>=" :: "['a maybe, 'a \<rightarrow> 'b maybe] \<Rightarrow> 'b maybe" (infixl ">>=" 50)
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
    55
translations "m >>= f" == "bind\<cdot>m\<cdot>f"
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    56
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    57
nonterminals
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    58
  maybebind maybebinds
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    59
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    60
syntax 
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    61
  "_MBIND"  :: "pttrn \<Rightarrow> 'a maybe \<Rightarrow> maybebind"         ("(2_ <-/ _)" 10)
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    62
  ""        :: "maybebind \<Rightarrow> maybebinds"                ("_")
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    63
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    64
  "_MBINDS" :: "[maybebind, maybebinds] \<Rightarrow> maybebinds"  ("_;/ _")
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    65
  "_MDO"    :: "[maybebinds, 'a maybe] \<Rightarrow> 'a maybe"     ("(do _;/ (_))" 10)
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    66
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    67
translations
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    68
  "_MDO (_MBINDS b bs) e" == "_MDO b (_MDO bs e)"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    69
  "do (x,y) <- m; e" == "m >>= (LAM <x,y>. e)" 
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    70
  "do x <- m; e"            == "m >>= (LAM x. e)"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    71
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    72
text {* monad laws *}
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    73
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    74
lemma bind_strict [simp]: "UU >>= f = UU"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    75
by (simp add: bind_def)
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    76
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    77
lemma bind_fail [simp]: "fail >>= f = fail"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    78
by (simp add: bind_def fail_def)
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    79
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    80
lemma left_unit [simp]: "(return\<cdot>a) >>= k = k\<cdot>a"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    81
by (simp add: bind_def return_def)
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    82
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    83
lemma right_unit [simp]: "m >>= return = m"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    84
by (rule_tac p=m in maybeE, simp_all)
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    85
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    86
lemma bind_assoc [simp]:
16779
ac1dc3d4746a changed orientation of bind_assoc rule
huffman
parents: 16776
diff changeset
    87
 "(do b <- (do a <- m; k\<cdot>a); h\<cdot>b) = (do a <- m; b <- k\<cdot>a; h\<cdot>b)"
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    88
by (rule_tac p=m in maybeE, simp_all)
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    89
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
    90
subsubsection {* Run operator *}
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    91
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    92
constdefs
16776
a3899ac14a1c generalized types of monadic operators to class cpo; added match function for UU
huffman
parents: 16758
diff changeset
    93
  run:: "'a::pcpo maybe \<rightarrow> 'a"
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    94
  "run \<equiv> sscase\<cdot>\<bottom>\<cdot>(fup\<cdot>ID)"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    95
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    96
text {* rewrite rules for run *}
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    97
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    98
lemma run_strict [simp]: "run\<cdot>\<bottom> = \<bottom>"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
    99
by (simp add: run_def)
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   100
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   101
lemma run_fail [simp]: "run\<cdot>fail = \<bottom>"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   102
by (simp add: run_def fail_def)
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   103
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   104
lemma run_return [simp]: "run\<cdot>(return\<cdot>x) = x"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   105
by (simp add: run_def return_def)
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   106
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   107
subsubsection {* Monad plus operator *}
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   108
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   109
constdefs
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   110
  mplus :: "'a maybe \<rightarrow> 'a maybe \<rightarrow> 'a maybe"
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   111
  "mplus \<equiv> \<Lambda> m1 m2. sscase\<cdot>(\<Lambda> x. m2)\<cdot>(fup\<cdot>return)\<cdot>m1"
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   112
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   113
syntax "+++" :: "['a maybe, 'a maybe] \<Rightarrow> 'a maybe" (infixr "+++" 65)
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   114
translations "m1 +++ m2" == "mplus\<cdot>m1\<cdot>m2"
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   115
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   116
text {* rewrite rules for mplus *}
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   117
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   118
lemma mplus_strict [simp]: "\<bottom> +++ m = \<bottom>"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   119
by (simp add: mplus_def)
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   120
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   121
lemma mplus_fail [simp]: "fail +++ m = m"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   122
by (simp add: mplus_def fail_def)
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   123
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   124
lemma mplus_return [simp]: "return\<cdot>x +++ m = return\<cdot>x"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   125
by (simp add: mplus_def return_def)
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   126
16460
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   127
lemma mplus_fail2 [simp]: "m +++ fail = m"
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   128
by (rule_tac p=m in maybeE, simp_all)
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   129
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   130
lemma mplus_assoc: "(x +++ y) +++ z = x +++ (y +++ z)"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   131
by (rule_tac p=x in maybeE, simp_all)
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   132
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   133
subsubsection {* Fatbar combinator *}
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   134
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   135
constdefs
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   136
  fatbar :: "('a \<rightarrow> 'b maybe) \<rightarrow> ('a \<rightarrow> 'b maybe) \<rightarrow> ('a \<rightarrow> 'b maybe)"
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   137
  "fatbar \<equiv> \<Lambda> a b x. a\<cdot>x +++ b\<cdot>x"
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   138
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   139
syntax
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   140
  "\<parallel>" :: "['a \<rightarrow> 'b maybe, 'a \<rightarrow> 'b maybe] \<Rightarrow> 'a \<rightarrow> 'b maybe" (infixr "\<parallel>" 60)
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   141
translations
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   142
  "m1 \<parallel> m2" == "fatbar\<cdot>m1\<cdot>m2"
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   143
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   144
lemma fatbar1: "m\<cdot>x = \<bottom> \<Longrightarrow> (m \<parallel> ms)\<cdot>x = \<bottom>"
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   145
by (simp add: fatbar_def)
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   146
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   147
lemma fatbar2: "m\<cdot>x = fail \<Longrightarrow> (m \<parallel> ms)\<cdot>x = ms\<cdot>x"
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   148
by (simp add: fatbar_def)
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   149
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   150
lemma fatbar3: "m\<cdot>x = return\<cdot>y \<Longrightarrow> (m \<parallel> ms)\<cdot>x = return\<cdot>y"
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   151
by (simp add: fatbar_def)
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   152
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   153
lemmas fatbar_simps = fatbar1 fatbar2 fatbar3
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   154
18112
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   155
lemma run_fatbar1: "m\<cdot>x = \<bottom> \<Longrightarrow> run\<cdot>((m \<parallel> ms)\<cdot>x) = \<bottom>"
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   156
by (simp add: fatbar_def)
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   157
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   158
lemma run_fatbar2: "m\<cdot>x = fail \<Longrightarrow> run\<cdot>((m \<parallel> ms)\<cdot>x) = run\<cdot>(ms\<cdot>x)"
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   159
by (simp add: fatbar_def)
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   160
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   161
lemma run_fatbar3: "m\<cdot>x = return\<cdot>y \<Longrightarrow> run\<cdot>((m \<parallel> ms)\<cdot>x) = y"
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   162
by (simp add: fatbar_def)
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   163
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   164
lemmas run_fatbar_simps [simp] = run_fatbar1 run_fatbar2 run_fatbar3
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   165
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   166
subsection {* Case branch combinator *}
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   167
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   168
constdefs
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   169
  branch :: "('a \<rightarrow> 'b maybe) \<Rightarrow> ('b \<rightarrow> 'c) \<rightarrow> ('a \<rightarrow> 'c maybe)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   170
  "branch p \<equiv> \<Lambda> r x. bind\<cdot>(p\<cdot>x)\<cdot>(\<Lambda> y. return\<cdot>(r\<cdot>y))"
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   171
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   172
lemma branch_rews:
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   173
  "p\<cdot>x = \<bottom> \<Longrightarrow> branch p\<cdot>r\<cdot>x = \<bottom>"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   174
  "p\<cdot>x = fail \<Longrightarrow> branch p\<cdot>r\<cdot>x = fail"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   175
  "p\<cdot>x = return\<cdot>y \<Longrightarrow> branch p\<cdot>r\<cdot>x = return\<cdot>(r\<cdot>y)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   176
by (simp_all add: branch_def)
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   177
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   178
lemma branch_return [simp]: "branch return\<cdot>r\<cdot>x = return\<cdot>(r\<cdot>x)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   179
by (simp add: branch_def)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   180
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   181
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   182
subsection {* Case syntax *}
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   183
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   184
nonterminals
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   185
  Case_syn  Cases_syn
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   186
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   187
syntax
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   188
  "_Case_syntax":: "['a, Cases_syn] => 'b"               ("(Case _ of/ _)" 10)
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   189
  "_Case1"      :: "['a, 'b] => Case_syn"                ("(2_ =>/ _)" 10)
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   190
  ""            :: "Case_syn => Cases_syn"               ("_")
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   191
  "_Case2"      :: "[Case_syn, Cases_syn] => Cases_syn"  ("_/ | _")
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   192
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   193
syntax (xsymbols)
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   194
  "_Case1"      :: "['a, 'b] => Case_syn"                ("(2_ \<Rightarrow>/ _)" 10)
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   195
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   196
translations
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   197
  "_Case_syntax x ms" == "run\<cdot>(ms\<cdot>x)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   198
  "_Case2 m ms" == "m \<parallel> ms"
18112
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   199
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   200
text {* Parsing Case expressions *}
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   201
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   202
syntax
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   203
  "_pat" :: "'a"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   204
  "_var" :: "'a"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   205
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   206
translations
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   207
  "_Case1 p r" => "branch (_pat p)\<cdot>(_var p r)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   208
  "_var (_args x y) r" => "csplit\<cdot>(_var x (_var y r))"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   209
  "_var () r" => "unit_when\<cdot>r"
18112
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   210
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   211
parse_translation {*
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   212
(* rewrites (_pat x) => (return) *)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   213
(* rewrites (_var x t) => (Abs_CFun (%x. t)) *)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   214
  [("_pat", K (Syntax.const "return")),
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   215
   mk_binder_tr ("_var", "Abs_CFun")];
18112
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   216
*}
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   217
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   218
text {* Printing Case expressions *}
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   219
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   220
syntax
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   221
  "_match" :: "'a"
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   222
18112
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   223
print_translation {*
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   224
  let
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   225
    fun dest_LAM (Const ("Rep_CFun",_) $ Const ("unit_when",_) $ t) =
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   226
          (Syntax.const "Unity", t)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   227
    |   dest_LAM (Const ("Rep_CFun",_) $ Const ("csplit",_) $ t) =
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   228
          let
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   229
            val (v1, t1) = dest_LAM t;
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   230
            val (v2, t2) = dest_LAM t1;
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   231
          in (Syntax.const "_args" $ v1 $ v2, t2) end 
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   232
    |   dest_LAM (Const ("Abs_CFun",_) $ t) =
18112
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   233
          let
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   234
            val abs = case t of Abs abs => abs
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   235
                | _ => ("x", dummyT, incr_boundvars 1 t $ Bound 0);
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   236
            val (x, t') = atomic_abs_tr' abs;
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   237
          in (Syntax.const "_var" $ x, t') end
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   238
    |   dest_LAM _ = raise Match; (* too few vars: abort translation *)
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   239
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   240
    fun Case1_tr' [Const("branch",_) $ p, r] =
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   241
          let val (v, t) = dest_LAM r;
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   242
          in Syntax.const "_Case1" $ (Syntax.const "_match" $ p $ v) $ t end;
18112
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   243
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   244
  in [("Rep_CFun", Case1_tr')] end;
18112
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   245
*}
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   246
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   247
translations
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   248
  "x" <= "_match return (_var x)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   249
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   250
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   251
subsection {* Pattern combinators for data constructors *}
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   252
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   253
types ('a, 'b) pat = "'a \<rightarrow> 'b maybe"
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   254
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   255
constdefs
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   256
  cpair_pat :: "('a, 'c) pat \<Rightarrow> ('b, 'd) pat \<Rightarrow> ('a \<times> 'b, 'c \<times> 'd) pat"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   257
  "cpair_pat p1 p2 \<equiv> \<Lambda>\<langle>x, y\<rangle>. do a <- p1\<cdot>x; b <- p2\<cdot>y; return\<cdot>\<langle>a, b\<rangle>"
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   258
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   259
  spair_pat ::
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   260
  "('a, 'c) pat \<Rightarrow> ('b, 'd) pat \<Rightarrow> ('a::pcpo \<otimes> 'b::pcpo, 'c \<times> 'd) pat"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   261
  "spair_pat p1 p2 \<equiv> \<Lambda>(:x, y:). cpair_pat p1 p2\<cdot>\<langle>x, y\<rangle>"
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   262
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   263
  sinl_pat :: "('a, 'c) pat \<Rightarrow> ('a::pcpo \<oplus> 'b::pcpo, 'c) pat"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   264
  "sinl_pat p \<equiv> sscase\<cdot>p\<cdot>(\<Lambda> x. fail)"
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   265
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   266
  sinr_pat :: "('b, 'c) pat \<Rightarrow> ('a::pcpo \<oplus> 'b::pcpo, 'c) pat"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   267
  "sinr_pat p \<equiv> sscase\<cdot>(\<Lambda> x. fail)\<cdot>p"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   268
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   269
  up_pat :: "('a, 'b) pat \<Rightarrow> ('a u, 'b) pat"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   270
  "up_pat p \<equiv> fup\<cdot>p"
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   271
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   272
  TT_pat :: "(tr, unit) pat"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   273
  "TT_pat \<equiv> \<Lambda> b. If b then return\<cdot>() else fail fi"
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   274
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   275
  FF_pat :: "(tr, unit) pat"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   276
  "FF_pat \<equiv> \<Lambda> b. If b then fail else return\<cdot>() fi"
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   277
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   278
  ONE_pat :: "(one, unit) pat"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   279
  "ONE_pat \<equiv> \<Lambda> ONE. return\<cdot>()"
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   280
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   281
text {* Parse translations (patterns) *}
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   282
translations
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   283
  "_pat (cpair\<cdot>x\<cdot>y)" => "cpair_pat (_pat x) (_pat y)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   284
  "_pat (spair\<cdot>x\<cdot>y)" => "spair_pat (_pat x) (_pat y)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   285
  "_pat (sinl\<cdot>x)" => "sinl_pat (_pat x)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   286
  "_pat (sinr\<cdot>x)" => "sinr_pat (_pat x)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   287
  "_pat (up\<cdot>x)" => "up_pat (_pat x)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   288
  "_pat TT" => "TT_pat"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   289
  "_pat FF" => "FF_pat"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   290
  "_pat ONE" => "ONE_pat"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   291
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   292
text {* Parse translations (variables) *}
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   293
translations
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   294
  "_var (cpair\<cdot>x\<cdot>y) r" => "_var (_args x y) r"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   295
  "_var (spair\<cdot>x\<cdot>y) r" => "_var (_args x y) r"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   296
  "_var (sinl\<cdot>x) r" => "_var x r"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   297
  "_var (sinr\<cdot>x) r" => "_var x r"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   298
  "_var (up\<cdot>x) r" => "_var x r"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   299
  "_var TT r" => "_var () r"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   300
  "_var FF r" => "_var () r"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   301
  "_var ONE r" => "_var () r"
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   302
18112
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   303
text {* Print translations *}
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   304
translations
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   305
  "cpair\<cdot>(_match p1 v1)\<cdot>(_match p2 v2)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   306
      <= "_match (cpair_pat p1 p2) (_args v1 v2)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   307
  "spair\<cdot>(_match p1 v1)\<cdot>(_match p2 v2)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   308
      <= "_match (spair_pat p1 p2) (_args v1 v2)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   309
  "sinl\<cdot>(_match p1 v1)" <= "_match (sinl_pat p1) v1"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   310
  "sinr\<cdot>(_match p1 v1)" <= "_match (sinr_pat p1) v1"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   311
  "up\<cdot>(_match p1 v1)" <= "_match (up_pat p1) v1"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   312
  "TT" <= "_match TT_pat ()"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   313
  "FF" <= "_match FF_pat ()"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   314
  "ONE" <= "_match ONE_pat ()"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   315
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   316
lemma cpair_pat1:
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   317
  "branch p\<cdot>r\<cdot>x = \<bottom> \<Longrightarrow> branch (cpair_pat p q)\<cdot>(csplit\<cdot>r)\<cdot>\<langle>x, y\<rangle> = \<bottom>"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   318
apply (simp add: branch_def cpair_pat_def)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   319
apply (rule_tac p="p\<cdot>x" in maybeE, simp_all)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   320
done
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   321
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   322
lemma cpair_pat2:
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   323
  "branch p\<cdot>r\<cdot>x = fail \<Longrightarrow> branch (cpair_pat p q)\<cdot>(csplit\<cdot>r)\<cdot>\<langle>x, y\<rangle> = fail"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   324
apply (simp add: branch_def cpair_pat_def)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   325
apply (rule_tac p="p\<cdot>x" in maybeE, simp_all)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   326
done
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   327
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   328
lemma cpair_pat3:
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   329
  "branch p\<cdot>r\<cdot>x = return\<cdot>s \<Longrightarrow>
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   330
   branch (cpair_pat p q)\<cdot>(csplit\<cdot>r)\<cdot>\<langle>x, y\<rangle> = branch q\<cdot>s\<cdot>y"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   331
apply (simp add: branch_def cpair_pat_def)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   332
apply (rule_tac p="p\<cdot>x" in maybeE, simp_all)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   333
apply (rule_tac p="q\<cdot>y" in maybeE, simp_all)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   334
done
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   335
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   336
lemmas cpair_pat [simp] =
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   337
  cpair_pat1 cpair_pat2 cpair_pat3
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   338
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   339
lemma spair_pat [simp]:
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   340
  "branch (spair_pat p1 p2)\<cdot>r\<cdot>\<bottom> = \<bottom>"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   341
  "\<lbrakk>x \<noteq> \<bottom>; y \<noteq> \<bottom>\<rbrakk>
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   342
     \<Longrightarrow> branch (spair_pat p1 p2)\<cdot>r\<cdot>(:x, y:) =
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   343
         branch (cpair_pat p1 p2)\<cdot>r\<cdot>\<langle>x, y\<rangle>"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   344
by (simp_all add: branch_def spair_pat_def)
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   345
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   346
lemma sinl_pat [simp]:
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   347
  "branch (sinl_pat p)\<cdot>r\<cdot>\<bottom> = \<bottom>"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   348
  "x \<noteq> \<bottom> \<Longrightarrow> branch (sinl_pat p)\<cdot>r\<cdot>(sinl\<cdot>x) = branch p\<cdot>r\<cdot>x"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   349
  "y \<noteq> \<bottom> \<Longrightarrow> branch (sinl_pat p)\<cdot>r\<cdot>(sinr\<cdot>y) = fail"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   350
by (simp_all add: branch_def sinl_pat_def)
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   351
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   352
lemma sinr_pat [simp]:
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   353
  "branch (sinr_pat p)\<cdot>r\<cdot>\<bottom> = \<bottom>"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   354
  "x \<noteq> \<bottom> \<Longrightarrow> branch (sinr_pat p)\<cdot>r\<cdot>(sinl\<cdot>x) = fail"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   355
  "y \<noteq> \<bottom> \<Longrightarrow> branch (sinr_pat p)\<cdot>r\<cdot>(sinr\<cdot>y) = branch p\<cdot>r\<cdot>y"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   356
by (simp_all add: branch_def sinr_pat_def)
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   357
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   358
lemma up_pat [simp]:
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   359
  "branch (up_pat p)\<cdot>r\<cdot>\<bottom> = \<bottom>"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   360
  "branch (up_pat p)\<cdot>r\<cdot>(up\<cdot>x) = branch p\<cdot>r\<cdot>x"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   361
by (simp_all add: branch_def up_pat_def)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   362
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   363
lemma TT_pat [simp]:
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   364
  "branch TT_pat\<cdot>(unit_when\<cdot>r)\<cdot>\<bottom> = \<bottom>"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   365
  "branch TT_pat\<cdot>(unit_when\<cdot>r)\<cdot>TT = return\<cdot>r"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   366
  "branch TT_pat\<cdot>(unit_when\<cdot>r)\<cdot>FF = fail"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   367
by (simp_all add: branch_def TT_pat_def)
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   368
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   369
lemma FF_pat [simp]:
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   370
  "branch FF_pat\<cdot>(unit_when\<cdot>r)\<cdot>\<bottom> = \<bottom>"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   371
  "branch FF_pat\<cdot>(unit_when\<cdot>r)\<cdot>TT = fail"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   372
  "branch FF_pat\<cdot>(unit_when\<cdot>r)\<cdot>FF = return\<cdot>r"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   373
by (simp_all add: branch_def FF_pat_def)
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   374
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   375
lemma ONE_pat [simp]:
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   376
  "branch ONE_pat\<cdot>(unit_when\<cdot>r)\<cdot>\<bottom> = \<bottom>"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   377
  "branch ONE_pat\<cdot>(unit_when\<cdot>r)\<cdot>ONE = return\<cdot>r"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   378
by (simp_all add: branch_def ONE_pat_def)
18097
d196d84c306f add case syntax stuff
huffman
parents: 18096
diff changeset
   379
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   380
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   381
subsection {* Wildcards, as-patterns, and lazy patterns *}
18112
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   382
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   383
syntax
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   384
  "_as_pat" :: "[idt, 'a] \<Rightarrow> 'a" (infixr "\<as>" 10)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   385
  "_lazy_pat" :: "'a \<Rightarrow> 'a" ("\<lazy> _" [1000] 1000)
18112
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   386
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   387
constdefs
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   388
  wild_pat :: "'a \<rightarrow> unit maybe"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   389
  "wild_pat \<equiv> \<Lambda> x. return\<cdot>()"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   390
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   391
  as_pat :: "('a \<rightarrow> 'b maybe) \<Rightarrow> 'a \<rightarrow> ('a \<times> 'b) maybe"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   392
  "as_pat p \<equiv> \<Lambda> x. do a <- p\<cdot>x; return\<cdot>\<langle>x, a\<rangle>"
18112
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   393
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   394
  lazy_pat :: "('a \<rightarrow> 'b::pcpo maybe) \<Rightarrow> ('a \<rightarrow> 'b maybe)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   395
  "lazy_pat p \<equiv> \<Lambda> x. return\<cdot>(run\<cdot>(p\<cdot>x))"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   396
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   397
text {* Parse translations (patterns) *}
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   398
translations
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   399
  "_pat _" => "wild_pat"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   400
  "_pat (_as_pat x y)" => "as_pat (_pat y)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   401
  "_pat (_lazy_pat x)" => "lazy_pat (_pat x)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   402
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   403
text {* Parse translations (variables) *}
18112
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   404
translations
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   405
  "_var _ r" => "_var () r"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   406
  "_var (_as_pat x y) r" => "_var (_args x y) r"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   407
  "_var (_lazy_pat x) r" => "_var x r"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   408
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   409
text {* Print translations *}
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   410
translations
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   411
  "_" <= "_match wild_pat ()"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   412
  "_as_pat x (_match p v)" <= "_match (as_pat p) (_args (_var x) v)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   413
  "_lazy_pat (_match p v)" <= "_match (lazy_pat p) v"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   414
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   415
lemma wild_pat [simp]: "branch wild_pat\<cdot>(unit_when\<cdot>r)\<cdot>x = return\<cdot>r"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   416
by (simp add: branch_def wild_pat_def)
18112
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   417
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   418
lemma as_pat [simp]:
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   419
  "branch (as_pat p)\<cdot>(csplit\<cdot>r)\<cdot>x = branch p\<cdot>(r\<cdot>x)\<cdot>x"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   420
apply (simp add: branch_def as_pat_def)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   421
apply (rule_tac p="p\<cdot>x" in maybeE, simp_all)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   422
done
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   423
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   424
lemma lazy_pat [simp]:
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   425
  "branch p\<cdot>r\<cdot>x = \<bottom> \<Longrightarrow> branch (lazy_pat p)\<cdot>r\<cdot>x = return\<cdot>(r\<cdot>\<bottom>)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   426
  "branch p\<cdot>r\<cdot>x = fail \<Longrightarrow> branch (lazy_pat p)\<cdot>r\<cdot>x = return\<cdot>(r\<cdot>\<bottom>)"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   427
  "branch p\<cdot>r\<cdot>x = return\<cdot>s \<Longrightarrow> branch (lazy_pat p)\<cdot>r\<cdot>x = return\<cdot>s"
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   428
apply (simp_all add: branch_def lazy_pat_def)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   429
apply (rule_tac [!] p="p\<cdot>x" in maybeE, simp_all)
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   430
done
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   431
18112
dc1d6f588204 reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
huffman
parents: 18110
diff changeset
   432
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   433
subsection {* Match functions for built-in types *}
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   434
16776
a3899ac14a1c generalized types of monadic operators to class cpo; added match function for UU
huffman
parents: 16758
diff changeset
   435
defaultsort pcpo
a3899ac14a1c generalized types of monadic operators to class cpo; added match function for UU
huffman
parents: 16758
diff changeset
   436
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   437
constdefs
16776
a3899ac14a1c generalized types of monadic operators to class cpo; added match function for UU
huffman
parents: 16758
diff changeset
   438
  match_UU :: "'a \<rightarrow> unit maybe"
a3899ac14a1c generalized types of monadic operators to class cpo; added match function for UU
huffman
parents: 16758
diff changeset
   439
  "match_UU \<equiv> \<Lambda> x. fail"
a3899ac14a1c generalized types of monadic operators to class cpo; added match function for UU
huffman
parents: 16758
diff changeset
   440
a3899ac14a1c generalized types of monadic operators to class cpo; added match function for UU
huffman
parents: 16758
diff changeset
   441
  match_cpair :: "'a::cpo \<times> 'b::cpo \<rightarrow> ('a \<times> 'b) maybe"
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   442
  "match_cpair \<equiv> csplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   443
16551
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   444
  match_spair :: "'a \<otimes> 'b \<rightarrow> ('a \<times> 'b) maybe"
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   445
  "match_spair \<equiv> ssplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)"
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   446
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   447
  match_sinl :: "'a \<oplus> 'b \<rightarrow> 'a maybe"
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   448
  "match_sinl \<equiv> sscase\<cdot>return\<cdot>(\<Lambda> y. fail)"
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   449
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   450
  match_sinr :: "'a \<oplus> 'b \<rightarrow> 'b maybe"
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   451
  "match_sinr \<equiv> sscase\<cdot>(\<Lambda> x. fail)\<cdot>return"
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   452
16776
a3899ac14a1c generalized types of monadic operators to class cpo; added match function for UU
huffman
parents: 16758
diff changeset
   453
  match_up :: "'a::cpo u \<rightarrow> 'a maybe"
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   454
  "match_up \<equiv> fup\<cdot>return"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   455
16460
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   456
  match_ONE :: "one \<rightarrow> unit maybe"
18094
404f298220af simplify definitions
huffman
parents: 16779
diff changeset
   457
  "match_ONE \<equiv> \<Lambda> ONE. return\<cdot>()"
404f298220af simplify definitions
huffman
parents: 16779
diff changeset
   458
 
16460
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   459
  match_TT :: "tr \<rightarrow> unit maybe"
18094
404f298220af simplify definitions
huffman
parents: 16779
diff changeset
   460
  "match_TT \<equiv> \<Lambda> b. If b then return\<cdot>() else fail fi"
404f298220af simplify definitions
huffman
parents: 16779
diff changeset
   461
 
16460
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   462
  match_FF :: "tr \<rightarrow> unit maybe"
18094
404f298220af simplify definitions
huffman
parents: 16779
diff changeset
   463
  "match_FF \<equiv> \<Lambda> b. If b then fail else return\<cdot>() fi"
16460
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   464
16776
a3899ac14a1c generalized types of monadic operators to class cpo; added match function for UU
huffman
parents: 16758
diff changeset
   465
lemma match_UU_simps [simp]:
a3899ac14a1c generalized types of monadic operators to class cpo; added match function for UU
huffman
parents: 16758
diff changeset
   466
  "match_UU\<cdot>x = fail"
a3899ac14a1c generalized types of monadic operators to class cpo; added match function for UU
huffman
parents: 16758
diff changeset
   467
by (simp add: match_UU_def)
a3899ac14a1c generalized types of monadic operators to class cpo; added match function for UU
huffman
parents: 16758
diff changeset
   468
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   469
lemma match_cpair_simps [simp]:
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   470
  "match_cpair\<cdot><x,y> = return\<cdot><x,y>"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   471
by (simp add: match_cpair_def)
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   472
16551
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   473
lemma match_spair_simps [simp]:
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   474
  "\<lbrakk>x \<noteq> \<bottom>; y \<noteq> \<bottom>\<rbrakk> \<Longrightarrow> match_spair\<cdot>(:x,y:) = return\<cdot><x,y>"
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   475
  "match_spair\<cdot>\<bottom> = \<bottom>"
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   476
by (simp_all add: match_spair_def)
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   477
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   478
lemma match_sinl_simps [simp]:
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   479
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinl\<cdot>x) = return\<cdot>x"
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   480
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinr\<cdot>x) = fail"
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   481
  "match_sinl\<cdot>\<bottom> = \<bottom>"
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   482
by (simp_all add: match_sinl_def)
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   483
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   484
lemma match_sinr_simps [simp]:
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   485
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinr\<cdot>x) = return\<cdot>x"
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   486
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinl\<cdot>x) = fail"
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   487
  "match_sinr\<cdot>\<bottom> = \<bottom>"
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   488
by (simp_all add: match_sinr_def)
7abf8a713613 added match functions for spair, sinl, sinr
huffman
parents: 16463
diff changeset
   489
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   490
lemma match_up_simps [simp]:
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   491
  "match_up\<cdot>(up\<cdot>x) = return\<cdot>x"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   492
  "match_up\<cdot>\<bottom> = \<bottom>"
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   493
by (simp_all add: match_up_def)
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   494
16460
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   495
lemma match_ONE_simps [simp]:
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   496
  "match_ONE\<cdot>ONE = return\<cdot>()"
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   497
  "match_ONE\<cdot>\<bottom> = \<bottom>"
18094
404f298220af simplify definitions
huffman
parents: 16779
diff changeset
   498
by (simp_all add: match_ONE_def)
16460
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   499
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   500
lemma match_TT_simps [simp]:
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   501
  "match_TT\<cdot>TT = return\<cdot>()"
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   502
  "match_TT\<cdot>FF = fail"
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   503
  "match_TT\<cdot>\<bottom> = \<bottom>"
18094
404f298220af simplify definitions
huffman
parents: 16779
diff changeset
   504
by (simp_all add: match_TT_def)
16460
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   505
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   506
lemma match_FF_simps [simp]:
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   507
  "match_FF\<cdot>FF = return\<cdot>()"
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   508
  "match_FF\<cdot>TT = fail"
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   509
  "match_FF\<cdot>\<bottom> = \<bottom>"
18094
404f298220af simplify definitions
huffman
parents: 16779
diff changeset
   510
by (simp_all add: match_FF_def)
16460
72a08d509d62 added match functions for ONE, TT, and FF; added theorem mplus_fail2
huffman
parents: 16417
diff changeset
   511
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   512
subsection {* Mutual recursion *}
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   513
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   514
text {*
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   515
  The following rules are used to prove unfolding theorems from
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   516
  fixed-point definitions of mutually recursive functions.
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   517
*}
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   518
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   519
lemma cpair_equalI: "\<lbrakk>x \<equiv> cfst\<cdot>p; y \<equiv> csnd\<cdot>p\<rbrakk> \<Longrightarrow> <x,y> \<equiv> p"
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   520
by (simp add: surjective_pairing_Cprod2)
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   521
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   522
lemma cpair_eqD1: "<x,y> = <x',y'> \<Longrightarrow> x = x'"
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   523
by simp
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   524
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   525
lemma cpair_eqD2: "<x,y> = <x',y'> \<Longrightarrow> y = y'"
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   526
by simp
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   527
16463
342d74ca8815 fixrec shows unsolved subgoals when proofs of rewrites fail
huffman
parents: 16460
diff changeset
   528
text {* lemma for proving rewrite rules *}
342d74ca8815 fixrec shows unsolved subgoals when proofs of rewrites fail
huffman
parents: 16460
diff changeset
   529
342d74ca8815 fixrec shows unsolved subgoals when proofs of rewrites fail
huffman
parents: 16460
diff changeset
   530
lemma ssubst_lhs: "\<lbrakk>t = s; P s = Q\<rbrakk> \<Longrightarrow> P t = Q"
342d74ca8815 fixrec shows unsolved subgoals when proofs of rewrites fail
huffman
parents: 16460
diff changeset
   531
by simp
342d74ca8815 fixrec shows unsolved subgoals when proofs of rewrites fail
huffman
parents: 16460
diff changeset
   532
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   533
ML {*
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   534
val cpair_equalI = thm "cpair_equalI";
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   535
val cpair_eqD1 = thm "cpair_eqD1";
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   536
val cpair_eqD2 = thm "cpair_eqD2";
16463
342d74ca8815 fixrec shows unsolved subgoals when proofs of rewrites fail
huffman
parents: 16460
diff changeset
   537
val ssubst_lhs = thm "ssubst_lhs";
18293
4eaa654c92f2 reimplement Case expression pattern matching to support lazy patterns
huffman
parents: 18112
diff changeset
   538
val branch_def = thm "branch_def";
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16229
diff changeset
   539
*}
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   540
16758
c32334d98fcd fix typo
huffman
parents: 16754
diff changeset
   541
subsection {* Initializing the fixrec package *}
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   542
16229
77cae9c8e73e use fixrec_package.ML
huffman
parents: 16221
diff changeset
   543
use "fixrec_package.ML"
16221
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   544
879400e029bf New theory with lemmas for the fixrec package
huffman
parents:
diff changeset
   545
end