progs/app61.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 18 Oct 2024 05:59:04 +0100
changeset 970 1d4659dd83fe
parent 520 2849c305b12d
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
def F_ID(v: Val): Val = v
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
  case Right(v) => Right(f2(v))
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
  case Left(v) => Left(f1(v))
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
}
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
520
2849c305b12d updated
cu
parents: 422
diff changeset
     9
  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
}
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = 
520
2849c305b12d updated
cu
parents: 422
diff changeset
    12
  (v:Val) => Sequ(f1(Empty), f2(v))
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = 
520
2849c305b12d updated
cu
parents: 422
diff changeset
    14
  (v:Val) => Sequ(f1(v), f2(Empty))
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
def F_ERROR(v: Val): Val = throw new Exception("error")
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
// simplification of regular expressions returning also a
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
// rectification function; no simplification under STAR 
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
def simp(r: Rexp): (Rexp, Val => Val) = r match {
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  case ALT(r1, r2) => {
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
    val (r1s, f1s) = simp(r1)
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
    val (r2s, f2s) = simp(r2)
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
    (r1s, r2s) match {
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
      case (ZERO, _) => (r2s, F_RIGHT(f2s))
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
      case (_, ZERO) => (r1s, F_LEFT(f1s))
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
      case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
                else (ALT (r1s, r2s), F_ALT(f1s, f2s)) 
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
    }
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
  }
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  case SEQ(r1, r2) => {
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
    val (r1s, f1s) = simp(r1)
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
    val (r2s, f2s) = simp(r2)
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
    (r1s, r2s) match {
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
      case (ZERO, _) => (ZERO, F_ERROR)
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
      case (_, ZERO) => (ZERO, F_ERROR)
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
      case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
    }
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  }
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  case r => (r, F_ID)
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
}