thys3/Rsimp.thy
author Chengsong
Wed, 29 Jun 2022 12:38:05 +0100
changeset 556 c27f04bb2262
permissions -rw-r--r--
hello
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
556
Chengsong
parents:
diff changeset
     1
theory Rsimp imports "Lexer"
Chengsong
parents:
diff changeset
     2
begin
Chengsong
parents:
diff changeset
     3
Chengsong
parents:
diff changeset
     4
datatype rrexp = 
Chengsong
parents:
diff changeset
     5
  RZERO
Chengsong
parents:
diff changeset
     6
| RONE 
Chengsong
parents:
diff changeset
     7
| RCHAR char
Chengsong
parents:
diff changeset
     8
| RSEQ rrexp rrexp
Chengsong
parents:
diff changeset
     9
| RALTS "rrexp list"
Chengsong
parents:
diff changeset
    10
| RSTAR rrexp
Chengsong
parents:
diff changeset
    11
Chengsong
parents:
diff changeset
    12
abbreviation
Chengsong
parents:
diff changeset
    13
  "RALT r1 r2 \<equiv> RALTS [r1, r2]"
Chengsong
parents:
diff changeset
    14
Chengsong
parents:
diff changeset
    15
Chengsong
parents:
diff changeset
    16
fun
Chengsong
parents:
diff changeset
    17
  RL :: "rrexp \<Rightarrow> string set"
Chengsong
parents:
diff changeset
    18
where
Chengsong
parents:
diff changeset
    19
  "RL (RZERO) = {}"
Chengsong
parents:
diff changeset
    20
| "RL (RONE) = {[]}"
Chengsong
parents:
diff changeset
    21
| "RL (RCHAR c) = {[c]}"
Chengsong
parents:
diff changeset
    22
| "RL (RSEQ r1 r2) = (RL r1) ;; (RL r2)"
Chengsong
parents:
diff changeset
    23
| "RL (RALTS rs) = (\<Union> (set (map RL rs)))"
Chengsong
parents:
diff changeset
    24
| "RL (RSTAR r) = (RL r)\<star>"
Chengsong
parents:
diff changeset
    25
Chengsong
parents:
diff changeset
    26
Chengsong
parents:
diff changeset
    27
Chengsong
parents:
diff changeset
    28
fun
Chengsong
parents:
diff changeset
    29
 rnullable :: "rrexp \<Rightarrow> bool"
Chengsong
parents:
diff changeset
    30
where
Chengsong
parents:
diff changeset
    31
  "rnullable (RZERO) = False"
Chengsong
parents:
diff changeset
    32
| "rnullable (RONE) = True"
Chengsong
parents:
diff changeset
    33
| "rnullable (RCHAR c) = False"
Chengsong
parents:
diff changeset
    34
| "rnullable (RALTS rs) = (\<exists>r \<in> set rs. rnullable r)"
Chengsong
parents:
diff changeset
    35
| "rnullable (RSEQ r1 r2) = (rnullable r1 \<and> rnullable r2)"
Chengsong
parents:
diff changeset
    36
| "rnullable (RSTAR r) = True"
Chengsong
parents:
diff changeset
    37
Chengsong
parents:
diff changeset
    38
Chengsong
parents:
diff changeset
    39
fun
Chengsong
parents:
diff changeset
    40
 rder :: "char \<Rightarrow> rrexp \<Rightarrow> rrexp"
Chengsong
parents:
diff changeset
    41
where
Chengsong
parents:
diff changeset
    42
  "rder c (RZERO) = RZERO"
Chengsong
parents:
diff changeset
    43
| "rder c (RONE) = RZERO"
Chengsong
parents:
diff changeset
    44
| "rder c (RCHAR d) = (if c = d then RONE else RZERO)"
Chengsong
parents:
diff changeset
    45
| "rder c (RALTS rs) = RALTS (map (rder c) rs)"
Chengsong
parents:
diff changeset
    46
| "rder c (RSEQ r1 r2) = 
Chengsong
parents:
diff changeset
    47
     (if rnullable r1
Chengsong
parents:
diff changeset
    48
      then RALT   (RSEQ (rder c r1) r2) (rder c r2)
Chengsong
parents:
diff changeset
    49
      else RSEQ   (rder c r1) r2)"
Chengsong
parents:
diff changeset
    50
| "rder c (RSTAR r) = RSEQ  (rder c r) (RSTAR r)"
Chengsong
parents:
diff changeset
    51
Chengsong
parents:
diff changeset
    52
Chengsong
parents:
diff changeset
    53
fun 
Chengsong
parents:
diff changeset
    54
  rders :: "rrexp \<Rightarrow> string \<Rightarrow> rrexp"
Chengsong
parents:
diff changeset
    55
where
Chengsong
parents:
diff changeset
    56
  "rders r [] = r"
Chengsong
parents:
diff changeset
    57
| "rders r (c#s) = rders (rder c r) s"
Chengsong
parents:
diff changeset
    58
