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

Define functions in scala, support up to 32 #1039

Merged
merged 1 commit into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading