mirror of
https://github.com/ton-blockchain/ton
synced 2025-02-14 20:22:19 +00:00
FunC's (and Tolk's before this PR) type system is based on Hindley-Milner. This is a common approach for functional languages, where types are inferred from usage through unification. As a result, type declarations are not necessary: () f(a,b) { return a+b; } // a and b now int, since `+` (int, int) While this approach works for now, problems arise with the introduction of new types like bool, where `!x` must handle both int and bool. It will also become incompatible with int32 and other strict integers. This will clash with structure methods, struggle with proper generics, and become entirely impractical for union types. This PR completely rewrites the type system targeting the future. 1) type of any expression is inferred and never changed 2) this is available because dependent expressions already inferred 3) forall completely removed, generic functions introduced (they work like template functions actually, instantiated while inferring) 4) instantiation `<...>` syntax, example: `t.tupleAt<int>(0)` 5) `as` keyword, for example `t.tupleAt(0) as int` 6) methods binding is done along with type inferring, not before ("before", as worked previously, was always a wrong approach)
80 lines
1.6 KiB
Text
80 lines
1.6 KiB
Text
fun f(a: int, b: int, c: int, d: int, e: int, f: int): (int, int) {
|
|
// solve a 2x2 linear equation
|
|
var D: int = a*d - b*c;;;; var Dx: int = e*d-b*f ;;;; var Dy: int = a * f - e * c;
|
|
return (Dx/D,Dy/D);
|
|
};;;;
|
|
|
|
fun calc_phi(): int {
|
|
var n = 1;
|
|
repeat (70) { n*=10; };
|
|
var p= 1;
|
|
var `q`=1;
|
|
_=`q`;
|
|
do {
|
|
(p,q)=(q,p+q);
|
|
} while (q <= n); //;;
|
|
return mulDivRound(p, n, q);
|
|
}
|
|
|
|
fun calc_sqrt2(): int {
|
|
var n = 1;
|
|
repeat (70) { n *= 10; }
|
|
var p = 1;
|
|
var q = 1;
|
|
do {
|
|
var t = p + q;
|
|
(p, q) = (q, t + q);
|
|
} while (q <= n);
|
|
return mulDivRound(p, n, q);
|
|
}
|
|
|
|
fun calc_root(m: int) {
|
|
var base: int=1;
|
|
repeat(70) { base *= 10; }
|
|
var (a, b, c) = (1,0,-m);
|
|
var (p1, q1, p2, q2) = (1, 0, 0, 1);
|
|
do {
|
|
var k: int=-1;
|
|
var (a1, b1, c1) = (0, 0, 0);
|
|
do {
|
|
k+=1;
|
|
(a1, b1, c1) = (a, b, c);
|
|
c+=b;
|
|
c += b += a;
|
|
} while (c <= 0);
|
|
(a, b, c) = (-c1, -b1, -a1);
|
|
(p1, q1) = (k * p1+q1, p1);
|
|
(p2, q2) = (k * p2+q2, p2);
|
|
} while (p1 <= base);
|
|
return (p1, q1, p2, q2);
|
|
}
|
|
|
|
fun ataninv(base: int, q: int): int { // computes base*atan(1/q)
|
|
base=base~/q;
|
|
q*=-q;
|
|
var sum: int = 0;
|
|
var n: int = 1;
|
|
do {
|
|
sum += base~/n;
|
|
base = base~/q;
|
|
n += 2;
|
|
} while (base != 0);
|
|
return sum;
|
|
}
|
|
|
|
fun calc_pi(): int {
|
|
var base: int = 64;
|
|
repeat (70) { base *= 10; }
|
|
return (ataninv(base << 2, 5) - ataninv(base, 239))~>>4;
|
|
}
|
|
|
|
fun main(): int {
|
|
return calc_pi();
|
|
}
|
|
|
|
/**
|
|
method_id | in | out
|
|
@testcase | 0 | | 31415926535897932384626433832795028841971693993751058209749445923078164
|
|
|
|
@code_hash 84337043972311674339187056298873613816389434478842780265748859098303774481976
|
|
*/
|