15
Functors Today’s music: "Uptown Funk" by Mark Ronson feat. Bruno Mars Prof. Clarkson Fall 2018 Please try to sit with your team & have your iClicker out and ready.

Functors Please try to sit with your team

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Functors Please try to sit with your team

Functors

Today’s music: "Uptown Funk" by Mark Ronson feat. Bruno Mars

Prof. ClarksonFall 2018

Please try to sit with your

team&

have your iClicker out and ready.

Page 2: Functors Please try to sit with your team

Attendance question

Fill in the blank to complete the analogy:

A(n) __________ is to a structureas

a type is to a value

A. abstract typeB. moduleC. signatureD. compilation unit

Page 3: Functors Please try to sit with your team

Review

Previously in 3110:

• modules, structures, signatures, abstract types

• aspects of modularity: namespaces, abstraction

Today:

• code reuse: functors and includes

Page 4: Functors Please try to sit with your team

Review

Encapsulation: hide parts of module from clients

module type Stack = sigtype 'a tval push : 'a -> 'a t -> 'a t

end

module ListStack : Stack = structtype 'a t = 'a listlet push x s = x::s

end

type constructor t is abstract: clients of this signature know the

type exists but not what it is

Page 5: Functors Please try to sit with your team

Review

Encapsulation: hide parts of module from clients

module type Stack = sigtype 'a tval push : 'a -> 'a t -> 'a t

end

module ListStack : Stack = structtype 'a t = 'a listlet push x s = x::s

endmodule is sealed: all definitions in it except those given in signature Stack are hidden from clients

Page 6: Functors Please try to sit with your team

Question

Consider this code:

module type Stack =

sig

type 'a t

val empty : 'a t

val push : 'a -> 'a t -> 'a t

end

module ListStack : Stack =

structtype 'a t = 'a list

let empty = []

let push x s = x :: s

end

Which of the following expressions will type check?

A. Stack.empty

B. ListStack.push 1 []

C. fun (s:ListStack) -> ListStack.push 1 s

D. All of the above

E. None of the above

Page 7: Functors Please try to sit with your team

FUNCTORS(funk you up?)

Cornell (CS) funk you up:https://www.youtube.com/watch?v=Au56Ah92Ulk

Page 8: Functors Please try to sit with your team

Functors are "functions"

on structures

Demo

Page 9: Functors Please try to sit with your team

Matching

A structure Struct matches a signature Sig if:

1.Struct defines every declaration in Sig

2. The type of each definition in Struct is the same as or more general than the declaration in Sig

Page 10: Functors Please try to sit with your team

Question

Which structure matches?

sigtype tval c : t list

end

structlet c = [42]

end

structtype t = intlet c = [42]

end

structtype t = intlet c = []

end

A.

B.

C.

D. two of the aboveE. all three of the above

Page 11: Functors Please try to sit with your team

PARAMETERIZED MODULE: TEST SUITE

Re-using code

Demo

Page 12: Functors Please try to sit with your team

PARAMETERIZED MODULE: MAP

Re-using code

Demohttps://www.cs.cornell.edu/courses/cs3110/2018fa/manual-4.6/libref/Map.html

Page 13: Functors Please try to sit with your team

INCLUDES

Demo

Page 14: Functors Please try to sit with your team

Code reuse from includes

• Interface inheritance• Implementation inheritance

Page 15: Functors Please try to sit with your team

Upcoming events

• N/A

This is higher-order funk.

THIS IS 3110