on understanding t yp es data abstraction and p olymo
play

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


  1. 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 Kepler Universit y , A-4040 Linz, Austria W olfgang.Schreiner@risc.un i-linz.ac.at http://www.risc.uni-linz.ac.at/p eople/schrein e W olfgang Schreiner RISC-Linz

  2. On Understanding T yp es, Data Abstraction, and P olymo rphism F rom Unt yp ed to T yp ed Universes � Unt yp ed universes. { Only one t yp e fo r all objects: { Bit strings in computer memo ry , { S-exp ressions in pure LISP , { � -exp ressions in the � -calculus, { Sets in set theo ry . � Organization of objects: { Classi�cation of 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 olean o r of cha racter and machine op eration? W olfgang Schreiner 1

  3. On Understanding T yp es, Data Abstraction, and P olymo rphism Static and Strong T yping � T yp es imp ose constraints to enfo rce co r- rectness. { Protection of underlying (unt yp ed) rep resentation. { Constrains interaction of objects. � Static t yp e structure of p rograms. { Little o r no t yp e info rmation given explicit y . { T yp es asso ciated to constants/function symb ols. { T yp e inference system infers t yp es of 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 olfgang Schreiner 2

  4. On Understanding T yp es, Data Abstraction, and P olymo rphism Kinds of P olym o rphis m � Monomo rphic languages: { All functions and p ro cedures have unique t yp e. { All values and va riables of one and only t yp e. { P ascal-lik e t yp e systems. � P olymo rphic languages: { V alues and va riables ma y have mo re than one t yp e. { P olymo rphic functions have op eratnds of mo re than one t yp e. { P olymo rphic t yp es have op erations applicable to op erands of mo re than one t yp e. � Universal p olymo rphism: { F unction w o rks unifo rmly on range of t yp es. { P a rametric and inclusion p olymo rphism. � Ad-ho c p olymo rphism: { F unction w o rks on several unrelated t yp es. { Overlading and co ercion . W olfgang Schreiner 3

  5. On Understanding T yp es, Data Abstraction, and P olymo rphism Universal P olymo rphism � P a rametric p olymo rphism: { Unifo rmit y achieved b y t yp e pa rameters . { Determines a rgument t yp e fo r each application of p oly- mo rphic function. { ML-lik e t yp e systems. � Inclusion p olymo 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 o rk is done fo r a rguments of many t yp es. { Length function over lists. W olfgang Schreiner 4

  6. On Understanding T yp es, Data Abstraction, and P olymo rphism Ad-ho c P olymo rphis m � Overloading { Same name denotes di�erent functions. { Context decides which function is denoted b y pa rticula r o ccurence of a name. { Prep ro cessing ma y eliminate overloading b y giving di�er- ent names to di�erent 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 olymo rphism { Op erato rs/functions only have one t yp e. { Only syntax \p retends" p olymo rphism. W olfgang Schreiner 5

  7. On Understanding T yp es, Data Abstraction, and P olymo rphism Overloading and Co ercion � Distinction ma y b e blurred: 3 + 4 3.0 + 4 3 + 4.0 3.0 + 4.0 � Di�erent explanations p ossible: { + has four overloaded meanings. { + has t w o overloaded 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 o r b oth! F o r histo ry of t yp e evolution see [Ca rdelli and W egner, 1985]. W olfgang Schreiner 6

  8. On Understanding T yp es, Data Abstraction, and P olymo rphism Preview of 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 olymo r- phism and object-o riented languages. � First-o rder t yp es { Bo ol, Int, Real, String. � V a rious fo rms of t yp e quanti�ers { T yp e ::= . . . j Quanti�ed T yp e { Quanti�ed T yp e ::= 8 A. T yp e j 9 A. T yp e j 8 A � T yp e. T yp e j 9 A � T yp e. T yp e � Mo deling of advanced t yp e systems: { Universal quanti�cation: pa rameterized t yp es. { Existential quanti�ers: abstract data t yp es. { Bounded quanti�cation: t yp e inheritance. W olfgang Schreiner 7

  9. On Understanding T yp es, Data Abstraction, and P olymo rphism The Unt yp ed � -Calculus � Exp ressions: { e ::= x { e ::= fun(x) e { e ::= e(e) � Intro duction of names { value id = fun(x) x { value succ = fun(x) x+1 { value t wice = fun(f ) fun(y) f(f(y)) W olfgang Schreiner 8

  10. On Understanding T yp es, Data Abstraction, and P olymo rphism The T yp ed � -Calculus � Extension of 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 o 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 olfgang Schreiner 9

  11. On Understanding T yp es, Data Abstraction, and P olymo rphism Basic and Structured T yp es � Basic t yp es: { Unit (trivial t yp e, only element ()) { Bo ol (with if-then-else) { Int (with a rithmetic and compa rison) { Real (with a rithmetic and compa rison) { String (with in�x 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 ol = 3, true fst(p), snd(p) W olfgang Schreiner 10

  12. On Understanding T yp es, Data Abstraction, and P olymo rphism Reco rd T yp es � Example: { t yp e ARec = f a: Int, b: Bo ol g value r: ARec = f a = 3, b = true g r.b � F unctions as comp onents: { t yp e F unRec = f f1: Int ! Int, f2: Real ! Real g value funRec: F unRec = f f1 = succ, f2 = sin g � Concatenation of reco rd t yp es: { t yp e NewRec = F unRec & f f3: Bo ol ! Bo ol g � 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 olfgang Schreiner 11

  13. On Understanding T yp es, Data Abstraction, and P olymo 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 of [a = anInt] \int" [b = aString] \string" ^ aString otherwise \erro r" � Recursive function de�nitions { rec value fact = fun(n: Int) if n=0 then 1 else n*fact(n-1) � Recursive t yp e de�nitions { rec t yp e IntList = [nil: Unit cons: f head: Int, tail: IntList g ] W olfgang Schreiner 12

Recommend


More recommend