On Understanding T yp es, Data Abstraction, and P olymo - - PDF document

on understanding t yp es data abstraction and p olymo
SMART_READER_LITE
LIVE PREVIEW

On Understanding T yp es, Data Abstraction, and P olymo - - PDF document

On Understanding T yp es, Data Abstraction, and P olymo rphism On Understanding T yp es, Data Abstraction, and P olymo rphism W olfgang Schreiner Resea rch Institute fo r Symb olic Computation (RISC-Linz) Johannes


slide-1
SLIDE 1 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism W
  • lfgang
Schreiner Resea rch Institute fo r Symb
  • lic
Computation (RISC-Linz) Johannes Kepler Universit y , A-4040 Linz, Austria W
  • lfgang.Schreiner@risc.un
i-linz.ac.at http://www.risc.uni-linz.ac.at/p eople/schrein e W
  • lfgang
Schreiner RISC-Linz
slide-2
SLIDE 2 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism F rom Unt yp ed to T yp ed Universes
  • Unt
yp ed universes. { Only
  • ne
t yp e fo r all
  • bjects:
{ Bit strings in computer memo ry , { S-exp ressions in pure LISP , {
  • exp
ressions in the
  • calculus,
{ Sets in set theo ry .
  • Organization
  • f
  • bjects:
{ Classication
  • f
usage/b ehavio r, { Cha racters, integers, { Lists, pairs, { F unctions, p rograms.
  • T
yp e distinction still shallo w: { Easy to violate t yp e distinctions: { Bo
  • lean
  • r
  • f
cha racter and machine
  • p
eration? W
  • lfgang
Schreiner 1
slide-3
SLIDE 3 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Static and Strong T yping
  • T
yp es imp
  • se
constraints to enfo rce co r- rectness. { Protection
  • f
underlying (unt yp ed) rep resentation. { Constrains interaction
  • f
  • bjects.
  • Static
t yp e structure
  • f
p rograms. { Little
  • r
no t yp e info rmation given explicit y . { T yp es asso ciated to constants/function symb
  • ls.
{ T yp e inference system infers t yp es
  • f
exp ressions b y static analysis.
  • Strong
t yping system. { Exp ressions a re gua ranteed to b e t yp e consistent. { T yp e itself ma y b e statically unkno wn. { Intro duce run time t yp e checking. W
  • lfgang
Schreiner 2
slide-4
SLIDE 4 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Kinds
  • f
P
  • lym
  • rphis
m
  • Monomo
rphic languages: { All functions and p ro cedures have unique t yp e. { All values and va riables
  • f
  • ne
and
  • nly
t yp e. { P ascal-lik e t yp e systems.
  • P
  • lymo
rphic languages: { V alues and va riables ma y have mo re than
  • ne
t yp e. { P
  • lymo
rphic functions have
  • p
eratnds
  • f
mo re than
  • ne
t yp e. { P
  • lymo
rphic t yp es have
  • p
erations applicable to
  • p
erands
  • f
mo re than
  • ne
t yp e.
  • Universal
p
  • lymo
rphism: { F unction w
  • rks
unifo rmly
  • n
range
  • f
t yp es. { P a rametric and inclusion p
  • lymo
rphism.
  • Ad-ho
c p
  • lymo
rphism: { F unction w
  • rks
  • n
several unrelated t yp es. { Overlading and co ercion. W
  • lfgang
Schreiner 3
slide-5
SLIDE 5 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Universal P
  • lymo
rphism
  • P
a rametric p
  • lymo
rphism: { Unifo rmit y achieved b y t yp e pa rameters. { Determines a rgument t yp e fo r each application
  • f
p
  • ly-
mo rphic function. { ML-lik e t yp e systems.
  • Inclusion
p
  • lymo
rphism: { Object ma y b elong to several t yp es. { T yp es related b y inclusion relation. { Object-o riented t yp e systems.
  • Generic
functions: { \Same" w
  • rk
is done fo r a rguments
  • f
many t yp es. { Length function
  • ver
lists. W
  • lfgang
Schreiner 4
slide-6
SLIDE 6 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Ad-ho c P
  • lymo
rphis m
  • Overloading
{ Same name denotes dierent functions. { Context decides which function is denoted b y pa rticula r
  • ccurence
  • f
a name. { Prep ro cessing ma y eliminate
  • verloading
b y giving dier- ent names to dierent functions.
  • Co
ercion { T yp e conversions convert an a rgument to a t yp e exp ected b y a function. { Ma y b e p rovided statically at compile time. { Ma y b e determined dynamically b y run-time tests.
  • Only
appa rent p
  • lymo
rphism { Op erato rs/functions
  • nly
have
  • ne
t yp e. { Only syntax \p retends" p
  • lymo
rphism. W
  • lfgang
Schreiner 5
slide-7
SLIDE 7 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Overloading and Co ercion
  • Distinction
ma y b e blurred: 3 + 4 3.0 + 4 3 + 4.0 3.0 + 4.0
  • Dierent
explanations p
  • ssible:
{ + has four
  • verloaded
meanings. { + has t w
  • verloaded
meanings (integer and real addition) and integers ma y b e co erced to reals. { + is real addition and integers a re alw a ys co erced to reals.
  • Overloading
and/o r co ercion
  • r
b
  • th!
F
  • r
histo ry
  • f
t yp e evolution see [Ca rdelli and W egner, 1985]. W
  • lfgang
Schreiner 6
slide-8
SLIDE 8 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Preview
  • f
F un
  • calculus
based language { Basis is rst-o rder t yp ed
  • calculus.
{ Enriched b y second-o rder features fo r mo deling p
  • lymo
r- phism and
  • bject-o
riented languages.
  • First-o
rder t yp es { Bo
  • l,
Int, Real, String.
  • V
a rious fo rms
  • f
t yp e quantiers { T yp e ::= . . . j Quantied T yp e { Quantied T yp e ::= 8A. T yp e j 9A. T yp e j 8A T yp e. T yp e j 9A T yp e. T yp e
  • Mo
deling
  • f
advanced t yp e systems: { Universal quantication: pa rameterized t yp es. { Existential quantiers: abstract data t yp es. { Bounded quantication: t yp e inheritance. W
  • lfgang
Schreiner 7
slide-9
SLIDE 9 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism The Unt yp ed
  • Calculus
  • Exp
ressions: { e ::= x { e ::= fun(x) e { e ::= e(e)
  • Intro
duction
  • f
names { value id = fun(x) x { value succ = fun(x) x+1 { value t wice = fun(f ) fun(y) f(f(y)) W
  • lfgang
Schreiner 8
slide-10
SLIDE 10 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism The T yp ed
  • Calculus
  • Extension
  • f
Unt yp ed
  • Calculus
{ Every va riable must b e explicitly t yp ed when intro duced as t yp ed va riable { Result t yp es can b e deduced from function b
  • dy
.
  • Examples
{ value succ = fun(x:Int) x+1 { value t wice = fun(f: Int ! Int) fun(y:Int) f(f(y))
  • T
yp e decla rations: { t yp e IntP air = Int
  • Int
{ t yp e IntF un = Int ! Int
  • T
yp e annotations/assertions: { (3, 4): IntP air { value intP air: IntP air = (3, 4)
  • Lo
cal va riables { let a = 3 in a+1 { let a: Int = 3 in a+1 W
  • lfgang
Schreiner 9
slide-11
SLIDE 11 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Basic and Structured T yp es
  • Basic
t yp es: { Unit (trivial t yp e,
  • nly
element ()) { Bo
  • l
(with if-then-else) { Int (with a rithmetic and compa rison) { Real (with a rithmetic and compa rison) { String (with inx concatenation ^)
  • T
yp e constructo rs: { ! (function space) {
  • (Ca
rtesian p ro duct) { reco rd t yp es (lab eled Ca rtesian p ro ducts) { va riant t yp es (lab eled disjoint sums)
  • Example:
{ value p: Int
  • Bo
  • l
= 3, true fst(p), snd(p) W
  • lfgang
Schreiner 10
slide-12
SLIDE 12 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Reco rd T yp es
  • Example:
{ t yp e ARec = fa: Int, b: Bo
  • lg
value r: ARec = fa = 3, b = trueg r.b
  • F
unctions as comp
  • nents:
{ t yp e F unRec = ff1: Int ! Int, f2: Real ! Realg value funRec: F unRec = ff1 = succ, f2 = sing
  • Concatenation
  • f
reco rd t yp es: { t yp e NewRec = F unRec & ff3: Bo
  • l
! Bo
  • lg
  • Private
data structures: { value counter = let count = ref(0) in f inc = fun(n:Int) count := count+n total = fun() count g { counter.inc(3), counter.total() W
  • lfgang
Schreiner 11
slide-13
SLIDE 13 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism V a riant T yp es and Recursion
  • Example:
{ t yp e A V a r = [a: Int, b: String] value v1: A V a r = [a = 3] value v2: A V a r = [b = \ab cd"] { value f = fun(x: A V a r) case x
  • f
[a = anInt] \int" [b = aString] \string" ^ aString
  • therwise
\erro r"
  • Recursive
function denitions { rec value fact = fun(n: Int) if n=0 then 1 else n*fact(n-1)
  • Recursive
t yp e denitions { rec t yp e IntList = [nil: Unit cons: fhead: Int, tail: IntListg ] W
  • lfgang
Schreiner 12
slide-14
SLIDE 14 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Universal Quantication
  • T
yp ed
  • calculus
describ es monomo rphic functions. { Unsucient to describ e functions that b ehave the same w a y fo r a rgumentes
  • f
dierent t yp es.
  • Intro
duce t yp es as pa rameters: { value id = all[a] fun(x:a) x id[Int](3)
  • Ma
y
  • mit
t yp e info rmation: { value id = fun(x:a) x id(3) { T yp e-check er reintro duces all[a] and [Int]
  • P
  • lymo
rphic t yp es: { t yp e GenericId = 8a. a ! a id: GenericId { value inst = fun(f: 8a. a ! a)(f[Int], f[Bo
  • l])
{ value intid: Int ! Int = fst(inst(id)) value b
  • lid:
Bo
  • l
! Bo
  • l
= snd(inst(id)) W
  • lfgang
Schreiner 13
slide-15
SLIDE 15 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism P
  • lym
  • rphic
F unctions
  • First
version
  • f
p
  • lymo
rphic t wice: { value t wice1 = all[t] fun(f: 8a: a ! a) fun(x: t) f[t](f[t](x)) { t wice1[Int](id)(3) is legal. { t wice1[Int](succ) is illegal!
  • Second
version
  • f
p
  • lymo
rphic t wice: { value t wice2 = all[t] fun(f: t ! t) fun(x: t) f(f(x)) { t wice2[Int](succ) is legal. { t wice2[Int](id[Int])(3) is legal.
  • Both
versions dierent in nature
  • f
f: { In t wice1, f is a p
  • lymo
rphic function
  • f
t yp e 8a: a ! a. { In t wice2, f is a monomo rphic function
  • f
t yp e t ! t (fo r some instantiation
  • f
t) W
  • lfgang
Schreiner 14
slide-16
SLIDE 16 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism P a rametric T yp es
  • T
yp e denitions with simila r structure: { t yp e Bo
  • lP
air = Bo
  • l
  • Bo
  • l
{ t yp e IntP air = Int
  • Int
  • Use
pa rametric denition: { t yp e P air[T] = T
  • T
{ t yp e P airOfBo
  • l
= P air[Bo
  • l]
{ t yp e P airOfInt = P air[Int]
  • T
yp e
  • p
erato rs a re not t yp es: { t yp e A[T] = T ! T { t yp e B = 8T. T ! T { Dierent notions! W
  • lfgang
Schreiner 15
slide-17
SLIDE 17 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Recursive Denitions
  • Recursively
dened t yp e
  • p
erato rs: { rec t yp e List[Item] = [nil: Unit cons: fhead: Item, tail: List[Item]g ] { value nil: 8Item.List[Item] = all[Item]. [nil = ()] value intNil: List[Int] = nil[Int] { value cons: 8Item. (Item
  • List[Intem])
! List[Item] = all[Item]. fun(h Item, t: List[Item]) [cons = fhead = h, tail = tg] Cons can b e
  • nly
used to build homogeneous lists! W
  • lfgang
Schreiner 16
slide-18
SLIDE 18 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Existential Quantication
  • Existential
t yp e quantication: { p: 9a. t(a) { F
  • r
some t yp e a, p has t yp e t(a)
  • Examples:
{ (3, 4): 9a. a
  • a
{ (3, 4): 9a. a { Constant can satisfy dierent existential t yp es!
  • Sample
existential t yp es: { t yp e T
  • p
= 9a. a (t yp e
  • f
any value) { 9a. 9b. a
  • b
(t yp e
  • f
any pair)
  • P
a rticula rly useful: { x: 9a. a
  • (a
! Int) { (snd(x))(fst(x)) W
  • lfgang
Schreiner 17
slide-19
SLIDE 19 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Info rmation Hiding
  • Abstract
t yp es: { Unkno wn t yp e rep resentation. { P ack aged with
  • p
erations that ma y b e applied to t yp e.
  • Another
example: { x: 9a. fconst: a,
  • p:
a ! Intg { x.op(x.const)
  • Restrict
use
  • f
abstract t yp es: { Simplify t yp e checking. { value p: 9a. a
  • (a
! Int) = pack[a = Int in a
  • (a
! Int)](3, succ) { V alue p is a pack age { T yp e a
  • (a
! Int) is the interface. { Binding a=Int is the t yp e rep resentation.
  • General
fo rm: { pack [a = t yp erep in interface](contents) W
  • lfgang
Schreiner 18
slide-20
SLIDE 20 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Use
  • f
P ack ages
  • P
ack age must b e
  • p
ened b efo re use: { value p = pack[a = Int in a
  • (a
! Int)](3, succ)
  • p
en p as x in (snd(x))(fst(x)) { value p = pack[a = Int in fa rg: a,
  • p:
a ! Intg](3, succ)
  • p
en p as x in x.op(x.a rg)
  • Reference
to hidden t yp e: {
  • p
en p as x[b] in . . . fun(y:b) (snd(x))(y) . . . W
  • lfgang
Schreiner 19
slide-21
SLIDE 21 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism P ack ages and Abstract Data T yp es
  • Mo
deling
  • f
Ada t yp e system: { Reco rds with function comp
  • nents
mo del Ada pack ages. { Existential quantication mo dels Ada t yp e abstraction. t yp e P
  • int
= Real
  • Real
t yp e P
  • int1
= fmak ep
  • int:
(Real
  • Real)
! P
  • int,
x co
  • rd:
P
  • int
  • Real,
y co
  • rd:
P
  • int
  • Realg
value p
  • int1:
P
  • int1
= fmak ep
  • int
= fun(x:Real, y:Real)(x, y), x co
  • rd
= fun(p:P
  • int)
fst(p), y co
  • rd
= fun(p:P
  • int)
snd(p)g W
  • lfgang
Schreiner 20
slide-22
SLIDE 22 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Ada P ack ages pack age p
  • int1
is function mak ep
  • int(x:
Real, y: Real) return P
  • int;
function x co
  • rd(P:
P
  • int)
return Real; function y co
  • rd(P:
P
  • int)
return Real; end p
  • int1;
pack age b
  • dy
p
  • int1
is function mak ep
  • int(x:
Real, y: Real) return P
  • int;
  • implementation
  • f
mak ep
  • int
function x co
  • rd(P:
P
  • int)
return Real;
  • implementation
  • f
x co
  • rd
function y co
  • rd(P:
P
  • int)
return Real;
  • implementation
  • f
y co
  • rd
end p
  • int1;
P ack age sp ecication and b
  • dy
a re pa rt
  • f
value sp ecication! W
  • lfgang
Schreiner 21
slide-23
SLIDE 23 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Hidden Data Structures
  • Ada:
pack age b
  • dy
lo calp
  • int
is p
  • int:
P
  • int;
p ro cedure mak eP
  • int(x,
y: Real); . . . function x co
  • rd
return Real; . . . function y co
  • rd
return Real; . . . end lo calp
  • int
  • F
un: value lo calp
  • int
= let p: P
  • int
= ref((0,0)) in fmak ep
  • int
= fun(x: Real, y: Real) p := (x, y), x co
  • rd
= fun() fst(p) y co
  • rd
= fun() snd(p)g
  • First-o
rder info rmation hiding: { Use let construct to restrict scoping at value level (hide reco rd comp
  • nents).
W
  • lfgang
Schreiner 22
slide-24
SLIDE 24 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Hidden Data T yp es
  • Second-o
rder info rmation hiding: { Use existential quantication to restrict scoping at t yp e level (hide t yp e rep resentation). pack age p
  • int2
t yp e P
  • int
is p rivate; function mak ep
  • int(x:
Real, y: Real) return P
  • int;
. . . p rivate
  • hidden
lo cal denition
  • f
t yp e P
  • int
end p
  • int2;
t yp e P
  • int2
= 9P
  • int.
fmak ep
  • int:
(Real
  • Real)
! P
  • int,
. . . g t yp e P
  • int2WRT[P
  • int]
= fmak ep
  • int:
(Real
  • Real)
! P
  • int,
. . . g value p
  • int2:
P
  • int2
= pack[P
  • int
= (Real
  • Real)
in P
  • int2WRT[P
  • int]]
p
  • int1
W
  • lfgang
Schreiner 23
slide-25
SLIDE 25 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Combini ng Universal and Existential Quantication
  • Combination
{ Universal quantication: generic t yp es. { Existential quantication: abstract data t yp es. { Combination: pa rametric data abstractions. nil: 8a. List[a] cons: 8a. (a
  • List[a])
! List[a] hd: 8a. List[a] ! a tl: 8a. List[a] ! List[a] Null: 8a. List[a] ! Bo
  • l
a rra y: 8a. Int ! Arra y[a] index: 8a. (Arra y[a]
  • Int)
! a up date: 8a. (Arra y[a]
  • Int
  • a)
! Unit W
  • lfgang
Schreiner 24
slide-26
SLIDE 26 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Concrete Stacks t yp e IntListStack = fempt yStack: List[Int], push: (Int
  • List[Int])
! List[Int] p
  • p:
List[Int]
  • List[Int],
top:List[Int] ! Intg value intListStack: IntListStack = fempt yStack = nil[Int], push = fun(a: Int, s: List[Int]) cons[Int](a,s), p
  • p
= fun(s: List[Int]) tl[Int](s) top = fun(s: List[Int]) hd[Int](s)g t yp e IntArra yStack = fempt yStack: (Arra y[Int]
  • Int),
push: (Int
  • (Arra
y[Int]
  • Int))
! (Arra y[Int]
  • Int),
p
  • p:
(Arra y[Int]
  • Int)
  • (Arra
y[Int]
  • Int),
top:(Arra y[Int]
  • Int)
! Intg value intArra yStack: IntArra yStack = fempt yStack = (Arra y[Int](100),
  • 1)
. . . g W
  • lfgang
Schreiner 25
slide-27
SLIDE 27 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Generic Element T yp es t yp e GenericListStack = 8Item. fempt yStack: List[Item], push: (Item
  • List[Item])
! List[Item] p
  • p:
List[Item]
  • List[Item],
top: List[Item] ! Itemg value genericListStack: GenericListStack = all[Item] fempt yStack = nil[Item], push = fun(a: Item, s: List[Item]) cons[Item](a,s), p
  • p
= fun(s: List[Item]) tl[Item](s) top = fun(s: List[Item]) hd[Item](s)g t yp e GenericArra yStack = . . . value genericArra yStack: GenericArra yStack = . . . W
  • lfgang
Schreiner 26
slide-28
SLIDE 28 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Hiding the Rep resentation t yp e GenericStack = 8Item. 9Stack. GenericStackWRT[Item][Stack] t yp e GenericStackWRT[Item][Stack] = fempt yStack: List[Item], push: (Item
  • List[Item])
! List[Item] p
  • p:
List[Item]
  • List[Item],
top: List[Item] ! Itemg value listStackP ack age: GenericStack = all[Item] pack[Stack = List[Item] in GenericStackWRT[Item][Stack]] genericListStack[Item] value useStack = fun(stackP ack age: GenericStack)
  • p
en stackP ack age[Int] as p[stackRep] in p.top(p.push(3, p.empt ystack)) useStack(listStackP ack age) W
  • lfgang
Schreiner 27
slide-29
SLIDE 29 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Another Example t yp e P
  • int
= 9P
  • intRep.
fmkP
  • int:
(Real
  • Real)
! P
  • intRep,
x-co
  • rd:
P
  • intRep
! Real, y-co
  • rd:
P
  • intRep
! Realg t yp e P
  • intWRT[P
  • intRep]
= fmkP
  • int:
(Real
  • Real)
! P
  • intRep,
x-co
  • rd:
P
  • intRep
! Real, y-co
  • rd:
P
  • intRep
! Realg t yp e P
  • int
= 9P
  • intRep.
P
  • intWRT[P
  • intRep]
value ca rtesianP
  • intOps
= fmkp
  • int
= fun(x: Real, y: Real) (x,y), x-co
  • rd
= fun(p: Real
  • Real)
fst(p), y-co
  • rd
= fun(p: Real
  • Real)
snd(p)g value ca rtesianP
  • intP
ack age: P
  • int
= pack[P
  • intRep
= Real
  • Real
in P
  • intWRT[P
  • intRep]]
ca rtesianP
  • intOps
W
  • lfgang
Schreiner 28
slide-30
SLIDE 30 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Quantication and Mo dules
  • Mo
dules { Abstract data t yp e pack aged with
  • p
erato rs. { Can imp
  • rt
  • ther
(kno wn ) mo dules. { Can b e pa rameterized with (unkno wn ) mo dules.
  • P
a rametric mo dules { F unctions
  • ver
existential t yp es. W
  • lfgang
Schreiner 29
slide-31
SLIDE 31 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism P a rametric Mo dules t yp e ExtendedP
  • intWRT[P
  • intRep]
= P
  • intWRT[P
  • intRep]
& fadd: (P
  • intRep
  • P
  • intRep)
! P
  • intRepg
t yp e ExtendedP
  • int
= 9P
  • intRep.
ExtendedP
  • intWRT[P
  • intRep]
value extendP
  • intP
ack age = fun(p
  • intP
ack age: P
  • int)
  • p
en p
  • intP
ack age as p[P
  • intRep]
in pack[P
  • intRep'
= P
  • intRep
in ExtendedP
  • intWRT[P
  • intRep']]
p & fadd = fun(a: P
  • intRep,
b: P
  • intRep)
p.mkp
  • int(p.x-co
  • rd(a)+p.x-co
  • rd(b),
p.y-co
  • rd(a)+p.x-co
  • rd(b))g
value extendedCa rtesianP
  • intP
ack age = extendP
  • intP
ack age(ca rtesianP
  • intP
ack age) W
  • lfgang
Schreiner 30
slide-32
SLIDE 32 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism A Circle P ack age t yp e CircleWRT2[CircleRep, P
  • intRep]
= fp
  • intP
ack age: P
  • intWRT[P
  • intRep],
mk circle: (P
  • intRep
  • Real)
! CircleRep, center: CircleRep ! P
  • intRep,
. . . g t yp e CircleWRT1[P
  • intRep]
= 9CircleRep. CircleWRT2[CircleRep, P
  • intRep]
t yp e Circle = 9P
  • intRep.
CircleWRT1[P
  • intRep]
value circleMo dule: CircleMo dule = all[P
  • intRep]
fun(p: P
  • intWRT[P
  • intRep])
pack[CircleRep = P
  • intRep
  • Real
in CircleWRT2[CircleRep,P
  • intRep]]
fp
  • intP
ack age = p, mk circle = fun(m: P
  • intRep,
r: Real)(m, r) . . . g value ca rtesianCircleP ack age =
  • p
enCa rtesianP
  • intP
ack age as p[Rep] in pack[P
  • intRep
= Rep in CircleWRT1[P
  • intRep]]
circleMo dule[Rep](p)
  • p
en ca rtesian CircleP ack age as c[P
  • intRep][CircleRep]
in . . . c.mk circle(c.p
  • intP
ack age.mkp
  • int(3,
4), 5) . . . W
  • lfgang
Schreiner 31
slide-33
SLIDE 33 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism A Rectangle P ack age t yp e RectWRT2[RectRep, P
  • intRep]
= fp
  • intP
ack age: P
  • intWRT[P
  • intRep],
mkrect: (P
  • intRep
  • P
  • intRep)
! RectRep, . . . g t yp e RectWRT1[P
  • intRep]
= 9RectRep. RectWRT2[RectRep, P
  • intRep]
t yp e Rect = 9P
  • intRep.
RectWRT1[P
  • intRep]
t yp e RectMo dule = 8P
  • intRep.
P
  • intWRT[P
  • intRep]
! RectWRT1[P
  • intRep]
value rectMo dule: RectMo dule = all[P
  • intRep]
fun(p: P
  • intWRT[P
  • intRep])
pack[P
  • intRep'
= P
  • intRep
in RectWRT1[P
  • intRep']]
fp
  • intP
ack age = p, mkrect = fun(tl: P
  • intRep,
b r: P
  • intRep)
. . . g W
  • lfgang
Schreiner 32
slide-34
SLIDE 34 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism A Figures P ack age t yp e FiguresWRT3[RectRep, CircleRep, P
  • intRep]
  • fcircleP
ack age: CircleWRT[CircleRep, P
  • intRep],
rectP ack age: RectWRT[RectRep, P
  • intRep],
b
  • undingRect:
CircleRep ! RectRepg t yp e FIguresWRT1[P
  • intRep]
= 9RectRep. 9CircleRep. FigureWRT3[RectRep, CircleRep, P
  • intRep]
t yp e Figures = 9P
  • intRep.
FIgureWRT1[P
  • intRep]
t yp e FiguresMo dule = 8P
  • intRep.
P
  • intWRT[P
  • intRep]
! FiguresWRT1[P
  • intRep]
value guresMo dule: FIguresMo dule = all[P
  • intRep]
fun(p: P
  • intWRT[P
  • intRep])
pack[P
  • intRep'
= P
  • intRep
in FiguresWRT1[P
  • intRep]]
  • p
en circleMo dule[P
  • intRep](p)
as c[CircleRep] in
  • p
en rectMo dule[P
  • intRep](p)
as r[RectRep] in fcircleP ack age = c, . . . g W
  • lfgang
Schreiner 33
slide-35
SLIDE 35 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Bounded Quantication
  • T
yp e inclusion: { T yp e A is included in (a subt yp e
  • f
) t yp e B when all values
  • f
A a re also values
  • f
B . { Inclusion relation
  • n
sub ranges, reco rds, va riants, function, universally and existentially quantied t yp es.
  • Integer
sub range t yp e n : : : m { n : : : m
  • n
m i n
  • n
and m
  • m
{ value f = fun(x: 2..5) x+1 f: 2..5 ! 3..6 f(3) value g = fun(y: 3..4) f(y)
  • F
unction t yp e { s ! t
  • s
! t i s
  • s
and t
  • t
{ F unction
  • f
t yp e 3 . . . 7 can b e also considered as function
  • f
t yp e 4 . . . 6 !6 . . . 10 W
  • lfgang
Schreiner 34
slide-36
SLIDE 36 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Reco rd T yp es and Inheritance
  • Reco
rd t yp e: { fa 1 : t1; : : : ; a n : t n ; : : : ; a m : t m g
  • fa
1 : u 1 ,. . . ,a n : u n g i t i
  • u
i (fo r i 2 1 . . . n) { A is subt yp e
  • f
B if A has all the elds
  • f
B (p
  • ssibly
mo re) and the t yp es
  • f
the common elds a re subt yp es.
  • Concept
  • f
inheritance (sub classes ) { Reco rds ma y have functional comp
  • nents.
{ Class instance is reco rd with functions and lo cal va riables. { Sub class instance is reco rd with at least those functions and va riables.
  • V
a riant t yp es: { [a 1 : t1; : : : ; a n : t n ]
  • [a
1 : u 1 ; : : : ; a n : u n ; : : : ; a m : u m ] i t i
  • u
i (fo r i 2 1 . . . n) W
  • lfgang
Schreiner 35
slide-37
SLIDE 37 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Bounded Quantication 'n Subt yping
  • Mix
subt yping and p
  • lymo
rphism. { value f = fun(x: fone: Intg) x.one f (fone = 3, t w
  • =
trueg) { value f = all[a] fun(x: fone: ag) x.one f[Int](fone = 3, t w
  • =
trueg)
  • Constraint
all[a T] e { value g = all[a
  • fone:
Intg] fun(x: a) x.one g [fone:Int, t w
  • :Bo
  • lg](fone=3,
t w
  • =trueg)
  • Tw
  • fo
rms
  • f
inclusion constraints: { In f , implicit b y function pa rameters. { In g , explicit b y b
  • unded
quantication.
  • T
yp e exp ressions: { g : 8a
  • fone:
Intg. a ! Int
  • T
yp e abstraction: { value g = all[b] all[afone: bg] fun(x:a)x:one g[Int][(fone:In t,t w
  • :Bo
  • lg)]
(fone=3,. . . g) W
  • lfgang
Schreiner 36
slide-38
SLIDE 38 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Object Oriented Programming t yp e P
  • int
= fx: Int, y: Intg value moveX = fun(p: P
  • int,
dx: Int) p.x := p.x + dx; p value moveX = all[P
  • P
  • int]
fun(p:P , dx: Int) p.x := p.x + dx; p t yp e Tile = fx: Int, y: Int, ho r: Int, ver: Intg moveX[Tile](fx = 0, y = 0, ho r
  • 1,
ver = 1g, 1).ho r
  • Result
  • f
moveX is same as a rgument t yp e.
  • moveX
can b e applied to
  • bjects
  • f
(y et) unkno wn t yp e. W
  • lfgang
Schreiner 37
slide-39
SLIDE 39 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Bounded Existential Quantication and P a rtial Abstraction
  • Bounding
existential quantiers: { 9a t. t' { 9a. t := 9a T
  • p.
t
  • P
a rtially abstract t yp es: { a is abstract. { W e kno w a is subt yp e
  • f
t. { a is not mo re abstract than t is.
  • Mo
died packing construct: { pack [a
  • t
= t' in t"] e W
  • lfgang
Schreiner 38
slide-40
SLIDE 40 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism P
  • ints
and Tiles t yp e Tile = 9P . 9T
  • P
. TileWRT2[P , T] t yp e TileWRT2[P , T] = fmktile: (Int
  • Int
  • Int
  • Int)
! T,
  • rigin:
T ! P , ho r: T ! Int, ver: T ! Intg t yp e TileWRT[P] = 9T
  • P
. TileWRT2[P , T] t yp e Tile = 9P . TileWRT[P] t yp e P
  • intRep
= fx: Int, y: Intg t yp e TileRep = fx: Int, y: Int, ho r: Int, ver: Intg W
  • lfgang
Schreiner 39
slide-41
SLIDE 41 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism P
  • ints
and Tiles pack [P = P
  • intRep
in TileWRT[P]] pack [T
  • P
  • intRep
= TileRep in TileWRT2[P , T]] fmktile = fun(x:Int, y: Int, ho r: Int, ver: Int) fx=x, y-y , ho r=ho r, ver=verg,
  • rigin
= fun(t: TileRep) t, ho r = fun(t: TileRep) t.ho r, ver = fun(t: TileRep) t.verg fun(tileP ack: Tile)
  • p
en tileP ack as t[p
  • intRep][tileRep]
let f = fun(p: p
  • intRep)
. . . in f(t.tile(0, 0, 1, 1)) W
  • lfgang
Schreiner 40
slide-42
SLIDE 42 On Understanding T yp es, Data Abstraction, and P
  • lymo
rphism Summa ry
  • Three
main p rinciples { Universal t yp e quantication (p
  • lymo
rphism). { Existential t yp e quantication (abstraction). { Bounded t yp e quantication (subt yping).
  • Resulting
p rograms ma y b e statically t yp e- check ed. { Bottom-construction
  • f
t yp es. { Mo re sophisticated t yp e inference p
  • ssible
(ML).
  • Mo
re general t yp e systems. { T yp e-checking t ypically not decidable any mo re. { Dep endent t yp es (Ma rtin-L
  • f
). { Calculus
  • f
constructions (Co quand and Huet).. W
  • lfgang
Schreiner 41