progs/lecture5.scala
author Christian Urban <urbanc@in.tum.de>
Sat, 15 Dec 2018 13:46:54 +0000
changeset 247 50a3b874008a
parent 242 e6b34f617915
child 326 e5453add7df6
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
238
046f37a262d0 updated
updated
parents: 226
diff changeset
     1
// Scala Lecture 5
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     2
//=================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     3
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     4
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     5
238
046f37a262d0 updated
updated
parents: 226
diff changeset
     6
// Laziness with style
046f37a262d0 updated
updated
parents: 226
diff changeset
     7
//=====================
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     8
240
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
     9
// The concept of lazy evaluation doesn’t really 
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
    10
// exist in non-functional languages, but it is 
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
    11
// pretty easy to grasp. Consider first 
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    12
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    13
def square(x: Int) = x * x
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    14
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    15
square(42 + 8)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    16
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    17
// this is called strict evaluation
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    18
240
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
    19
// say we have a pretty expensive operation
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    20
def peop(n: BigInt): Boolean = peop(n + 1) 
240
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
    21
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    22
val a = "foo"
242
e6b34f617915 updated
Christian Urban <urbanc@in.tum.de>
parents: 240
diff changeset
    23
val b = "bar"
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    24
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    25
if (a == b || peop(0)) println("true") else println("false")
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    26
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    27
// this is called lazy evaluation
046f37a262d0 updated
updated
parents: 226
diff changeset
    28
// you delay compuation until it is really 
046f37a262d0 updated
updated
parents: 226
diff changeset
    29
// needed; once calculated though, does not 
046f37a262d0 updated
updated
parents: 226
diff changeset
    30
// need to be re-calculated
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    31
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    32
// a useful example is
046f37a262d0 updated
updated
parents: 226
diff changeset
    33
