Types & Programs

2025 James B. Wilson

class Pair<A,B> {
  A myA;
  B myB;
  Pair(A a, B b) { myA = a; myB = b;}
  
  A piA() { return myA; }
  B piB() { return myB; }
}
\begin{aligned} A, B & \vdash A \wedge B\\ A\wedge B & \vdash A \\ A\wedge B & \vdash B\\ \end{aligned}

Programming

Logic

Wanna get paid?

Turn the logic into substance.

\(P\) a concept \(\longrightarrow\) \(t\) stuff of type \(T\) \((t:T)\)

\(P\).  If \(541=12\cdot q+r\) then \(q=45\) and \(r=1\)

Given (541,12), 
compute t=(541/12, 541 mod 12).

\(t:T\)

 

\(P\).  If has paper & non-empty queue then printer should be printing.

while hasPaper()&& queue.size()>0 

       print queue.next()

Three Steps to a Program

  • Reason about flow
  • Annotate the data
  • Match to Programming Idioms
\begin{aligned} \begin{array}{rl} \Gamma & \vdash A \\ \Gamma &\vdash B \\ \hline \Gamma & \vdash A\wedge B \end{array} \end{aligned}

Starting Point: reasoning

Conjunction:

  • context \(\Gamma\) (also ctx) says events A and B hold;

  • Make  a single combined event
\begin{aligned} \begin{array}{rl} \Gamma & \vdash A\wedge B \\ \hline \Gamma & \vdash A \end{array} \end{aligned}

Simplification:

  • Given a conjoined event
  • Extract a constituent

Translate to symbols

"Logic"

\begin{aligned} \begin{array}{rl} \Gamma & \vdash A\wedge B \\ \hline \Gamma & \vdash B \end{array} \end{aligned}
\begin{aligned} \begin{array}{rl} & a:A \\ & b:B \\ \hline & (a,b):(A,B) \end{array} \end{aligned}

Mid Point: make it data

Introduction:

  • Logic: \[A,B\vdash A\wedge B\]

  • Data: \[a:A, b:B\vdash (a,b):(A,B)\]

\begin{aligned} \begin{array}{rl} & x:(A,B) \\ \hline & \pi_A(x):A \end{array} \end{aligned}

Elimination:

  • \[(a,b):(A,B) \vdash a:A\]
  • \[(a,b):(A,B)\vdash b:B\]

Get paid more...

\begin{aligned} \begin{array}{rl} & A,B:Type\\\hline & (A,B):Type \end{array} \end{aligned}

Form a type

Introduce

data

\begin{aligned} \begin{array}{rl} & x:(A,B) \\ \hline & \pi_B(x):B \end{array} \end{aligned}

Eliminate the type

\begin{aligned} \begin{array}{rl} & a:A \\ & b:B \\ \hline & \pi_A(a,b)=a \end{array} \end{aligned}

Computation

\begin{aligned} \begin{array}{rl} & a:A \\ & b:B \\ \hline & \pi_B(a,b)=b \end{array} \end{aligned}

Oops, forgot context

\begin{aligned} \begin{array}{rl} \Gamma & \vdash a:A \\ \Gamma &\vdash b:B \\ \hline \Gamma & \vdash (a,b):(A,B) \end{array} \end{aligned}
\begin{aligned} \begin{array}{rl} \Gamma &\vdash x:(A,B) \\ \hline \Gamma & \vdash \pi_A(x):A \end{array} \end{aligned}
\begin{aligned} \begin{array}{rl} \Gamma &\vdash A,B:Type\\\hline \Gamma &\vdash (A,B):Type \end{array} \end{aligned}

Formation

Introduction/

Constructor

\begin{aligned} \begin{array}{rl} \Gamma &\vdash x:(A,B) \\ \hline \Gamma &\vdash \pi_B(x):B \end{array} \end{aligned}

Eliminations/"Getters"

\begin{aligned} \begin{array}{rl} \Gamma & \vdash a:A \\ \Gamma &\vdash b:B \\ \hline \Gamma &\vdash \pi_A(a,b)=a \end{array} \end{aligned}

Computation/Program

\begin{aligned} \begin{array}{rl} \Gamma & \vdash a:A \\ \Gamma &\vdash b:B \\ \hline \Gamma & \vdash \pi_B(a,b)=b \end{array} \end{aligned}
\begin{aligned} \begin{array}{rl} \Gamma & \vdash A \\ \Gamma &\vdash B \\ \hline \Gamma & \vdash A\wedge B \end{array} \end{aligned}
\begin{aligned} \begin{array}{rl} \Gamma & \vdash A\wedge B \\ \hline \Gamma & \vdash A \end{array} \end{aligned}
\begin{aligned} \begin{array}{rl} \Gamma & \vdash A\wedge B \\ \hline \Gamma & \vdash B \end{array} \end{aligned}

Logic

Program

(A,B) also denoted \(A\times B\)

Final Point: make it code

\begin{aligned} \begin{array}{rl} \Gamma &\vdash A, B:Type\\\hline \Gamma &\vdash (A,B):Type \end{array} \end{aligned}

Formation

import ctx;

class Pair<A,B> {
  ...
}

Keyword for "formation"

Parameters to formation rule

Text version of \(\times\) and prefix notation because of language rules.

