Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming LanguagesSection 61 Programming Languages Section 6. Racket v.s. ML Xiaojuan Cai Spring 2015.

Similar presentations


Presentation on theme: "Programming LanguagesSection 61 Programming Languages Section 6. Racket v.s. ML Xiaojuan Cai Spring 2015."— Presentation transcript:

1 Programming LanguagesSection 61 Programming Languages Section 6. Racket v.s. ML Xiaojuan Cai Spring 2015

2 Programming LanguagesSection 62 Roadmap Building new types in Racket Towards implementing a language ML versus Racket Type systems Static versus Dynamic typing

3 Programming LanguagesSection 63 Life without datatypes Racket has nothing like a datatype binding for one-of types No need in a dynamically typed language: Can just mix values of different types and use primitives like number?, string?, pair?, etc. to “see what you have” Can use cons cells to build up any kind of data

4 Programming LanguagesSection 64 Mixed collections In ML, cannot have a list of “ints or strings,” so use a datatype: How to implement this in Racket? datatype int_or_string = I of int | S of string fun funny_sum xs = (* int_or_string list -> int *) case xs of [] => 0 | (I i)::xs’ => i + funny_sum xs’ | (S s)::xs’ => String.size s + funny_sum xs’

5 Programming LanguagesSection 65 Recursive structures More interesting datatype-programming we know: datatype exp = Const of int | Negate of exp | Add of exp * exp | Multiply of exp * exp fun eval_exp e = case e of Constant i => i | Negate e2 => ~ (eval_exp e2) | Add(e1,e2) => (eval_exp e1) + (eval_exp e2) | Multiply(e1,e2)=>(eval_exp e1)*(eval_exp e2)

6 Programming LanguagesSection 66 Change how we do this Previous version of eval_exp has type exp -> int From now on will write such functions with type exp -> exp New way in Racket: Using lists where car of list encodes “what kind of exp”

7 Programming LanguagesSection 67 Symbols Will not focus on Racket symbols like 'foo, but in brief: Syntactically start with quote character Like strings, can be almost any character sequence Unlike strings, compare two symbols with eq? which is fast

8 Programming LanguagesSection 68 Eval Racket, Scheme, LISP, Javascript, Ruby, … have eval At run-time create some data (in Racket a nested list, in Javascript a string) however you want Then treat the data as a program and run it Since we do not know ahead of time what data will be created, we need a language implementation at run-time to support eval

