-
Notifications
You must be signed in to change notification settings - Fork 12
/
haskell-exts-cs.tex
153 lines (126 loc) · 4.38 KB
/
haskell-exts-cs.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
%% haskell-exts-cs.tex
%
% Copyright 2018 Rudy Matela
%
% This text is available under (at your option):
% * Creative Commons Attribution-ShareAlike 3.0 Licence
% * GNU Free Documentation License version 1.3 or Later
%
\documentclass{refcard}
\usepackage[T1]{fontenc}
\usepackage{rotating}
\usepackage{amssymb}
\renewcommand{\familydefault}{\sfdefault}
\newcommand{\la}{\textbackslash}
\title{Haskell Extensions Cheat Sheet}
\cright{
Copyright 2018-2019, Rudy Matela --
Compiled on \today{} \\
Upstream: \texttt{https://github.com/rudymatela/concise-cheat-sheets}
}{
This text is available under
the Creative Commons Attribution-ShareAlike 3.0 Licence, \\
\textbf{or} (at your option), the GNU Free Documentation License version 1.3 or Later.
}
\version[~\\]{0.0}
\begin{document}
\maketitle
\section{Type Families (Type Functions)
\hfill\C{\normalsize TypeFamilies}}
\begin{ldesc}
\li[enabling] \C{\{-\# LANGUAGE TypeFamilies \#-\}}
\end{ldesc}\\[1em]
\begin{tabularlc}{r@{\s$\equiv$\s}l}
type family & type function \\
type family instance & type function pattern match \\
\end{tabularlc} \\[1em]
\begin{ldesc}
\li[declaring] type family \I{Family} a
\li[instantiating] type instance \I{Family} \I{TypeA} = \I{TypeB} \li
\li[declaring (data)] data family \I{DataFam} a
\li[instantiating (data)] data instance \I{DataFam} \I{Type} = \I{Conses...} \li
\li[declaring (class)]
class \I{Class} a where \li
~~type \I{Family} a \li
~~fun ::~...~->~\I{Family}~a~->~... \li
\li[instantiating (class)]
instance \I{Class} \I{TypeA} where \li
~~type \I{Family} \I{TypeA} = \I{TypeB} \li
~~fun ...~= ... \li
\end{ldesc}
\section{Tuple Sections
\hfill\C{\normalsize TupleSections}}
\begin{ldesc}
\li[enabling] \C{\{-\# LANGUAGE TupleSections \#-\}}
\end{ldesc}\\[1em]
\begin{tabularlc}{C@{\s$\equiv$\s}C}
~~(x,) & \la{}y -> (x,y) \\
~~(,y) & \la{}x -> (x,y) \\
~(x,{},) & \la{}y z -> (x,y,z) \\
~(,y,) & \la{}x z -> (x,y,z) \\
~(,{},z) & \la{}x y -> (x,y,z) \\
(x,y,) & \la{}z -> (x,y,z) \\
(x,{},z) & \la{}y -> (x,y,z) \\
(,y,z) & \la{}x -> (x,y,z) \\
\end{tabularlc} \\[1em]
\begin{tabularlc}{C@{\s$\equiv$\s}C}
~~~~~~~~~~~~~~~~(0,{},2) 1 & (0,1,2) \\
~~~~~~~~~~~(False,) True & (False,True) \\
(False,{},0,{},{},) True 1 2 3 & (False,True,0,1,2,3) \\
\end{tabularlc} \\[1em]
\section{Lambda Case
\hfill\C{\normalsize LambdaCase}}
\begin{ldesc}
\li[enabling] \C{\{-\# LANGUAGE LambdaCase \#-\}}
\end{ldesc}\\[1ex]
\newcommand{\thead}[1]{\textnormal{\textbf{#1}}}
\newcommand{\theads}[2]{\multicolumn{1}{C@{\s$\approx$\s}}{\thead{#1}} & \thead{#2} \\}
\begin{tabular}{CC}
\theads{lambda case}{explicit lambda case}
\la{}case & \la{}\I{x} -> case~\I{x}~of \\
~~\I{pat1} -> \I{exA}~~~~ & ~~\T{x}~~~~~\I{pat1} -> \I{exA} \\
~~\I{pat1} -> \I{exA} & ~~\T{x}~~~~~\I{pat2} -> \I{exB} \\
~~\_\T{at2} -> \I{exC} & ~~\T{x}~~~~~\_\T{at2} -> \I{exC} \\[1ex]
\end{tabular}
\section{Generalized Algebraic Datatypes
\hfill\C{\normalsize GADTs}}
\begin{ldesc}
\li[enabling] \C{\{-\# LANGUAGE GADTs\#-\}}
\end{ldesc}\\[1ex]
\begin{tabular}{CC}
\theads{GADT}{regular data definition}
\\
data Maybe a where~~~~~~~~~~ & data~Maybe~a \\
~~Nothing~::~Maybe~a & ~~=~Nothing \\
~~Just~~~~::~a~->~Maybe~a & ~~|~Just~a \\
\\
data List a where & data List a \\
~~Nil~~::~List a & ~~= Nil \\
~~Cons~::~a -> List a -> List a & ~~| Cons a (List a) \\
\\
data [a] where & data [a] = [] | a:[a] \\
~~[]~~::~[a] & ~~\rotatebox{90}{$\Rsh$}\textnormal{\footnotesize ~(not real Haskell)} \\
~~(:)~::~a -> [a] -> [a] & \\
\\
data Thing a where & \textnormal{regular defn.~impossible} \\
~~Thing ::~a -> Thing a & \\
~~AnInt ::~Int -> Thing Int & \\
~~ABool ::~Bool -> Thing Bool & \\
\\
data Nat where & data Nat = Z | S Nat \\
~~Z~::~Nat & \\
~~S~::~Nat -> Nat & \\
\\
data Vec n a where & \textnormal{regular defn.~impossible} \\
~~Nil~~~::~Vec Z a & \\
~~(:::)~::~a -> Vec n a -> Vec (S n) a \hspace{-8ex} & \\
\end{tabular}
\section{Rank-2 and Rank-N Types
\hfill\C{\normalsize Rank2Types, RankNTypes}}
\begin{tabularlc}{C@{\s$\equiv$\s}C}
x~::~a & x~::~forall a.~a \\
xs~::~{}[a] & xs~::~forall a.~[a] \\
mx~::~Maybe a & mx~::~forall a.~Maybe a \\
f~::~a~->~b & f~::~forall~a~b.~a~->~b
\end{tabularlc} \\[1em]
\end{document}