Skip to content

Implement Monomorphization optimization#1024

Draft
mattisboeckle wants to merge 109 commits intomainfrom
mono
Draft

Implement Monomorphization optimization#1024
mattisboeckle wants to merge 109 commits intomainfrom
mono

Conversation

@mattisboeckle
Copy link
Contributor

@mattisboeckle mattisboeckle commented May 26, 2025

Aims to implement monomorphization for effekt.

  • Simple polymorphism
  • Functions with multiple polymorphic types
  • Different orders of types
  • Polymorphic data types
  • Polymorphic effects
  • 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(())
  }
  ()
}

@jiribenes jiribenes added the experiment Experimental branch, do not merge! label Jun 17, 2025
@mattisboeckle mattisboeckle force-pushed the mono branch 2 times, most recently from 1f90ac7 to a4439d5 Compare August 28, 2025 06:53
@mattisboeckle mattisboeckle changed the title Very WIP: Implement Monomorphization optimization Implement Monomorphization optimization Nov 30, 2025
@mattisboeckle
Copy link
Contributor Author

mattisboeckle commented Nov 30, 2025

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)

b-studios pushed a commit that referenced this pull request Feb 24, 2026
We were throwing away information about extern data types when
transforming to core.
This info is relevant for some Core -> Core phases (like
Monomorphization #1024), so we know whether a type is Extern instead of
guessing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

experiment Experimental branch, do not merge!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants