updated
authorChristian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 02 Oct 2013 01:02:50 +0100
changeset 121 43c116860e47
parent 120 3e71efb25ce9
child 122 4123344e6915
updated
progs/re-internal.rb
progs/re.rb
progs/re2.scala
progs/re3.scala
progs/re4.scala
slides/slides02.pdf
slides/slides02.tex
--- a/progs/re-internal.rb	Wed Oct 02 00:10:52 2013 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,22 +0,0 @@
-# provided by Daniel Baldwin
-
-nums = (1..100)
-
-#iterate through the nums 1-100
-nums.each do |i|
-
-	start_time = Time.now
-	string = "a" * i
-
-	#create a new regular expression based on current value of i
-	re = Regexp.new(/((a?){#{i}})(a{#{i}})/)
-
-        re.match(string)
-	#if re.match(string)
-	#	puts "matched string  a * #{i} with regex #{re}"
-	#else
-	#	puts "unmatched string a * #{i} with regex #{re}"
-	#end
-	
-  puts "#{i} %.5f" % (Time.now - start_time)
-end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/re.rb	Wed Oct 02 01:02:50 2013 +0100
@@ -0,0 +1,22 @@
+# provided by Daniel Baldwin
+
+nums = (1..100)
+
+#iterate through the nums 1-100
+nums.each do |i|
+
+	start_time = Time.now
+	string = "a" * i
+
+	#create a new regular expression based on current value of i
+	re = Regexp.new(/((a?){#{i}})(a{#{i}})/)
+
+        re.match(string)
+	#if re.match(string)
+	#	puts "matched string  a * #{i} with regex #{re}"
+	#else
+	#	puts "unmatched string a * #{i} with regex #{re}"
+	#end
+	
+  puts "#{i} %.5f" % (Time.now - start_time)
+end
--- a/progs/re2.scala	Wed Oct 02 00:10:52 2013 +0100
+++ b/progs/re2.scala	Wed Oct 02 01:02:50 2013 +0100
@@ -1,3 +1,4 @@
+import scala.language.implicitConversions    
     
 abstract class Rexp 
 
--- a/progs/re3.scala	Wed Oct 02 00:10:52 2013 +0100
+++ b/progs/re3.scala	Wed Oct 02 01:02:50 2013 +0100
@@ -1,3 +1,4 @@
+import scala.language.implicitConversions    
     
 abstract class Rexp {
   def simp : Rexp = this
@@ -98,7 +99,7 @@
 }
 
 
-for (i <- 1 to 11001 by 500) {
+for (i <- 1 to 12001 by 500) {
   println(i + " " + "%.5f".format(time_needed(1, matcher(EVIL(i), "a" * i))))
 }
 
--- a/progs/re4.scala	Wed Oct 02 00:10:52 2013 +0100
+++ b/progs/re4.scala	Wed Oct 02 01:02:50 2013 +0100
@@ -1,4 +1,5 @@
 import scala.annotation.tailrec    
+    
 abstract class Rexp {
   def simp : Rexp = this
 }
@@ -12,7 +13,7 @@
     case (r, NULL) => r
     case (r, EMPTY) => if (nullable(r)) r else ALT(r, EMPTY)
     case (EMPTY, r) => if (nullable(r)) r else ALT(r, EMPTY)
-    case (r1, r2) => ALT(r1, r2)
+    case (r1, r2) => if (r1 == r2) r1 else ALT(r1, r2)
   }
 }
 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp {
@@ -24,8 +25,21 @@
     case (r1, r2) => SEQ(r1, r2)
   }
 }
-case class STAR(r: Rexp) extends Rexp 
-case class NTIMES(r: Rexp, n: Int) extends Rexp 
+case class STAR(r: Rexp) extends Rexp {
+  override def simp = r.simp match {
+    case NULL => EMPTY
+    case EMPTY => EMPTY
+    case r => STAR(r)
+  }
+}
+case class NTIMES(r: Rexp, n: Int) extends Rexp {
+  override def simp = if (n == 0) EMPTY else 
+    r.simp match {
+      case NULL => NULL
+      case EMPTY => EMPTY
+      case r => NTIMES(r, n)
+    }
+}
 
 // some convenience for typing in regular expressions
 def charlist2rexp(s : List[Char]) : Rexp = s match {
@@ -45,7 +59,7 @@
   case ALT(r1, r2) => nullable(r1) || nullable(r2)
   case SEQ(r1, r2) => nullable(r1) && nullable(r2)
   case STAR(_) => true
-  case NTIMES(r, i) => if (i == 0) false else nullable(r)
+  case NTIMES(r, i) => if (i == 0) true else nullable(r)
 }
 
 // derivative of a regular expression w.r.t. a character
@@ -73,18 +87,10 @@
 def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
 
 
-
 //one or zero
 def OPT(r: Rexp) = ALT(r, EMPTY)
 
-//n-times
-/*def NTIMES(r: Rexp, n: Int) : Rexp = n match {
-  case 0 => NULL
-  case 1 => r
-  case n => SEQ(r, NTIMES(r, n - 1))
-}*/
-
-def RTEST(n: Int) = SEQ(NTIMES(OPT("a"), n), NTIMES("a", n))
+def EVIL(n: Int) = SEQ(NTIMES(OPT("a"), n), NTIMES("a", n))
 
 def time_needed[T](i: Int, code: => T) = {
   val start = System.nanoTime()
@@ -94,8 +100,8 @@
 }
 
 
-for (i <- 1 to 13001 by 500) {
-  println(i + " " + time_needed(1, matcher(RTEST(i), "a" * i)))
+for (i <- 1 to 12001 by 500) {
+  println(i + " " + "%.5f".format(time_needed(1, matcher(EVIL(i), "a" * i))))
 }
 
 
Binary file slides/slides02.pdf has changed
--- a/slides/slides02.tex	Wed Oct 02 00:10:52 2013 +0100
+++ b/slides/slides02.tex	Wed Oct 02 01:02:50 2013 +0100
@@ -199,6 +199,36 @@
 951 31.96038
 \end{filecontents}
 
+\begin{filecontents}{re3.data}
+1 0.001605
+501 0.131066
+1001 0.057885
+1501 0.136875
+2001 0.176238
+2501 0.254363
+3001 0.37262
+3501 0.500946
+4001 0.638384
+4501 0.816605
+5001 1.00491
+5501 1.232505
+6001 1.525672
+6501 1.757502
+7001 2.092784
+7501 2.429224
+8001 2.803037
+8501 3.463045
+9001 3.609
+9501 4.081504
+10001 4.54569
+10501 6.17789
+11001 6.77242
+11501 7.95864
+\end{filecontents}
+
+
+
+
 \begin{document}
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -857,8 +887,6 @@
 \end{frame}}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
 
-
-
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \mode<presentation>{
 \begin{frame}[t]
@@ -913,15 +941,87 @@
 \end{frame}}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
 
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\mode<presentation>{
+\begin{frame}[c]
+\frametitle{\begin{tabular}{c}Examples\end{tabular}}
+
+Recall the example of \bl{$r \dn ((a \cdot b) + b)^*$} with
+
+\begin{center}
+\begin{tabular}{l}
+\bl{$der\,a\,r = ((\epsilon \cdot b) + \varnothing) \cdot r$}\\
+\bl{$der\,b\,r = ((\varnothing \cdot b) + \epsilon)\cdot r$}
+\end{tabular}
+\end{center}
+
+What are these regular expressions equal to?
+
+\end{frame}}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
+
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\mode<presentation>{
+\begin{frame}[t]
+\frametitle{\begin{tabular}{c}\bl{$(a?\{n\}) \cdot a\{n\}$}\end{tabular}}
+
+\mbox{}\\[-9mm]
+
+\begin{tabular}{@ {\hspace{-5mm}}l}
+\begin{tikzpicture}[y=.2cm, x=.0008cm]
+ 	%axis
+	\draw (0,0) -- coordinate (x axis mid) (12000,0);
+    	\draw (0,0) -- coordinate (y axis mid) (0,30);
+    	%ticks
+    	\foreach \x in {0,2000,...,12000}
+     		\draw (\x,1pt) -- (\x,-3pt)
+			node[anchor=north] {\x};
+    	\foreach \y in {0,5,...,30}
+     		\draw (1pt,\y) -- (-3pt,\y) 
+     			node[anchor=east] {\y}; 
+	%labels      
+	\node[below=0.6cm] at (x axis mid) {\bl{a}s};
+	\node[rotate=90, left=1.2cm] at (y axis mid) {secs};
+
+	%plots
+	\draw[color=red] plot[mark=triangle*, mark options={fill=white} ] 
+		file {re1.data};
+         \draw[color=green] plot[mark=square*, mark options={fill=white} ] 
+		file {re2b.data};
+	\draw[color=black] plot[mark=square*, mark options={fill=white} ] 
+		file {re3.data};	 
+    
+	%legend
+	\begin{scope}[shift={(2000,20)}] 
+	\draw[color=red] (0,0) -- 
+		plot[mark=triangle*, mark options={fill=white}] (0.25,0) -- (50,0) 
+		node[right]{\small Scala V1};
+	\draw[yshift=13, color=green] (0,0) -- 
+		plot[mark=square*, mark options={fill=white}] (0.25,0) -- (50,0)
+		node[right]{\small Scala V2};	
+	\draw[yshift=26, color=black] (0,0) -- 
+		plot[mark=square*, mark options={fill=white}] (0.25,0) -- (50,0)
+		node[right]{\small Scala V3};	
+	\end{scope}
+\end{tikzpicture}
+\end{tabular}
+
+\end{frame}}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
+
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \mode<presentation>{
 \begin{frame}[c]
 \frametitle{\begin{tabular}{c}Regular Languages\end{tabular}}
 
-A language (set of strings) is \alert{regular} iff there exists
-a regular expression that recognises all its strings.
+A language (a set of strings) is \alert{regular} iff there exists
+a regular expression that recognises all its strings.\bigskip\bigskip\pause
 
+
+Do you think there are languages that are {\bf not} regular?
 \end{frame}}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
 
@@ -947,20 +1047,6 @@
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
 
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\mode<presentation>{
-\begin{frame}[c]
-\frametitle{\begin{tabular}{c}The Rexp Matcher\end{tabular}}
-
-
-{\lstset{language=Scala}\fontsize{8}{10}\selectfont
-\texttt{\lstinputlisting{../progs/app7.scala}}}
-
-
-
-\end{frame}}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
-
 
 
 \end{document}