-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1-zilch.tex
1414 lines (1150 loc) · 63 KB
/
part1-zilch.tex
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
.\begingroup
\usingnamespace{zilch:grammar}
\usingnamespace{zilch:rules}
\part{The language}\label{part:zilch}
\chapter{Introduction}\label{chap:zilch-introduction}
Low-level programming is programming with a few abstractions over the hardware.
For example, assembly languages mostly provide no or very little abstractions over CPU instructions (having one instruction set per target).
Functional programming is a kind of programming focused around functions and their composition.
However, because of this, many functional programming languages have a tendancy to be quite slow, at least compared to ``ordinary'' imperative programming languages.
\href{http://www.ats-lang.org/}{ATS} is one of those programming languages claiming to be both functional and quite low-level.
It compiles to C (therefore has a free FFI), has a complex dependent type system and many other things making it kind of great.
But ATS is hard to learn, because it has so much stuff\ldots
Yet Zilch is also a low-level functional programming language.
But the goal is to design a not-so-hard to learn programming language, while still having strong guarantees about the code written\footnote{When this was written, Zilch was far from production-ready, so most
of the guarantees have not yet been described here.}.
Prior knowledge about ML-style programming is still recommended before trying to tackle Zilch, but anybody without this kind of knowledge should also be able to learn Zilch fairly easily.
This part will try to describe and formalize (at least a bit, but some parts may be left informal) the Zilch programming language, going from the usual syntax onto the operational semantics.
Implementation details (such as error messages, program optimisations, \ldots) will not be covered by this document.
\section*{Some notes on the notation used}\label{sec:zilch-introduction-notation}
Zilch is really not a small programming language.
Because of that, sometimes we need some specific notation to described things like inference rules.
The mainly used notation is described here, but not necessarily everything will be described (sometimes, a little bit of common sense helps to understand).
\begin{itemize}
\item Grammar
\begin{figure}[H]
\centering
\scalebox{.5}{
\includegraphics{grammar-template-2}
}
\end{figure}
\begin{itemize}
\item Sharp rectangles describe that another rule is to be used there (the name of the rule is given in the rectangle);
\item Rounded rectangles describe terminal tokens, i.e.\ pieces of string that must be litterally matched;
\item The name of the grammar rule defined is given in the top-left corner of the diagram.
A rule matches if and only if it is possible to go from the left to the right, only following the rails;
\end{itemize}
\item Type inference rules
\begin{itemize}
\item $\Gamma$ is the context containing all type-level information such as types of expressions;
\item The typing judgment $\TypeSequent{\Gamma}{e}{\rho}{\tau}{\epsilon}$ means that $e$ has type $\tau$ with multiplicity $\rho$ in the context $\Gamma$ and that evaluating it may yield effects in $\epsilon$;
\item $\scale{\pi}{\Gamma}$ denotes the scaling (multiplication) of all the multiplicities within $\Gamma$ by $\pi$;
\item $\add{\Gamma_1}{\Gamma_2}$ is pointwise addition between the two contexts $\Gamma_1$ and $\Gamma_2$.
\end{itemize}
\end{itemize}
\chapter{Grammar of Zilch programs}\label{chap:zilch-grammar}
A Zilch program is comprised of three different levels, each included in the next one:
\begin{itemize}
\item The expression level, where an expression denotes a value which has a statically determined type;
\item The declaration level, containing all function definitions, type definitions, etc;
\item The module level, where imports and module declarations live in;
\end{itemize}
\noindent Because it is not easy to materialize indentation properties in the grammar, it will instead be marked using the non-terminals\footnote{Those symbols are reserved and actually used as terminals, so we put them as non-terminals to remove any ambiguity opportunities.} \texttt{\{}, \texttt{\}} and \texttt{;}.
Note that these are not actually present in the source code, and are only a mean of hinting an indentation change.
\texttt{x \{ y; z \}} really means \texttt{x} followed by \texttt{y} which may be more indented or on the same line, and \texttt{z} which must be aligned with the beginning of \texttt{y}.
It therefore describes both layouts:
\noindent\begin{minted}{\zilchlexer}
-- `y` on the same line as `x`:
x y
z
-- or `y` on a new line:
x
y
z
\end{minted}
\vspace*{\baselineskip}
\begin{warningbox}
If you are willing to make your own Zilch compiler, please note that you may also (or only) support the alternative layout as described in grammar rules, by making use of \verb|{|, \verb|;| and \verb|}| as terminals.
It is not recommended, as \verb|;| is \textit{not} a reserved word, but may be considered for simplicity's sake.
It is also not recommended as using \verb|{| and \verb|}| may clash with inline records and implicit parameters.
\end{warningbox}
\section{Lexicon}\label{sec:zilch-grammar-lexical}
This section describes the lexical structure of any Zilch program.
Note that the Unicode alternative syntax does not need to be supported, but makes the code look nicer.
\subsection{Identifiers, operators and reserved words}\label{subsec:zilch-grammar-lexical-identifiers}
Identifiers and operators are composed of only printable characters which are not considered special, and must not form keywords.
Lexically distinguishing between both types of symbols is done by only allowing alphanumeraical characters in identifiers, and symbolic characters in operators.
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\parbox{\textwidth}{
\centering
\scalebox{.75}{
\includegraphics[width=\linewidth]{zilch/lexicon/keywords}
}
\\
\scalebox{.8}{
\includegraphics[width=\linewidth]{zilch/lexicon/special}
}
\\
\scalebox{.4}{
\includegraphics[width=\linewidth]{zilch/lexicon/identifier}
}
}
}
\caption{Lexical units for identifiers and operators.}
\label{fig:zilch-grammar-lexical-identifiers-grammar}
\end{figure}
\subsection{Whitespaces}\label{subsec:zilch-grammar-lexical-whitespaces}
Whitespace tokens are basically word separators.
Because of that, comments are also counted as whitespaces, despite not really being an invisible sequence.
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\scalebox{.5}{
\includegraphics{zilch/lexicon/whitespaces}
}
}
\caption{Whitespace lexical units.}
\label{fig:zilch-grammar-lexical-whitespaces-grammar}
\end{figure}
\subsection{Numerical tokens}\label{subsec:zilch-grammar-lexical-numbers}
There are two kinds of numerical tokens: integers and floating points.
While floating points are always written using decimal digits, integers may be written using decimal digits, octal digits (if prefixed by \verb|0o| or \verb|0O|), hexadecimal digits (if prefixed by \verb|0x| or \verb|0X|) or binary digits (if prefixed by \verb|0b| or \verb|0B|).
A floating point number must always have digits on both sides of the dot (i.e.\ it is impossible to write \verb|0.| as in other programming languages) because it could else be mistaken with the dot syntax.
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\scalebox{.5}{
\includegraphics{zilch/lexicon/number}
}
}
\caption{Numeric lexical units.}
\label{fig:zilch-grammar-lexical-numbers-grammar}
\end{figure}
\subsection{String and character literals}\label{subsec:zilch-grammar-lexical-strings}
Strings are arrays of characters.
However, because it is tedious to write such arrays, we provide syntactic sugar to make it easier.
String literals are enclosed in double quotes \verb|"|, and character literal are enclosed in single quotes \verb|'|.
They can contain any character (both graphical and whitespaces), but some characters may be easier to type.
These basically have ``shorthands'' defined as escape sequences, each defined in Table~\ref{table:zilch-grammar-lexical-strings-escapesequences}.
Note that, while double and single quotes may be escaped, these may not necessarily be in respectively character and string literals.
Multiple string literals directly next to each other (as in \mintinline{\zilchlexer}{"abc" "def"}) are concatenated into one (so \mintinline{\zilchlexer}{"abcdef"}).
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\parbox{\textwidth}{
\centering
\scalebox{.5}{
\includegraphics{zilch/lexicon/string}
}
\\
\scalebox{.5}{
\includegraphics{zilch/lexicon/character}
}
}
}
\caption{String and character lexical units.}
\label{fig:zilch-grammar-lexical-strings-grammar}
\end{figure}
\begin{table}[H]
\begin{tabularx}{\textwidth}{*{5}{Y}}
\toprule
\verb|\a| & \verb|\b| & \verb|\f| & \verb|\n| & \verb|\r| \\
\verb|\t| & \verb|\v| & \verb|\\| & \verb|\"| & \verb|\'| \\
\bottomrule
\end{tabularx}
\caption{All available escape sequences.}
\label{table:zilch-grammar-lexical-strings-escapesequences}
\end{table}
\section{Expressions}\label{sec:zilch-grammar-expressions}
Expressions are the basic building block of Zilch.
\textit{Everything} is an expression, from simple arithmetic like \texttt{3 + 6} to complex \texttt{let-in} expressions.
\subsection{Lambda abstraction}\label{subsec:zilch-grammar-expressions-lambda}
Lambda abstractions allow creating ``in-place'' anonymous functions.
For example, both following alternatives are equivalent (because the lambda abstraction does not capture anything there):
\begin{listing}[H]
\inputminted{\zilchlexer}{examples/captureless-lambda-equivalence.zc}
\captionsetup{style=invisible}
\caption{Equivalence of non-capturing lambda with top-level function.}
\end{listing}
\vspace*{-\baselineskip}
\noindent However, these two are not quite equivalent:
\begin{listing}[H]
\inputminted{\zilchlexer}{examples/closure-non-equivalence.zc}
\captionsetup{style=invisible}
\caption{Non-equivalence of capturing lambda with top-level function.}
\end{listing}
\vspace*{-\baselineskip}
\begin{infobox}
Recursive lambda abstractions are unsupported.
If you are feeling the need to have those, simply bind them to \texttt{rec} declaration, such as:
\vspace*{\baselineskip}
\begin{minted}{\zilchlexer}
rec mylam := lam(x, y) => mylam(x, y)
\end{minted}
\end{infobox}
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\scalebox{.85}{
\includegraphics[width=\linewidth]{zilch/expression/lambda}
}
}
\caption{Grammar for lambda abstractions.}
\label{fig:zilch-gramma-expressions-lambda-grammar}
\end{figure}
\subsection{Conditional}\label{subsec:zilch-grammar-expressions-conditional}
Conditional expressions allow selecting an expression based off the value of a condition.
However, it only works as a binary selector.
If n-ary selectors are needed, chaining \texttt{if}s is the only solution.
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\scalebox{.5}{
\includegraphics{zilch/expression/conditional}
}
}
\caption{Grammar for conditional expressions.}
\label{fig:zilch-gramma-expressions-conditional-grammar}
\end{figure}
\subsection{Let-in}\label{subsec:zilch-grammar-expressions-letin}
Let-in expressions allow binding local variables in the context of another expression.
However, it does not act the same as simply replacing the variable just like a macro because of evaluation order and potential side-effects.
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\scalebox{.5}{
\includegraphics{zilch/expression/let-in}
}
}
\caption{Grammar for let-in blocks.}
\label{fig:zilch-grammar-expressions-letin-grammar}
\end{figure}
\subsection{Local imports}\label{subsec:zilch-grammar-expressions-import}
One may want to import specific functions from given modules only in the scope of a function as these functions will only be used there.
This construct allows to scope imports closer to where they actually are used, by allowing the import to be local to an expression which may use it.
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\scalebox{.5}{
\includegraphics[width=\linewidth]{zilch/expression/import}
}
}
\caption{Grammar for local import blocks.}
\label{fig:zilch-grammar-expressions-import-grammar}
\end{figure}
\subsection{Pattern matching}\label{subsec:zilch-grammar-expressions-match}
Pattern matching is achieved through the use of a \texttt{match} expression.
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\parbox{\textwidth}{
\centering
\scalebox{.5}{
\includegraphics{zilch/expression/pattern}
}\\
\scalebox{.9}{
\includegraphics[width=\linewidth]{zilch/expression/match}
}
}
}
\caption{Grammar for match expressions.}
\label{fig:zilch-grammar-expressions-match-grammar}
\end{figure}
\subsection{Record literals}\label{subsec:zilch-grammar-expressions-record}
Record literals allow constructing records in the same fashion as in e.g.\ C.
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\scalebox{.5}{
\includegraphics[width=\linewidth]{zilch/expression/record}
}
}
\caption{Grammar for record literals.}
\label{fig:zilch-gramma-expressions-record-grammar}
\end{figure}
\subsection{Variables, typed holes and literals}\label{subsec:zilch-grammar-expressions-basicexpr}
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\scalebox{.7}{
\includegraphics[width=\linewidth]{zilch/expression/atom}
}
}
\caption{Grammar for expression atoms.}
\label{fig:zilch-grammar-expressions-atom-grammar}
\end{figure}
% NOTE: &x now denotes making a reference
%
% The by-ref operator \texttt{\&id} is only allowed if it appears within the top-most application in a \texttt{let} local binding.
% For example, these uses of the by-ref operator are valid:
% \begin{listing}[H]
% \inputminted{\zilchlexer}{examples/correct-byref.zc}
% \captionsetup{style=invisible}
% \caption{Correct usage of by-ref operator.}
% \end{listing}
% \vspace*{-\baselineskip}
% Hoewver, these uses are invalid:
% \begin{listing}[H]
% \inputminted{\zilchlexer}{examples/incorrect-byref.zc}
% \captionsetup{style=invisible}
% \caption{Incorrect usage of by-ref operator.}
% \end{listing}
% \vspace*{-\baselineskip}
% In \texttt{test2}, the by-ref operator is in fact used in the application of \texttt{+} (recall that \texttt{a + b} is \texttt{\_+\_(a, b)}), which is not the top-most application in this local binding.
\subsection{Additive and multiplicative pairs}\label{subsec:zilch-grammar-expressions-pairs}
Pairs are separated into two distinct types, following LTT\footnote{Linear Type Theory}, to encode whether the two elements must be used or if only one is expected to be consumed.
It is possible to consume a single element from an additive pair, while all the elements must be consumed in a multiplicative pair.
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\parbox{\textwidth}{
\centering
\scalebox{.5}{
\includegraphics[width=\linewidth]{zilch/expression/pair-mult-intro}
}\\
\scalebox{.8}{
\includegraphics[width=\linewidth]{zilch/expression/pair-mult-elim}
}
}
}
\caption{Grammar for multiplicative pairs and eliminator.}
\label{fig:zilch-grammar-expressions-pairs-mult-grammar}
\end{figure}
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\parbox{\textwidth}{
\centering
\scalebox{.5}{
\includegraphics[width=\linewidth]{zilch/expression/pair-add-intro}
}\\
\scalebox{.6}{
\includegraphics[width=\linewidth]{zilch/expression/pair-add-elim}
}
}
}
\caption{Grammar for additive pairs and eliminator.}
\label{fig:zilch-grammar-expressions-pairs-add-grammar}
\end{figure}
\section{Types}\label{sec:zilch-grammar-types}
Types lie at the same level as expressions, but are put in a separate section for comprehensiveness sake.
An expression \verb|e| is said to be of type \verb|t| if evaluating it yields (or at least is supposed to) any value in the set \verb|t|.
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\scalebox{.5}{
\includegraphics{zilch/types/builtin}
}
}
\caption{Grammar for builtin types.}
\label{fig:zilch-grammar-types-builtin-grammar}
\end{figure}
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\scalebox{.8}{
\includegraphics[width=\linewidth]{zilch/types/effect-row}
}
}
\caption{Grammar for effects rows.}
\label{fig:zilch-grammar-types-effectrow-grammar}
\end{figure}
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\parbox{\textwidth}{
\centering
\scalebox{.8}{
\includegraphics[width=\linewidth]{zilch/types/implicit-parameter}
}\\
\scalebox{.8}{
\includegraphics[width=\linewidth]{zilch/types/explicit-parameter}
}\\
\scalebox{.8}{
\includegraphics[width=\linewidth]{zilch/types/instance-parameter}
}\\
\scalebox{.85}{
\includegraphics[width=\linewidth]{zilch/types/dependent}
}
}
}
\caption{Grammar for dependent types.}
\label{fig:zilch-grammar-types-dependent-grammar}
\end{figure}
\begin{warningbox}
The grammar for dependent types presented in figure~\ref{fig:zilch-grammar-types-dependent-grammar} allows more programs to be parsed than what is actually valid.
Here is a list of erroneous syntactic objects:
\begin{itemize}
\item \texttt{$\otimes$}, \texttt{×} and \texttt{\&} types with implicit or instance parameters;
\item \texttt{$\otimes$}, \texttt{×} and \texttt{\&} types with an effect row (unless it is the empty row \texttt{$\langle\rangle$});
\item \texttt{\&} types with a multiplicity on any parameter (unless it is the unrestricted multiplicity \texttt{@$\omega$}).
\end{itemize}
\end{warningbox}
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\scalebox{.4}{
\includegraphics[width=\linewidth]{zilch/types/universe-level}
}
}
\caption{Grammar for universe levels.}
\label{fig:zilch-grammar-types-level-grammar}
\end{figure}
\section{Declarations}\label{sec:zilch-grammar-declarations}
A Zilch module is composed of zero or more declarations.
There are 4 possible declarations:
\begin{itemize}
\item New type definition;
\item Effect definition;
\item Value definition;
\item Definition opening.
\end{itemize}
\noindent Each of them will be syntactically described in this section.
\subsection{Value definition}\label{subsec:zilch-grammar-declarations-value}
A function binding binds a variable to a value.
Both when the value is of a function type or of a simple value type, the binding is called a \textit{function binding}.
\vspace*{\baselineskip}
Because functions are first-class in Zilch, both function bindings defined below are completely equivalent (the lambda abstraction does not capture anything here):
\begin{listing}[H]
\inputminted{\zilchlexer}{examples/function-value-equivalence.zc}
\captionsetup{style=invisible}
\caption{\texttt{let} with parameters and lambda equivalence.}
\end{listing}
\vspace*{-\baselineskip}
Function bindings may additionally be accompanied by \verb|where| blocks, which allow binding local variables in the scope of the expression.
This is mostly equivalent to putting those bindings at the beginning of a \verb|let-in| block:
\begin{listing}[H]
\inputminted{\zilchlexer}{examples/function-where-let-equivalence.zc}
\captionsetup{style=invisible}
\caption{\texttt{where} equivalence with local \texttt{let} bindings.}
\end{listing}
\vspace*{-\baselineskip}
Note that recursive function bindings (bindings calling themselves in their definitions) are only allowed for \textit{functions}, and must be prefixed by \verb|rec| instead of \verb|let|.
Mutable function bindings are only allowed for non-recursive value bindings (i.e. \mintinline{\zilchlexer}{mut x : t := e} is allowed (if \verb|t| is a value type) but neither \mintinline{\zilchlexer}{mut x : t := x} nor \mintinline{\zilchlexer}{mut f(x) := e} are actually valid).
Top-level function bindings can additionnally be prefixed with special meta-attributes (described later in Section~\ref{subsec:zilch-grammar-declarations-meta}~``\nameref{subsec:zilch-grammar-declarations-meta}'').
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\parbox{\textwidth}{
\centering
\scalebox{.9}{
\includegraphics[width=\linewidth]{zilch/toplevel/function-definition}
}\\
\scalebox{.85}{
\includegraphics[width=\linewidth]{zilch/toplevel/implicit-parameter}
}\\
\scalebox{.85}{
\includegraphics[width=\linewidth]{zilch/toplevel/explicit-parameter}
}\\
\scalebox{.85}{
\includegraphics[width=\linewidth]{zilch/toplevel/instance-parameter}
}
}
}
\caption{Grammar for value declarations.}
\label{fig:zilch-grammar-declarations-value-grammar}
\end{figure}
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\scalebox{.9}{
\includegraphics[width=\linewidth]{zilch/toplevel/toplevel-function-definition}
}
}
\caption{Grammar for top-level value definitions.}
\label{fig:zilch-grammar-declarations-value-toplevel-grammar}
\end{figure}
\subsection{Effect definition}\label{subsec:zilch-grammar-declaration-effect}
Side effects lie everywhere in programming: from basic console I/O, networking, etc to mutating global variables.
Some programming languages (e.g. C, Java, etc) do not restrict those side effects to occur, leading to uncontrolled/undefined/undesirable behaviors.
Some other take a stricter route (e.g. Haskell, Koka, etc) and do not allow side effects occurring anywhere (Haskell uses monads, Koka uses algebraic effects).
In Zilch, the algebraic effects route has been taken as it seems to lead to more readable code, and can be compiled more efficiently, and mostly without surprises.
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\scalebox{.9}{
\includegraphics[width=\linewidth]{zilch/toplevel/effect-definition}
}
}
\caption{Grammar for effect definitions.}
\label{fig:zilch-grammar-declarations-effects-grammar}
\end{figure}
\subsection{New type definition}\label{subsec:zilch-grammar-declarations-type}
Sometimes, only working with primitive types is limiting.
For example, a function returning an unsigned integer may need to indicate some sort of error.
\noindent C-style programming languages use special values which are considered invalid as return values (e.g.\ $-1$).
This doesn't quite work when no return value from the return set is actually invalid, but the function is still expected to be able to indicate failure.
\noindent This is where custom types come in: it is possible to define a new ``or''-type (called ``enum'' in Zilch) to represent either a return value or no return value (in the case of a failure), usually called ``\texttt{Option}'' or ``\texttt{Maybe}''.
A function may also be expected to return multiple values.
In C-style programming languages, making use of references/pointers is one way, but using structures (called ``record''s) is the way to go in Zilch.
And some other times, using a specific type is not semantically clear enough.
For example, what does a \texttt{u64} mean as input to a function?
Zilch allows defining type aliases to address this issue, leading to code more clear.
When using the Foreign Function Interface (FFI), we may also want to interface with types.
However, types do not necessarily exist in the FFI, so they cannot be dynamically checked.
Moreover, some types usually defined in C (such as \texttt{FILE}) are very much implementation dependent, and it is not a good idea to rewrite them in Zilch.
In such cases, we can define \texttt{opaque} erased types (and only erased types, functions cannot be \texttt{opaque}) which can only be passed around behind pointers\footnote{Because the size of the type is unknown at compile-time, it is not possible to statically allocate enough memory on the stack to pass opaque data for function calls. However, if they are necessarily found behind pointers, we can just allocate the size of the pointer onto the stack.}.
An opaque type is of an unknown size which is guaranteed to be greater than 0.
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\parbox{\textwidth}{
\centering
\scalebox{.85}{
\includegraphics[width=\linewidth]{zilch/toplevel/enum-definition}
}\\
\scalebox{.95}{
\includegraphics[width=\linewidth]{zilch/toplevel/record-definition}
}
}
}
\caption{Grammar for new type definitions.}
\label{fig:zilch-grammar-declarations-type-grammar}
\end{figure}
\begin{warningbox}
The expression on the right hand side of the \texttt{:} in the declaration of a \texttt{record} is required to be of the form \texttt{type l} (where \texttt{l} can be anything).
Any other expression must be considered invalid.
\end{warningbox}
\subsection{Mutually recursive definitions}\label{subsec:zilch-grammar-declarations-mutual}
Top-level bindings can refer to other top-level bindings as long as these are declared beforehand.
However, sometimes it is useful to define a few functions or data types which are mutually recursive.
For example, consider the following definitions for the two functions \texttt{even} and \texttt{odd} which return \texttt{true} if the number is even (resp. odd):
\begin{align*}
\operatorname{even}(n) & = \begin{cases*}
\mathrm{true} & iff $n = 0$ \\
\operatorname{odd}(n - 1) & otherwise
\end{cases*} \\
\operatorname{odd}(n) & = \begin{cases*}
\mathrm{false} & iff $n = 0$ \\
\operatorname{even}(n - 1) & otherwise
\end{cases*}
\end{align*}
This can be modeled in Zilch as:
\begin{listing}[H]
\inputminted{\zilchlexer}{examples/mutual-even-odd.zc}
\captionsetup{style=invisible}
\caption{Mutual definitions of \texttt{even} and \texttt{odd}.}
\end{listing}\vspace*{-\baselineskip}
Note that without the \texttt{mutual} block, the definition of \texttt{even} would be considered ill, as \texttt{odd} does not exist in the scope yet.
An alternative modeling of this problem is by using \textit{forward declarations} (as in C) to declare the function \texttt{odd} before the function \texttt{even} so that \texttt{even} is allowed to make references to it.
This would be done as follows:
\begin{listing}[H]
\inputminted{\zilchlexer}{examples/forwarddecls-even-odd.zc}
\captionsetup{style=invisible}
\caption{\texttt{even}/\texttt{odd} with forward declarations.}
\end{listing}
\vspace*{-\baselineskip}
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\scalebox{.8}{
\includegraphics[width=\linewidth]{zilch/toplevel/function-declaration}
}
}
\caption{Grammar for value forward declarations.}
\label{fig:zilch-grammar-declarations-mutual-forwarddecls-grammar}
\end{figure}
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\scalebox{.6}{
\includegraphics[width=\linewidth]{zilch/toplevel/mutual-definition}
}
}
\caption{Grammar for mutual block definition.}
\label{fig:zilch-grammar-declarations-mutual-block-grammar}
\end{figure}
\subsection{Meta-information}\label{subsec:zilch-grammar-declarations-meta}
Meta-information can be added to any top-level declaration.
They can be used for example to declare operator fixities, or add deriving clauses to auto-generate type class instances.
Any declaration accepts zero or more meta-specifiers, but each specifier is restricted as per these rules:
\begin{itemize}
\item Fixity specifiers can only be applied to mixfix operators.
These are used to disambiguate expressions, and come in 5 flavors:
\begin{itemize}
\item \verb|infix <prec>|, \verb|infixr <prec>| and \verb|infixl <prec>| allow specifying the associativity (left, right or none) and precedence of infix operators (operators of the form \verb|_..._|);
\end{itemize}
\item Deriving clauses can only be added to \verb|enum| and \verb|record| types;
\item Inlining hints tell the compiler that a function may not have a huge cost, therefore can be inlined more easily;
\item FFI imports/exports, to either retrieve or give a function defined in another compilation unit (most probably coming from another programming language like C);
\item Default effect handlers can be added to any function binding of type \texttt{\{e\} -> \{a\} -> (() -> < eff | e > a) -> e a} (meaning it consumes the effect \verb|eff| given) (implicits may be given in any order).
These are used to remove any additional effects on the \verb|main| function without having to specify each of them.
Note that those handlers are implicitly internally exported (even when not \verb|public|), but still need to be \verb|public| to be used by hand.
\end{itemize}
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\parbox{\textwidth}{
\centering
\scalebox{.7}{
\includegraphics[width=\linewidth]{zilch/toplevel/meta-information}
}\\
\scalebox{.75}{
\includegraphics[width=\linewidth]{zilch/toplevel/meta-specifier}
}
}
}
\caption{Grammar for meta-specifiers.}
\label{fig:zilch-grammar-declarations-meta-grammar}
\end{figure}
\section{Modules}\label{sec:zilch-grammar-modules}
Modules allow grouping functions and types that are related to each other under a common namespace.
In Zilch, modules provide grouping, importing and visibility restrictions.
A Zilch module is declared according to the name and position of the file in the include path.
For example, if a file is named \verb|Internal.zc| in the directory \verb|Data/Something/A|, then it exports a module \verb|Data::Something::A::Internal|.
It can contain, in any order, many (possibly none) \texttt{import} statements (see Figure~\ref{fig:zilch-grammar-modules-importgrammar}) and any number of declarations as described in Section~\ref{sec:zilch-grammar-declarations}~``\nameref{sec:zilch-grammar-declarations}''.
A module $A$ may export another module $B$, meaning that anything imported from $B$ into $A$'s scope is exported by $A$.
If a plain function is aliased\footnote{Infix operators cannot be aliased because it would lead to many syntactical problems.} from the import of $B$ in $A$, the new name is exported by $A$ instead of the old one.
Operator fixities are exported only if their corresponding operators are also exported.
All imports are qualified, unless when \verb|open|ed.
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\parbox{\textwidth}{
\centering
\scalebox{.5}{
\includegraphics[width=\linewidth]{zilch/modules/import}
}\\
\scalebox{.95}{
\includegraphics[width=\linewidth]{zilch/modules/import-path}
}
}
}
\caption{Grammar for import statements.}
\label{fig:zilch-grammar-modules-importgrammar}
\end{figure}
\chapter{Static semantics}\label{chap:zilch-staticsem}
Zilch is statically and strongly typed, meaning that types of expressions are checked at compile-time, and no implicit conversion are performed.
It also is type-infered, so there is no need to write the exact type of a function (unless in specific ambiguous states, e.g. with constraints).
The type system of Zilch is based off a quantitative dependent type system with non-cumulative universe polymorphism and algebraic effects.
In short, this means that types are allowed to be dependent on runtime values (which are not \texttt{@0}), and that the type \texttt{type l} has a single type which is \texttt{type (lsuc l)}.
This chapter will contain type inference rules accompanied by informal descriptions.
Some of the inference rules may be elided for simplicity, as writing them out would be either too cumbersome or difficult to understand.
In such case, the informal description will be more furnished to compensate.
\section{Multiplicities}\label{sec:zilch-staticsem-multiplicities}
The Quantitative Type Theory (QTT for short) relies on usage annotations called ``multiplicities'' to determine the status of a given computation.
The fragment $0,1,\omega$ is used to model these in Zilch, where:
\begin{itemize}
\item $0$ is the multiplicity of erased expressions, that is expressions which are guaranteed to be fully evaluated at compile time and be completely stripped off the executable code;
\item $1$ is the multiplicity of linear expressions, meaning that they can be referenced only once (as in Linear Logic);
\item and $\omega$ which is the multiplicity for unrestricted values (which can be used either at compile time or at runtime).
\end{itemize}
There is a very important typing rule for multiplicities, as presented in figure~\ref{fig:zilch-staticsem-multiplicities-conv-typerules}, which relies on the relation $\leqslant$ defined in figure~\ref{fig:zilch-staticsem-multiplicities-partialorder}, allowing to specialise a multiplicity.
The $\cong$ relation embodies that two types are equivalent.
\begin{figure}[H]
\framebox[\textwidth][c]{
\begin{prooftree}
\hypo{\rho\in\{0, 1, \omega\}}
\infer1{\rho\leqslant\omega}
\end{prooftree}
\hspace{1.5em}
\begin{prooftree}
\infer0{0\leqslant 0}
\end{prooftree}
\hspace{1.5em}
\begin{prooftree}
\infer0{1\leqslant 1}
\end{prooftree}
}
\caption{Definition of the $\leqslant$ relation on multiplicities.}
\label{fig:zilch-staticsem-multiplicities-partialorder}
\end{figure}
In the general sense, if $\pi \leqslant \rho$ then it means that $\pi$ is more specialised than $\rho$, or that the multiplicity $\pi$ can be used in place of $\rho$ at the specific point where this relation holds.
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\begin{prooftree}
\hypo{\TypeSequent{\Gamma}{e}{\rho}{\tau_1}{\epsilon}}
\hypo{\tau_1\cong\tau_2}
\hypo{\pi \leqslant \rho}
\infer3[\textsc{[Type-Conv]}]{\TypeSequent{\Gamma}{e}{\pi}{\tau_2}{\epsilon}}
\end{prooftree}
}
\caption{Type inference rules for multiplicity specialisation.}
\label{fig:zilch-staticsem-multiplicities-conv-typerules}
\end{figure}
\section{Types, universes and effects}\label{sec:zilch-staticsem-types}
In Zilch, not only expressions have types, but types also do.
While this may sound a little bit complex, it in fact allows easily solving a few problems happening in some of other programming languages allowing generic programming:
\begin{itemize}
\item A type may be passed partially applied to a function or a type;
\item Type constraints live as inhabitants of other sets of types;
As such, constraints cannot be passed in place of other types, which would imply weird type system behaviours.
\item A partially applied type (in opposition to a fully applied type, called a ``concrete kind'') cannot be given to a function requiring a concrete type.
\end{itemize}
Types of types are mainly organized in 4 categories:
\begin{itemize}
\item Concrete types, which are denoted as \verb|type l| in Zilch, are types which are fully applied to arguments;
\item Partially applied types (also called ``type constructors'') can only be applied to more arguments, but may yield any kind of type.
Just like functions at the expression level, type constructors are written using the arrow syntax \verb|_->_|;
\item Effect types are special kinds of types which are only allowed to specify possible effects when computing a function.
Such types are grouped under the \verb|effect| kind.
\end{itemize}
Type inference rules are proposed in figure~\ref{fig:zilch-staticsem-types-universes-typerules} for type universes, and in figure~\ref{fig:zilch-staticsem-types-effects-typerules} for algebraic effects.
\begin{figure}[H]
\centering
\framebox[\textwidth][c]{
\parbox{\textwidth}{
\centering
\begin{prooftree}
\hypo{\TypeSequent{\scale{0}{\Gamma}}{\ell}{0}{\Elevel}{\Etotal}}
\hypo{\TypeSequent{\scale{0}{\Gamma}}{\tau}{0}{\Etype{\ell}}{\Etotal}}
\infer2[\textsc{[Universe-Lift]}]{\TypeSequent{\scale{0}{\Gamma}}{\Ellift{\tau}}{0}{\Etype({\Elsuc{\ell}})}{\Etotal}}
\end{prooftree}
\\\vspace{\baselineskip}
\begin{prooftree}
\hypo{\TypeSequent{\scale{0}{\Gamma}}{\ell}{0}{\Elevel}{\Etotal}}
\infer1[\textsc{[Universe-F]}]{\TypeSequent{\scale{0}{\Gamma}}{\Etype{\ell}}{0}{\Etype({\Elsuc{\ell}})}{\Etotal}}
\end{prooftree}
\hspace{1.5em}
\begin{prooftree}
\infer0[\textsc{[Universe-Var]}]{\TypeSequent{\scale{0}{\Gamma}, \ell :^0 \Elevel}{\ell}{0}{\Elevel}{\Etotal}}
\end{prooftree}
\\\vspace{\baselineskip}
\begin{prooftree}
\hypo{\TypeSequent{\scale{0}{\Gamma}}{\ell_1}{0}{\Elevel}{\Etotal}}
\hypo{\TypeSequent{\scale{0}{\Gamma}}{\ell_2}{0}{\Elevel}{\Etotal}}
\infer2[\textsc{[Universe-$\sqcup$]}]{\TypeSequent{\scale{0}{\Gamma}}{\Elmax{\ell_1}{\ell_2}}{0}{\Elevel}{\Etotal}}
\end{prooftree}
\\\vspace{\baselineskip}
\begin{prooftree}
\hypo{\TypeSequent{\scale{0}{\Gamma}}{\ell}{0}{\Elevel}{\Etotal}}
\infer1[\textsc{[Universe-Suc]}]{\TypeSequent{\scale{0}{\Gamma}}{\Elsuc{\ell}}{0}{\Elevel}{\Etotal}}
\end{prooftree}
\hspace{1.5em}
\begin{prooftree}
\hypo{n$ is a natural$}
\infer1[\textsc{[Universe-I]}]{\TypeSequent{\scale{0}{\Gamma}}{n}{0}{\Elevel}{\Etotal}}
\end{prooftree}
}
}
\caption{Type inference rules for type universes.}
\label{fig:zilch-staticsem-types-universes-typerules}
\end{figure}
\begin{figure}[H]
\framebox[\textwidth][c]{
\parbox{\textwidth}{
\centering
\begin{prooftree}
\hypo{\TypeSequent{\scale{0}{\Gamma}}{\tau_i}{0}{\Eeffect}{\Etotal}}
\hypo{\TypeSequent{\scale{0}{\Gamma}}{e_j}{0}{\Eeffect}{\Etotal}}
\infer2[\textsc{[Effect-Row]}]{\TypeSequent{\scale{0}{\Gamma}}{\Eeffectrow{\tau_i}{e_j}}{0}{\Eeffect}{\Etotal}}
\end{prooftree}
\\\vspace*{\baselineskip}
\begin{prooftree}
\infer0[\textsc{[Effect-Var]}]{\TypeSequent{\scale{0}{\Gamma}, e :^0 \Eeffect}{e}{0}{\Eeffect}{\Etotal}}
\end{prooftree}
}
}
\caption{Type inference rules for algebraic effects.}
\label{fig:zilch-staticsem-types-effects-typerules}
\end{figure}
\section{Types of expressions}\label{sec:zilch-staticsem-exprs}
\subsection{Function types and lambda expressions}\label{subsec:zilch-staticsem-exprs-pi}
\begin{figure}[H]
\framebox[\textwidth][c]{
\parbox{\textwidth}{
\centering
\begin{prooftree}
\hypo{\TypeSequent{\scale{0}{\Gamma}}{A}{0}{\Etype \ell_1}{\Etotal}}
\hypo{\TypeSequent{\scale{0}{\Gamma}, x :^0 \tau_A}{B}{0}{\Etype \ell_2}{\Etotal}}
\hypo{\TypeSequent{\scale{0}{\Gamma}}{\epsilon}{0}{\Eeffect}{\Etotal}}
\infer3[\textsc{[$\rightarrow$-F]}]{\TypeSequent{\scale{0}{\Gamma}}{\Epi{x}{\rho}{A}{\epsilon}{B}}{0}{\Etype(\Elmax{\ell_1}{\ell_2})}{\Etotal}}
\end{prooftree}
\\\vspace*{\baselineskip}
\begin{prooftree}
\hypo{\TypeSequent{\scale{0}{\Gamma}}{A}{0}{\Etype \ell_1}{\Etotal}}
\hypo{\TypeSequent{\Gamma, x :^{\scale{\rho}{\pi}} A}{e}{\pi}{B}{\epsilon}}
\hypo{\pi = 0 \Rightarrow \epsilon,\epsilon^\prime \equiv \Etotal}
\infer3[\textsc{[$\rightarrow$-I$_1$]}]{\TypeSequent{\Gamma}{\Elam{x}{\rho}{A}{e}}{\pi}{\Epi{x}{\rho}{A}{\epsilon}{B}}{\epsilon^\prime}}
\end{prooftree}
\\\vspace*{\baselineskip}
\begin{prooftree}
\hypo{\TypeSequent{\Gamma}{e}{\pi}{A}{\epsilon}}
\hypo{\pi=0 \Rightarrow \epsilon^\prime \equiv \Etotal}
\infer2[\textsc{[$\rightarrow$-I$_2$]}]{\TypeSequent{\Gamma}{\Edo{e}}{\pi}{\Efun{\omega\ \Eone}{\epsilon}{A}}{\epsilon^\prime}}
\end{prooftree}
\\\vspace*{\baselineskip}
\begin{prooftree}
\hypo{\TypeSequent{\Gamma}{f}{\rho}{\Epi{y}{\pi}{A}{\epsilon}{B}}{\Etotal}}
\hypo{\TypeSequent{\scale{0}{\Gamma}}{x}{0}{A}{\Etotal}}
\hypo{\scale{\rho}{\pi} = 0}
\hypo{\pi=0 \Rightarrow \epsilon \equiv \Etotal}
\infer4[\textsc{[$\rightarrow$-E$_0$]}]{\TypeSequent{\Gamma}{\Eapp{f}{x}}{\pi}{\subst{B}{y}{x}}{\epsilon}}
\end{prooftree}
\\\vspace*{\baselineskip}
\begin{prooftree}
\hypo{\TypeSequent{\Gamma_1}{f}{\rho}{\Epi{y}{\pi}{A}{\epsilon}{B}}{\epsilon^\prime}}
\hypo{\TypeSequent{\Gamma_2}{x}{1}{A}{\epsilon^\second}}
\infer2[\textsc{[$\rightarrow$-E$_1$]}]{\TypeSequent{\add{\Gamma_1}{\scale{\scale{\rho}{\pi}}{\Gamma_2}}}{\Eapp{f}{x}}{\rho}{\subst{B}{y}{x}}{\epsilon,\epsilon^\prime,\epsilon^\second}}
\end{prooftree}
}
}
\caption{Type inference rule for function types.}
\label{fig:zilch-staticsem-exprs-pi-typerules}
\end{figure}
The rule \textsc{[$\rightarrow$-I]} may seem quite weird, as creating a lambda does not perform any effect (or it performs the effect $\Etotal$).