ocaml n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
OCaml PowerPoint Presentation
Download Presentation
OCaml

Loading in 2 Seconds...

  share
play fullscreen
1 / 46
payton

OCaml - PowerPoint PPT Presentation

185 Views
Download Presentation
OCaml
An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. OCaml The PL for the discerning hacker.

  2. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu

  3. ML Anatomy 101 ML Program = ? ? ? ML Program = One Giant, Complex Expression Controlling complexity is the essence of computer programming. B. Kerninghan A complex system that works is invariably found to have evolved from a simple system that worked. J. Gall

  4. Building ML Programs ML provides tools to control complexity Build complex exprs from simple exprs Build complex types from simple types PREV NOW

  5. Building Types basic (recap) function record variant demo M.C. Escher’s Relativity in LEGO

  6. basic Who cares about types anyway? Every good programmer! (not just old timers) Types provide: Documentation Early bug warning system Performance!

  7. basic Who cares about types anyway? Even programmers without a type system! I think it may just be a property of large systems in dynamic languages, that eventually you end up rewriting your own type system, and you sort of do it badly. -- Twitter

  8. basic

  9. more on tuples

  10. more on tuples

  11. more on tuples

  12. basic Don’t know how it works ? Try it in the toplevel !

  13. Building Types basic (recap) function record variant demo M.C. Escher’s Relativity in LEGO

  14. function A -> B Type of a function which: expects parameter of type A produces a value of type B Contract: caller satisfies A callee satisfies B precondition postcondition

  15. function let f x = x + 5 f: int -> int let f x = “hello “ ^ x f: string -> string let f x = “number “ ^ (string_of_int x) f: int -> string let f x y = x * x + y * y f: int -> int -> int

  16. function let f x y = 1 :: x :: y f: int -> int list -> int list let f x y z = [1] @ x @ [y + z] f: int list -> int -> int -> int list let rec f = f f ERROR

  17. polymorphic functions Some functions work on many types: let id x = x id: ‘a -> ‘a Takes a value of any type, call it ‘a Returns a value of that type ‘a

  18. polymorphic functions let f a b = a f: ‘a -> ‘b -> ‘a let f a b = b f: ‘a -> ‘b -> ‘b let pipe x f = f x f: ‘a -> (‘a -> ‘b) -> ‘b let (|>) = pipe “hello” |> id |> print_string (print_string (id (“hello”))) binary infix operator

  19. polymorphic functions let f g x = g x f: (‘a -> ‘b) -> ‘a -> ‘b let f g h x = g (h x) f: (‘a -> ‘b) -> (‘c -> ‘a) -> ‘c -> ‘b

  20. Building Types basic (recap) function record variant demo M.C. Escher’s Relativity in LEGO

  21. record type RT = { NM1 : T1 ; ... ; NMN : TN } Like a tuple, but refer to members by name.

  22. record type person = { name : string ; age : int ; hair : string ; job : string }

  23. making a record value let pres = { name = “Obama” ; age = 49 ; hair = “black” ; job = “president” }

  24. updating a record value let pres = { name = “Obama” ; age = 49 ; hair = “black” ; job = “president” } let pres’ = {pres with hair = “gray” }

  25. updating a record value let year_older p = if p.age > 45 then { p with age = p.age + 1, hair = “gray” } else { p with age = p.age + 1 } year_older: person -> person

  26. Building Types basic (recap) function record variant demo M.C. Escher’s Relativity in LEGO

  27. variant type VT = | C1 of T1 | ... | CN of TN C1 to CN are “constructors” Ci like function from Ti to VT value of type VT can only be constructed with one of these

  28. variant type pet = | Dog of string | Cat of string | Fish of string Value of type pet constructed with one of: Dog, Cat, Fish Each takes a string and returns a pet

  29. variant values type pet = | Dog of string | Cat of string | Fish of string let d = Dog “spot” let c = Cat “whiskers” let f = Fish “nemo”

  30. matching variant values let name pet = match pet with | Dog nm -> nm | Cat nm -> nm | Fish nm -> nm let d = Dog “sparky” in name d

  31. matching variant values let says pet = match pet with | Dog _ -> “woof” | Cat _ -> “meow” | Fish _ -> “bubble bubble” let c = Cat “walter” in says c

  32. variant type fuel_level = | Empty | Middle | Full type vehicle = | Car of int * fuel_level | Tank of int * fuel_level | Boat of int * fuel_level

  33. matching variant values let miles v = match v with | Car (m, _) -> m | Tank (m, _) -> m | Boat (m, -) -> m

  34. updating variant values let reduce f = match f with | Empty -> Empty | Middle -> Empty | Full -> Middle let drive v = match v with | Car (m, f) -> Car (m, reduce f) | Tank (m, f) -> Tank (m, reduce f) | Boat (m, f) -> Boat (m, reduce f)

  35. updating variant values let refill v = match v with | Car (m, f) -> Car (m, Full) | Tank (m, f) -> Tank (m, Full) | Boat (m, f) -> Boat (m, Full)

  36. recursive variant type expr = | Val of int | Add of expr * expr | Sub of expr * expr | Mul of expr * expr let e1 = Val 5 let e2 = Add (e1, e1) let e3 = Mul (e2, e1)

  37. recursive variant let receval e =

  38. recursive variant let receval e = match e with | Val i -> i | Add (l, r) -> (eval l) + (eval r) | Sub (l, r) -> (eval l) - (eval r) | Mul (l, r) -> (eval l) * (eval r)

  39. mutual recursion let recexpr_dot e = match e with | Val i -> string_of_inti | Add (l, r) -> (aux "add" l) ^ (aux "add" r) | Sub (l, r) -> (aux "sub" l) ^ (aux "sub" r) | Mul (l, r) -> (aux "mul" l) ^ (aux "mul" r) and aux p e = match e with | Val i -> p ^ " -> " ^ string_of_inti ^ ";\n" | Add _ -> p ^ " -> add;\n" ^ (expr_dot e) | Sub _ -> p ^ " -> sub;\n" ^ (expr_dot e) | Mul _ -> p ^ " -> mul;\n" ^ (expr_dot e)

  40. Pattern Matching: a PL Masterpiece match is one of ML’s very best features simultaneous test / extract / bind auto checks any missed cases leads to compact, readable code

  41. Building Types basic (recap) function record variant demo M.C. Escher’s Relativity in LEGO

  42. demo Conway’s Game of Life

  43. demo Conway’s Game of Life code at: http://github.com/ztatlock/simple-life

  44. demo OCaml in the “real world” at JaneStreet http://ocaml.janestreet.com/ Check out their leader, YaronMinsky http://ocaml.janestreet.com/?q=node/82

  45. Building Types basic (recap) function record variant demo M.C. Escher’s Relativity in LEGO