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; }
}
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
Starting Point: reasoning
Conjunction:
-
context \(\Gamma\) (also
ctx
) says events A and B hold; - Make a single combined event
Simplification:
- Given a conjoined event
- Extract a constituent
Translate to symbols
"Logic"
Mid Point: make it data
Introduction:
-
Logic: \[A,B\vdash A\wedge B\]
-
Data: \[a:A, b:B\vdash (a,b):(A,B)\]
Elimination:
- \[(a,b):(A,B) \vdash a:A\]
- \[(a,b):(A,B)\vdash b:B\]
Get paid more...
Form a type
Introduce
data
Eliminate the type
Computation
Oops, forgot context
Formation
Introduction/
Constructor
Eliminations/"Getters"
Computation/Program
Logic
Program
(A,B) also denoted \(A\times B\)
Final Point: make it code
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"
Final Point: make it code
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
Final Point: make it code
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
Elimination
Final Point: make it code
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.
Elimination
Computation
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