Skip to content

Commit

Permalink
Define functions in scala, support up to 32
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek committed Sep 10, 2023
1 parent f70e7e0 commit 05bccc1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
10 changes: 0 additions & 10 deletions core/src/main/resources/bosatsu/predef.bosatsu
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export (
Order(),
Unit(),
Dict,
Fn1, Fn2, Fn3, Fn4, Fn5, Fn6, Fn7, Fn8,
add,
add_key,
cmp_Int,
Expand Down Expand Up @@ -53,15 +52,6 @@ struct TupleCons(first, second)
enum Bool:
False, True

external struct Fn1[a: -*, z: +*]
external struct Fn2[a: -*, b: -*, z: +*]
external struct Fn3[a: -*, b: -*, c: -*, z: +*]
external struct Fn4[a: -*, b: -*, c: -*, d: -*, z: +*]
external struct Fn5[a: -*, b: -*, c: -*, d: -*, e: -*, z: +*]
external struct Fn6[a: -*, b: -*, c: -*, d: -*, e: -*, f: -*, z: +*]
external struct Fn7[a: -*, b: -*, c: -*, d: -*, e: -*, f: -*, g: -*, z: +*]
external struct Fn8[a: -*, b: -*, c: -*, d: -*, e: -*, f: -*, g: -*, h: -*, z: +*]

#############
# Support for built-in lists
#############
Expand Down
19 changes: 18 additions & 1 deletion core/src/main/scala/org/bykn/bosatsu/Package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,24 @@ object Package {
*/
lazy val predefPackage: Package.Parsed =
parser(None).parse(Predef.predefString) match {
case Right((_, pack)) => pack
case Right((_, pack)) =>
// Make function defs:
def paramType(n: Int) = (TypeRef.TypeVar(s"i$n"), Some(Kind.Arg(Variance.contra, Kind.Type)))
def makeFns(n: Int,
typeArgs: List[(TypeRef.TypeVar, Option[Kind.Arg])],
acc: List[Statement.ExternalStruct]): List[Statement.ExternalStruct] =
if (n > Type.FnType.MaxSize) acc
else {
val fn = Statement.ExternalStruct(Identifier.Constructor(s"Fn$n"), typeArgs)(Region(0, 1))
val acc1 = fn :: acc
makeFns(n + 1, paramType(n) :: typeArgs, acc1)
}

val out = (TypeRef.TypeVar("z"), Some(Kind.Arg(Variance.co, Kind.Type)))
val allFns = makeFns(1, paramType(0) :: out :: Nil, Nil).reverse
val exported = allFns.map { extstr => ExportedName.TypeName(extstr.name, ()) }
// Add functions into the predef
pack.copy(exports = exported ::: pack.exports, program = allFns ::: pack.program)
case Left(err) =>
val idx = err.failedAtOffset
val lm = LocationMap(Predef.predefString)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/org/bykn/bosatsu/rankn/Type.scala
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ object Type {
val DictType: Type.TyConst = TyConst(Const.predef("Dict"))

object FnType {
final val MaxSize = 8
final val MaxSize = 32

private def predefFn(n: Int) = TyConst(Const.predef(s"Fn$n"))
private val tpes = (1 to MaxSize).map(predefFn)
Expand Down

0 comments on commit 05bccc1

Please sign in to comment.