From ec40f58b6db08b352fdc28fd7987feba40377432 Mon Sep 17 00:00:00 2001 From: "P. Oscar Boykin" Date: Sun, 10 Sep 2023 07:19:31 -1000 Subject: [PATCH] Define functions in scala, support up to 32 (#1039) --- .../src/main/resources/bosatsu/predef.bosatsu | 10 ---------- .../main/scala/org/bykn/bosatsu/Package.scala | 19 ++++++++++++++++++- .../scala/org/bykn/bosatsu/rankn/Type.scala | 2 +- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/core/src/main/resources/bosatsu/predef.bosatsu b/core/src/main/resources/bosatsu/predef.bosatsu index b56da575e..48cd4a99f 100644 --- a/core/src/main/resources/bosatsu/predef.bosatsu +++ b/core/src/main/resources/bosatsu/predef.bosatsu @@ -12,7 +12,6 @@ export ( Order(), Unit(), Dict, - Fn1, Fn2, Fn3, Fn4, Fn5, Fn6, Fn7, Fn8, add, add_key, cmp_Int, @@ -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 ############# diff --git a/core/src/main/scala/org/bykn/bosatsu/Package.scala b/core/src/main/scala/org/bykn/bosatsu/Package.scala index 282154bf4..0d26e47bc 100644 --- a/core/src/main/scala/org/bykn/bosatsu/Package.scala +++ b/core/src/main/scala/org/bykn/bosatsu/Package.scala @@ -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) diff --git a/core/src/main/scala/org/bykn/bosatsu/rankn/Type.scala b/core/src/main/scala/org/bykn/bosatsu/rankn/Type.scala index 1979864ed..01f078891 100644 --- a/core/src/main/scala/org/bykn/bosatsu/rankn/Type.scala +++ b/core/src/main/scala/org/bykn/bosatsu/rankn/Type.scala @@ -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)