Abstraction 1/ 45 Abstraction the interactions of difgerent - - PowerPoint PPT Presentation

abstraction
SMART_READER_LITE
LIVE PREVIEW

Abstraction 1/ 45 Abstraction the interactions of difgerent - - PowerPoint PPT Presentation

Abstraction 1/ 45 Abstraction the interactions of difgerent components can be simplifjed by hiding the details of each components implementation from the rest of the system. protecting it with an interface . system is invariant to changes


slide-1
SLIDE 1

Abstraction

1/ 45

slide-2
SLIDE 2

Abstraction

▶ When faced with creating and maintaining a complex system,

the interactions of difgerent components can be simplifjed by hiding the details of each component’s implementation from the rest of the system.

▶ Details of a component’s implementation are hidden by

protecting it with an interface.

▶ Abstraction is maintained by ensuring that the rest of the

system is invariant to changes of implementation that do not afgect the interface.

2/ 45

slide-3
SLIDE 3

Modules: structures

module I n t S e t = s t r u c t type t = i n t l i s t l e t empty = [ ] l e t is_empty = f u n c t i o n | [ ] −> true | _ −> f a l s e l e t equal_member ( x : i n t ) ( y : i n t ) = x = y l e t rec mem x = f u n c t i o n | [ ] −> f a l s e | y : : r e s t −>

3/ 45

slide-4
SLIDE 4

Modules: structures

i f ( equal_member x y ) then true e l s e mem x r e s t l e t add x t = i f (mem x t ) then t e l s e x : : t l e t rec remove x = f u n c t i o n | [ ] −> [ ] | y : : r e s t −> i f ( equal_member x y ) then r e s t e l s e y : : ( remove x r e s t ) l e t t o _ l i s t t = t end

4/ 45

slide-5
SLIDE 5

Modules: structures

l e t

  • ne_two_three

: I n t S e t . t = I n t S e t . add 1 ( I n t S e t . add 2 ( I n t S e t . add 3 I n t S e t . empty ))

5/ 45

slide-6
SLIDE 6

Modules: structures

  • pen

I n t S e t l e t

  • ne_two_three

: t = add 1 ( add 2 ( add 3 empty ))

6/ 45

slide-7
SLIDE 7

Modules: structures

l e t

  • ne_two_three

: I n t S e t . t = I n t S e t . ( add 1 ( add 2 ( add 3 empty ) ) )

7/ 45

slide-8
SLIDE 8

Modules: structures

module I n t S e t P l u s = s t r u c t i n c l u d e I n t S e t l e t s i n g l e t o n x = add x empty end

8/ 45

slide-9
SLIDE 9

Modules: signatures

s i g type t = i n t l i s t v a l empty : ’ a l i s t v a l is_empty : ’ a l i s t −> bool v a l equal_member : i n t −> i n t −> bool v a l mem : i n t −> i n t l i s t −> bool v a l add : i n t −> i n t l i s t −> i n t l i s t v a l remove : i n t −> i n t l i s t −> i n t l i s t v a l t o _ l i s t : ’ a −> ’ a end

9/ 45

slide-10
SLIDE 10

Modules: signatures

module I n t S e t : s i g type t = i n t l i s t v a l empty : i n t l i s t v a l is_empty : i n t l i s t −> bool v a l mem : i n t −> i n t l i s t −> bool v a l add : i n t −> i n t l i s t −> i n t l i s t v a l remove : i n t −> i n t l i s t −> i n t l i s t v a l t o _ l i s t : i n t l i s t −> i n t l i s t end = s t r u c t . . . end

10/ 45

slide-11
SLIDE 11

Modules: signatures

module type IntSetS = s i g type t = i n t l i s t v a l empty : i n t l i s t v a l is_empty : i n t l i s t −> bool v a l mem : i n t −> i n t l i s t −> bool v a l add : i n t −> i n t l i s t −> i n t l i s t v a l remove : i n t −> i n t l i s t −> i n t l i s t v a l t o _ l i s t : i n t l i s t −> i n t l i s t end module I n t S e t : IntSetS = s t r u c t . . . end

11/ 45

slide-12
SLIDE 12

