DOC PREVIEW
U of I CS 421 - s

This preview shows page 1-2-15-16-31-32 out of 32 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Programming Languages and Compilers (CS 421)How to encode Objects with Functions?What is an Object?PreliminariesPoint with StateSlide 6Working with the Point objectImprovement - Use RecordsSlide 9Slide 10Adding selfMemorySlide 13So far…Message DispatchingmkPointUsing mkPointAdding a new methodAdding thisExample: fastpoint subclassImplementingCode: superpointCode: pointCode: fastpointSlide 25Till now…PolymorphismStructural PolymorphismInheritance polymorphismDiscussion: Dynamic DispatchDiscussion: Class variablesConclusionsProgramming Languages and Compilers (CS 421)Elsa L Gunter2112 SC, UIUChttp://www.cs.uiuc.edu/class/sp07/cs421/Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul AghaElsa L. Gunter How to encode Objects with Functions?•Functional Languages have fairly straightforward semantics•Object Oriented Languages are more common•Problem: How to encode in functional language?–To understand their semantics–To be able to simulate objects in a language without themElsa L. Gunter What is an Object?•Data (state) and functions (interface) are grouped together.•Functions have their own local state•Objects can send and receive messages•Objects can refer to themselves•Object Oriented Programming is a programming language paradigm that facilitates defining, handling and coordinating objects.Elsa L. Gunter Preliminaries•We will use the following funcitons:let pi1 (x,y) = x let pi2 (x,y) = y let report (x,y) = print_string "Point: "; print_int x; print_string ","; print_int y; print_newline () let movept (x,y) (dx,dy) = (x+dx,y+dy)Elsa L. Gunter Point with Statelet mktPoint myloc = let myloc = ref myloc in ( myloc, (fun () -> pi1 !myloc), (fun () -> pi2 !myloc), (fun () -> report !myloc), (fun dl -> myloc := movept !myloc dl) )Elsa L. Gunter Point with State•mktPoint creates a point with local state•Defines a tuple of functions that share a common state. let (lref,getx,gety,show,move) = mktPoint (2,4);; •It is cumbersome to use.Elsa L. Gunter Working with the Point objectlet (lref,getx,gety,show,move) = mktPoint (2,4);;# getx ();;# move (3,4);;# show ();; - : int = 2- : unit = ()Point: 5,8- : unit = ()Elsa L. Gunter Improvement - Use Recordstype point ={ loc : (int * int) ref; getx : unit -> int; gety : unit -> int; draw : unit -> unit; move : int * int -> unit;}Elsa L. Gunter Improvement - Use Recordslet mkrPoint newloc = let myloc = ref newloc in { loc = myloc; getx = (fun () -> pi1 !myloc); gety = (fun () -> pi2 !myloc); draw = (fun () -> report !myloc); move = (fun dl -> myloc := movept !myloc dl) }Elsa L. Gunter Working with the Point objectHow do you instantiate the object ?let point = mkrPoint (2,4);;How do you invoke the function getx? How do you invoke the function move? # point.getx ();;- : int = 2# point.move (2,3);;- : unit = ()Elsa L. Gunter Adding selflet mkPoint newloc = let rec this = { loc = ref newloc; getx = (fun() -> pi1 ! (this.loc)); gety = (fun() -> pi2 ! (this.loc)); draw = (fun() -> report ! (this.loc)); move = (fun dl -> this.loc := movept ! (this.loc) dl) }in this;;Elsa L. Gunter Memory•The record point references to the fields•If you copy a point, the data does not get copied!# let p1 = mkPoint (4,7);;val p1 : point = {loc={contents=4, 7}; ...}# let p2 = mkPoint(6,2);;val p2 : point = {loc={contents=6, 2}; ...}Elsa L. Gunter Memory# let p3 = p1;;# p1.move(5,5);;# p3;;val p3 : point = {loc={contents=4, 7}; ...}- : point = {loc={contents=9, 12}; ...}- : unit = ()Elsa L. Gunter So far…• We used a record to implement a type for points.–Advantages:•Every method had its own name and type.•Simple syntax for manipulating the object.•It’s fast: we know at compile time which method is been called.–Disadvantages•Inheritance is very difficult with this model.–Adding a new message type means updating everything.Elsa L. Gunter Message Dispatching•Object is kind of data that can receive messages from program or other objects.–Need implementation where type doesn’t change when new methods are added.•Let a point object be a function that takes a string and returns an appropriate matching for that string.Elsa L. Gunter mkPointlet mkPoint x y = let x = ref x in let y = ref y in fun st -> match st with | “getx” -> (fun _ -> !x) | “gety” -> (fun _ -> !y) | “movx” -> (fun nx -> x := !x + nx; !x) | “movy” -> (fun ny -> y := !y + ny; !y) | _ -> raise(Failure (“Unknown message.”))•All methods now have to have type int -> intElsa L. Gunter Using mkPointHow do you instantiate the object ?let point = mkPoint (2,4);;How do you invoke the function getx? How do you invoke the function move? # point "getx" 0;;- : int = 2# point "movx" 2;;- : int = 4Elsa L. Gunter Adding a new method•Exercise: How would we add a report method?# let mkPoint x y = … fun st -> match st with ………. | "report" -> (fun _ -> print_string "X = "; print_int !x; print_string "\n"; print_string "Y = "; print_int !y; print_string "\n";0) | _ -> raise (Failure("Function not understood"));;Elsa L. Gunter Adding this •Exercise: How would we add this?# let mkPoint x y = let this = … (fun st -> match st with ………. | _ -> raise (Failure("Function not understood"))) in this;;Elsa L. Gunter Example: fastpoint subclassThree entities involved: the superclass (superpoint) and the subclasses (point) and (fastpoint). fastpoint moves twice as fast as the original pointWhat does it mean for fastpoint to be a subclass of superpoint?•fastpoint should respond to the same messages.–It may override some of them.–It may add its own.–It may not remove any methods.Elsa L. Gunter Implementing•Point construction needs to return the “public” data to fastpoint and point.•fastpoint returns a dispatcher:–If fastpoint dispatcher can handler a message, it does.–Otherwise, it sends the message to point.Elsa L. Gunter Code: superpointlet mkSuperPoint x y = let x = ref x in let y = ref y in ((x,y), fun st -> match st with "getx" -> (fun _ -> !x) | "gety" -> (fun _ -> !y) | "movx" -> (fun nx -> x := !x + nx; !x) | "movy" -> (fun ny -> y := !y + ny; !y) | “report” -> (fun _ -> report (!x, !y);0) | _ -> raise (Failure ("Function not understood")));; Our instance variables are now public.Elsa L. Gunter Code: pointlet


View Full Document

U of I CS 421 - s

Documents in this Course
Lecture 2

Lecture 2

12 pages

Exams

Exams

20 pages

Lecture

Lecture

32 pages

Lecture

Lecture

21 pages

Lecture

Lecture

15 pages

Lecture

Lecture

4 pages

Lecture

Lecture

68 pages

Lecture

Lecture

68 pages

Lecture

Lecture

84 pages

Parsing

Parsing

52 pages

Lecture 2

Lecture 2

45 pages

Midterm

Midterm

13 pages

LECTURE

LECTURE

10 pages

Lecture

Lecture

5 pages

Lecture

Lecture

39 pages

Load more
Download s
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view s and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view s 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?