Chengsong
parents:
diff changeset
    59
fun rdistinct :: "'a list \<Rightarrow>'a set \<Rightarrow> 'a list"
Chengsong
parents:
diff changeset
    60
  where
Chengsong
parents:
diff changeset
    61
  "rdistinct [] acc = []"
Chengsong
parents:
diff changeset
    62
| "rdistinct (x#xs)  acc = 
Chengsong
parents:
diff changeset
    63
     (if x \<in> acc then rdistinct xs  acc 
Chengsong
parents:
diff changeset
    64
      else x # (rdistinct xs  ({x} \<union> acc)))"
Chengsong
parents:
diff changeset
    65
Chengsong
parents:
diff changeset
    66
Chengsong
parents:
diff changeset
    67
fun rflts :: "rrexp list \<Rightarrow> rrexp list"
Chengsong
parents:
diff changeset
    68
  where 
Chengsong
parents:
diff changeset
    69
  "rflts [] = []"
Chengsong
parents:
diff changeset
    70
| "rflts (RZERO # rs) = rflts rs"
Chengsong
parents:
diff changeset
    71
| "rflts ((RALTS rs1) # rs) = rs1 @ rflts rs"
Chengsong
parents:
diff changeset
    72
| "rflts (r1 # rs) = r1 # rflts rs"
Chengsong
parents:
diff changeset
    73
Chengsong
parents:
diff changeset
    74
fun nonalt :: "rrexp \<Rightarrow> bool"
Chengsong
parents:
diff changeset
    75
  where
Chengsong
parents:
diff changeset
    76
  "nonalt (RALTS  rs) = False"
Chengsong
parents:
diff changeset
    77
| "nonalt r = True"
Chengsong
parents:
diff changeset
    78
Chengsong
parents:
diff changeset
    79
fun rsimp_ALTs :: " rrexp list \<Rightarrow> rrexp"
Chengsong
parents:
diff changeset
    80
  where
Chengsong
parents:
diff changeset
    81
  "rsimp_ALTs  [] = RZERO"
Chengsong
parents:
diff changeset
    82
| "rsimp_ALTs [r] =  r"
Chengsong
parents:
diff changeset
    83
| "rsimp_ALTs rs = RALTS rs"
Chengsong
parents:
diff changeset
    84
Chengsong
parents:
diff changeset
    85
fun rsimp_SEQ :: " rrexp \<Rightarrow> rrexp \<Rightarrow> rrexp"
Chengsong
parents:
diff changeset
    86
  where
Chengsong
parents:
diff changeset
    87
  "rsimp_SEQ  RZERO _ = RZERO"
Chengsong
parents:
diff changeset
    88
| "rsimp_SEQ  _ RZERO = RZERO"
Chengsong
parents:
diff changeset
    89
| "rsimp_SEQ RONE r2 = r2"
Chengsong
parents:
diff changeset
    90
| "rsimp_SEQ r1 r2 = RSEQ r1 r2"
Chengsong
parents:
diff changeset
    91
Chengsong
parents:
diff changeset
    92
Chengsong
parents:
diff changeset
    93
fun rsimp :: "rrexp \<Rightarrow> rrexp" 
Chengsong
parents:
diff changeset
    94
  where
Chengsong
parents:
diff changeset
    95
  "rsimp (RSEQ r1 r2) = rsimp_SEQ  (rsimp r1) (rsimp r2)"
Chengsong
parents:
diff changeset
    96
| "rsimp (RALTS rs) = rsimp_ALTs  (rdistinct (rflts (map rsimp rs))  {}) "
Chengsong
parents:
diff changeset
    97
| "rsimp r = r"
Chengsong
parents:
diff changeset
    98
Chengsong
parents:
diff changeset
    99
Chengsong
parents:
diff changeset
   100
fun 
Chengsong
parents:
diff changeset
   101
  rders_simp :: "rrexp \<Rightarrow> string \<Rightarrow> rrexp"
Chengsong
parents:
diff changeset
   102
where
Chengsong
parents:
diff changeset
   103
  "rders_simp r [] = r"
Chengsong
parents:
diff changeset
   104
| "rders_simp r (c#s) = rders_simp (rsimp (rder c r)) s"
Chengsong
parents:
diff changeset
   105
Chengsong
parents:
diff changeset
   106
fun rsize :: "rrexp \<Rightarrow> nat" where
Chengsong
parents:
diff changeset
   107
  "rsize RZERO = 1"
Chengsong
parents:
diff changeset
   108
| "rsize (RONE) = 1" 
Chengsong
parents:
diff changeset
   109
| "rsize (RCHAR  c) = 1"
Chengsong
parents:
diff changeset
   110
| "rsize (RALTS  rs) = Suc (sum_list (map rsize rs))"
Chengsong
parents:
diff changeset
   111
| "rsize (RSEQ  r1 r2) = Suc (rsize r1 + rsize r2)"
Chengsong
parents:
diff changeset
   112
| "rsize (RSTAR  r) = Suc (rsize r)"
Chengsong
parents:
diff changeset
   113
Chengsong
parents:
diff changeset
   114
abbreviation rsizes where
Chengsong
parents:
diff changeset
   115
  "rsizes rs \<equiv> sum_list (map rsize rs)"
Chengsong
parents:
diff changeset
   116
Chengsong
parents:
diff changeset
   117
fun nonnested :: "rrexp \<Rightarrow> bool"
Chengsong
parents:
diff changeset
   118
  where
Chengsong
parents:
diff changeset
   119
  "nonnested (RALTS []) = True"
Chengsong
parents:
diff changeset
   120
| "nonnested (RALTS ((RALTS rs1) # rs2)) = False"
Chengsong
parents:
diff changeset
   121
| "nonnested (RALTS (r # rs2)) = nonnested (RALTS rs2)"
Chengsong
parents:
diff changeset
   122
| "nonnested r = True"
Chengsong
parents:
diff changeset
   123
Chengsong
parents:
diff changeset
   124
Chengsong
parents:
diff changeset
   125
fun good :: "rrexp \<Rightarrow> bool" where
Chengsong
parents:
diff changeset
   126
  "good RZERO = False"
Chengsong
parents:
diff changeset
   127
| "good (RONE) = True" 
Chengsong
parents:
diff changeset
   128
| "good (RCHAR c) = True"
Chengsong
parents:
diff changeset
   129
| "good (RALTS []) = False"
Chengsong
parents:
diff changeset
   130
| "good (RALTS [r]) = False"
Chengsong
parents:
diff changeset
   131
| "good (RALTS (r1 # r2 # rs)) = 
Chengsong
parents:
diff changeset
   132
    ((distinct ( (r1 # r2 # rs))) \<and>
Chengsong
parents:
diff changeset
   133
     (\<forall>r' \<in> set (r1 # r2 # rs). good r' \<and> nonalt r'))"
Chengsong
parents:
diff changeset
   134
| "good (RSEQ RZERO _) = False"
Chengsong
parents:
diff changeset
   135
| "good (RSEQ RONE _) = False"
Chengsong
parents:
diff changeset
   136
| "good (RSEQ  _ RZERO) = False"
Chengsong
parents:
diff changeset
   137
| "good (RSEQ r1 r2) = (good r1 \<and> good r2)"
Chengsong
parents:
diff changeset
   138
| "good (RSTAR r) = True"
Chengsong
parents:
diff changeset
   139
Chengsong
parents:
diff changeset
   140
fun nonazero :: "rrexp \<Rightarrow> bool"
Chengsong
parents:
diff changeset
   141
  where
Chengsong
parents:
diff changeset
   142
  "nonazero RZERO = False"
Chengsong
parents:
diff changeset
   143
| "nonazero r = True"
Chengsong
parents:
diff changeset
   144
Chengsong
parents:
diff changeset
   145
Chengsong
parents:
diff changeset
   146
Chengsong
parents:
diff changeset
   147
Chengsong
parents:
diff changeset
   148
Chengsong
parents:
diff changeset
   149
Chengsong
parents:
diff changeset
   150
lemma basic_rsimp_SEQ_property1:
Chengsong
parents:
diff changeset
   151
  shows "rsimp_SEQ RONE r = r"
Chengsong
parents:
diff changeset
   152
  apply(induct r)
Chengsong
parents:
diff changeset
   153
       apply simp+
Chengsong
parents:
diff changeset
   154
  done
Chengsong
parents:
diff changeset
   155
Chengsong
parents:
diff changeset
   156
Chengsong
parents:
diff changeset
   157
lemma basic_rsimp_SEQ_property3:
Chengsong
parents:
diff changeset
   158
  shows "rsimp_SEQ r RZERO = RZERO"  
Chengsong
parents:
diff changeset
   159
  using rsimp_SEQ.elims by blast
Chengsong
parents:
diff changeset
   160
Chengsong
parents:
diff changeset
   161
Chengsong
parents:
diff changeset
   162
lemma rsimpalts_conscons:
Chengsong
parents:
diff changeset
   163
  shows "rsimp_ALTs (r1 # rsa @ r2 # rsb) = RALTS (r1 # rsa @ r2 # rsb)"
Chengsong
parents:
diff changeset
   164
  by (metis Nil_is_append_conv list.exhaust rsimp_ALTs.simps(3))
Chengsong
parents:
diff changeset
   165
Chengsong
parents:
diff changeset
   166
lemma rsimp_alts_equal:
Chengsong
parents:
diff changeset
   167
  shows "rsimp_ALTs (rsa @ a # rsb @ a # rsc) = RALTS (rsa @ a # rsb @ a # rsc) "
Chengsong
parents:
diff changeset
   168
  by (metis append_Cons append_Nil neq_Nil_conv rsimpalts_conscons)
Chengsong
parents:
diff changeset
   169
Chengsong
parents:
diff changeset
   170
Chengsong
parents:
diff changeset
   171
end