Context = "import packages"

\begin{aligned} \begin{array}{rl} \Gamma & \vdash a:A \\ \Gamma &\vdash b:B \\ \hline \Gamma & \vdash (a,b):(A,B) \end{array} \end{aligned}

Final Point: make it code

\begin{aligned} \begin{array}{rl} \Gamma & \vdash A,B:Type\\\hline \Gamma & \vdash (A,B):Type \end{array} \end{aligned}

Formation

Introduction

import ctx;

class Pair<A,B> {
  Pair(A a, B b) { ... }
}

 Hint: no output signals possible introduction.

 

 

Will invoke this with

new Pair(...)

Parameters to introduction rule

\begin{aligned} \begin{array}{rl} \Gamma & \vdash a:A \\ \Gamma &\vdash b:B \\ \hline \Gamma & \vdash (a,b):(A,B) \end{array} \end{aligned}

Final Point: make it code

\begin{aligned} \begin{array}{rl} \Gamma & \vdash A,B:Type\\\hline \Gamma & \vdash (A,B):Type \end{array} \end{aligned}

Formation

Introduction

import ctx;

class Pair<A,B> {
  Pair(A a, B b) { ... }
    
  A getA() { ... }
  B getB() { ... }
}

Elimination for B.

 

Hint: no input signals possible elimination

Elimination for A

\begin{aligned} \begin{array}{rl} \Gamma & \vdash x:(A,B) \\ \hline \Gamma & \vdash \pi_A(x):A \end{array} \end{aligned}
\begin{aligned} \begin{array}{rl} \Gamma & \vdash x:(A,B) \\ \hline \Gamma & \vdash \pi_B(x):B \end{array} \end{aligned}

Elimination

\begin{aligned} \begin{array}{rl} \Gamma & \vdash a:A \\ \Gamma &\vdash b:B \\ \hline \Gamma & \vdash (a,b):(A,B) \end{array} \end{aligned}

Final Point: make it code

\begin{aligned} \begin{array}{rl} \Gamma & \vdash A,B:Type\\\hline \Gamma & \vdash (A,B):Type \end{array} \end{aligned}

Formation

Introduction

import ctx;

class Pair<A,B> {
  A myA; B myB;
  Pair(A a, B b) {
    myA = A; myB=b;
  }
    
  A getA() { return myA; }
  B getB() { return myB; }
}

Retrieve b from storage when asked

 

Internally store a.

\begin{aligned} \begin{array}{rl} \Gamma & \vdash x:(A,B) \\ \hline \Gamma & \vdash \pi_A(x):A \end{array} \end{aligned}
\begin{aligned} \begin{array}{rl} \Gamma & \vdash x:(A,B) \\ \hline \Gamma & \vdash \pi_B(x):B \end{array} \end{aligned}

Elimination

\begin{aligned} \begin{array}{rl} \Gamma & \vdash a:A \\ \Gamma&\vdash b:B \\ \hline \Gamma & \vdash \pi_A(a,b)=a \end{array} \end{aligned}

Computation

\begin{aligned} \begin{array}{rl} \Gamma & \vdash a:A \\ \Gamma&\vdash b:B \\ \hline \Gamma & \vdash \pi_B(a,b)=b \end{array} \end{aligned}

TRY IT YOURSELF!

Try this right now in

  • Java

  • Scala

  • Haskell

(All you need is a web browser.)

class Pair<A,B> {
  A a;  B b;
  Pair(A a, B b) { 
    this.a = a; 
    this.b = b;
  }
  
  A getA() { return a; }
  B getB() { return b; }
}

public class TrialRun {
  // A way to try the code out
  public static void main(String args[]) {
    Pair<String,Integer> p = new Pair("5",5);
    System.out.println("Type " + p.getA().getClass());
    System.out.println("Type " + p.getB().getClass());
  }
}

Try it!  (Java Edition)

Visit https://www.jdoodle.com/online-java-compiler/

Or us VS Code with your personal Java install

class Pair[A,B](a:A, b:B) {
  def getA():A = a
  def getB():B = b
}

val p:Pair[String,Int] = new Pair("5",5)
val a:String = p.getA()
val b:Int = p.getB()

print(a)
print(b)

Try it!  (Scala Edition)

Visit https://scastie.scala-lang.org/

Or us VS Code with your personal Scala install

In Scala, formation and introduction can be merged together to make a concise program (but less obvious)

module Main where

data Pair a b = Pair {
  x::a,
  y::b
}

main = print (y p)
      where 
        p = Pair "5" 5

Try it!  (Haskell Edition)

Visit https://replit.com/

Or us VS Code with your personal Haskall install

Haskell prefer \(\sin x\) style to \(\sin(x)\).

Pair a b means Pair(a,b);

Pair "5" 5 is Pair("5",5).

Also Haskell requires lower case for generic type names, i.e. a not A.

(y p) is Haskell for p.y

Logic into types...

  • Introductions become
    • Formation of data type (encapsulate in class)
    • Constructors for the data type (called by "new" or "make")
  • Elimination become
    • "Getters" for the encapsulated data
    • Computation/program rules for storing/computing outcomes of getters

Types & Programs

By James Wilson

Types & Programs

Demonstration of mathematical types and programming comparables.

  • 81