2018-02-07 18:44:27 +00:00
|
|
|
theory RegExp
|
|
|
|
imports Main
|
|
|
|
begin
|
2018-05-21 09:19:40 +00:00
|
|
|
|
2018-02-07 18:44:27 +00:00
|
|
|
datatype 'a rexp = Empty ("<>")
|
|
|
|
| Atom 'a ("\<lfloor>_\<rfloor>" 65)
|
2018-05-11 13:51:26 +00:00
|
|
|
| Alt "('a rexp)" "('a rexp)" (infixr "||" 55)
|
|
|
|
| Conc "('a rexp)" "('a rexp)" (infixr "~~" 60)
|
|
|
|
| Star "('a rexp)" ("\<lbrace>(_)\<rbrace>\<^sup>*" [0]100)
|
|
|
|
|
|
|
|
definition rep1 :: "'a rexp \<Rightarrow> 'a rexp" ("\<lbrace>(_)\<rbrace>\<^sup>+")
|
|
|
|
where "\<lbrace>A\<rbrace>\<^sup>+ \<equiv> A ~~ \<lbrace>A\<rbrace>\<^sup>*"
|
|
|
|
|
|
|
|
definition opt :: "'a rexp \<Rightarrow> 'a rexp" ("\<lbrakk>(_)\<rbrakk>")
|
|
|
|
where "\<lbrakk>A\<rbrakk> \<equiv> A || <>"
|
|
|
|
|
2018-02-07 18:44:27 +00:00
|
|
|
value "Star (Conc(Alt (Atom(CHR ''a'')) (Atom(CHR ''b''))) (Atom(CHR ''c'')))"
|
|
|
|
text{* or better equivalently: *}
|
2018-05-11 13:51:26 +00:00
|
|
|
value "\<lbrace>(\<lfloor>CHR ''a''\<rfloor> || \<lfloor>CHR ''b''\<rfloor>) ~~ \<lfloor>CHR ''c''\<rfloor>\<rbrace>\<^sup>*"
|
2018-02-07 18:44:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
section{* Definition of a semantic function: the ``language'' of the regular expression *}
|
|
|
|
|
|
|
|
text{* In the following, we give a semantics for our regular expressions, which so far have
|
|
|
|
just been a term language (i.e. abstract syntax). The semantics is a ``denotational semantics'',
|
|
|
|
i.e. we give a direct meaning for regular expressions in some universe of ``denotations''.
|
|
|
|
|
|
|
|
This universe of denotations is in our concrete case: *}
|
|
|
|
|
|
|
|
type_synonym 'a lang = "'a list set"
|
|
|
|
|
|
|
|
inductive_set star :: "'a lang \<Rightarrow> 'a lang"
|
|
|
|
for A:: "'a lang"
|
2018-05-11 13:51:26 +00:00
|
|
|
where NilI : "[] \<in> star A"
|
|
|
|
| ConsI : "[| a\<in>A; as \<in> star A |] ==> a@as \<in> star A"
|
2018-02-07 18:44:27 +00:00
|
|
|
|
|
|
|
|
2018-05-11 13:51:26 +00:00
|
|
|
lemma NilI : "[] \<in> star A"
|
2018-02-07 18:44:27 +00:00
|
|
|
by(rule NilI)
|
|
|
|
|
|
|
|
|
|
|
|
lemma ConsI : "a\<in>A \<Longrightarrow> as \<in> star A \<Longrightarrow> a@as \<in> star A"
|
|
|
|
apply(rule ConsI)
|
|
|
|
apply(assumption)
|
|
|
|
by(assumption)
|
|
|
|
|
|
|
|
lemma epsilonExists: "star {[]} = {[]}"
|
|
|
|
apply(rule Set.set_eqI)
|
|
|
|
apply(rule iffI)
|
|
|
|
apply(erule star.induct)
|
|
|
|
apply(auto intro: NilI )
|
|
|
|
done
|
|
|
|
|
|
|
|
lemma epsilonAlt: "star {} = {[]}"
|
|
|
|
apply(rule Set.set_eqI)
|
|
|
|
apply(rule iffI)
|
|
|
|
apply(erule star.induct, simp,simp)
|
|
|
|
apply(auto intro: NilI )
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
text{* ... and of course, we have the consequence : *}
|
|
|
|
|
|
|
|
lemma epsilon': "star (star {[]}) = {[]}"
|
|
|
|
by(simp add: epsilonExists)
|
|
|
|
|
|
|
|
lemma epsilon'': "star (star ((star {[]}))) = {[]}"
|
|
|
|
by(simp add: epsilonExists)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
text{* Now the denotational semantics for regular expression can be defined on a post-card: *}
|
|
|
|
|
|
|
|
fun L :: "'a rexp => 'a lang"
|
|
|
|
where L_Emp : "L Empty = {}"
|
|
|
|
|L_Atom: "L (\<lfloor>a\<rfloor>) = {[a]}"
|
2018-05-11 13:51:26 +00:00
|
|
|
|L_Un: "L (el || er) = (L el) \<union> (L er)"
|
|
|
|
|L_Conc: "L (el ~~ er) = {xs@ys | xs ys. xs:L el \<and> ys:L er}"
|
2018-02-07 18:44:27 +00:00
|
|
|
|L_Star: "L (Star e) = star(L e)"
|
|
|
|
|
|
|
|
|
2018-05-11 13:51:26 +00:00
|
|
|
schematic_goal WeCompute: "L(Star ((\<lfloor>CHR ''a''\<rfloor> || \<lfloor>CHR ''b''\<rfloor>) ~~ \<lfloor>CHR ''c''\<rfloor>)) = ?X"
|
2018-02-07 18:44:27 +00:00
|
|
|
by simp
|
|
|
|
|
|
|
|
thm WeCompute
|
|
|
|
|
|
|
|
|
|
|
|
text{* Well, an attempt to evaluate this turns out not to work too well ... *}
|
|
|
|
|
|
|
|
|
|
|
|
text{* Now let's reconsider our `obvious lemma' from above, this time by stating that
|
|
|
|
the alternative-operator produces \emph{semantically} equivalent ewpressions. *}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-05-11 13:51:26 +00:00
|
|
|
lemma alt_commute' : "L((A::'a rexp) || B) = L(B || A)"
|
2018-02-07 18:44:27 +00:00
|
|
|
by auto
|
|
|
|
|
|
|
|
|
2018-05-11 13:51:26 +00:00
|
|
|
lemma mt_seq' : "L(Empty ~~ Empty) = {}"
|
2018-02-07 18:44:27 +00:00
|
|
|
by simp
|
|
|
|
|
|
|
|
|
|
|
|
lemma eps : "L (Star Empty) = {[]}"
|
|
|
|
by (simp add: epsilonAlt)
|
|
|
|
|
|
|
|
|
|
|
|
no_notation Atom ("\<lfloor>_\<rfloor>")
|
|
|
|
|
|
|
|
|
2018-05-19 21:23:44 +00:00
|
|
|
end
|