9 Programming LanguagesSection 69 eval in Racket Appropriate idioms for eval are a matter of contention Programs with eval are harder to analyze (define (make-some-code y) ; just returns a list (if y (list 'begin (list 'print "hi") (list '+ 4 2)) (list '+ 5 3))) (eval (make-some-code #t)) ; prints "hi", result 6

10 Programming LanguagesSection 610 Quote Quoting (quote …) or '(…) is a special form that makes “everything underneath” atoms and lists, not variables and calls But then calling eval on it looks up symbols as code So quote and eval are inverses (list 'begin (list 'print "hi") (list '+ 4 2)) (quote (begin (print "hi") (+ 4 2)))

11 Programming LanguagesSection 611 Contrasting Approaches Versus This is not a case of syntactic sugar (struct add (e1 e2) #:transparent) (define (add e1 e2) (list 'add e1 e2)) (define (add? e) (eq? (car e) 'add)) (define (add-e1 e) (car (cdr e))) (define (add-e2 e) (car (cdr (cdr e))))

12 Programming LanguagesSection 612 The key difference The result of calling (add x y) is not a list And there is no list for which add? returns #t struct makes a new kind of thing: extending Racket with a new kind of data So calling car, cdr, or mult-e1 on “an add” is a run-time error (struct add (e1 e2) #:transparent)

13 Programming LanguagesSection 613 List approach is error-prone Can break abstraction by using car, cdr, and list-library functions directly on “add expressions” Can make data that add? wrongly answers #t to (cons 'add "I am not an add") (define (add e1 e2) (list 'add e1 e2)) (define (add? e) (eq? (car e) 'add)) (define (add-e1 e) (car (cdr e))) (define (add-e2 e) (car (cdr (cdr e))))

14 Programming LanguagesSection 614 Advantages of Struct Struct approach: Is better style and more concise for defining data types Is about equally convenient for using data types But much better at timely errors when misusing data types Cannot access functions on wrong kind of data Cannot confuse tester functions

15 Programming LanguagesSection 615 More with abstraction Struct approach is even better combined with other Racket features not discussed here: The module system lets us hide the constructor function to enforce invariants List-approach cannot hide cons from clients The contract system lets us check invariants even if constructor is exposed For example, fields of “an add” must also be “expressions”

16 Programming LanguagesSection 616 Struct is special Often we end up learning that some convenient feature could be coded up with other features Not so with struct definitions: Neither functions nor macros can create a new kind of data Result of constructor function returns #f for every other tester function: number?, pair?, other structs’ tester functions, etc.

17 Programming LanguagesSection 617 Where are we? Building new types in Racket Towards implementing a language ML versus Racket Type systems: Static versus Dynamic typing Static versus Dynamic typing

18 Programming LanguagesSection 618 Typical workflow "(fn x => x + x) 4” Parsing Call Function + Constant 4 x xx Var Type checking? Possible errors / warnings Possible errors / warnings concrete syntax (string) abstract syntax (tree)

19 Programming LanguagesSection 619 Interpreter or compiler Fundamentally, two approaches to implement a PL B: Write an interpreter in another language A Better names: evaluator, executor Take a program in B and produce an answer (in B) Write a compiler in another language A to a third language C Better name: translator Translation must preserve meaning (equivalence) We call A the metalanguage.

20 Programming LanguagesSection 620 Reality more complicated Evaluation (interpreter) and translation (compiler) are your options But in modern practice have both and multiple layers A plausible example: Java compiler to bytecode intermediate language Have an interpreter for bytecode (itself in binary), but compile frequent functions to binary at run-time Racket uses a similar mix

21 Programming LanguagesSection 621 Sermon Interpreter versus compiler versus combinations is about a particular language implementation, not the language definition So there is no such thing as a “compiled language” or an “interpreted language” Programs cannot “see” how the implementation works

22 Programming LanguagesSection 622 Typical workflow "(fn x => x + x) 4” Parsing Call Function + Constant 4 x xx Var Type checking? Possible errors / warnings Possible errors / warnings concrete syntax (string) abstract syntax (tree) Interpreter or translater

23 Programming LanguagesSection 623 Skipping parsing ; define B’s abstract syntax (struct call …)) (struct function …) (struct var …) … ; example B program (call (function (list “x”) (add (var “x”) (var “x”))) (const 4)) Call Function + Constant 4 x xx Var

24 Programming LanguagesSection 624 Already did an example! Let the metalanguage A = Racket Let the language-implemented B = “Arithmetic Language” Arithmetic programs written with calls to Racket constructors The interpreter is eval-exp (struct const (int) #:transparent) (struct negate (e) #:transparent) (struct add (e1 e2) #:transparent) (struct multiply (e1 e2) #:transparent) (define (eval-exp e) (cond [(const? e) e] [(negate? e) (const (- (const-int (eval-exp (negate-e e)))))] [(add? e) …] [(multiply? e) …]… Racket data structure is Arithmetic Language program, which eval- exp runs

25 Programming LanguagesSection 625 What we know Define (abstract) syntax of language B with Racket structs B called MUPL in homework Write B programs directly in Racket via constructors Implement interpreter for B as a (recursive) Racket function Now, a subtle-but-important distinction: Interpreter can assume input is a “legal AST for B” Interpreter must check that recursive results are the right kind of value

26 Programming LanguagesSection 626 Legal ASTs Can assume “right types” for struct fields const holds a number negate holds a legal AST add and multiply hold 2 legal ASTs Illegal ASTs can “crash the interpreter” – this is fine (multiply (add (const 3) "uh-oh") (const 4)) (negate -7)

27 Programming LanguagesSection 627 Interpreter results Our interpreters return expressions, but not any expressions Result should always be a value, a kind of expression that evaluates to itself If not, the interpreter has a bug So far, only values are from const, e.g., (const 17) But a larger language has more values than just numbers Booleans, strings, etc. Pairs of values (definition of value recursive) Closures …

28 Programming LanguagesSection 628 Example What if the program is a legal AST, but evaluation of it tries to use the wrong kind of value? For example, “add a boolean” You should detect this and give an error message not in terms of the interpreter implementation Means checking a recursive result whenever a particular kind of value is needed No need to check if any kind of value is okay (struct bool (b) #:transparent) (struct eq-num (e1 e2) #:transparent) (struct if-then-else (e1 e2 e3) #:transparent)

29 Programming LanguagesSection 629 Dealing with variables Interpreters so far have been for languages without variables No let-expressions, functions-with-arguments, etc. Language in homework has all these things

30 Programming LanguagesSection 630 Dealing with variables An environment is a mapping from variables to values Only ever put pairs of strings and values in the environment Evaluation takes place in an environment Environment passed as argument to interpreter helper function A variable expression looks up the variable in the environment Most subexpressions use same environment as outer expression A let-expression evaluates its body in a larger environment

31 Programming LanguagesSection 631 The Set-up So now a recursive helper function has all the interesting stuff: Recursive calls must “pass down” correct environment Then eval-exp just calls eval-under-env with same expression and the empty environment (define (eval-under-env e env) (cond … ; case for each kind of )) ; expression

32 Programming LanguagesSection 632 A grading detail Stylistically eval-under-env would be a helper function one could define locally inside eval-exp But do not do this on your homework We have grading tests that call eval-under- env directly, so we need it at top-level

33 Programming LanguagesSection 633 The best part The most interesting and mind-bending part of the homework is that the language being implemented has first-class closures With lexical scope of course Fortunately, what you have to implement is what we have been stressing since we first learned about closures…

34 Programming LanguagesSection 634 Higher-order functions The interpreter uses a closure data structure (with two parts) to keep the environment it will need to use later Evaluate a function expression: A function is not a value; a closure is a value Evaluating a function returns a closure Create a closure out of (a) the function and (b) the current environment when the function was evaluated Evaluate a function call: … (struct closure (env fun) #:transparent)

35 Programming LanguagesSection 635 Function calls Use current environment to evaluate e1 to a closure Error if result is a value that is not a closure Use current environment to evaluate e2 to a value Evaluate closure’s function’s body in the closure’s environment, extended to: Map the function’s argument-name to the argument-value And for recursion, map the function’s name to the whole closure (call e1 e2)

36 Programming LanguagesSection 636 Is that expensive? Time to build a closure is tiny: a struct with two fields Space to store closures might be large if environment is large But environments are immutable, so natural and correct to have lots of sharing, e.g., of list tails Alternative used in practice: When creating a closure, store a possibly-smaller environment holding only the variables that are free variables in the function body

37 Programming LanguagesSection 637 Free variables examples (lambda () (+ x y z)) ; {x, y, z} (lambda (x) (+ x y z)) ; {y, z} (lambda (x) (if x y z)) ; {y, z} (lambda (x) (let ([y 0]) (+ x y z))) ; {z} (lambda (x y z) (+ x y z)) ; {} (lambda (x) (+ y (let ([y z]) (+ y y)))) ; {y, z}

38 Programming LanguagesSection 638 Computing free variables So does the interpreter have to analyze the code body every time it creates a closure? No: Before evaluation begins, compute free variables of every function in program and store this information with the function Compared to naïve store-entire-environment approach, building a closure now takes more time but less space And time proportional to number of free variables And various optimizations are possible

39 Programming LanguagesSection 639 Compiling higher-order functions If we are compiling to a language without closures (like assembly), cannot rely on there being a “current environment” So compile functions by having the translation produce “regular” functions that all take an extra explicit argument called “environment” And compiler replaces all uses of free variables with code that looks up the variable using the environment argument Running program still creates closures and every function call passes the closure’s environment to the closure’s code

40 Programming LanguagesSection 640 Recall… Our approach to language implementation: Implementing language B in language A Skipping parsing by writing language B programs directly in terms of language A constructors An interpreter written in A recursively evaluates What we know about macros: Extend the syntax of a language Use of a macro expands into language syntax before the program is run, i.e., before calling the main interpreter function

41 Programming LanguagesSection 641 Put it together With our set-up, we can use language A (i.e., Racket) functions that produce language B abstract syntax as language B “macros” Language B programs can use the “macros” as though they are part of language B No change to the interpreter or struct definitions Just a programming idiom enabled by our set- up Helps teach what macros are “macro expansion” happens before calling eval-exp

42 Programming LanguagesSection 642 Optional: Hygiene issues Earlier we had material on hygiene issues with macros (Among other things), problems with shadowing variables when using local variables to avoid evaluating expressions more than once The “macro” approach described here does not deal well with this

43 Programming LanguagesSection 643 Where are we? Building new types in Racket Towards implementing a language ML versus Racket Type systems: Static versus Dynamic typing Static versus Dynamic typing

44 Programming LanguagesSection 644 Key differences Racket and ML have much in common Key differences Syntax Pattern-matching vs. struct-tests and accessor- functions Semantics of various let-expressions … Biggest difference: ML’s type system and Racket’s lack thereof

45 Programming LanguagesSection 645 ML from a Racket perspective Syntax, etc. aside, ML is like a well-defined subset of Racket Many of the programs it disallows have bugs But other programs it disallows I may actually want to write  (define (f x) (if (> x 0) #t (list 1 2))) (define xs (list 1 #t "hi")) (define y (f (car xs))) (define (g x) (+ x x)) ; ok (define (f y) (+ y (car y))) (define (h z) (g (cons z 2)))

46 Programming LanguagesSection 646 Racket from an ML Perspective One way to describe Racket is that it has “one big datatype” Constructors are applied implicitly (values are tagged) 42 is really like Int 42 Primitives implicitly check tags and extract data, raising errors for wrong constructors fun car v = case v of Pair(a,b) => a | _ => raise … fun pair? v = case v of Pair _ => true | _ => false datatype theType = Int of int | String of string | Pair of theType * theType | Fun of theType -> theType | … inttag 42

47 Programming LanguagesSection 647 More on The One Type Built-in constructors for “theType”: numbers, strings, booleans, pairs, symbols, procedures, etc. Each struct-definition creates a new constructor, dynamically adding to “theType”

48 Programming LanguagesSection 648 Where are we? Building new types in Racket Towards implementing a language ML versus Racket Type systems Static versus Dynamic typing

49 Programming LanguagesSection 649 Static checking Static checking is anything done to reject a program after it (successfully) parses but before it runs Part of a PL’s definition: what static checking is performed Common way to define a PL’s static checking is via a type system Dynamically-typed languages do (almost) no static checking

50 Programming LanguagesSection 650 Type system The type system of a language consists of a set of inductive definitions with judgment form: Γ |-- e: t Γ is a typing context (type map) which is a set of hypothesis of the form x: t —x is the variable name appearing in e —t is a type that’s bound to x If the current typing context is Γ, then expression e has type t.

51 Programming LanguagesSection 651 Example: Simply typed Lambda-calculus E ::= x variables | E 1 E 2 function application | x. E function creation

52 Programming LanguagesSection 652 Type system: types + type rules

53 Programming LanguagesSection 653 Type system: properties Standard properties: type safety Progress: well-typed term is not stuck if Γ|-e: t, then either e is a value or e -> e’ Preservation: well-typed term preserves type if Γ|-e: t and e -> e’, then Γ|-e’: t

54 Programming LanguagesSection 654 Simply typed Lambda-calculus, What types prevents Divergence: ( x. xx)( x. xx) Recursion: Y-combinator... Simply typed lambda-calculus is not Turing Complete

55 Programming LanguagesSection 655 ML, what types prevent In ML, type-checking ensures a program (when run) will never have: A primitive operation used on a value of the wrong type Arithmetic on a non-number e1 e2 where e1 does not evaluate to a function A non-boolean between if and then A variable not defined in the environment A pattern-match with a redundant pattern Code outside a module call a function not in the module’s signature …

56 Programming LanguagesSection 656 ML, what types allow In ML, type-checking does not prevent any of these errors Instead, detected at run-time Calling functions such that exceptions occur, e.g., hd [] An array-bounds error Division-by-zero In general, no type system prevents logic / algorithmic errors.

57 Programming LanguagesSection 657 Purpose is to prevent something Language design includes deciding what is checked and how Hard part is making sure the type system “achieves its purpose” That “the how” accomplishes “the what” More precise definition next

58 Programming LanguagesSection 658 A question of eagerness “Catching a bug before it matters” is in inherent tension with “Don’t report a bug that might not matter” Silly example: Suppose we just want to prevent evaluating 3/0 Keystroke time: disallow it in the editor Compile time: disallow it if seen in code Link time: disallow it if seen in code that may be called to evaluate main Run time: disallow it right when we get to the division Later: Instead of doing the division, return +inf.0 instead

59 Programming LanguagesSection 659 Correctness Suppose a type system is supposed to prevent X for some X A type system is sound if it never accepts a program that, when run with some input, does X No false negatives A type system is complete if it never rejects a program that, no matter what input it is run with, will not do X No false positives The goal is usually for a PL type system to be sound (so you can rely on it) but not complete Notice soundness/completeness is with respect to X

60 Programming LanguagesSection 660 Incompleteness A few functions ML rejects even though they do not divide by a string fun f1 x = 4 div "hi" (* but f1 never called *) fun f2 x = if true then 0 else 4 div "hi" fun f3 x = if x then 0 else 4 div "hi" val x = f3 true fun f4 x = if x <= abs x then 0 else 4 div "hi" fun f5 x = 4 div x val y = f5 (if true then 1 else "hi")

61 Programming LanguagesSection 661 Why incompleteness Almost anything you might like to check statically is undecidable: Any static checker cannot do all of: (1) always terminate, (2) be sound, (3) be complete Examples: Will this function terminate on some input? Will this function ever use a variable not in the environment? Will this function treat a string as a function? Will this function divide by zero? Undecidability is an essential concept at the core of computing

62 Programming LanguagesSection 662 Weak typing Weak typing: There exist programs that, by definition, must pass static checking but then when run can “set the computer on fire”? Ease of language implementation: Checks left to the programmer Weak typing is a poor name: Really about doing neither static nor dynamic checks A big problem is array bounds, which most PLs check dynamically

63 Programming LanguagesSection 663 What weak typing has caused Old now-much-rarer saying: “strong types for weak minds” Reality: humans are really bad at avoiding bugs We need all the help we can get! And type systems have gotten much more expressive (fewer false positives)

64 Programming LanguagesSection 664 Example: Racket Racket is not weakly typed It just checks most things dynamically* Not having ML or Java’s rules can be convenient Cons cells can build anything Anything except #f is true … This is nothing like the “catch-fire semantics” of weak typing *Checks macro usage and undefined-variables in modules statically

65 Programming LanguagesSection 665 Another misconception What operations are primitives defined on and when an error? Example: Is “foo” + “bar” allowed? Example: Is “foo” + 3 allowed? Example: Is arr[10] allowed if arr has only 5 elements? This is not static vs. dynamic checking It is “what is the run-time semantics of the primitive” It is related because it also involves trade-offs between catching bugs sooner versus maybe being more convenient Racket generally less lenient on these things than, e.g., Ruby

66 Programming LanguagesSection 666 Where are we? Building new types in Racket Towards implementing a language ML versus Racket Type systems Static versus Dynamic typing

67 Programming LanguagesSection 667 Now can argue… Having carefully stated facts about static checking, can now consider arguments about which is better: static checking or dynamic checking Remember most languages do some of each For example, perhaps types for primitives are checked statically, but array bounds are not

68 Programming LanguagesSection 668 Claim 1a: Dynamic is more convenient Dynamic typing lets you build a heterogeneous list or return a “number or a string” without workarounds (define (f y) (if (> y 0) (+ y y) "hi")) (let ([ans (f x)]) (if (number? ans) (number->string ans) ans)) datatype t = Int of int | String of string fun f y = if y > 0 then Int(y+y) else String "hi" case f x of Int i => Int.toString i | String s => s

69 Programming LanguagesSection 669 Claim 1b: Static is more convenient Can assume data has the expected type without cluttering code with dynamic checks or having errors far from the logical mistake (define (cube x) (if (not (number? x)) (error "bad arguments") (* x x x))) (cube 7) fun cube x = x * x * x cube 7

70 Programming LanguagesSection 670 Claim 2a: Static prevents useful programs Any sound static type system forbids programs that do nothing wrong, forcing programmers to code around limitations fun f g = (g 7, g true) (* does not type-check *) val pair_of_pairs = f (fn x => (x,x)) (define (f g) (cons (g 7) (g #t))) (define pair_of_pairs (f (lambda (x) (cons x x))))

71 Programming LanguagesSection 671 Claim 2b: Static lets you tag as needed Rather than suffer time, space, and late-errors costs of tagging everything, statically typed languages let programmers “tag as needed” (e.g., with datatypes) datatype tort = Int of int | String of string | Cons of tort * tort | Fun of tort -> tort | … if e1 then Fun (fn x => case x of Int i => Int (i*i*i)) else Cons (Int 7, String "hi")

72 Programming LanguagesSection 672 Claim 3a: Static catches bugs earlier Static typing catches many simple bugs as soon as “compiled” Since such bugs are always caught, no need to test for them In fact, can code less carefully and “lean on” type-checker (define (pow x) ; curried (lambda (y) (if (= y 0) 1 (* x (pow x (- y 1)))))) ; oops fun pow x y = (* does not type-check *) if y = 0 then 1 else x * pow (x,y-1)

73 Programming LanguagesSection 673 Claim 3b: Static catches only easy bugs But static often catches only “easy” bugs, so you still have to test your functions, which should find the “easy” bugs too (define (pow x) ; curried (lambda (y) (if (= y 0) 1 (+ x ((pow x) (- y 1)))))) ; oops fun pow x y = (* curried *) if y = 0 then 1 else x + pow x (y-1) (* oops *)

74 Programming LanguagesSection 674 Claim 4a: Static typing is faster Language implementation: Does not need to store tags (space, time) Does not need to check tags (time) Your code: Does not need to check arguments and results

75 Programming LanguagesSection 675 Claim 4b: Dynamic typing is faster Your code: Do not need to “code around” type-system limitations with extra tags, functions etc.

76 Programming LanguagesSection 676 Claim 5a: Code reuse easier with dynamic Without a restrictive type system, more code can just be reused with data of different types If you use cons cells for everything, libraries that work on cons cells are useful

77 Programming LanguagesSection 677 Claim 5b: Code reuse easier with static If you use cons cells for everything, you will confuse what represents what and get hard-to- debug errors Use separate static types to keep ideas separate Static types help avoid library misuse

78 Programming LanguagesSection 678 So far Considered 5 things important when writing code: 1. Convenience 2. Not preventing useful programs 3. Catching bugs early 4. Performance 5. Code reuse But took the naive view that software is developed by taking an existing spec, coding it up, testing it, and declaring victory. Reality: Often a lot of prototyping before a spec is stable Often a lot of maintenance / evolution

79 Programming LanguagesSection 679 Claim 6a: Dynamic better for prototyping Early on, you may not know what cases you need in datatypes and functions So you make premature commitments to data structures And end up writing code to appease the type- checker that you later throw away Particularly frustrating while prototyping

80 Programming LanguagesSection 680 Claim 6b: Static better for prototyping What better way to document your evolving decisions on data structures and code-cases than with the type system? New, evolving code most likely to make inconsistent assumptions Easy to put in temporary stubs as necessary, such as | _ => raise Unimplemented

81 Programming LanguagesSection 681 Claim 7a: Dynamic better for evolution Can change code to be more permissive without affecting old callers Take an int or a string instead of an int All ML callers must now use a constructor on arguments and pattern-match on results Existing Racket callers can be oblivious (define (f x) (* 2 x))(define (f x) (if (number? x) (* 2 x) (string-append x x))) fun f x = 2 * x fun f x = case f x of Int i => Int (2 * i) | String s => String(s ^ s)

82 Programming LanguagesSection 682 Claim 7b: Static better for evolution When we change type of data or code, the type- checker gives us a “to do” list of everything that must change Avoids introducing bugs The more of your spec that is in your types, the more the type-checker lists what to change when your spec changes Example: Changing the return type of a function Example: Adding a new constructor to a datatype Good reason not to use wildcard patterns

83 Programming LanguagesSection 683 Coda Static vs. dynamic typing is too coarse a question Better question: What should we enforce statically? Legitimate trade-offs you should know Rational discussion informed by facts! Ideal (?): Flexible languages allowing best-of- both-worlds? Would programmers use such flexibility well? Who decides?

84 Programming LanguagesSection 684 Conclusion Building new types in Racket Towards implementing a language ML versus Racket Type systems: Static versus Dynamic typing


Download ppt "Programming LanguagesSection 61 Programming Languages Section 6. Racket v.s. ML Xiaojuan Cai Spring 2015."

Similar presentations


Ads by Google