Skip to content

Commit 87eede5

Browse files
committed
describe cation in terms of category theory
1 parent 7e11981 commit 87eede5

File tree

1 file changed

+214
-0
lines changed

1 file changed

+214
-0
lines changed

content/category.md

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
+++
2+
description = "Cation Language in Terms of Category Theory"
3+
template = "page.html"
4+
+++
5+
6+
# The Categorical Language
7+
8+
Explaining Cation language in terms of Category Theory
9+
10+
Cation is a categorical language, meaning a strong equivalence between Category theory and language constructions.
11+
This article dives into the details of such equivalences, which are made into a core of the language design.
12+
13+
## Basics
14+
15+
In Cation language everything – data types, functions, expressions, values, literals – corresponds to categories,
16+
functors and objects. Since categories can be seen as objects in a higher-order category, all of them need just
17+
two main language constructions: **types** and **values**, introduced and explained in this section.
18+
19+
### Types and Values
20+
21+
The very fundamental concept of a Cation language is *type*. Each <dfn>type</dfn> is a *category* in itself.
22+
Contravariantly, a *category* in Cation language is named *type*.
23+
24+
<aside>
25+
<p>Types in Cation are declared using a <code>type</code> keyword followed by a type name:</p>
26+
<pre><code>type TypeName</code></pre>
27+
</aside>
28+
29+
This is a full and sufficient definition of what *type* is; everything else about types in Cation and Cation
30+
itself are just consequences of this definition. For instance, saying that Cation is a language of operations
31+
with types defines Cation as a categorical language.
32+
33+
Important to note that all Cation types are small categories and can be represented as finite sets, inhabited by their
34+
*values*. Hence, a <dfn>value</dfn> in Cation is an object in one of the *type* categories.
35+
36+
<aside>
37+
<p>Values are defined using <code>val</code> keyword followed by an optional type annotation and a literal or an
38+
expression which evaluation results in the value from the type:</p>
39+
<pre><code>val valName: TypeName := expression</code></pre>
40+
</aside>
41+
42+
As all small categories can be seen as objects in the category of all small categories <nobr>$\mathbf{Cat}$,</nobr>
43+
all Cation *types* as categories may also be seen as objects in a category of all types of Cation language –
44+
<nobr>$\mathbf{Cation}$.</nobr> As a category, $\mathbf{Cation}$ is small category with infinite number of members.
45+
46+
### Expressions, functions, programs
47+
48+
A Cation <dfn>expression</dfn> maps a type <nobr>($A$)</nobr> (named <dfn>argument type</dfn>) to a type
49+
<nobr>($R$)</nobr> (named <dfn>return type</dfn>: it can be the same as the argument type, or a different type).
50+
Thus, an expression in Cation is always a functor <nobr>$F: A \rightarrow R$.</nobr>
51+
52+
<aside>
53+
<p>One can guess that an expression can also be seen as a morphism in the <nobr>$\mathbf{Cation}$.</nobr></p>
54+
</aside>
55+
56+
Evaluation of an <dfn>expression</dfn> selects a *value* <nobr>($a$)</nobr> from the *argument type* <nobr>($A$)</nobr>
57+
and maps it to another *value* <nobr>($r$)</nobr> in the *return type* <nobr>($R$).</nobr> Thus, it is an application
58+
of the *functor* <nobr>($F$)</nobr> of the *expression* to an object <nobr>($a$):</nobr>
59+
60+
$$\mathsf{eval}\ F\ a \doteqdot a \xrightarrow{F} r$$
61+
62+
<aside>
63+
<p>Cation uses syntax close to math notation: a function type and expression evaluation are written as</p>
64+
<pre><code>type F: A -> R
65+
val r := F a</code></pre>
66+
<p>You can even use Cation compiler to prove that `r` has the type `R`:</p>
67+
<pre><code>val r := (F a)#R</code></pre>
68+
</aside>
69+
70+
The signature of an expression $F: A \rightarrow R$ is [a type by itself][1]: this is a <dfn>function type</dfn>. It
71+
can also be defined via the evaluation, as shown above, applied to all objects of the argument category:
72+
73+
$$F\ a = r \in R\ |\ \forall a \in A$$
74+
75+
In Cation, each <dfn>function</dfn> is a named expression – and each expression is a function (which maybe anonymous).
76+
All expressions and functions are
77+
- functors,
78+
- morphisms in <nobr>$\mathbf{Cation}$,</nobr>
79+
- objects in $\mathrm{hom}(\mathbf{Cation})$
80+
81+
<aside>
82+
<p>Named functions are defined from expressions</p>
83+
84+
```
85+
type name: A -> R := expr
86+
```
87+
88+
<p>or, with a multi-line syntax,</p>
89+
90+
```
91+
type name: A -> R
92+
expression(s)
93+
```
94+
</aside>
95+
96+
A Cation <dfn>program</dfn> is a composition of expressions, which is an expression itself (as a composition of
97+
functors is always a functor). <dfn>Execution of program</dfn> corresponds to an evaluation of the expression: an
98+
application of the composed functor to a program argument giving a result.
99+
100+
101+
### Data types
102+
103+
<dfn>Data types</dfn> is another example of Cation *types*, such that data types are the first-class language citizen
104+
alongside functions and expressions.
105+
106+
There are only two foundational data types, named *unit* and *uninhabited*. <dfn>Unit</dfn> type is an *initial object*
107+
in the $\mathbf{Cation}$ category and is written as `(,)`. <dfn>Uninhabited</dfn> type is a *terminal object* in the
108+
$\mathbf{Cation}$ category and is written as `(|)`. All other data types are derived from these two types via ... yes,
109+
expressions!
110+
111+
<aside>
112+
<p>As will be seen later, <em>unit</em> and <em>uninhabited</em> types are just empty product and co-product types</p>
113+
</aside>
114+
115+
Data type is defined as an expression composition of other types. As with any algebraic data type system there are two
116+
forms of composition: *product* and *co-product*.
117+
118+
<aside>
119+
<p>Here, we are taking advantage of all Cation types being small categories and thus sets; so a cartesian product
120+
and unit operations over two types can be always defined in terms of sets.</p>
121+
</aside>
122+
123+
*Product data types* are bi-functors from categories representing types $L$ and $R$ to a new category $P$; where
124+
$P$ is a cartesian product of sets of $L$ and $R$ objects: $P \doteqdot L \times R$. This also defines two
125+
<dfn>projections</dfn> as functors from the type $P$ to the original types $L$ and $R$. Cation expresses product with a
126+
comma operator (`,`) and the new type may be defined in the following way:
127+
128+
```
129+
data Prod: A, B
130+
131+
-- using the new Prod type
132+
val new := (valueA, valueB)#Prod
133+
val projA := new.a
134+
val projB := new.b
135+
136+
-- testing that the result is the same as the original values:
137+
projA =? valueA && projB =? valueB !! mustBeEqual
138+
```
139+
140+
<aside>
141+
<p>In fact, this is a syntactic sugar: <code>data</code> is a macro coming with a standard library, which expends into
142+
the code defining type, constructor function and two projections:</p>
143+
144+
```
145+
type Prod
146+
type `#Prod`: a A, b B @op -> Prod
147+
(a, b)
148+
type `.a`: self Prod -> A
149+
val ($, _) := self
150+
type `.b`: self Prod -> B
151+
val (_, $) := self
152+
```
153+
</aside>
154+
155+
*Co-product data types* are bi-functors corresponding to the morphism in the $\mathbf{Cation^\mathrm{op}}$: they
156+
map a union of sets representing all possible values in $L$ and $R$ to a new category $S$. Cation union with a
157+
pipe operator (`|`) and the new type may be defined in the following way:
158+
159+
```
160+
data Either: A | B
161+
162+
-- using the new Either type
163+
let either := a#A -- `a` is the value from the type `A`
164+
either >|
165+
(a)#A |? print "I am A"
166+
(b)#B |? print "I am B"
167+
```
168+
169+
<aside>
170+
<p>Again, this is a syntactic sugar for</p>
171+
172+
```
173+
type Either
174+
-- two constructurs:
175+
type `#Either`: a A -> Either := a
176+
type `#Either`: b B -> Either := b
177+
```
178+
</aside>
179+
180+
181+
## Advanced topics
182+
183+
### Currying
184+
185+
Since each data type constructor is a functor, and each functor takes exactly one argument, the multi-argument function
186+
type `type fn: A, B -> C` can be seen as a functor of a single argument of an unnamed product type `(A, B)` – or as a
187+
composition of two unnamed functors `type fn: A -> (B -> C)` – similar to the Haskell language.
188+
189+
190+
### Lambda expressions
191+
192+
### Collections
193+
194+
### Generics
195+
196+
Now it is time to introduce Cation language constructs build using the last component of the Category theory:
197+
*natural transformations*.
198+
199+
### Monads
200+
201+
202+
## Expert topics
203+
204+
Here we show how all language constructs can be interpreted as natural transformations, such that their composability
205+
properties are used for terminal analysis and parallel computations without creation of racing conditions.
206+
207+
### Naturality conditions
208+
209+
### Termination analysis
210+
211+
### Rho expressions
212+
213+
214+
[1]: https://bartoszmilewski.com/2015/03/13/function-types/

0 commit comments

Comments
 (0)