def time_needed[T](i: Int, code: => T) = {
046f37a262d0 updated
updated
parents: 226
diff changeset
    34
  val start = System.nanoTime()
046f37a262d0 updated
updated
parents: 226
diff changeset
    35
  for (j <- 1 to i) code
046f37a262d0 updated
updated
parents: 226
diff changeset
    36
  val end = System.nanoTime()
046f37a262d0 updated
updated
parents: 226
diff changeset
    37
  f"${(end - start) / (i * 1.0e9)}%.6f secs"
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    38
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    39
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    40
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    41
// streams (I do not care how many)
046f37a262d0 updated
updated
parents: 226
diff changeset
    42
// primes: 2, 3, 5, 7, 9, 11, 13 ....
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    43
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    44
def generatePrimes (s: Stream[Int]): Stream[Int] =
046f37a262d0 updated
updated
parents: 226
diff changeset
    45
  s.head #:: generatePrimes(s.tail.filter(_ % s.head != 0))
046f37a262d0 updated
updated
parents: 226
diff changeset
    46
240
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
    47
val primes = generatePrimes(Stream.from(2))
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    48
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    49
// the first 10 primes
242
e6b34f617915 updated
Christian Urban <urbanc@in.tum.de>
parents: 240
diff changeset
    50
primes.take(10).par.toList
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    51
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    52
time_needed(1, primes.filter(_ > 100).take(3000).toList)
242
e6b34f617915 updated
Christian Urban <urbanc@in.tum.de>
parents: 240
diff changeset
    53
time_needed(1, primes.filter(_ > 100).take(1000).toList)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    54
240
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
    55
// a stream of successive numbers
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    56
242
e6b34f617915 updated
Christian Urban <urbanc@in.tum.de>
parents: 240
diff changeset
    57
Stream.from(2).print
e6b34f617915 updated
Christian Urban <urbanc@in.tum.de>
parents: 240
diff changeset
    58
Stream.from(2).take(10).force
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    59
Stream.from(2).take(10).print
046f37a262d0 updated
updated
parents: 226
diff changeset
    60
Stream.from(10).take(10).print
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    61
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    62
Stream.from(2).take(10).force
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    63
240
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
    64
// iterative version of the Fibonacci numbers
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    65
def fibIter(a: BigInt, b: BigInt): Stream[BigInt] =
046f37a262d0 updated
updated
parents: 226
diff changeset
    66
  a #:: fibIter(b, a + b)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    67
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    68
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    69
fibIter(1, 1).take(10).force
046f37a262d0 updated
updated
parents: 226
diff changeset
    70
fibIter(8, 13).take(10).force
046f37a262d0 updated
updated
parents: 226
diff changeset
    71
046f37a262d0 updated
updated
parents: 226
diff changeset
    72
fibIter(1, 1).drop(10000).take(1).print
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    73
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    74
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    75
// good for testing
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    76
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    77
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    78
// Regular expressions - the power of DSLs in Scala
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    79
//                                     and Laziness
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    80
//==================================================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    81
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    82
abstract class Rexp
226
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
    83
case object ZERO extends Rexp                     // nothing
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
    84
case object ONE extends Rexp                      // the empty string
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
    85
case class CHAR(c: Char) extends Rexp             // a character c
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
    86
case class ALT(r1: Rexp, r2: Rexp) extends Rexp   // alternative  r1 + r2
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
    87
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence     r1 . r2  
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
    88
case class STAR(r: Rexp) extends Rexp             // star         r*
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    89
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    90
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    91
// some convenience for typing in regular expressions
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    92
import scala.language.implicitConversions    
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    93
import scala.language.reflectiveCalls 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    94
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    95
def charlist2rexp(s: List[Char]): Rexp = s match {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    96
  case Nil => ONE
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    97
  case c::Nil => CHAR(c)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    98
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    99
}
224
42d760984496 updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   100
implicit def string2rexp(s: String): Rexp = 
42d760984496 updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   101
  charlist2rexp(s.toList)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   102
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   103
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   104
implicit def RexpOps (r: Rexp) = new {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   105
  def | (s: Rexp) = ALT(r, s)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   106
  def % = STAR(r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   107
  def ~ (s: Rexp) = SEQ(r, s)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   108
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   109
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   110
implicit def stringOps (s: String) = new {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   111
  def | (r: Rexp) = ALT(s, r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   112
  def | (r: String) = ALT(s, r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   113
  def % = STAR(s)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   114
  def ~ (r: Rexp) = SEQ(s, r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   115
  def ~ (r: String) = SEQ(s, r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   116
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   117
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   118
046f37a262d0 updated
updated
parents: 226
diff changeset
   119
def depth(r: Rexp) : Int = r match {
046f37a262d0 updated
updated
parents: 226
diff changeset
   120
  case ZERO => 0
046f37a262d0 updated
updated
parents: 226
diff changeset
   121
  case ONE => 0
046f37a262d0 updated
updated
parents: 226
diff changeset
   122
  case CHAR(_) => 0
046f37a262d0 updated
updated
parents: 226
diff changeset
   123
  case ALT(r1, r2) => Math.max(depth(r1), depth(r2)) + 1
046f37a262d0 updated
updated
parents: 226
diff changeset
   124
  case SEQ(r1, r2) => Math.max(depth(r1), depth(r2)) + 1 
046f37a262d0 updated
updated
parents: 226
diff changeset
   125
  case STAR(r1) => depth(r1) + 1
046f37a262d0 updated
updated
parents: 226
diff changeset
   126
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   127
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   128
//example regular expressions
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   129
val digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   130
val sign = "+" | "-" | ""
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   131
val number = sign ~ digit ~ digit.% 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   132
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   133
// task: enumerate exhaustively regular expression
046f37a262d0 updated
updated
parents: 226
diff changeset
   134
// starting from small ones towards bigger ones.
046f37a262d0 updated
updated
parents: 226
diff changeset
   135
240
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
   136
// 1st idea: enumerate them all in a Set
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
   137
// up to a level
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   138
046f37a262d0 updated
updated
parents: 226
diff changeset
   139
def enuml(l: Int, s: String) : Set[Rexp] = l match {
046f37a262d0 updated
updated
parents: 226
diff changeset
   140
  case 0 => Set(ZERO, ONE) ++ s.map(CHAR).toSet
046f37a262d0 updated
updated
parents: 226
diff changeset
   141
  case n =>  
046f37a262d0 updated
updated
parents: 226
diff changeset
   142
    val rs = enuml(n - 1, s)
046f37a262d0 updated
updated
parents: 226
diff changeset
   143
    rs ++
046f37a262d0 updated
updated
parents: 226
diff changeset
   144
    (for (r1 <- rs; r2 <- rs) yield ALT(r1, r2)) ++
046f37a262d0 updated
updated
parents: 226
diff changeset
   145
    (for (r1 <- rs; r2 <- rs) yield SEQ(r1, r2)) ++
046f37a262d0 updated
updated
parents: 226
diff changeset
   146
    (for (r1 <- rs) yield STAR(r1))
046f37a262d0 updated
updated
parents: 226
diff changeset
   147
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   148
240
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
   149
enuml(1, "a")
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   150
enuml(1, "a").size
046f37a262d0 updated
updated
parents: 226
diff changeset
   151
enuml(2, "a").size
240
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
   152
enuml(3, "a").size 
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
   153
enuml(4, "a").size // out of heap space
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   154
046f37a262d0 updated
updated
parents: 226
diff changeset
   155
046f37a262d0 updated
updated
parents: 226
diff changeset
   156
def enum(rs: Stream[Rexp]) : Stream[Rexp] = 
046f37a262d0 updated
updated
parents: 226
diff changeset
   157
  rs #::: enum( (for (r1 <- rs; r2 <- rs) yield ALT(r1, r2)) #:::
046f37a262d0 updated
updated
parents: 226
diff changeset
   158
                (for (r1 <- rs; r2 <- rs) yield SEQ(r1, r2)) #:::
046f37a262d0 updated
updated
parents: 226
diff changeset
   159
                (for (r1 <- rs) yield STAR(r1)) )
046f37a262d0 updated
updated
parents: 226
diff changeset
   160
046f37a262d0 updated
updated
parents: 226
diff changeset
   161
046f37a262d0 updated
updated
parents: 226
diff changeset
   162
enum(ZERO #:: ONE #:: "ab".toStream.map(CHAR)).take(200).force
240
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
   163
enum(ZERO #:: ONE #:: "ab".toStream.map(CHAR)).take(5000000)
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   164
046f37a262d0 updated
updated
parents: 226
diff changeset
   165
046f37a262d0 updated
updated
parents: 226
diff changeset
   166
val is = 
046f37a262d0 updated
updated
parents: 226
diff changeset
   167
  (enum(ZERO #:: ONE #:: "ab".toStream.map(CHAR))
046f37a262d0 updated
updated
parents: 226
diff changeset
   168
    .dropWhile(depth(_) < 3)
046f37a262d0 updated
updated
parents: 226
diff changeset
   169
    .take(10).foreach(println))
046f37a262d0 updated
updated
parents: 226
diff changeset
   170
046f37a262d0 updated
updated
parents: 226
diff changeset
   171
046f37a262d0 updated
updated
parents: 226
diff changeset
   172
046f37a262d0 updated
updated
parents: 226
diff changeset
   173
// Parsing - The Solved Problem That Isn't
046f37a262d0 updated
updated
parents: 226
diff changeset
   174
//=========================================
046f37a262d0 updated
updated
parents: 226
diff changeset
   175
//
046f37a262d0 updated
updated
parents: 226
diff changeset
   176
// https://tratt.net/laurie/blog/entries/parsing_the_solved_problem_that_isnt.html
046f37a262d0 updated
updated
parents: 226
diff changeset
   177
//
046f37a262d0 updated
updated
parents: 226
diff changeset
   178
// Or, A topic of endless "fun"(?)
046f37a262d0 updated
updated
parents: 226
diff changeset
   179
046f37a262d0 updated
updated
parents: 226
diff changeset
   180
046f37a262d0 updated
updated
parents: 226
diff changeset
   181
// input type: String
046f37a262d0 updated
updated
parents: 226
diff changeset
   182
// output type: Int
247
50a3b874008a updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   183
Integer.parseInt("123u456")
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   184
046f37a262d0 updated
updated
parents: 226
diff changeset
   185
/* Note, in the previous lectures I did not show the type consraint
046f37a262d0 updated
updated
parents: 226
diff changeset
   186
 * I <% Seq[_] , which means that the input type I can be
046f37a262d0 updated
updated
parents: 226
diff changeset
   187
 * treated, or seen, as a sequence. */
046f37a262d0 updated
updated
parents: 226
diff changeset
   188
046f37a262d0 updated
updated
parents: 226
diff changeset
   189
abstract class Parser[I <% Seq[_], T] {
046f37a262d0 updated
updated
parents: 226
diff changeset
   190
  def parse(ts: I): Set[(T, I)]
046f37a262d0 updated
updated
parents: 226
diff changeset
   191
046f37a262d0 updated
updated
parents: 226
diff changeset
   192
  def parse_all(ts: I) : Set[T] =
046f37a262d0 updated
updated
parents: 226
diff changeset
   193
    for ((head, tail) <- parse(ts); 
046f37a262d0 updated
updated
parents: 226
diff changeset
   194
        if (tail.isEmpty)) yield head
046f37a262d0 updated
updated
parents: 226
diff changeset
   195
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   196
046f37a262d0 updated
updated
parents: 226
diff changeset
   197
// the idea is that a parser can parse something
046f37a262d0 updated
updated
parents: 226
diff changeset
   198
// from the input and leaves something unparsed => pairs
046f37a262d0 updated
updated
parents: 226
diff changeset
   199
046f37a262d0 updated
updated
parents: 226
diff changeset
   200
class AltParser[I <% Seq[_], T](
046f37a262d0 updated
updated
parents: 226
diff changeset
   201
  p: => Parser[I, T], 
046f37a262d0 updated
updated
parents: 226
diff changeset
   202
  q: => Parser[I, T]) extends Parser[I, T] {
046f37a262d0 updated
updated
parents: 226
diff changeset
   203
046f37a262d0 updated
updated
parents: 226
diff changeset
   204
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
046f37a262d0 updated
updated
parents: 226
diff changeset
   205
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   206
046f37a262d0 updated
updated
parents: 226
diff changeset
   207
046f37a262d0 updated
updated
parents: 226
diff changeset
   208
class SeqParser[I <% Seq[_], T, S](
046f37a262d0 updated
updated
parents: 226
diff changeset
   209
  p: => Parser[I, T], 
046f37a262d0 updated
updated
parents: 226
diff changeset
   210
  q: => Parser[I, S]) extends Parser[I, (T, S)] {
046f37a262d0 updated
updated
parents: 226
diff changeset
   211
046f37a262d0 updated
updated
parents: 226
diff changeset
   212
  def parse(sb: I) = 
046f37a262d0 updated
updated
parents: 226
diff changeset
   213
    for ((head1, tail1) <- p.parse(sb); 
046f37a262d0 updated
updated
parents: 226
diff changeset
   214
         (head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2)
046f37a262d0 updated
updated
parents: 226
diff changeset
   215
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   216
046f37a262d0 updated
updated
parents: 226
diff changeset
   217
046f37a262d0 updated
updated
parents: 226
diff changeset
   218
class FunParser[I <% Seq[_], T, S](
046f37a262d0 updated
updated
parents: 226
diff changeset
   219
  p: => Parser[I, T], 
046f37a262d0 updated
updated
parents: 226
diff changeset
   220
  f: T => S) extends Parser[I, S] {
046f37a262d0 updated
updated
parents: 226
diff changeset
   221
046f37a262d0 updated
updated
parents: 226
diff changeset
   222
  def parse(sb: I) = 
046f37a262d0 updated
updated
parents: 226
diff changeset
   223
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
046f37a262d0 updated
updated
parents: 226
diff changeset
   224
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   225
046f37a262d0 updated
updated
parents: 226
diff changeset
   226
046f37a262d0 updated
updated
parents: 226
diff changeset
   227
// atomic parsers  
046f37a262d0 updated
updated
parents: 226
diff changeset
   228
case class CharParser(c: Char) extends Parser[String, Char] {
046f37a262d0 updated
updated
parents: 226
diff changeset
   229
  def parse(sb: String) = 
046f37a262d0 updated
updated
parents: 226
diff changeset
   230
    if (sb != "" && sb.head == c) Set((c, sb.tail)) else Set()
046f37a262d0 updated
updated
parents: 226
diff changeset
   231
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   232
046f37a262d0 updated
updated
parents: 226
diff changeset
   233
import scala.util.matching.Regex
046f37a262d0 updated
updated
parents: 226
diff changeset
   234
case class RegexParser(reg: Regex) extends Parser[String, String] {
046f37a262d0 updated
updated
parents: 226
diff changeset
   235
  def parse(sb: String) = reg.findPrefixMatchOf(sb) match {
046f37a262d0 updated
updated
parents: 226
diff changeset
   236
    case None => Set()
046f37a262d0 updated
updated
parents: 226
diff changeset
   237
    case Some(m) => Set((m.matched, m.after.toString))  
046f37a262d0 updated
updated
parents: 226
diff changeset
   238
  }
046f37a262d0 updated
updated
parents: 226
diff changeset
   239
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   240
046f37a262d0 updated
updated
parents: 226
diff changeset
   241
val NumParser = RegexParser("[0-9]+".r)
046f37a262d0 updated
updated
parents: 226
diff changeset
   242
def StringParser(s: String) = RegexParser(Regex.quote(s).r)
046f37a262d0 updated
updated
parents: 226
diff changeset
   243
247
50a3b874008a updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   244
NumParser.parse_all("12u345")
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   245
println(NumParser.parse_all("12u45"))
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   246
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   247
046f37a262d0 updated
updated
parents: 226
diff changeset
   248
// convenience
046f37a262d0 updated
updated
parents: 226
diff changeset
   249
implicit def string2parser(s: String) = StringParser(s)
046f37a262d0 updated
updated
parents: 226
diff changeset
   250
implicit def char2parser(c: Char) = CharParser(c)
046f37a262d0 updated
updated
parents: 226
diff changeset
   251
046f37a262d0 updated
updated
parents: 226
diff changeset
   252
implicit def ParserOps[I<% Seq[_], T](p: Parser[I, T]) = new {
046f37a262d0 updated
updated
parents: 226
diff changeset
   253
  def | (q : => Parser[I, T]) = new AltParser[I, T](p, q)
046f37a262d0 updated
updated
parents: 226
diff changeset
   254
  def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
046f37a262d0 updated
updated
parents: 226
diff changeset
   255
  def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
046f37a262d0 updated
updated
parents: 226
diff changeset
   256
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   257
046f37a262d0 updated
updated
parents: 226
diff changeset
   258
implicit def StringOps(s: String) = new {
046f37a262d0 updated
updated
parents: 226
diff changeset
   259
  def | (q : => Parser[String, String]) = new AltParser[String, String](s, q)
046f37a262d0 updated
updated
parents: 226
diff changeset
   260
  def | (r: String) = new AltParser[String, String](s, r)
046f37a262d0 updated
updated
parents: 226
diff changeset
   261
  def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f)
046f37a262d0 updated
updated
parents: 226
diff changeset
   262
  def ~[S] (q : => Parser[String, S]) = 
046f37a262d0 updated
updated
parents: 226
diff changeset
   263
    new SeqParser[String, String, S](s, q)
046f37a262d0 updated
updated
parents: 226
diff changeset
   264
  def ~ (r: String) = 
046f37a262d0 updated
updated
parents: 226
diff changeset
   265
    new SeqParser[String, String, String](s, r)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   266
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   267
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   268
247
50a3b874008a updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   269
val NumParserInt = NumParser ==> (s => 2 * s.toInt)
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   270
046f37a262d0 updated
updated
parents: 226
diff changeset
   271
NumParser.parse_all("12345")
046f37a262d0 updated
updated
parents: 226
diff changeset
   272
NumParserInt.parse_all("12345")
046f37a262d0 updated
updated
parents: 226
diff changeset
   273
NumParserInt.parse_all("12u45")
046f37a262d0 updated
updated
parents: 226
diff changeset
   274
046f37a262d0 updated
updated
parents: 226
diff changeset
   275
046f37a262d0 updated
updated
parents: 226
diff changeset
   276
// grammar for arithmetic expressions
046f37a262d0 updated
updated
parents: 226
diff changeset
   277
//
046f37a262d0 updated
updated
parents: 226
diff changeset
   278
//  E ::= T + E | T - E | T
046f37a262d0 updated
updated
parents: 226
diff changeset
   279
//  T ::= F * T | F
046f37a262d0 updated
updated
parents: 226
diff changeset
   280
//  F ::= ( E ) | Number
046f37a262d0 updated
updated
parents: 226
diff changeset
   281
046f37a262d0 updated
updated
parents: 226
diff changeset
   282
046f37a262d0 updated
updated
parents: 226
diff changeset
   283
lazy val E: Parser[String, Int] = 
046f37a262d0 updated
updated
parents: 226
diff changeset
   284
  (T ~ "+" ~ E) ==> { case ((x, y), z) => x + z } |
046f37a262d0 updated
updated
parents: 226
diff changeset
   285
  (T ~ "-" ~ E) ==> { case ((x, y), z) => x - z } | T 
046f37a262d0 updated
updated
parents: 226
diff changeset
   286
lazy val T: Parser[String, Int] = 
046f37a262d0 updated
updated
parents: 226
diff changeset
   287
  (F ~ "*" ~ T) ==> { case ((x, y), z) => x * z } | F
046f37a262d0 updated
updated
parents: 226
diff changeset
   288
lazy val F: Parser[String, Int] = 
046f37a262d0 updated
updated
parents: 226
diff changeset
   289
  ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } | NumParserInt
046f37a262d0 updated
updated
parents: 226
diff changeset
   290
247
50a3b874008a updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   291
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   292
println(E.parse_all("4*2+3"))
046f37a262d0 updated
updated
parents: 226
diff changeset
   293
println(E.parse_all("4*(2+3)"))
046f37a262d0 updated
updated
parents: 226
diff changeset
   294
println(E.parse_all("(4)*((2+3))"))
046f37a262d0 updated
updated
parents: 226
diff changeset
   295
println(E.parse_all("4/2+3"))
046f37a262d0 updated
updated
parents: 226
diff changeset
   296
println(E.parse_all("(1+2)+3"))
046f37a262d0 updated
updated
parents: 226
diff changeset
   297
println(E.parse_all("1+2+3")) 
046f37a262d0 updated
updated
parents: 226
diff changeset
   298
046f37a262d0 updated
updated
parents: 226
diff changeset
   299
046f37a262d0 updated
updated
parents: 226
diff changeset
   300
046f37a262d0 updated
updated
parents: 226
diff changeset
   301
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   302
240
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
   303
// The End ... Almost Christmas
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   304
//===============================
046f37a262d0 updated
updated
parents: 226
diff changeset
   305
046f37a262d0 updated
updated
parents: 226
diff changeset
   306
// I hope you had fun!
046f37a262d0 updated
updated
parents: 226
diff changeset
   307
046f37a262d0 updated
updated
parents: 226
diff changeset
   308
// A function should do one thing, and only one thing.
046f37a262d0 updated
updated
parents: 226
diff changeset
   309
046f37a262d0 updated
updated
parents: 226
diff changeset
   310
// Make your variables immutable, unless there's a good 
046f37a262d0 updated
updated
parents: 226
diff changeset
   311
// reason not to.
046f37a262d0 updated
updated
parents: 226
diff changeset
   312
046f37a262d0 updated
updated
parents: 226
diff changeset
   313
// I did it, but this is actually not a good reason:
240
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
   314
// generating new labels:
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
   315
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   316
var counter = -1
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   317
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   318
def Fresh(x: String) = {
046f37a262d0 updated
updated
parents: 226
diff changeset
   319
  counter += 1
046f37a262d0 updated
updated
parents: 226
diff changeset
   320
  x ++ "_" ++ counter.toString()
046f37a262d0 updated
updated
parents: 226
diff changeset
   321
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   322
046f37a262d0 updated
updated
parents: 226
diff changeset
   323
Fresh("x")
046f37a262d0 updated
updated
parents: 226
diff changeset
   324
Fresh("x")
046f37a262d0 updated
updated
parents: 226
diff changeset
   325
046f37a262d0 updated
updated
parents: 226
diff changeset
   326
046f37a262d0 updated
updated
parents: 226
diff changeset
   327
046f37a262d0 updated
updated
parents: 226
diff changeset
   328
// You can be productive on Day 1, but the language is deep.
046f37a262d0 updated
updated
parents: 226
diff changeset
   329
//
046f37a262d0 updated
updated
parents: 226
diff changeset
   330
// http://scalapuzzlers.com
046f37a262d0 updated
updated
parents: 226
diff changeset
   331
//
046f37a262d0 updated
updated
parents: 226
diff changeset
   332
// http://www.latkin.org/blog/2017/05/02/when-the-scala-compiler-doesnt-help/
046f37a262d0 updated
updated
parents: 226
diff changeset
   333
247
50a3b874008a updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   334
List(1, 2, 3).contains("your mom")
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   335
046f37a262d0 updated
updated
parents: 226
diff changeset
   336
// I like best about Scala that it lets me often write
046f37a262d0 updated
updated
parents: 226
diff changeset
   337
// concise, readable code. And it hooks up with the 
046f37a262d0 updated
updated
parents: 226
diff changeset
   338
// Isabelle theorem prover.
046f37a262d0 updated
updated
parents: 226
diff changeset
   339