# Ideas for a type system

Pipe-calculus is a typeless core calculus, but a language based on it doesn't have to be. There are a few preliminary thoughts that guide the search for a suitable type system.

• Types are not added directly to the core calculus but to the surface language.
• The type system should naturally fit to the calculus (this is a subtle guideline that is elaborated below).
• The type checking algorithm must be easy to implement in the typeless core language.

The last point comes from the hope that the surface language will be homoiconic, and the type system will not break this property.

## Dynamic vs. static typing

In this article we will call the typeless core calculus the dynamic or runtime language.

A type annotation can be treated in two ways: either translating it to a runtime check or in advance prove that a runtime check is unnecessary and remove it from the program. This makes the distinction between dynamic and static typing.

With dynamic typing type checking and evaluation overlap. A dynamically typed program can be modeled as a type safe domain that is surrounded by an unsafe environment. Once the program has to receive an input value from the environment, its external representation has to be decoded and it must be proved that it is an element of the required type. On success the program proceeds in a type safe manner. On failure the program either rejects the input and proceeds as an alternative branch or its execution is aborted.

If two type safe domains share the same internal representation and type system, they can directly exchange values. In this case values are transferred together with type annotations (probably in an optimized form) and only type annotations have to be checked.

With static typing, the type of a program is checked before execution. After successful type checking, type annotations may be safely erased. At runtime the program can still receive input. The type checker has to ensure that on failure to parse the input the program either proceeds in an alternative branch or is aborted.

## Linguistic analogy

In a linguistic model of dynamic typing every data type $\displaystyle{ T_i }$ where $\displaystyle{ 1 \le i \le n }$ is associated with a language $\displaystyle{ L_i }$. The environment carries words of the language $\displaystyle{ \sum_{i=1}^{n} L_i }$. When outputting a value $\displaystyle{ v : T_i }$, it is translated to an external representation in $\displaystyle{ L_i }$. When inputting a value $\displaystyle{ v : T_i }$ from the environment, its external representation is parsed. A program that implements a function $\displaystyle{ f : T_i \to T_j }$ is modeled by a translator from $\displaystyle{ L_i }$ to $\displaystyle{ L_j }$.

Every data type $\displaystyle{ T_i }$ can be conservatively modeled by a generator $\displaystyle{ g_{L_i} }$ that generates all words of $\displaystyle{ L_i }$ and a recognizer $\displaystyle{ r_{L_i} }$ that recognizes exactly the words of $\displaystyle{ L_i }$. Accordingly, every first order function $\displaystyle{ f : T_i → T_j }$ can be modeled by the composition of $\displaystyle{ r_{L_i} }$ and $\displaystyle{ g_{L_j} }$.

Algebraic sum types perfectly fit into this framework. For any type definition $\displaystyle{ T = c_1~A_{11}~...~A_{1n}~|~...~|~c_l~A_{l1}~...~A_{lm} }$ we can construct a corresponding unambiguous context-free grammar as follows.

• For each distinct $\displaystyle{ T }$ and $\displaystyle{ A_{ij} }$ type names we choose distinct nonterminal symbols $\displaystyle{ \alpha(T) }$ and $\displaystyle{ \alpha(A_{ij}) }$ respectively.
• For each $\displaystyle{ c_i }$ constructor symbol we choose a distinct $\displaystyle{ s(c_i) }$ terminal symbol. 
• For each $\displaystyle{ c_i~A_{i1}~...~A_{io} }$ product type we create a production rule $\displaystyle{ \alpha(T) ::= s(c_i)\alpha(A_{i1})...\alpha(A_{io}) }$.

This method can be extended to multiple type definitions as long as we use the same mappings $\displaystyle{ s }$ and $\displaystyle{ \alpha }$ and constructor symbols are globally unique. The resulting grammar is in Greybach normal form.