Modules: abstract types

l e t p r i n t _ s e t ( s : I n t S e t . t ) : u n i t = l e t rec loop = f u n c t i o n | x : : xs −> p r i n t _ i n t x ; p r i n t _ s t r i n g ” ” ; loop xs | [ ] −> () in p r i n t _ s t r i n g ”{ ” ; loop s ; p r i n t _ s t r i n g ”}”

12/ 45

slide-13
SLIDE 13

Modules: abstract types

module type IntSetS : s i g type t v a l empty : t v a l is_empty : t −> bool v a l mem : i n t −> t −> bool v a l add : i n t −> t −> t v a l remove : i n t −> t −> t v a l t o _ l i s t : t −> i n t l i s t end module I n t S e t : IntSetS = s t r u c t . . . end

13/ 45

slide-14
SLIDE 14

Modules: abstract types

# l e t p r i n t _ s e t ( s : I n t S e t . t ) : u n i t = l e t rec loop = f u n c t i o n | x : : xs −> p r i n t _ i n t x ; p r i n t _ s t r i n g ” ” ; loop xs | [ ] −> () in p r i n t _ s t r i n g ”{ ” ; loop s ; p r i n t _ s t r i n g ” } ” ; ; Characters 172-173: loop s; ^ Error: This expression has type IntSet.t but an expression was expected of type int list

14/ 45

slide-15
SLIDE 15

Existential types

NatSetImpl = 𝜇𝛽 : : * . 𝛽 × (𝛽 → Bool ) × ( Nat → 𝛽 → Bool ) × ( Nat → 𝛽 → 𝛽) × ( Nat → 𝛽 → 𝛽) × (𝛽 → L i s t Nat ) empty = Λ𝛽 : : * . 𝜇s : NatSetImpl 𝛽 . 𝜌1 s is_empty = Λ𝛽 : : * . 𝜇s : NatSetImpl 𝛽 . 𝜌2 s mem = Λ𝛽 : : * . 𝜇s : NatSetImpl 𝛽 . 𝜌3 s add = Λ𝛽 : : * . 𝜇s : NatSetImpl 𝛽 . 𝜌4 s remove = Λ𝛽 : : * . 𝜇s : NatSetImpl 𝛽 . 𝜌5 s t o _ l i s t = Λ𝛽 : : * . 𝜇s : NatSetImpl 𝛽 . 𝜌6 s

15/ 45

slide-16
SLIDE 16

Existential types

nat_set_package = pack L i s t Nat , 〈 n i l [ Nat ] , isempty [ Nat ] , 𝜇n : Nat . f o l d [ Nat ] [ Bool ] (𝜇x : Nat . 𝜇y : Bool . or y ( equal_nat n x )) f a l s e , cons [ Nat ] , 𝜇n : Nat . f o l d [ Nat ] [ L i s t Nat ] (𝜇x : Nat . 𝜇 l : L i s t Nat i f ( equal_nat n x ) [ L i s t Nat ] l ( cons [ Nat ] x l )) ( n i l [ Nat ] ) , 𝜇 l : L i s t Nat . l 〉 as ∃𝛽 : : * . NatSetImpl 𝛽

16/ 45

slide-17
SLIDE 17

Existential types

  • pen nat_set_package as NatSet ,

nat_set

  • ne_two_three =

( add [ NatSet ] nat_set ) one (( add [ NatSet ] nat_set ) two (( add [ NatSet ] nat_set ) three ( empty [ NatSet ] nat_set ) ) )

17/ 45

slide-18
SLIDE 18

Existential types

Γ ⊢ 𝑁 ∶ 𝐵[𝛽 ∶= 𝐶] Γ ⊢ ∃𝛽∶∶𝐿.𝐵 ∶∶ ∗ ∃-intro Γ ⊢ pack 𝐶, 𝑁 as ∃𝛽∶∶𝐿.𝐵 ∶ ∃𝛽∶∶𝐿.𝐵

18/ 45

slide-19
SLIDE 19

Existential types in OCaml

Λ𝛽 : : * . 𝜇p : Bool . 𝜇x :𝛽. 𝜇y :𝛽. i f p [𝛽] x y Λ𝛽 : : * . Λ𝛾 : : * . 𝜇p : Bool . 𝜇x :𝛽. 𝜇y : 𝛾 . i f p [ ∃𝛿 . 𝛿 ] ( pack 𝛽 , x as ∃𝛿 . 𝛿 ) ( pack 𝛾 , y as ∃𝛿 . 𝛿 )

19/ 45

slide-20
SLIDE 20

Existential types in OCaml

Λ𝛽 : : * . 𝜇p : Bool . 𝜇x :𝛽. 𝜇y :𝛽. i f p [𝛽] x y Λ𝛽 : : * . Λ𝛾 : : * . 𝜇p : Bool . 𝜇x :𝛽. 𝜇y : 𝛾 . i f p [ ∃𝛿 . 𝛿 ] ( pack 𝛽 , x as ∃𝛿 . 𝛿 ) ( pack 𝛾 , y as ∃𝛿 . 𝛿 )

19/ 45

slide-21
SLIDE 21

Existential types in OCaml

fun p x y −> i f p then x e l s e y ∀𝛽 : : * . Bool → 𝛽 → 𝛽 → 𝛽 ∀𝛽 : : * . ∀𝛾 : : * . Bool → 𝛽 → 𝛾 → ∃𝛿 : : * . 𝛿

20/ 45

slide-22
SLIDE 22

Existential types in OCaml

(* ∃𝛽.𝛽 × (𝛽 → 𝛽) × (𝛽 → string) *) type t = E : ’ a * ( ’ a −> ’ a )* ( ’ a −> s t r i n g ) −> t l e t i n t s = E(0 , ( fun x −> x + 1) , s t r i n g _ o f _ i n t ) l e t f l o a t s = E ( 0 . 0 , ( fun x −> x +. 1 . 0 ) , s t r i n g _ o f _ f l o a t ) l e t E( z , s , p) = i n t s in p ( s ( s z ))

21/ 45

slide-23
SLIDE 23

Parametricity

22/ 45

slide-24
SLIDE 24

Parametricity

▶ Polymorphism allows a single piece of code to be instantiated

with multiple types.

▶ Polymorphism is parametric when all of the instances behave

uniformly.

▶ Where abstraction hides details about an implementation

from the outside world, parametricity hides details about the

  • utside world from an implementation.

23/ 45

slide-25
SLIDE 25

Modules: functors

module type Eq = s i g type t v a l equal : t −> t −> bool end module type SetS = s i g type t type e l t v a l empty : t v a l is_empty : t −> bool v a l mem : e l t −> t −> bool v a l add : e l t −> t −> t v a l remove : e l t −> t −> t v a l t o _ l i s t : t −> e l t l i s t end

24/ 45

slide-26
SLIDE 26

Modules: functors

SetS with type e l t = foo expands to s i g type t type e l t = foo v a l empty : t v a l is_empty : t −> bool v a l mem : e l t −> t −> bool v a l add : e l t −> t −> t v a l remove : e l t −> t −> t v a l t o _ l i s t : t −> e l t l i s t end

25/ 45

slide-27
SLIDE 27

Modules: functors

SetS with type e l t := foo expands to s i g type t v a l empty : t v a l is_empty : t −> bool v a l mem : foo −> t −> bool v a l add : foo −> t −> t v a l remove : foo −> t −> t v a l t o _ l i s t : t −> foo l i s t end

26/ 45

slide-28
SLIDE 28

Modules: functors

module Set (E : Eq) : SetS with type e l t := E . t = s t r u c t type t = E . t l i s t l e t empty = [ ] l e t is_empty = f u n c t i o n | [ ] −> true | _ −> f a l s e l e t rec mem x = f u n c t i o n | [ ] −> f a l s e | y : : r e s t −> i f (E . equal x y ) then true e l s e mem x r e s t

27/ 45

slide-29
SLIDE 29

Modules: functors

l e t add x t = i f (mem x t ) then t e l s e x : : t l e t rec remove x = f u n c t i o n | [ ] −> [ ] | y : : r e s t −> i f (E . equal x y ) then r e s t e l s e y : : ( remove x r e s t ) l e t t o _ l i s t t = t end

28/ 45

slide-30
SLIDE 30

Modules: functors

module IntEq = s t r u c t type t = i n t l e t equal ( x : i n t ) ( y : i n t ) = x = y end module I n t S e t = Set ( IntEq )

29/ 45

slide-31
SLIDE 31

Universal types

SetImpl = 𝜇𝛿 : : * . 𝜇𝛽 : : * . 𝛽 × (𝛽 → Bool ) × (𝛿 → 𝛽 → Bool ) × (𝛿 → 𝛽 → 𝛽) × (𝛿 → 𝛽 → 𝛽) × (𝛽 → L i s t 𝛿 ) empty = Λ𝛿 : : * . Λ𝛽 : : * . 𝜇s : SetImpl 𝛿 𝛽 . 𝜌1 s is_empty = Λ𝛿 : : * . Λ𝛽 : : * . 𝜇s : SetImpl 𝛿 𝛽 . 𝜌2 s mem = Λ𝛿 : : * . Λ𝛽 : : * . 𝜇s : SetImpl 𝛿 𝛽 . 𝜌3 s add = Λ𝛿 : : * . Λ𝛽 : : * . 𝜇s : SetImpl 𝛿 𝛽 . 𝜌4 s remove = Λ𝛿 : : * . Λ𝛽 : : * . 𝜇s : SetImpl 𝛿 𝛽 . 𝜌5 s t o _ l i s t = Λ𝛿 : : * . Λ𝛽 : : * . 𝜇s : SetImpl 𝛿 𝛽 . 𝜌6 s

30/ 45

slide-32
SLIDE 32

Universal types

EqImpl = 𝜇𝛿 : : * . 𝛿 → 𝛿 → Bool equal = Λ𝛿 : : * . 𝜇s : EqImpl 𝛿 . s

31/ 45

slide-33
SLIDE 33

Universal types

set_package = Λ𝛿 : : * . 𝜇eq : EqImpl 𝛿 . pack L i s t 𝛿 ,〈 n i l [ 𝛿 ] , isempty [ 𝛿 ] , 𝜇n : 𝛿 . f o l d [ 𝛿 ] [ Bool ] (𝜇x : 𝛿 . 𝜇y : Bool . or y ( equal [ 𝛿 ] eq n x )) f a l s e , cons [ 𝛿 ] , 𝜇n : 𝛿 . f o l d [ 𝛿 ] [ L i s t 𝛿 ] (𝜇x : 𝛿 . 𝜇 l : L i s t 𝛿 . i f ( equal [ 𝛿 ] eq n x ) [ L i s t 𝛿 ] l ( cons [ 𝛿 ] x l )) ( n i l [ 𝛿 ] ) , 𝜇 l : L i s t 𝛿 . l 〉 as ∃𝛽 : : * . SetImpl 𝛿 𝛽

32/ 45

slide-34
SLIDE 34

Universal types

Γ ⊢ 𝑁 ∶ ∀𝛽∶∶𝐿.𝐵 Γ ⊢ 𝐶 ∶∶ 𝐿 ∀-elim Γ ⊢ 𝑁 [𝐶] ∶ 𝐵[𝛽 ∶= 𝐶]

33/ 45

slide-35
SLIDE 35

Universal types in OCaml

(* ∀𝛽.𝛽 → 𝛽 *) l e t f x = x (* (∀𝛽.List 𝛽 → 𝐽𝑜𝑢) → 𝐽𝑜𝑢 *) l e t g h = h [ 1 ; 2; 3] + h [ 1 . 0 ; 2 . 0 ; 3 . 0 ] Characters 27-30: let g h = h [1; 2; 3] + h [1.0; 2.0; 3.0] ^^^ Error: This expression has type float but an expression was expected of type int

34/ 45

slide-36
SLIDE 36

Universal types in OCaml

