concepts of programming languages
play

Concepts of programming languages OCaml - low-level Daan Knoope, - PowerPoint PPT Presentation

[Faculty of Science Information and Computing Sciences] Concepts of programming languages OCaml - low-level Daan Knoope, Bas van Rooij, Jorrit Dorrestijn, Ivor van der Hoog, Wouter ten Bosch 1 [Faculty of Science Information and Computing


  1. [Faculty of Science Information and Computing Sciences] Concepts of programming languages OCaml - low-level Daan Knoope, Bas van Rooij, Jorrit Dorrestijn, Ivor van der Hoog, Wouter ten Bosch 1

  2. [Faculty of Science Information and Computing Sciences] Content ▶ Introduction OCaml ▶ Interfacing ▶ Memory ▶ Garbage collection 2

  3. [Faculty of Science Information and Computing Sciences] OCaml ▶ OCaml is an object oriented, functional and imperative language. ▶ OCaml uses type-inference. 3

  4. 1.0 + 2.0;; File "", line 1, characters 0-3: Error: This expression has type float but an expression was expected of type int 1.0 +. 2.0;; - : float = 3. [Faculty of Science Information and Computing Sciences] Operators Lets start with a simple addition: 1 + 2;; - : int = 3 4

  5. 1.0 +. 2.0;; - : float = 3. [Faculty of Science Information and Computing Sciences] Operators Lets start with a simple addition: 1 + 2;; - : int = 3 1.0 + 2.0;; File "", line 1, characters 0-3: Error: This expression has type float but an expression was expected of type int 4

  6. [Faculty of Science Information and Computing Sciences] Operators Lets start with a simple addition: 1 + 2;; - : int = 3 1.0 + 2.0;; File "", line 1, characters 0-3: Error: This expression has type float but an expression was expected of type int 1.0 +. 2.0;; - : float = 3. 4

  7. let add x y = x + y;; val add : int -> int -> int = < fun > add 1 2;; - : int = 3 [Faculty of Science Information and Computing Sciences] Expressions The let keyword is used to defjne a named expressions. let x = 5;; val x : int = 5 5

  8. add 1 2;; - : int = 3 [Faculty of Science Information and Computing Sciences] Expressions The let keyword is used to defjne a named expressions. let x = 5;; val x : int = 5 let add x y = x + y;; val add : int -> int -> int = < fun > 5

  9. [Faculty of Science Information and Computing Sciences] Expressions The let keyword is used to defjne a named expressions. let x = 5;; val x : int = 5 let add x y = x + y;; val add : int -> int -> int = < fun > add 1 2;; - : int = 3 5

  10. let y = ref 5;; val y : int ref = {contents = 5} y := 4; !y;; - : int = 4 [Faculty of Science Information and Computing Sciences] Variables Expressions in OCaml are immutable. If you want to change a variable, you can declare a reference variable. 6

  11. [Faculty of Science Information and Computing Sciences] Variables Expressions in OCaml are immutable. If you want to change a variable, you can declare a reference variable. let y = ref 5;; val y : int ref = {contents = 5} y := 4; !y;; - : int = 4 6

  12. justatwo 3;; - : int = 2 justatwo "Foo";; - : int = 2 [Faculty of Science Information and Computing Sciences] Polymorphic functions let justatwo x = 2;; val justatwo : 'a -> int = < fun > 'a is an argument that can be anything. 7

  13. [Faculty of Science Information and Computing Sciences] Polymorphic functions let justatwo x = 2;; val justatwo : 'a -> int = < fun > 'a is an argument that can be anything. justatwo 3;; - : int = 2 justatwo "Foo";; - : int = 2 7

  14. [Faculty of Science Information and Computing Sciences] Classes class stack = object (self) ... end ;; 8

  15. [Faculty of Science Information and Computing Sciences] Classes class stack = object (self) val mutable list = ([] : int list) ... end ;; 9

  16. [Faculty of Science Information and Computing Sciences] Classes class stack = object (self) val mutable list = ([] : int list) method size = List.length list ... end ;; 10

  17. [Faculty of Science Information and Computing Sciences] Classes class stack = object (self) val mutable list = ([] : int list) method size = List.length list method push x = list <- x :: list method pop = let result = List.hd list in list <- List.tl list; result end ;; 11

  18. s#push 1; s#push 2; s#push 3; s#size;; - : int = 3 [Faculty of Science Information and Computing Sciences] Classes Classes are initiated using the ‘new’ keyword. let s = new stack;; val s : stack = <obj> 12

  19. [Faculty of Science Information and Computing Sciences] Classes Classes are initiated using the ‘new’ keyword. let s = new stack;; val s : stack = <obj> s#push 1; s#push 2; s#push 3; s#size;; - : int = 3 12

  20. [Faculty of Science Information and Computing Sciences] Classes class ['a] stack = object (self) val mutable list = ([] : 'a list) method size = List.length list method push x = list <- x :: list method pop = let result = List.hd list in list <- List.tl list; result end ;; 13

  21. [Faculty of Science Information and Computing Sciences] Classes let s = new stack;; val s : '_a stack = <obj> s#push 1.0; s;; - : float stack = <obj> 14

  22. [Faculty of Science Information and Computing Sciences] Classes let clear_stack s = while s#size > 0 do s#pop done ;; val clear_stack : < pop : 'a; size : int; .. > -> unit = < fun > 15

  23. Records: tuples with named elements type coordinates = {long : float; lat: float};; let uithof = {long = 52.1; lat = 5.2};; val uithof : coordinates = {long = 52.1; lat = 5.2} [Faculty of Science Information and Computing Sciences] Tuples and Records ▶ Tuples let cityPopulation = ("Utrecht", 312634);; val cityPopulation : bytes * int 16

  24. [Faculty of Science Information and Computing Sciences] Tuples and Records ▶ Tuples let cityPopulation = ("Utrecht", 312634);; val cityPopulation : bytes * int ▶ Records: tuples with named elements type coordinates = {long : float; lat: float};; let uithof = {long = 52.1; lat = 5.2};; val uithof : coordinates = {long = 52.1; lat = 5.2} 16

  25. Variants as qualifjed unions: type location = | Building of building | Coordinates of coordinates | City of string;; [Faculty of Science Information and Computing Sciences] Variants ▶ Variants as enums: type building = BBG | KBG | EDUC;; 17

  26. [Faculty of Science Information and Computing Sciences] Variants ▶ Variants as enums: type building = BBG | KBG | EDUC;; ▶ Variants as qualifjed unions: type location = | Building of building | Coordinates of coordinates | City of string;; 17

  27. [Faculty of Science Information and Computing Sciences] Polymorphic variants Creating a polymorphic binary tree: type 'a binary_tree = | Leaf of 'a | Tree of 'a binary_tree * 'a binary_tree;; Tree (Leaf 4, Leaf 5);; 18

  28. [Faculty of Science Information and Computing Sciences] Pattern matching Turning a tree into list let rec asList tree = match tree with | Leaf l -> [l] | Tree (a,b) -> asList(a) @ asList(b);; val asList : 'a binary_tree -> 'a list = < fun > 19

  29. let increment = add 1;; val increment : int -> int = < fun > [Faculty of Science Information and Computing Sciences] Partial application Is used for creating more specifjc functions based of a more general function let add x y = x + y;; val add : int -> int -> int = < fun > 20

  30. [Faculty of Science Information and Computing Sciences] Partial application Is used for creating more specifjc functions based of a more general function let add x y = x + y;; val add : int -> int -> int = < fun > let increment = add 1;; val increment : int -> int = < fun > 20

  31. [Faculty of Science Information and Computing Sciences] Partial application - issues This is very consice code, but ▶ in certain cases it might be much slower 1 ▶ it might lead to a loss of polymorphism 1 Jane Street: The dangers of being too partial 21

  32. Solution: weak polymorphism , i.e. polymorphic until the fjrst run. write : '_a -> unit() read : unit() -> '_a Information and Computing [Faculty of Science Sciences] Problem: polymorphism and mutability ▶ OCaml supports both polymorphism and mutability. ▶ This causes trouble. For example writing to a device: write : 'a -> unit() read : unit() -> 'a ▶ This is too permissive: if we have written a string fjrst, we cannot read an int later. 22

  33. [Faculty of Science Sciences] Information and Computing Problem: polymorphism and mutability ▶ OCaml supports both polymorphism and mutability. ▶ This causes trouble. For example writing to a device: write : 'a -> unit() read : unit() -> 'a ▶ This is too permissive: if we have written a string fjrst, we cannot read an int later. ▶ Solution: weak polymorphism , i.e. polymorphic until the fjrst run. write : '_a -> unit() read : unit() -> '_a 22

  34. Applications of functions are not strongly polymorphic: they can be evaluated further Partial applications are also weakly polymorphic [Faculty of Science Information and Computing Sciences] Value restriction ▶ OCaml needs to decide when to make something either strongly or weakly polymorphic ▶ Decision: only fully evaluated or immutable expressions can be strongly polymorphic ▶ Functions are strongly polymorphic 23

  35. [Faculty of Science Information and Computing Sciences] Value restriction ▶ OCaml needs to decide when to make something either strongly or weakly polymorphic ▶ Decision: only fully evaluated or immutable expressions can be strongly polymorphic ▶ Functions are strongly polymorphic ▶ Applications of functions are not strongly polymorphic: they can be evaluated further ▶ Partial applications are also weakly polymorphic 23

Recommend


More recommend