1 OCAML §nucleo funzionale puro l funzioni (ricorsive) l tipi e pattern matching l primitive utili: liste l trascriviamo pezzi della semantica denotazionale.

Slides:



Advertisements
Similar presentations
Advanced Piloting Cruise Plot.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
UNITED NATIONS Shipment Details Report – January 2006.
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
Properties of Real Numbers CommutativeAssociativeDistributive Identity + × Inverse + ×
My Alphabet Book abcdefghijklm nopqrstuvwxyz.
Multiplication Facts Review. 6 x 4 = 24 5 x 5 = 25.
Multiplying binomials You will have 20 seconds to answer each of the following multiplication problems. If you get hung up, go to the next problem when.
0 - 0.
2 pt 3 pt 4 pt 5 pt 1 pt 2 pt 3 pt 4 pt 5 pt 1 pt 2 pt 3 pt 4 pt 5 pt 1 pt 2 pt 3 pt 4 pt 5 pt 1 pt 2 pt 3 pt 4 pt 5 pt 1 pt Time Money AdditionSubtraction.
FACTORING Think Distributive property backwards Work down, Show all steps ax + ay = a(x + y)
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Addition Facts
Around the World AdditionSubtraction MultiplicationDivision AdditionSubtraction MultiplicationDivision.
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Modern Programming Languages, 2nd ed.
ABC Technology Project
EU market situation for eggs and poultry Management Committee 20 October 2011.
1 Lecture 16: Tables and OOP. 2 Tables -- get and put.
O X Click on Number next to person for a question.
VOORBLAD.
1 Directed Depth First Search Adjacency Lists A: F G B: A H C: A D D: C F E: C D G F: E: G: : H: B: I: H: F A B C G D E H I.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Squares and Square Root WALK. Solve each problem REVIEW:
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
© 2012 National Heart Foundation of Australia. Slide 2.
Adding Up In Chunks.
Lets play bingo!!. Calculate: MEAN Calculate: MEDIAN
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
Properties of Exponents
Chapter 5 Test Review Sections 5-1 through 5-4.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
1 of 31 Images from Africa. 2 of 31 My little Haitian friend Antoine (1985)
1 of 32 Images from Africa. 2 of 32 My little Haitian friend Antoine (1985)
Addition 1’s to 20.
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
25 seconds left…...
Test B, 100 Subtraction Facts
11 = This is the fact family. You say: 8+3=11 and 3+8=11
Week 1.
Analyzing Genes and Genomes
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Pointers and Arrays Chapter 12
Intracellular Compartments and Transport
O X Click on Number next to person for a question.
PSSA Preparation.
Essential Cell Biology
1 A simple abstract interpreter to compute Sign s.
Introduction to OCaml Slides prepared by Matt Gruskin Some material borrowed from the CIS 500 lecture notes.
Semantics of PLs via Interpreters: Getting Started CS784: Programming Languages Prabhaker Mateti.
4/11/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
1 OCAML - 2 §componente imperativo l variabili e assegnamento l primitive utili: arrays §moduli e oggetti.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
1 OCAML nucleo funzionale puro – funzioni (ricorsive) – tipi e pattern matching – trascriviamo pezzi della semantica operazionale – primitive utili: liste.
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
Presentation transcript:

1 OCAML §nucleo funzionale puro l funzioni (ricorsive) l tipi e pattern matching l primitive utili: liste l trascriviamo pezzi della semantica denotazionale §componente imperativo l variabili e assegnamento l primitive utili: arrays §moduli e oggetti

2 Espressioni pure Objective Caml version /Mac1.0a1 # 25;; - : int = 25 # true;; - : bool = true # 23 * 17;; - : int = 391 # true & false;; - : bool = false # 23 * true;; This expression has type bool but is here used with type int # if 2 = 3 then 23 * 17 else 15;; - : int = 15 # if 2 = 3 then 23 else true;; This expression has type bool but is here used with type int

3 Funzioni # function x -> x + 1;; - : int -> int = # (function x -> x + 1) 3;; - : int = 4 # (function x -> x + 1) true;; This expression has type bool but is here used with type int # function x -> x;; - : 'a -> 'a = # function x -> function y -> x y;; - : ('a -> 'b) -> 'a -> 'b = # (function x -> x) 2;; - : int = 2 # (function x -> x) (function x -> x + 1);; - : int -> int = # function (x, y) -> x + y;; - : int * int -> int = # (function (x, y) -> x + y) (2, 33);; - : int = 35

4 Let binding # let x = 3;; val x : int = 3 # x;; - : int = 3 # let y = 5 in x + y;; - : int = 8 # y;; Unbound value y # let f = function x -> x + 1;; val f : int -> int = # f 3;; - : int = 4 # let f x = x + 1;; val f : int -> int = # f 3;; - : int = 4 # let fact x = if x = 0 then 1 else x * fact(x - 1) ;; Unbound value fact

5 Let rec binding # let rec fact x = if x = 0 then 1 else x * fact(x - 1) ;; val fact : int -> int = # fact (x + 1);; - : int = 24

6 Tipi 1 # type ide = string;; type ide = string # type expr = | Den of ide | Val of ide | Fun of ide * expr | Plus of expr * expr | Apply of expr * expr;; type expr = | Den of ide | Val of ide | Fun of ide * expr | Plus of expr * expr | Apply of expr * expr E ::= I | val(I) | lambda(I, E 1 ) | plus(E 1, E 2 ) | apply(E 1, E 2 ) # Apply(Fun("x",Plus(Den "x", Den "x")), Val "y");; - : expr = Apply (Fun ("x", Plus (Den "x", Den "x")), Val "y")

