Abstraction
1/ 45
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/ 45
▶ 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
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
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
l e t
: 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
I n t S e t l e t
: t = add 1 ( add 2 ( add 3 empty ))
6/ 45
l e t
: I n t S e t . t = I n t S e t . ( add 1 ( add 2 ( add 3 empty ) ) )
7/ 45
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
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
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
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
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
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
# 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
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
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
nat_set
( add [ NatSet ] nat_set ) one (( add [ NatSet ] nat_set ) two (( add [ NatSet ] nat_set ) three ( empty [ NatSet ] nat_set ) ) )
17/ 45
Γ ⊢ 𝑁 ∶ 𝐵[𝛽 ∶= 𝐶] Γ ⊢ ∃𝛽∶∶𝐿.𝐵 ∶∶ ∗ ∃-intro Γ ⊢ pack 𝐶, 𝑁 as ∃𝛽∶∶𝐿.𝐵 ∶ ∃𝛽∶∶𝐿.𝐵
18/ 45
Λ𝛽 : : * . 𝜇p : Bool . 𝜇x :𝛽. 𝜇y :𝛽. i f p [𝛽] x y Λ𝛽 : : * . Λ𝛾 : : * . 𝜇p : Bool . 𝜇x :𝛽. 𝜇y : 𝛾 . i f p [ ∃𝛿 . 𝛿 ] ( pack 𝛽 , x as ∃𝛿 . 𝛿 ) ( pack 𝛾 , y as ∃𝛿 . 𝛿 )
19/ 45
Λ𝛽 : : * . 𝜇p : Bool . 𝜇x :𝛽. 𝜇y :𝛽. i f p [𝛽] x y Λ𝛽 : : * . Λ𝛾 : : * . 𝜇p : Bool . 𝜇x :𝛽. 𝜇y : 𝛾 . i f p [ ∃𝛿 . 𝛿 ] ( pack 𝛽 , x as ∃𝛿 . 𝛿 ) ( pack 𝛾 , y as ∃𝛿 . 𝛿 )
19/ 45
fun p x y −> i f p then x e l s e y ∀𝛽 : : * . Bool → 𝛽 → 𝛽 → 𝛽 ∀𝛽 : : * . ∀𝛾 : : * . Bool → 𝛽 → 𝛾 → ∃𝛿 : : * . 𝛿
20/ 45
(* ∃𝛽.𝛽 × (𝛽 → 𝛽) × (𝛽 → 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
22/ 45
▶ 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
23/ 45
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
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
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
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
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
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
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
EqImpl = 𝜇𝛿 : : * . 𝛿 → 𝛿 → Bool equal = Λ𝛿 : : * . 𝜇s : EqImpl 𝛿 . s
31/ 45
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
Γ ⊢ 𝑁 ∶ ∀𝛽∶∶𝐿.𝐵 Γ ⊢ 𝐶 ∶∶ 𝐿 ∀-elim Γ ⊢ 𝑁 [𝐶] ∶ 𝐵[𝛽 ∶= 𝐶]
33/ 45
(* ∀𝛽.𝛽 → 𝛽 *) 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
Λ𝛽 : : * . 𝜇 f :𝛽 → I n t . 𝜇x :𝛽. 𝜇y :𝛽. plus ( f x ) ( f y ) Λ𝛽 : : * . Λ𝛾 : : * . 𝜇 f :∀𝛿 . 𝛿 → I n t . 𝜇x :𝛽. 𝜇y : 𝛾 . plus ( f [𝛽] x ) ( f [ 𝛾 ] y )
35/ 45
Λ𝛽 : : * . 𝜇 f :𝛽 → I n t . 𝜇x :𝛽. 𝜇y :𝛽. plus ( f x ) ( f y ) Λ𝛽 : : * . Λ𝛾 : : * . 𝜇 f :∀𝛿 . 𝛿 → I n t . 𝜇x :𝛽. 𝜇y : 𝛾 . plus ( f [𝛽] x ) ( f [ 𝛾 ] y )
35/ 45
fun f x y −> f x + f y ∀𝛽 : : * . (𝛽 → I n t ) → 𝛽 → 𝛽 → I n t ∀𝛽 : : * . ∀𝛾 : : * . (∀𝛿 : : * . 𝛿 → I n t ) → 𝛽 → 𝛾 → I n t
36/ 45
(* ∀𝛽.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
f : ∀F : : * →*.∀𝛽 : : * . F 𝛽 → (F 𝛽 → 𝛽) → 𝛽 x : L i s t ( I n t × I n t ) f x
38/ 45
𝐺 𝛽 ∼ List(Int × Int) 𝐺 = List 𝛽 = Int × Int 𝐺 = Λ𝛾.List(𝛾 × 𝛾) 𝛽 = Int 𝐺 = Λ𝛾.List(Int × Int)
39/ 45
A set 𝐆 of functions such that: ∀𝐺, 𝐻 ∈ 𝐆. 𝐺 ≠ 𝐻 ⇒ ∀𝑢.𝐺(𝑢) ≠ 𝐻(𝑢)
40/ 45
type ’ a t = ( ’ a * ’ a ) l i s t
41/ 45
type l s t = L i s t type
type ( ’ a , ’ f ) app = | Lst : ’ a l i s t −> ( ’ a , l s t ) app | Opt : ’ a option −> ( ’ a ,
(’a, lst ) app ≈ ’a list (’a, opt) app ≈ ’a option
42/ 45
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 −>
( fun x −> ” I n t : ” ^ ( s t r i n g _ o f _ i n t x )) c
43/ 45
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 = fun f ( Opt o ) −> Opt ( Option . map f o )} l e t
44/ 45
Generalised in the Higher library
45/ 45