-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: InlineIdentity optimization pass
- Loading branch information
Showing
3 changed files
with
34 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package scalus.uplc | ||
import scalus.uplc.Term.* | ||
|
||
/** Inlines identity function application */ | ||
object InlineIdentity: | ||
/** Inlines identity function application */ | ||
def apply(term: Term): Term = inlineIdentity(term) | ||
|
||
/** Inlines identity function application */ | ||
def inlineIdentity(term: Term): Term = term match | ||
case Apply(LamAbs(param, Var(NamedDeBruijn(name, _))), arg) if param == name => | ||
inlineIdentity(arg) | ||
case Apply(f, arg) => Apply(inlineIdentity(f), inlineIdentity(arg)) | ||
case Force(term) => Force(inlineIdentity(term)) | ||
case Delay(term) => Delay(inlineIdentity(term)) | ||
case LamAbs(param, body) => LamAbs(param, inlineIdentity(body)) | ||
case Constr(tag, args) => Constr(tag, args.map(inlineIdentity)) | ||
case Case(arg, cases) => Case(inlineIdentity(arg), cases.map(inlineIdentity)) | ||
case _ => term |
13 changes: 13 additions & 0 deletions
13
shared/src/test/scala/scalus/uplc/InlineIdentitySpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package scalus | ||
package uplc | ||
|
||
import scalus.uplc.Term.* | ||
import org.scalatest.funsuite.AnyFunSuite | ||
|
||
class InlineIdentitySpec extends AnyFunSuite { | ||
test("inlineIdentity should inline identity function application") { | ||
val term = Apply(LamAbs("x", Var(NamedDeBruijn("x"))), Var(NamedDeBruijn("y"))) | ||
val expected = Var(NamedDeBruijn("y")) | ||
assert(InlineIdentity.inlineIdentity(term) == expected) | ||
} | ||
} |