forked from google-research/dex-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser-tests.dx
130 lines (97 loc) · 1.78 KB
/
parser-tests.dx
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'For now, arithmetic is not sensitive to whitespace:
:p 1.0+1.0
> 2.
:p 1.0 +1.0
> 2.
:p 1.0+ 1.0
> 2.
:p 1.0 + 1.0
> 2.
:p 1.0-1.0
> 0.
:p 1.0 -1.0
> 0.
:p 1.0- 1.0
> 0.
:p 1.0 - 1.0
> 0.
'Applying a function to a negative literal thus requires parentheses.
f = \x. x + 10.
:p f -1.0 -- parses as (-) f (-1.0)
> Type error:
> Expected: (Float32 -> Float32)
> Actual: Float32
>
> :p f -1.0 -- parses as (-) f (-1.0)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
:p f (-1.0)
> 9.
'Lambdas can have specific arrow annotations.
lam1 = \n ?-> \x. fromOrdinal n x
:t lam1
> ((n:Type) ?-> Int32 -> n)
lam2 = \x --o 2.0 * x
:t lam2
> (Float32 --o Float32)
lam3 = \d:(Eq Int) ?=> ()
:t lam3
> ((Eq Int32) ?=> Unit)
lam4 = \n m ?-> (0@n, 0@m)
:t lam4
> ((n:Type) ?-> (m:Type) ?-> (n & m))
-- Not allowed to write regular lambdas or tables using explicit arrows:
\x -> x + 1.0
> Parse error:62:7:
> |
> 62 | \x -> x + 1.0
> | ^
> To construct an explicit lambda function, use '.' instead of '->'
>
\i => i
> Parse error:71:7:
> |
> 71 | \i => i
> | ^
> To construct a table, use 'for i. body' instead of '\i => body'
>
:p (
1
+
2
)
> 3
:p
xs = [1,2,3]
for i.
if xs.i > 1
then 0
else 1
> [1, 0, 0]
:p
runState 5 \ref.
n = get ref
for_ i:(Fin n).
ref := get ref + 1
> ((), 10)
def myInt : Int = 1
:p myInt
> 1
def myInt : {State h} Int = 1
> Parse error:107:27:
> |
> 107 | def myInt : {State h} Int = 1
> | ^
> Nullary def can't have effects
:p
yieldAccum (AddMonoid Float) \ref.
x = if True then 1. else 3.
if True then ref += x
if True then
ref += 1.
ref += 2.
if False then ref += 100. else
ref += 1.
ref += 2.
if True
then ref += 2.
> 9.