7 Tipi 2 # type eval = Int of int | Bool of bool | Efun of myfun | Unbound and myfun = eval -> eval;; type eval = | Int of int | Bool of bool | Efun of myfun | Unbound type myfun = eval -> eval # type env = ide -> eval;; type env = ide -> eval -env = IDE  eval -eval = [ int + bool + fun ]

8 Tipi 3 # type com = Assign of ide * expr | Ifthenelse of expr * com list * com list | While of expr * com list;; type com = | Assign of ide * expr | Ifthenelse of expr * com list * com list | While of expr * com list C ::= ifthenelse(E, C 1, C 2 ) | while(E, C 1 ) | assign(I, E) | cseq(C 1, C 2 ) # While(Den "x", [Assign("y", Plus(Val "y", Val "x"))]);; - : com = While (Den "x", [Assign ("y", Plus (Val "y", Val "x"))])

9 Un tipo primitivo utile: le liste # let l1 = [1; 2; 1];; val l1 : int list = [1; 2; 1] # let l2 = 3 :: l1;; val l2 : int list = [3; 1; 2; 1] # let l3 = l2;; val l3 : int list = [1; 2; 1; 3; 1; 2; 1] # List.hd l3;; - : int = 1 # List.tl l3;; - : int list = [2; 1; 3; 1; 2; 1] # List.length l3;; - : int = 7

10 Tipi e pattern matching type expr = | Den of ide | Fun of ide * expr | Plus of expr * expr | Apply of expr * expr type eval = | Int of int | Bool of bool | Efun of myfun | Unbound type myfun = eval -> eval type env = ide -> eval E (I) =    (I) E (plus(E 1, E 2 ))=   ( E (E 1 )  ) + ( E (E 2 )  ) E (lambda(I, E 1 ))=     E (E 1 ) [  / I  d ] E (apply(E 1, E 2 )) =   ( E (E 1 )  E (E 2 )  ) # let rec sem e rho = match e with | Den i -> rho i | Plus(e1, e2) -> plus(sem e1 rho, sem e2 rho) | Fun(i, e) -> Efun(function d -> sem e (bind(rho, i, d))) | Apply(e1, e2) -> match sem e1 rho with | Efun f -> f (sem e2 rho) | _ -> failwith("wrong application");; val sem : expr -> env -> eval =

11 Punti fissi type com = | Assign of ide * expr | Ifthenelse of expr * com list * com list | While of expr * com list type loc = int type store = loc -> eval val sem : expr -> env -> store -> eval = val semcl : com list -> env -> store -> store = C (while(E, C 1 )) =   (  f.  ’  if E (E)  ’ then f( C (C 1 )  ’ ) else  ’)  # let rec semc c rho sigma = match c with | While(e, cl) -> let functional ((fi: store -> store)) = function s -> if sem e rho s = Bool(true) then fi(semcl cl rho s) else s in let rec ssfix = function x -> functional ssfix x in ssfix(sigma) | _ ->.... val semc : com -> env -> store -> store =

12 Variabili e frammento imperativo # let x = ref(3);; val x : int ref = {contents=3} # !x;; - : int = 3 # x := 25;; - : unit = () # !x;; - : int = 25 # x := !x + 2; !x;; - : int = 27

13 Un tipo primitivo mutabile: l’array # let a = [| 1; 2; 3 |];; val a : int array = [|1; 2; 3|] # let b = Array.make 12 1;; val b : int array = [|1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1|] # Array.length b;; - : int = 12 # Array.get a 0;; - : int = 1 # Array.get b 12;; Uncaught exception: Invalid_argument("Array.get") # Array.set b 3 131;; - : unit = () # b;; - : int array = [|1; 1; 1; 131; 1; 1; 1; 1; 1; 1; 1; 1|]

14 Moduli: interfaccie # module type PILA = sig type 'a stack (* abstract *) val emptystack : 'a stack val push : 'a stack -> 'a -> 'a stack val pop : 'a stack -> 'a stack val top : 'a stack -> 'a end;; module type PILA = sig type 'a stack val emptystack : 'a stack val push : 'a stack -> 'a -> 'a stack val pop : 'a stack -> 'a stack val top : 'a stack -> 'a end

15 Moduli: implementazione # module SpecPila: PILA = struct type 'a stack = Empty | Push of 'a stack * 'a let emptystack = Empty let push p a = Push(p,a) let pop p = match p with | Push(p1, _) -> p1 let top p = match p with | Push(_, a) -> a end;; module SpecPila : PILA

16 Classi e oggetti # class point x_init = object val mutable x = x_init method get_x = x method move d = x <- x + d end;; class point : int -> object val mutable x : int method get_x : int method move : int -> unit end # let p = new point (3);; val p : point = # p#get_x;; - : int = 3 # p#move 33;; - : unit = () # p#get_x;; - : int = 36

17 Ereditarietà class point : int -> object val mutable x : int method get_x : int method move : int -> unit end # class colored_point x (c : string) = object inherit point x val c = c method color = c end;; class colored_point : int -> string -> object val c : string val mutable x : int method color : string method get_x : int method move : int -> unit end # let p' = new colored_point 5 "red";; val p' : colored_point = # p'#color;; - : string = "red"

18 Il linguaggio didattico §le cose semanticamente importanti di OCAML, meno l tipi (e pattern matching) l moduli l eccezioni