The document discusses functions and the Liskov substitution principle. It defines functions with signatures like f: A => B, meaning a function f that takes an input of type A and returns an output of type B. It explains function composition using andThen and |>. It also discusses how the Liskov substitution principle states that if S is a subtype of T, then objects of type T may be replaced with objects of type S without altering any desirable properties of that program. This allows subclasses to substitute behavior while preserving contracts with external consumers.
2. FUNCTION[-A, +B]
f: A => B
g: B => C
F[+A] : if A’ <: A then F[‘A] <: F[A]
A’ <: A => if a: A’ then a:A
A
A’
F[- A] : if A’ <: A then F[A] <: F[A’]
3. FUNCTION[-A, +B]
f: A => B
g: B => C
F[+A] : if A’ <: A then F[‘A] <: F[A]
A’ <: A => if a: A’ then a:A
A
A’
F[- A] : if A’ <: A then F[A] <: F[A’]
‘T
T
4. FUNCTION[-A, +B]
f: A => B
g: B => C
f andThen g
g(f(x))
f |> g
A B С
f g
Liskov Principle: Expr[T], T’ < T => Expr[T’]
5. FUNCTION[-A, +B]
f: A => B
g: B => C
f andThen g
g(f(x))
f |> g
A B С
f g
Liskov Principle: Expr[T], T’ < T => Expr[T’]
f’ < f; f’ |> g
6. FUNCTION[-A, +B]
f: A => B
g: B => C
f andThen g
g(f(x))
f |> g
A B С
f g
Liskov Principle: Expr[T], T’ < T => Expr[T’]
f’ < f; f’ |> g f’ : A’ => B’
7. FUNCTION[-A, +B]
f: A => B
g: B => C
f andThen g
g(f(x))
f |> g
A B С
f’ g
f’ < f; f’ |> g f’ : A => B’ Let B’ :> B
Problem: g is not defined on B’ - B
8. FUNCTION[-A, +B]
f: A => B
g: B => C
f andThen g
g(f(x))
f |> g
A B С
f’ g
f’ < f; f’ |> g f’ : A => B’ Let B’ :< B
Function[.. , +B]
9. FUNCTION[-A, +B]
f: A => B
g: B => C
f andThen g
g(f(x))
f |> g
A B С
f g’
g’ < g; f |> g’ g’ : B’ => C Let B’ :< B
Function[.. , +B]
10. FUNCTION[-A, +B]
f: A => B
g: B => C
f andThen g
g(f(x))
f |> g
A B С
f g’
g’ < g; f |> g’ g’ : B’ => C Let B’ :< B
Problem: g’ is not defined on B - B’
11. FUNCTION[-A, +B]
f: A => B
g: B => C
f andThen g
g(f(x))
f |> g
A B С
f g’
g’ < g; f |> g’ g’ : B’ => C Let B’ :> B
Function[-A , +B]
12. FUNCTION[-A, +B]
f: A => B
g: B => C
f andThen g
g(f(x))
f |> g
A B С
f g’
Function[-A , +B] :
application of Liskov substitution principle
// Barbara Liskov, CLU Language ~ 1974
13. -A — IN, +A - OUT
Object — bundle of methods (functions)
- (in) — any input ; + (out) — any output