catala/doc/syntax/syntax_en.tex
2024-04-25 14:20:22 +02:00

746 lines
14 KiB
TeX

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{multicol}
\usepackage{calc}
\usepackage{ifthen}
\usepackage{epsfig}
%\usepackage{bookman}
%\usepackage{booktabs}
\usepackage{fontspec}
\usepackage{upquote} % Uncurly the quotes
\usepackage{etoolbox} % for backquote fix
\usepackage{mdframed} % nice frames
\usepackage[nobottomtitles]{titlesec} % better titles
\usepackage{enumitem}
\usepackage{minted}
\usepackage[a4paper,landscape,margin=1cm,includehead,headsep=2ex,nofoot]{geometry}
\usepackage{fancyhdr}
\usepackage{array}
\usepackage[none]{hyphenat}
\usepackage[document]{ragged2e}
\usemintedstyle{tango}
\setsansfont{DejaVu Sans}[Scale=0.9]
\setmonofont{DejaVu Sans Mono}[Scale=0.9]
\setlist[itemize]{noitemsep, topsep=0pt}
\renewcommand{\MintedPygmentize}{../../_build/default/compiler/catala.exe pygmentize}
% fancyvrb and mdframed stuff
\newmdenv[bottomline=false,rightline=false]{topleft}
\newmdenv[topline=false,leftline=false]{bottomright}
\DefineVerbatimEnvironment{Verbacorner}{Verbatim}{}
\surroundwithmdframed[bottomline=false,rightline=false]{Verbacorner}
\fvset{commandchars=@\{\}}
\ifthenelse{\lengthtest { \paperwidth = 11in}}
{ \geometry{top=.5in,left=.5in,right=.5in,bottom=.5in} }
{\ifthenelse{ \lengthtest{ \paperwidth = 297mm}}
{\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} }
{\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} }
}
\pagestyle{empty}
% Don't print section numbers
\setcounter{secnumdepth}{0}
\setlength{\parindent}{0pt}
\setlength{\parskip}{1ex}
\pagestyle{fancy}
\newcommand{\makeheader}[3]{
\fancyhead[L]{
\begin{minipage}{30pt}
\vspace{-3pt}
\includegraphics[width=\textwidth]{logo.pdf}
\end{minipage}
}
\fancyhead[C]{
\LARGE{#1}
}
\fancyhead[R]{
{\scriptsize
#2 $\cdot$ Revision \#1 $\cdot$ \copyright\ \the\year$\quad$
%% \begin{minipage}{30pt}
%% \vspace{-3pt}
%% \includegraphics[width=\textwidth]{logo.pdf} %defined at bottom
%% \end{minipage}
}}
\fancyfoot[L]{}
\fancyfoot[C]{}
\fancyfoot[R]{}
}
\renewcommand\headrule{}
\newenvironment{absolutelynopagebreak}
{\par\nobreak\vfil\penalty0\vfilneg
\vtop\bgroup}
{\par\xdef\tpd{\the\prevdepth}\egroup
\prevdepth=\tpd}
\newcommand{\vb}{\usefont{T1}{DejaVuSansMono-TLF}{b}{n}}
\newenvironment{libcomments}{\begin{itemize}[topsep=-10000pt]}{\end{itemize}}
\setlength{\headheight}{20.1pt}
\setlength{\headsep}{10pt}
\renewcommand{\familydefault}{\sfdefault} % everything sans-serif
% font size -> set to equivalent to footnotesize of 10 pt
% it affect only the \\normalsize command, all are still relative to default's
% article one (10pt)
\newcommand\articlenormalsize{\fontsize{10pt}{12pt}\selectfont}
\renewcommand\normalsize{\footnotesize}
% redefine sections padding
\makeatletter
\renewcommand{\section}{\@startsection{section}{1}{0mm}%
{-1ex plus -.5ex minus -.2ex}%
{1ex}%
{\normalfont\articlenormalsize\bfseries}}
\renewcommand{\subsection}{\@startsection{subsection}{2}{4mm}%
{-1ex}%
{0.5ex}%
{\normalfont\articlenormalsize\bfseries}}
\renewcommand{\subsubsection}{\@startsection{subsubsection}{3}{16mm}%
{-1ex plus -.5ex minus -.2ex}%
{1ex plus .2ex}%
{\normalfont\small\bfseries}}
\makeatother
%\pagestyle{empty}
% set section counter to 0
\setcounter{secnumdepth}{0}
% Text at right
%\raggedright
\renewcommand{\baselinestretch}{0.9}
% Set up the interline space in tables
\renewcommand{\arraystretch}{0}
\linespread{0.85}
% https://github.com/gpoore/minted/issues/82
\makeatletter
\newenvironment{catala}{%
\VerbatimEnvironment
\let\FV@ListVSpace\relax
\begin{minted}[autogobble]{catala_en}}%
{\end{minted}}
\makeatother
\setlength{\columnsep}{12pt}
\setlength{\columnseprule}{.1pt}
%
% NOTE: all examples should be kept in sync with the `syntax_en.catala_en` file
%
\begin{document}
\makeheader{\MakeUppercase{The Catala Syntax} \hspace{2em}\small english version}{v0.10.0}{1}
% warning: this is repeated for p.2
% \begin{center}
% 2022-12-15
% \hfill
% {\Huge\bfseries\sffamily
% Catala Syntax Cheat Sheet}
% \hfill
% v.0.8.0
% \end{center}
% \hrule
\null
\begin{multicols}{3}
\newlength\cola
\setlength\cola{\dimexpr .65\columnwidth -2\tabcolsep}
\newlength\colb
\setlength\colb{\dimexpr .35\columnwidth -2\tabcolsep}
\section{Literate programming}
\begin{tabular}{@{}p{\cola}>{\slshape}p{\colb}@{}}
\begin{catala}
# Title
### Sub-subtitle
\end{catala}
&
Heading
\\
\begin{catala}
```catala ```catala-metadata
``` ```
\end{catala}
& Code / metadata block
\\
\begin{catala}
> Module Mdl
\end{catala}
& Module declaration
\\
\begin{catala}
> Using Mdl as M
\end{catala}
& Module import+alias
\\
\begin{catala}
> Include: foo.catala_en
\end{catala}
& File inclusion
\\
\end{tabular}
\newcommand*\FancyVerbStartString{\PYG{l+s}{```catala}}
\newcommand*\FancyVerbStopString{\PYG{l+s}{```}}
\section{Literals and types}
\begin{tabular}{@{}p{\cola}p{\colb}@{}}
\begin{catala}
```catala
true false
```
\end{catala}
& \begin{catala}
```catala
boolean
```
\end{catala}
\\
\begin{catala}
```catala
65,536
```
\end{catala}
& \begin{catala}
```catala
integer
```
\end{catala}
\\
\begin{catala}
```catala
65536.262144 37%
```
\end{catala}
& \begin{catala}
```catala
decimal
```
\end{catala}
\\
\begin{catala}
```catala
$1,234,567.89
```
\end{catala}
& \begin{catala}
```catala
money
```
\end{catala}
\\
\begin{catala}
```catala
|2024-04-01|
```
\end{catala}
& \begin{catala}
```catala
date
```
\end{catala}
\\
\begin{catala}
```catala
254 day 4 month 1 year
```
\end{catala}
& \begin{catala}
```catala
duration
```
\end{catala}
\\
\begin{catala}
```catala
[ 12; 24; 36 ]
```
\end{catala}
& \begin{catala}
```catala
list of integer
```
\end{catala}
\\
\begin{catala}
```catala
(|2024-04-01|, $30, 1%)
```
\end{catala}
& \begin{catala}
```catala
(date,money,decimal)
```
\end{catala}
\\
\begin{catala}
```catala
f of x, y equals
x * y / $12.0
```
\end{catala}
& \begin{catala}
```catala
decimal depends on
x content money,
y content decimal
```
\end{catala}
\\
\begin{catala}
```catala
Struct1 { -- fld1: 9 -- fld2: 7% }
```
\end{catala}
& \begin{catala}
```catala
Struct1
```
\end{catala}
\\
\begin{catala}
```catala
Case1 content 12 Case2
```
\end{catala}
& \begin{catala}
```catala
Enum1
```
\end{catala}
\\
\end{tabular}
\section{Operators and built-ins}
\begin{tabular}{@{}p{\cola}>{\slshape}p{\colb}@{}}
\begin{catala}
```catala
not a a and b
a or b # "or otherwise"
a xor b # exclusive or
```
\end{catala}
& Logical operators
\\
\begin{catala}
```catala
- a a + b a - b
a * b a / b
```
\end{catala}
& Arithmetic
\\
\begin{catala}
```catala
a = b a < b a <= b
a != b a > b a >= b
```
\end{catala}
& Comparison
\\
\begin{catala}
```catala
decimal of 44
money of 23.15
```
\end{catala}
& Conversions
\\
\begin{catala}
```catala
round of $9.99
```
\end{catala}
& Rounding
\\
\begin{catala}
```catala
get_month of ...
first_day_of_month of ...
```
\end{catala}
& Date parts
\\
\begin{catala}
```catala
a +! b a +. b a +$ b a +^ b
# int. decimal money duration
```
\end{catala}
& Explicitly typed operators
\\
\end{tabular}
\section{Metadata declaration}
\begin{tabular}{@{}p{\cola}>{\slshape}p{\colb}@{}}
\begin{catala}
```catala
declaration structure Struct1:
data fld1 content integer
data fld2 content decimal
\end{catala}
& Structure declaration
\\
\begin{catala}
```catala
declaration enumeration Enum1:
-- Case1 content integer
-- Case2
```
\end{catala}
& Enumeration declaration
\\
\begin{catala}
```catala
declaration scope Scope1:
internal var1 content integer
internal var2 condition
sub1 scope Scope0
```
\end{catala}
& Scope declaration
\\
\begin{catala}
```catala
internal var1 content ...
output var3 content ...
input var4 content ...
input output var5 content ...
context var6 content ...
context output var7 content ...
output sub2 scope Scope0
```
\end{catala}
& Input-output qualifiers
\\
\begin{catala}
```catala
internal var1 content ...
state before
state after
```
\end{catala}
& State transitions declaration
\\
\begin{catala}
```catala
declaration const content decimal
equals 17.1
```
\end{catala}
& Global definition
\\
\begin{catala}
```catala
declaration square content decimal
depends on x content decimal
equals x * x
```
\end{catala}
& Global function definition
\\
\end{tabular}
\section{Expressions}
\begin{tabular}{@{}p{\cola}>{\slshape}p{\colb}@{}}
\begin{catala}
```catala
let x equals 36 - 5 in ...
```
\end{catala}
& Local definition
\\
\begin{catala}
```catala
match expr with pattern
-- Case1 of x : ...
-- Case2 : ...
-- anything : ...
```
\end{catala}
& Pattern matching
\\
\begin{catala}
```catala
expr with pattern Case1
expr with pattern Case1 of x
and x >= 2
```
\end{catala}
& Pattern test\newline and optional binding
\\
\begin{catala}
```catala
struc1 but replace { -- fld2: 8% }
```
\end{catala}
& Field replacement
\\
\begin{catala}
```catala
struc1.fld2 tuple1.2
sub1.var0
```
\end{catala}
& Field, tuple element, subscope variable
\\
\begin{catala}
```catala
f of $44.50, 1/3
```
\end{catala}
& Function call
\\
\begin{catala}
```catala
output of Scope1 with
{ -- fld1: 9 -- fld2: 15% }
```
\end{catala}
& Direct scope call
\\
\begin{catala}
```catala
if ... then ... else ...
```
\end{catala}
& Conditional
\\
\begin{catala}
```catala
var1 state before
```
\end{catala}
& Variable state access
\\
\end{tabular}
\columnbreak
\section{Scope definition}
\begin{tabular}{@{}p{\cola}>{\slshape}p{\colb}@{}}
\begin{catala}
```catala
scope Scope1: ...
```
\end{catala}
& Scope use
\\
\begin{catala}
```catala
scope Scope1
under condition var1 >= 2: ...
```
\end{catala}
& Use-wide condition
\\
\begin{catala}
```catala
definition var1 equals ...
```
\end{catala}
& Unconditional def.
\\
\begin{catala}
```catala
definition var1
under condition ...
consequence equals ...
```
\end{catala}
& Conditional definition
\\
\begin{catala}
```catala
rule var2
under condition var1 >= 2
consequence fulfilled
```
\end{catala}
& Rule (definition for conditions)
\\
\begin{catala}
```catala
consequence not fulfilled
```
\end{catala}
& Negative rule
\\
\begin{catala}
```catala
definition f of x, y equals ...
```
\end{catala}
& Function def. or rule
\\
\begin{catala}
```catala
label lbl1 definition var1 ...
```
\end{catala}
& Labeled def. or rule
\\
\begin{catala}
```catala
exception lbl1 definition var1 ...
```
\end{catala}
& Exception to label
\\
\begin{catala}
```catala
exception definition var1 ...
```
\end{catala}
& Exception to implicit
\\
\begin{catala}
```catala
definition var1
state before
equals ...
```
\end{catala}
& State definition
\\
\begin{catala}
```catala
assertion ...
```
\end{catala}
& Assertion
\\
\begin{catala}
```catala
date round decreasing
```
\end{catala}
& Date rounding mode
\\
\end{tabular}
\vfill
\section{List operations}
\begin{tabular}{@{}p{\cola}>{\slshape}p{\colb}@{}}
\begin{catala}
```catala
lst contains 3
```
\end{catala}
& Presence test
\\
\begin{catala}
```catala
exists x among lst such that x > 2
```
\end{catala}
& Existence test
\\
\begin{catala}
```catala
for all x among lst we have x > 2
```
\end{catala}
& For all test
\\
\begin{catala}
```catala
(x + 2) for x among lst
```
\end{catala}
& Mapping
\\
\begin{catala}
```catala
list of x among lst such that x > 2
```
\end{catala}
& Filter
\\
\begin{catala}
```catala
(x - 2) for x among lst
such that x > 2
```
\end{catala}
& Filter + map
\\
\begin{catala}
```catala
(x + y) for (x, y) among (lst1, lst2)
```
\end{catala}
& Multiple mapping
\\
\begin{catala}
```catala
lst1 ++ lst2
```
\end{catala}
& Merge
\\
\begin{catala}
```catala
sum integer of lst
```
\end{catala}
& Aggregation
\\
\begin{catala}
```catala
number of lst
```
\end{catala}
& Count
\\
\begin{catala}
```catala
maximum of lst
or if list empty then -1
```
\end{catala}
& Extremum
\\
\begin{catala}
```catala
content of x among lst
such that x * x is minimum
or if list empty then -1
```
\end{catala}
& Arg-extremum
\\
\end{tabular}
\end{multicols}
\end{document}