Abstraction Markus Roggenbach 4. März 2004
Preliminary remark
Preliminary remark 2 Aspects of Programming Languages Value Expression State(transformation) Command Context(transformation) Declaration
Preliminary remark 3 Idea behind abstraction Parametrise expression function abstraction
Preliminary remark 3 Idea behind abstraction Parametrise expression function abstraction command procedure abstraction
Preliminary remark 3 Idea behind abstraction Parametrise expression function abstraction command procedure abstraction declaration generic abstraction
Preliminary remark 4 Motivations language extension
Preliminary remark 4 Motivations language extension structuring (in the small)
Preliminary remark 4 Motivations language extension structuring (in the small) distinguish: effect versus implementation
Preliminary remark 5 Abstractions I: Function abstraction A Function abstraction abstracts of an expression. function F (FP 1;...; FP n): T = B body: (yields) expression. function call (yields a value): expression.
Abstractions I: Function abstraction 6 Example function power (x:real;n:integer):real; begin if n=1 then power:=x else power:=x*power(x,n-1) end
Abstractions I: Function abstraction 7 Properties of function abstractions A function abstraction is side-effect free, if it does not change the global state of a program.
Abstractions I: Function abstraction 7 Properties of function abstractions A function abstraction is side-effect free, if it does not change the global state of a program. referentially transparent, if its value (with identical actual parameters) is independent of the program context.
Abstractions I: Function abstraction 8 Pascal An example Haskell var x: Char; x = newioref::io(ioref Char) hugo:: Int -> IOInt function hugo (y:int): Int; hugo (y::int) = begin x:= c ; writeioref x c >> hugo:= 2 * y return 2 * y end; erna :: Int -> Int erna (y::int) = 2 * y Standard function of IORef: >> :: IO alpha -> IO beta -> IO beta
Preliminary remark 9 Abstractions II: procedure abstraction A procedure abstraction abstracts of a command. procedure P (FP 1;... ; FP n) = B Body: Command Procedure call (changes the state): Command.
Abstractions II: procedure abstraction 10 Example in Ada procedure swap (a: in out integer, b: in out integer) is temp : integer begin temp:=a; a := b; b := temp end swap
Preliminary remark 11 Abstractions III: Generic Abstraction A generic abstraction abstracts of a declaration. generic FP 1;... ; FP n package P is B Body: Declaration. generic instantiation (yields context): Declaration.
Abstractions III: Generic Abstraction 12 Example in Ada generic capacity: in Positive; package queue_class is procedure append (newitem: in Character); procedure remove (olditem: out Character); end queue_class; package body queue_class is items: array (1..capacity) of Character;... end queue_class; package line_buffer is new queue_class (120);
Preliminary remark 13 Abstraction in general to abstract = to forget about
Preliminary remark 13 Abstraction in general to abstract = to forget about in programming languages: 1. (parametrised) combination of elements of one aspect to a new element of this aspect. 2. Binding of the abstraction (to a name).
Abstraction in general 14 Abstractions principle It is possible to construct abstractions over any syntactic class.
Abstraction in general 14 Abstractions principle It is possible to construct abstractions over any syntactic class. (provided only that the phrases of that class specify some kind of computation. )
Abstraction in general 14 Abstractions principle It is possible to construct abstractions over any syntactic class. (provided only that the phrases of that class specify some kind of computation. ) Example: variable access.
Preliminary remark 15 Parameter mechanism declaration : function F (FP 1;...;FP n) T = B call: F (AP 1;...;AP n) declaration : procedure P (FP 1;...;FP n) = B call: P (AP 1;...;AP n) declaration : generic FP 1;...;FP n package P is B call: package X is new P(AP 1;...;AP n) FP i: formal parameter AP i: actual parameter
Parameter mechanism 16 Assignment: actual formal using positions: AP i is assigned to FP i.
Parameter mechanism 16 Assignment: actual formal using positions: AP i is assigned to FP i. using names: names of the formal parameters.
Parameter mechanism 16 Assignment: actual formal using positions: AP i is assigned to FP i. using names: names of the formal parameters. function ComputePay (Income: Flaot; TaxRate: Float) return Float; pay := ComputePay (TaxRate => 0.15, Income => 20000.0)
Parameter mechanism 17 Mode versus implementation Mode: in out in-out
Parameter mechanism 17 Mode versus implementation Mode: in out in-out Implementation: copy mechanism definitional mechanism
Parameter mechanism 18 Copy mechanism assignment: AP:=FP.
Parameter mechanism 18 Copy mechanism assignment: AP:=FP. Mode actual parameter entry exit in expression FP:=AP out variable AP:=FP in-out variable FP:=AP AP:=FP FP is a local variable of the abstraction.
Parameter mechanism 19 value: in result: out value result: in-out Example in Pseudo-Pascal type Vector= array [1..n] of Real; procedure add (value v,w: Vector; result: sum: Vector) var i: 1..n; begin for i:=1 to n do sum[i]:=v[i]+w[i] end;
Parameter mechanism 20 procedure normalise (value result u:vector) var i: 1..n; s: Real begin s:= 0.0 for i:=1 to n do s:= s + sqr(u[i]); s:= sqr(s); for i:=1 to n do u[i] := u[i] / s; end;
Parameter mechanism 21 Definitional mechanism Binding of the FPs to the APs.
Parameter mechanism 21 constant parameter: value Definitional mechanism Binding of the FPs to the APs.
Parameter mechanism 21 Definitional mechanism Binding of the FPs to the APs. constant parameter: value variable parameter: reference to a variable
Parameter mechanism 21 Definitional mechanism Binding of the FPs to the APs. constant parameter: value variable parameter: reference to a variable procedural parameter: procedure abstraction
Parameter mechanism 21 Definitional mechanism Binding of the FPs to the APs. constant parameter: value variable parameter: reference to a variable procedural parameter: procedure abstraction functional parameter: function abstraction (the only mechanism available in ML)
Parameter mechanism 22 const: in var: in-out Example in Pseudo-Pascal type Vector= array [1..n] of Real; procedure add (const v,w: Vector; var: sum: Vector) var i: 1..n; begin for i:=1 to n do sum[i]:=v[i]+w[i] end;
Parameter mechanism 23 procedure normalise (var u:vector) var i: 1..n; s: Real begin s:= 0.0 for i:=1 to n do s:= s + sqr(u[i]); s:= sqr(s); for i:=1 to n do u[i] := u[i] / s; end;
Parameter mechanism 24 The correspondence principle For each form of declaration there exists a corresponding parameter mechanism, and vice-versa.
Parameter mechanism 25 Be careful with out/in-out parameters procedure foolish (out x, out y) x:=7; y:=9 end foolish(p,p)
Parameter mechanism 26 Alias problems with variable parameters direct problem: (in C) void fun (int *first, int *second) fun (&total, &total)
Parameter mechanism 26 Alias problems with variable parameters direct problem: (in C) void fun (int *first, int *second) fun (&total, &total) undecidable problem: (in C) void fun (int *first, int *second) fun (&list[i], &list[j])
Parameter mechanism 27 Formal parameter and non-local variable: var global: integer; procedure hugo(var local: integer); begin... end; begin hugo(global);... end
Parameter mechanism 28 Copy versus definitional mechanism criterion copy def. add. memory x time to copy x access fast slow types with assignment all security in concurrent progr. yes no
Parameter mechanism 29 Parameter mechanisms in programing languages language in out in-out in-out copy definitional call by value call by reference C x (pointer) Pascal x (var) Modula-2 x (var) Java x (object param.) Fortran: in-out Modus
Parameter mechanism 30 Type check Languages with type check Fortran 77, Pascal, Modula-2, Ada, Java, Fortran 90
Parameter mechanism 30 Type check Languages with type check Fortran 77, Pascal, Modula-2, Ada, Java, Fortran 90 Languages without type check C the original version ANSI-C: type check as option
Parameter mechanism 31 Type completeness principle No operation should be restricted in the types of its operands. Results of Function abstractions:
Parameter mechanism 31 Type completeness principle No operation should be restricted in the types of its operands. Results of Function abstractions: Fortran 77, Pascal, Modula-2: simple types.
Parameter mechanism 31 Type completeness principle No operation should be restricted in the types of its operands. Results of Function abstractions: Fortran 77, Pascal, Modula-2: simple types. C: all types, with the exception of array and function.
Parameter mechanism 31 Type completeness principle No operation should be restricted in the types of its operands. Results of Function abstractions: Fortran 77, Pascal, Modula-2: simple types. C: all types, with the exception of array and function. Ada: all types
Parameter mechanism 32 Application order strict (applicative, eager): 1) evaluation at call v 2) binding: F P v.
Parameter mechanism 32 Application order strict (applicative, eager): 1) evaluation at call v 2) binding: F P v. normal order: 1) binding: F P AP. 2) evaluation at each use of F P.
Parameter mechanism 33 lazy: 1) binding: F P AP. 2) evaluation at first use of F P : v 3) binding: F P v.
Parameter mechanism 34 Examples fun sqr (n: int) = n * n sqr(p+q) fun cand (b1:bool, b2:bool) = if b1 then b2 else false cand( n > 0, t/0 > 1)