|
| 1 | +// Draft of higher order expressions |
| 2 | +// --------------------------------- |
| 3 | +// A higher order expression is a an expression that may contain unbound variables. |
| 4 | +// Unbound variables are listed as arguments of the type lambda of the expression. |
| 5 | +// Splicing can only be done on Expr[_ <: Any]. |
| 6 | +// `apply methods on higher order expression provide a way to replace the unbound variables. |
| 7 | + |
| 8 | +// This can be used for HOAS quoted patterns to not return a lambda (see HOAS patterns section in https://infoscience.epfl.ch/record/288718?ln=en). |
| 9 | +// The use would be the same as each higher order expression would have an apply method. |
| 10 | +// But as it would be an expression, the expression would showable. |
| 11 | +// The expression could also be directly transformed into a Term of the reflection API. |
| 12 | + |
| 13 | +// Question: How to extend this to allow unbound type (Type) in the expression? |
| 14 | + |
| 15 | +class Expr[+T <: AnyKind] |
| 16 | +object Expr: |
| 17 | + extension [T, R](hoe: Expr[[_ >: T <: T] =>> R]) def apply(x1: Expr[T]): Expr[R] = ??? |
| 18 | + extension [T1, T2, R](hoe: Expr[[_ >: T1 <: T1, _ >: T2 <: T2] =>> R]) def apply(x1: Expr[T1], x2: Expr[T2]): Expr[R] = ??? |
| 19 | + // Are lower bounds in lambda parameters necessary? |
| 20 | + // How could this be generalized to n arguments? |
| 21 | + |
| 22 | + |
| 23 | +def `'`[T](e: T): Expr[T] = ??? |
| 24 | +def `$`[T](e: Expr[T]): T = ??? |
| 25 | + |
| 26 | +def f(): Unit = |
| 27 | + val e: Expr[Int] = ??? |
| 28 | + val hoe1: Expr[[T >: Int <: Int] =>> Int] = ??? // assumed to come from HOAS pattern with one unbound variable of type Int |
| 29 | + val hoe2: Expr[[T1 >: Int <: Int, T2 >: Int <: Int] =>> Int] = ??? // assumed to come from HOAS pattern with 2 unbound variables of type Int |
| 30 | + val e2: Expr[Int] = hoe1(e) |
| 31 | + val e3: Expr[Int] = hoe2(e, e) |
| 32 | + |
| 33 | + { |
| 34 | + `$`{e} |
| 35 | + `$`{e2} |
| 36 | + `$`{e3} |
| 37 | + `$`{hoe1(e)} |
| 38 | + `$`{hoe2(e, e)} |
| 39 | + `$`{hoe1} // error: Found: Expr[[T >: Int <: Int] =>> Int]), Required: Expr[Any] // may contain references to 1 unbound variable |
| 40 | + `$`{hoe2} // error // may contain references to 2 unbound variables |
| 41 | + } |
| 42 | + `'`{1} |
| 43 | + `'`{`$`{e}} |
| 44 | + `'`{??? : ([T >: Int <: Int] =>> Int)} // error: Missing type parameter for [T >: Int <: Int] =>> Int // not a valid quote literal expression |
0 commit comments