Last active
September 13, 2024 01:44
-
-
Save xuwei-k/fa5f83035b9e73ab75aa6ca3e3d970c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.compiletime.ops.int | |
import scala.compiletime.ops.long | |
import scala.compiletime.ops.double | |
type Factorial[A <: Int] = | |
Factorial0[A, 1L] | |
type Factorial0[A <: Int, B <: Long] <: Long = | |
int.<[A, 1] match | |
case true => | |
B | |
case _ => | |
Factorial0[ | |
int.-[A, 1], | |
long.*[ | |
int.ToLong[A], | |
B | |
] | |
] | |
type Exponential[A <: Long, N <: Int] = | |
Exponential0[A, 1L, N] | |
type Exponential0[A <: Long, B <: Long, N <: Int] <: Long = | |
N match | |
case 0 => | |
B | |
case _ => | |
Exponential0[A, long.*[A, B], int.-[N, 1]] | |
type F1[N <: Int] = | |
double.*[ | |
double./[ | |
long.ToDouble[ | |
Exponential[ | |
Factorial[ | |
int.*[ | |
2, | |
N | |
] | |
], | |
3 | |
] | |
], | |
long.ToDouble[ | |
Exponential[ | |
Factorial[N], | |
6 | |
] | |
] | |
], | |
double./[ | |
long.ToDouble[ | |
long.+[ | |
long.*[ | |
42L, | |
int.ToLong[N] | |
], | |
5L | |
] | |
], | |
long.ToDouble[ | |
Exponential[ | |
2L, | |
int.*[12, N] | |
] | |
] | |
] | |
] | |
type F2[N <: Int, A <: Double] <: Double = | |
int.<[N, 0] match { | |
case true => | |
A | |
case _ => | |
F2[int.-[N, 1], double.+[F1[N], A]] | |
} | |
type F[N <: Int] = | |
double./[ | |
16.0, | |
F2[N, 0.0] | |
] | |
def f = { | |
summon[Exponential[2L, 3] =:= 8L] | |
summon[Exponential[4L, 5] =:= 1024L] | |
summon[Factorial[4] =:= 24L] | |
summon[Factorial[5] =:= 120L] | |
summon[Factorial[6] =:= 720L] | |
summon[F[5] =:= 3.141592655748593] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment