-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.fy
109 lines (89 loc) · 1.76 KB
/
test.fy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# hello comments!
# statements are terminated by ;
# constants
1;
2;
"hello world";
true;
# variables
x;
my_variable;
math.pi;
# function calls
f();
a[0];
f(x);
f(x,1);
f(g(x), true, "hello", 1);
# tuples
(1,2,3);
(1,);
# lists
[1,2,3];
# set
{1, 2, 3};
# dicts
{ "foo": "bar", "tar": "zar" };
#
# # lamdbas
lambda (x: int, y: int) => f(y, x);
# infix operators
1 + 2;
1 + 3 + 3;
1 + 2 * 3 - 4;
# parenthesized expressions
2 * (3 + 4);
# binary operations
1 & 2;
# boolean operators
1 == 1;
1 != 1;
1 > 1;
1 >= 1;
1 < 1;
1 <= 1;
# let expressions
let x = 1 in x + 1;
# function definitions, with optional generic arguments
def id_int(x: int): int = x;
def id_int[T](x: T): T = x;
def id_int[T: int](x: T): T = x;
def id_byte_or_string[T: (str, bytes)](x: T): T = x;
def id_byte_or_string(x: str ^ bytes): T = x;
#
# # imports
import math;
from op import mul, sub, eq;
def dsrqt(x: float): float =
op.mul(math.sqrt(x), 2);
# types
type Opt[T] = Union[Some[T], None];
type Resut[A, B] = Union[Ok[A], Err[B]];
def ret_some(x: int): Opt[int] = Some(x);
def bind_some(x: Opt[int], f: Arrow[int, Opt[int]]): Opt[int] =
match x with
| Some(x) => f(x)
| None => None
end;
#
type Opt[T] = Some[T] ^ None;
def bind_some2(x: Opt[int], f: int -> Opt[int]): Opt[int] =
match x with
| Some(x) => f(x)
| None => None
end;
def fact(x: int): int =
if eq(x, 1) then 1 else mul(x, fact(sub(x, 1)));
def opt_add[A](a: Opt[A], b: Opt[A]): Opt[A] =
match a with
| None => None
| Some(a) =>
match b with
| Some(b) => Some(add(a, b))
| None => Some(a)
end
end;
def opt_add2(a: Opt[int], b: Opt[int]): Opt[int] =
bind_some2(a, lambda (x: int) =>
bind_some2(b, lambda (y: int) =>
Some(x + y)));