progs/app06.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 29 Oct 2023 00:06:30 +0100
changeset 947 dc31a099dc98
parent 788 3b1136fb6bee
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
446
16742bf62365 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
def simp(r: Rexp): (Rexp, Val => Val) = r match {
16742bf62365 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
  case SEQ(r1, r2) => {
16742bf62365 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
    val (r1s, f1s) = simp(r1)
16742bf62365 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
    val (r2s, f2s) = simp(r2)
16742bf62365 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
    (r1s, r2s) match {
453
36e5752fa191 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 446
diff changeset
     6
      case (ZERO, _) => (ZERO, F_ERROR)
36e5752fa191 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 446
diff changeset
     7
      case (_, ZERO) => (ZERO, F_ERROR)
788
3b1136fb6bee updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 453
diff changeset
     8
      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
3b1136fb6bee updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 453
diff changeset
     9
      case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
446
16742bf62365 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
16742bf62365 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
    }
16742bf62365 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
  }
788
3b1136fb6bee updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 453
diff changeset
    13
  ...}
3b1136fb6bee updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 453
diff changeset
    14
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = 
3b1136fb6bee updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 453
diff changeset
    15
  (v:Val) => Sequ(f1(Empty), f2(v))
3b1136fb6bee updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 453
diff changeset
    16
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = 
3b1136fb6bee updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 453
diff changeset
    17
  (v:Val) => Sequ(f1(v), f2(Empty))
446
16742bf62365 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
def F_SEQ(f1: Val => Val, f2: Val => Val) = 
16742bf62365 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
  (v:Val) => v match {
16742bf62365 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
    case Sequ(v1, v2) => Sequ(f1(v1), f2(v2)) }