# HG changeset patch # User Christian Urban # Date 1475941451 -3600 # Node ID 16742bf623650c0a3a46bdb09949961f9d8d57dd # Parent e7d0157f047134d863197cb75d51043892b9eac5 updated diff -r e7d0157f0471 -r 16742bf62365 progs/app05.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/app05.scala Sat Oct 08 16:44:11 2016 +0100 @@ -0,0 +1,21 @@ +def simp(r: Rexp): (Rexp, Val => Val) = r match { + case ALT(r1, r2) => { + val (r1s, f1s) = simp(r1) + val (r2s, f2s) = simp(r2) + (r1s, r2s) match { + case (NULL, _) => (r2s, F_RIGHT(f2s)) + case (_, NULL) => (r1s, F_LEFT(f1s)) + case _ => + if (r1s == r2s) (r1s, F_LEFT(f1s)) + else (ALT (r1s, r2s), F_ALT(f1s, f2s)) + } + } + ... +} + +def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v)) +def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v)) +def F_ALT(f1: Val => Val, f2: Val => Val) = + (v:Val) => v match { + case Right(v) => Right(f2(v)) + case Left(v) => Left(f1(v)) } diff -r e7d0157f0471 -r 16742bf62365 progs/app06.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/app06.scala Sat Oct 08 16:44:11 2016 +0100 @@ -0,0 +1,21 @@ +def simp(r: Rexp): (Rexp, Val => Val) = r match { + case SEQ(r1, r2) => { + val (r1s, f1s) = simp(r1) + val (r2s, f2s) = simp(r2) + (r1s, r2s) match { + case (NULL, _) => (NULL, F_ERROR) + case (_, NULL) => (NULL, F_ERROR) + case (EMPTY, _) => (r2s, F_SEQ_Void1(f1s, f2s)) + case (_, EMPTY) => (r1s, F_SEQ_Void2(f1s, f2s)) + case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s)) + } + } + ... + +def F_SEQ_Void1(f1: Val => Val, f2: Val => Val) = + (v:Val) => Sequ(f1(Void), f2(v)) +def F_SEQ_Void2(f1: Val => Val, f2: Val => Val) = + (v:Val) => Sequ(f1(v), f2(Void)) +def F_SEQ(f1: Val => Val, f2: Val => Val) = + (v:Val) => v match { + case Sequ(v1, v2) => Sequ(f1(v1), f2(v2)) }