effekt icon indicating copy to clipboard operation
effekt copied to clipboard

Implement Monomorphization optimization

Open mattisboeckle opened this issue 9 months ago • 1 comments

Aims to implement monomorphization for effekt.

  • [x] Simple polymorphism
  • [x] Functions with multiple polymorphic types
  • [x] Different orders of types
  • [x] Polymorphic data types
  • [x] Polymorphic effects
  • [x] Local polymorphic functions

The following programs have been used to (manually) test the implementation and should give an overview of what already works.

Simple polymorphic functions

def a[A](in: A) = { in }
def b[B](in: B): B = { a(in) }
def c[C](in: C): C = { b(in) }
def d[D](in: D): D = { c(in) }

def main() = {
  println(a(1))
  println(d("32"))
}

Multiple type arguments + switching the order of types

def f[C, D](in: C, in2: D) = in
def g[A, B](in: A, in2: B) = { f[B, A](in2, in) }
def main() = {
  println(g(3, "3"))
  println(g('c', false))
}

Polymorphic data types

type Maybe[A] {
  Nothing()
  Just(x: A)
}

def f[T](a: T): Maybe[T] = Just(a)

def main() = {
  val a = Just[Int](5)
  val b = Nothing[Char]()
  f(5)
  ()
}

Polymorphic effects

effect yield[A](x: A): Unit

def f() = {
  do yield[Int](123)
  do yield[String]("test")
}

def main() = {
  try {
    f()
  } with yield[Int] {
    x => println(x)
      resume(())
      resume(())
  } with yield[String] {
    x => println(x)
    resume(())
  }
  ()
}

mattisboeckle avatar May 26 '25 13:05 mattisboeckle

This works for most programs now. There are still some open problems:

  • [ ] Polymorphic recursion detection is more of a guessing game right now
  • [ ] What to do with extern types
  • [ ] A normalizer (?) bug, that removes println in ImpureApp, causing some tests to fail (because the output is empty)

mattisboeckle avatar Nov 30 '25 09:11 mattisboeckle