1 / 43

CS 476 – Programming Language Design

CS 476 – Programming Language Design. William Mansky. Language #1: Expressions. Simple arithmetic and boolean operations Every term computes to a value , either int or bool Arithmetic operators: plus, minus, times Boolean operators: and, or, not, comparison, if-then-else

harrisr
Télécharger la présentation

CS 476 – Programming Language Design

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 476 – Programming Language Design William Mansky

  2. Language #1: Expressions • Simple arithmetic and boolean operations • Every term computes to a value, either int or bool • Arithmetic operators: plus, minus, times • Boolean operators: and, or, not, comparison, if-then-else • 3 + 5 * 9 should compute to 48 • if 1 = 0 or 1 = 1 then 2 else 4 should compute to 2

  3. Expressions: Syntax • Arithmetic operators: plus, minus, times • Boolean operators: and, or, not, comparison, if-then-else

  4. Expressions: Syntax type exp = Int of int | Add of exp * exp | … | Bool of bool | And of exp * exp | … | Not of exp | Eq of exp * exp | If of exp * exp * exp E ::= <#> | E + E | E – E | E * E | <bool> | E and E | E or E | not E | E = E | if E then E else E

  5. Expressions: Interpreter • Every term computes to a value, either int or bool type value = IntV of int | BoolV of bool let rec eval (e : exp) : value = (* let rec eval e = *) match e with | Int i -> | Add (e1, e2) -> | …

  6. Expressions: Interpreter • Every term computes to a value, either int or bool type value = IntV of int | BoolV of bool let rec eval (e : exp) : value = (* let rec eval e = *) match e with | Int i -> IntVi | Add (e1, e2) -> | …

  7. Expressions: Interpreter • Every term computes to a value, either int or bool type value = IntV of int | BoolV of bool let rec eval (e : exp) : value = (* let rec eval e = *) match e with | Int i -> IntVi | Add (e1, e2) -> eval e1 + eval e2 | … Type error!

  8. Expressions: Interpreter let rec eval (e : exp) : value = match e with | Int i -> IntVi | Add (e1, e2) -> (match eval e1, eval e2 with | IntV i1, IntV i2 -> IntV (i1 + i2) | _, _ -> ?) • Should our language have runtime errors?

  9. Expressions: Interpreter with Errors let rec eval (e : exp) : value = match e with | Int i -> IntVi | Add (e1, e2) -> (match eval e1, eval e2 with | IntV i1, IntV i2 -> IntV (i1 + i2) | _, _ -> None) type ‘a option = Some of ‘a | None

  10. Expressions: Interpreter with Errors let rec eval (e : exp) : value option = match e with | Int i -> IntVi | Add (e1, e2) -> (match eval e1, eval e2 with | IntV i1, IntV i2 -> IntV (i1 + i2) | _, _ -> None) type ‘a option = Some of ‘a | None

  11. Expressions: Interpreter with Errors let rec eval (e : exp) : value option = match e with | Int i -> Some (IntVi) | Add (e1, e2) -> (match eval e1, eval e2 with | Some (IntV i1), Some (IntV i2) -> Some (IntV (i1 + i2)) | _, _ -> None) type ‘a option = Some of ‘a | None

  12. Expressions: Interpreter with Errors let rec eval (e : exp) : value option = match e with | … | Bool b -> Some (BoolV b) | And (e1, e2) -> (match eval e1, eval e2 with | Some (BoolV b1), Some (BoolV b2) -> Some (BoolV (b1 && b2)) | _, _ -> None)

  13. Expressions: Interpreter with Errors let rec eval (e : exp) : value option = match e with | … | Eq (e1, e2) -> (match eval e1, eval e2 with | Some (IntV i1), Some (IntV i2) -> Some (BoolV (i1 = i2)) | …) • What kinds of results should we be able to compare?

  14. Expressions: Interpreting Comparison let rec eval (e : exp) : value option = match e with | … | Eq (e1, e2) -> (match eval e1, eval e2 with | Some v1, Some v2 -> Some (BoolV (v1 = v2)) | …)

  15. Expressions: Interpreting Comparison let rec eval (e : exp) : value option = match e with | … | Eq (e1, e2) -> Some (BoolV (eval e1 = eval e2)) • Should we be able to compare ints and bools? Should two erroneous expressions be equal? Depends on what kind of language we want!

  16. Expressions: Interpreter with Errors let rec eval (e : exp) : value option = match e with | … | If (e, e1, e2) -> (match eval e with | Some (BoolV b) -> if b then eval e1 else eval e2 | _ -> None)

  17. Expressions: Int-only Interpreter let rec eval (e : exp) : int = match e with | Int i -> i | Add (e1, e2) -> eval e1 + eval e2 | Bool b -> if b then 1 else 0 | … | If (e, e1, e2) -> if eval e <> 0 then eval e1 else eval e2 • Simpler interpreter, but behavior may surprise programmers!

  18. Structure of a language • Syntax • Concrete: what do programs look like? • Abstract: what are the pieces of a program? • Semantics • Static: which programs make sense? • Dynamic: what do programs do when we run them? • Pragmatics • Implementation: how can we actually make the semantics happen? • IDE, tool support, etc.

  19. Static Semantics: Types Type systems do several things: • Help programmers tell different kinds of program expressions apart • Stop bad programs from running/catch errors early • Tell us what kind of value to expect from a piece of code

  20. Static Semantics: Types • A type system is a relation between terms and types • We write for “term has type ” • We define type systems using inference rules: • Often sound: implies that evaluates to a value of type • Often conservative: not all programs that evaluate are well-typed

  21. Expressions: Types • Types: int, bool • Rules:

  22. Expressions: Types • Types: int, bool • Rules:

  23. Expressions: Types • Types: int, bool • Rules:

  24. Proof Trees

  25. Proof Trees

  26. Proof Trees

  27. Proof Trees

  28. Proof Trees

  29. Proof Trees

  30. Proof Trees

  31. Proof Trees

  32. Proof Trees

  33. Expressions: Type Checking • Types: int, bool type typ = TInt | TBool • These are “object-level” (expression) types, not “meta-level” (OCaml) types!

  34. Expressions: Type Checking type typ = TInt | TBool • These are “object-level” (expression) types, not “meta-level” (OCaml) types! let rec typecheck (e : exp) (t : typ) : bool = match e with | Int i -> | Bool b -> | Add (e1, e2) -> …

  35. Expressions: Type Checking type typ = TInt | TBool • These are “object-level” (expression) types, not “meta-level” (OCaml) types! let rec typecheck (e : exp) (t : typ) : bool = match e with | Int i -> t = TInt | Bool b -> | Add (e1, e2) -> …

  36. Expressions: Type Checking type typ = TInt | TBool • These are “object-level” (expression) types, not “meta-level” (OCaml) types! let rec typecheck (e : exp) (t : typ) : bool = match e with | Int i -> t = TInt | Bool b -> t = TBool | Add (e1, e2) -> …

  37. Expressions: Type Checking type typ = TInt | TBool • These are “object-level” (expression) types, not “meta-level” (OCaml) types! let rec typecheck (e : exp) (t : typ) : bool = match e with | Int i -> t = TInt | Bool b -> t = TBool | Add (e1, e2) -> …

  38. Expressions: Type Checking type typ = TInt | TBool • These are “object-level” (expression) types, not “meta-level” (OCaml) types! let rec typecheck (e : exp) (t : typ) : bool = match e with | Int i -> t = TInt | Bool b -> t = TBool | Add (e1, e2) -> typecheck e1 TInt && typecheck e2 Tint && t = TInt …

More Related