Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opaque traits without type encoding #1612

Open
vkuncak opened this issue Dec 6, 2024 · 0 comments
Open

opaque traits without type encoding #1612

vkuncak opened this issue Dec 6, 2024 · 0 comments
Labels

Comments

@vkuncak
Copy link
Collaborator

vkuncak commented Dec 6, 2024

The following program uses no type encoding:

  final class MyFun[A,B](f: A => B):
    def apply(x: A): B = f(x)

  def applyAll(l: List[MyFun[Int,Int]], init: Int): Int = {
    l match {
      case Nil() => init
      case Cons(h, t) => applyAll(t, h(init))
    }
  }

  val t1 = MyFun[Int,Int]((x:Int) => x + 1)

  val t2 = new MyFun[Int,Int]((x: Int) => x - 1)

  val l: List[MyFun[Int,Int]] = List(t2, t1, t2)

  @main @extern
  def f =
    val res: Int = applyAll(l, 100)

If instead we use traits, we end up using type encoding, which can make things much less efficient in large programs.

  trait MyFun[A,B]:
    def apply(x: A): B  

  def applyAll(l: List[MyFun[Int,Int]], init: Int): Int = {
    l match {
      case Nil() => init
      case Cons(h, t) => applyAll(t, h(init))
    }
  }

  val t1 = new MyFun[Int,Int]:
    def apply(x: Int) = x + 1

  val t2 = new MyFun[Int,Int]:
    def apply(x: Int) = x - 1

  val l: List[MyFun[Int,Int]] = List(t2, t1, t2)

  @main @extern
  def f =
    val res: Int = applyAll(l, 100)

I would like that, if we write @opaque trait MyFun then it can only be extended by final and anonymous classes, and we treat it more like the first case class, avoiding the type encoding. Using function types will not work in the presence of mutation, but we should be able to do it by generating extern methods. As with opaque methods, we are purposely giving up on the precision of the underapproximation and the ability to execute the resulting encoding.

@vkuncak vkuncak added the feature label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant