abstraction
play

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


  1. Abstraction 1/ 45

  2. Abstraction the interactions of difgerent components can be simplifjed by hiding the details of each component’s implementation from the rest of the system. protecting it with an interface . system is invariant to changes of implementation that do not afgect the interface. 2/ 45 ▶ When faced with creating and maintaining a complex system, ▶ Details of a component’s implementation are hidden by ▶ Abstraction is maintained by ensuring that the rest of the

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

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

  5. Modules: structures l e t one_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

  6. Modules: structures open I n t S e t l e t one_two_three : t = add 1 ( add 2 ( add 3 empty )) 6/ 45

  7. Modules: structures l e t one_two_three : I n t S e t . t = I n t S e t . ( add 1 ( add 2 ( add 3 empty ) ) ) 7/ 45

  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

  9. Modules: signatures : 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 − > bool l i s t − > i n t l i s t v a l t o _ l i s t : ’ a − > ’ a end v a l i n t − > i n t s i g l i s t type t = i n t l i s t v a l empty : ’ a v a l v a l mem : is_empty : ’ a l i s t − > bool v a l equal_member : i n t − > i n t − > bool 9/ 45

  10. Modules: signatures l i s t 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 v a l add 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 : v a l 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 − > bool 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 10/ 45

  11. Modules: signatures : 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 − > i n t l i s t end module I n t S e t : IntSetS = s t r u c t . . . end l i s t i n t − > i n t module l i s t type IntSetS = s i g type t = i n t l i s t v a l empty : i n 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 11/ 45

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

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

  14. Modules: abstract types s ; | [ ] − > () in p r i n t _ s t r i n g ”{ ” ; loop p r i n t _ s t r i n g loop ” } ” ; ; Characters 172-173: loop s; ^ Error: This expression has type IntSet.t but an expression was expected of type int list xs ” ; # l e t rec p r i n t _ s e t ( s : I n t S e t . t ) : u n i t = l e t 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 14/ 45

  15. Existential types × ( Nat → 𝛽 → 𝛽 ) Nat ) NatSetImpl = × ( Nat → 𝛽 → 𝛽 ) × ( 𝛽 → L i s t × ( Nat → 𝛽 → Bool ) × ( 𝛽 → Bool ) 𝛽 𝜇𝛽 : : * . 15/ 45 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

  16. Existential types ( cons Nat i f ( equal_nat n x ) [ L i s t Nat ] l [ Nat ] Nat ] x l )) ( n i l [ Nat ] ) , 𝜇 l : L i s t Nat . l 〉 as ∃𝛽 : : * . NatSetImpl 𝛽 ( 𝜇 x : Nat . 𝜇 l : L i s t [ L i s t nat_set_package = [ Nat ] , pack L i s t Nat , 〈 n i l [ Nat ] , isempty 𝜇 n : Nat . f o l d [ Nat ] [ 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 16/ 45

  17. Existential types (( add nat_set ) ) ) [ NatSet ] ( empty three nat_set ) [ NatSet ] nat_set ) two open nat_set_package as NatSet , [ NatSet ] (( add nat_set ) one [ NatSet ] ( add one_two_three = nat_set 17/ 45

  18. Existential types Γ ⊢ 𝑁 ∶ 𝐵[𝛽 ∶= 𝐶] Γ ⊢ pack 𝐶, 𝑁 as ∃𝛽∶∶𝐿.𝐵 ∶ ∃𝛽∶∶𝐿.𝐵 18/ 45 Γ ⊢ ∃𝛽∶∶𝐿.𝐵 ∶∶ ∗ ∃ -intro

  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

  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

  21. Existential types in OCaml fun p x y − > i f p then x e l s e y ∀𝛽 : : * . Bool → 𝛽 → 𝛽 → 𝛽 ∀𝛽 : : * . ∀𝛾 : : * . Bool → 𝛽 → 𝛾 → ∃𝛿 : : * . 𝛿 20/ 45

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

  23. Parametricity 22/ 45

  24. Parametricity with multiple types. uniformly . from the outside world, parametricity hides details about the outside world from an implementation. 23/ 45 ▶ Polymorphism allows a single piece of code to be instantiated ▶ Polymorphism is parametric when all of the instances behave ▶ Where abstraction hides details about an implementation

  25. Modules: functors v a l t − > bool v a l mem : e l t − > t − > bool v a l add : e l t − > t − > t remove is_empty : e l t − > t − > t v a l t o _ l i s t : t − > e l t l i s t end : v a l module module type Eq = s i g type t v a l equal : t − > t − > bool end type t SetS = s i g type t type e l t v a l empty : 24/ 45

  26. Modules: functors : v a l add : e l t − > t − > t v a l remove e l t − > t − > t v a l mem : v a l t o _ l i s t : t − > e l t l i s t end e l t − > t − > bool t − > bool SetS t with type e l t = foo expands to s i g type type : e l t = foo v a l empty : t v a l is_empty 25/ 45

  27. Modules: functors : v a l add : foo − > t − > t v a l remove foo − > t − > t v a l mem : v a l t o _ l i s t : t − > foo l i s t end foo − > t − > bool t − > bool SetS type with type e l t := foo expands to s i g t : v a l empty : t v a l is_empty 26/ 45

  28. Modules: functors | _ − > f a l s e r e s t e l s e mem x true then x y ) (E . equal i f r e s t − > : : y | [ ] − > f a l s e | l e t [ ] − > true module Set := E . t = s t r u c t (E : Eq) : SetS with type e l 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 27/ 45 rec mem x = f u n c t i o n

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

  30. Modules: functors ( y I n t S e t = Set ( IntEq ) module end x = y i n t ) = : i n t ) module : ( x equal l e t t = i n t type IntEq = s t r u c t 29/ 45

Recommend


More recommend