Λ𝛽 : : * . 𝜇 f :𝛽 → I n t . 𝜇x :𝛽. 𝜇y :𝛽. plus ( f x ) ( f y ) Λ𝛽 : : * . Λ𝛾 : : * . 𝜇 f :∀𝛿 . 𝛿 → I n t . 𝜇x :𝛽. 𝜇y : 𝛾 . plus ( f [𝛽] x ) ( f [ 𝛾 ] y )

35/ 45

slide-37
SLIDE 37

Universal types in OCaml

Λ𝛽 : : * . 𝜇 f :𝛽 → I n t . 𝜇x :𝛽. 𝜇y :𝛽. plus ( f x ) ( f y ) Λ𝛽 : : * . Λ𝛾 : : * . 𝜇 f :∀𝛿 . 𝛿 → I n t . 𝜇x :𝛽. 𝜇y : 𝛾 . plus ( f [𝛽] x ) ( f [ 𝛾 ] y )

35/ 45

slide-38
SLIDE 38

Universal types in OCaml

fun f x y −> f x + f y ∀𝛽 : : * . (𝛽 → I n t ) → 𝛽 → 𝛽 → I n t ∀𝛽 : : * . ∀𝛾 : : * . (∀𝛿 : : * . 𝛿 → I n t ) → 𝛽 → 𝛾 → I n t

36/ 45

slide-39
SLIDE 39

Universal types in OCaml

(* ∀𝛽.List 𝛽 → 𝐽𝑜𝑢 *) type t = { h : ’ a . ’ a l i s t −> i n t } l e t l e n = {h = L i s t . length } (* (∀𝛽.List 𝛽 → 𝐽𝑜𝑢) → 𝐽𝑜𝑢 *) l e t g r = r . h [ 1 ; 2; 3] + r . h [ 1 . 0 ; 2 . 0 ; 3 . 0 ]

37/ 45

slide-40
SLIDE 40

Higher-kinded types

f : ∀F : : * →*.∀𝛽 : : * . F 𝛽 → (F 𝛽 → 𝛽) → 𝛽 x : L i s t ( I n t × I n t ) f x

38/ 45

slide-41
SLIDE 41

Higher-kinded types

𝐺 𝛽 ∼ List(Int × Int) 𝐺 = List 𝛽 = Int × Int 𝐺 = Λ𝛾.List(𝛾 × 𝛾) 𝛽 = Int 𝐺 = Λ𝛾.List(Int × Int)

39/ 45

slide-42
SLIDE 42

Lightweight higher-kinded types

A set 𝐆 of functions such that: ∀𝐺, 𝐻 ∈ 𝐆. 𝐺 ≠ 𝐻 ⇒ ∀𝑢.𝐺(𝑢) ≠ 𝐻(𝑢)

40/ 45

slide-43
SLIDE 43

Lightweight higher-kinded types

type ’ a t = ( ’ a * ’ a ) l i s t

41/ 45

slide-44
SLIDE 44

Lightweight higher-kinded types

type l s t = L i s t type

  • pt = Option

type ( ’ a , ’ f ) app = | Lst : ’ a l i s t −> ( ’ a , l s t ) app | Opt : ’ a option −> ( ’ a ,

  • pt ) app

(’a, lst ) app ≈ ’a list (’a, opt) app ≈ ’a option

42/ 45

slide-45
SLIDE 45

Lightweight higher-kinded types

type ’ f map = { map : ’ a ’b . ( ’ a −> ’b) −> ( ’ a , ’ f ) app −> ( ’ b , ’ f ) app ; } l e t f : ’b map −> ( int , ’b) app −> ( s t r i n g , ’b) app = fun m c −>

  • m. map

( fun x −> ” I n t : ” ^ ( s t r i n g _ o f _ i n t x )) c

43/ 45

slide-46
SLIDE 46

Lightweight higher-kinded types

l e t lmap : l s t map = {map = fun f ( Lst l ) −> Lst ( L i s t . map f l )} l e t l = f lmap ( Lst [ 1 ; 2; 3 ] ) l e t

  • map :
  • pt map =

{map = fun f ( Opt o ) −> Opt ( Option . map f o )} l e t

  • = f omap ( Opt (Some 6))

44/ 45

slide-47
SLIDE 47

Lightweight higher-kinded types

Generalised in the Higher library

45/ 45