valex
play

Valex: Valex extends Bindex in the following ways: Dynamic Type - PowerPoint PPT Presentation

A New Mini-Language: Valex Valex: Valex extends Bindex in the following ways: Dynamic Type Checking o In addiCon to integer values, Valex also has boolean, character, string, symbol, and list values. and Desugaring A Valex program sCll


  1. A New Mini-Language: Valex Valex: Valex extends Bindex in the following ways: Dynamic Type Checking o In addiCon to integer values, Valex also has boolean, character, string, symbol, and list values. and Desugaring • A Valex program sCll takes a list of integers as arguments, but the result and intermediate values may be of any type. o Valex has an easy-to-extend library of primiCve operators for manipulaCng values of different types o Valex has a generalized primiCve operator applicaCon mechanism that performs dynamic type checking on the operands of primiCve operators CS251 Programming Languages o Valex has a condiConal ( if ) expression. Spring 2017, Lyn Turbak o Valex desugars numerous special forms into a small set of five kernel constructs: literals, variable references, primiCve applicaCons, bind expressions, condiConal expressions. Department of Computer Science Wellesley College 2 Valex Booleans Dynamic Type Checking of Primapps Valex dynamically checks the number and types of operands valex> (< 3 4) valex> (or (> 3 4) (> 5 5)) to primiCve applicaCons and reports dynamic type errors. #t #f valex> (= 3 4) valex> (< 5) valex> (and (< 4 3) #f Error: Expected two arguments but got: (5) (< 5 (/ 1 0))) Error: Division by 0: 1 valex> (!= 3 4) valex> (= 5 6 7) #t Error: Expected two arguments but got: (5 6 7) valex> (&& (< 4 3) (< 5 (/ 1 0))) valex> (not (= 3 4)) valex> (+ 1 #t) #f ; && is short-circuit and #t Error: Expected an integer but got: #t valex> (or (> 4 3) valex> (and (< 3 4) (>= 5 5)) valex> (and #t 3) (< 5 (/ 1 0))) #t Error: Expected a boolean but got: 3 Error: Division by 0: 1 valex> (and (< 3 4) (> 5 5)) valex> (= #t #f) valex> (|| (> 4 3) #f Error: Expected an integer but got: #t (< 5 (/ 1 0))) #t ; || is short-circuit or valex> (or (< 3 4) (> 5 5)) valex> (bool= #t #f) #t #f 3 4

  2. CondiConal ( if ) expresssions Mulitbranch condiConals ( cond ) Valex includes a mulCbranch cond condiConal like Racket’s cond : valex> (if (< 1 2) (+ 3 4) (* 5 6)) 7 (valex (x y) valex> (if (> 1 2) (+ 3 4) (* 5 6)) (cond ((< x y) -1) 30 ((= x y) 0) (else 1))) valex> (if (< 1 2) (+ 3 4) (/ 5 0)) 7 ; only evaluates then branch valex> (if (> 1 2) (+ 3 4 5) (* 5 6)) 30 ; only evaluates else branch valex> (if (- 1 2) (+ 3 4) (* 5 6)) Error: Non-boolean test value -1 in if expression racket> (if (- 1 2) (+ 3 4) (* 5 6)) 7 5 6 Strings Characters valex> (str= "foo" "bar") valex> (toString (* 3 4)) valex> (char= 'a' 'b') #f "12" #f valex> (str< "bar" "foo") valex> (toString (= 3 4)) valex> (char< 'a' 'b') #t "#f" #t valex> (str< "foo" "bar") valex> (char->int 'a') #f 97 Notes: The only string comparison ops • valex> (strlen "foo") valex> (int->char (- (char->int 'a') 32)) are str= and str< , though it 3 'A' would be easy to add others toString turns any Valex value • valex> (strlen "") The only character comparison ops are char= and char< , into a string. 0 though it would be easy to add others valex> (str+ "foo" "bar") "foobar” 7 8

  3. Symbols Lists Valex has Racket-like symbols that can only be valex> (prep 1 (prep 2 (prep 3 #e))) (1) tested for equality and (list 1 2 3) (2) converted to/from strings. valex> (prep (+ 3 4) (prep (= 3 4) (prep (str+ "foo" "bar") #e))) valex> (sym= (sym foo) (sym foo)) (list 7 #t "foo")) #t valex> (list (+ 3 4) (= 3 4) (str+ "foo" "bar")) valex> (sym= (sym foo) (sym bar)) (list 7 #f "foobar") #f valex> (head (list 7 #t "foo")) valex> (sym->string (sym baz)) 7 "baz" valex> (tail (list 7 #t "foo")) valex> (string->sym "quux") (list #t "foo") (sym quux) valex> (head (tail (list 7 #t "foo"))) #t valex> (head #e) 9 EvalError: Head of an empty list 10 More Lists Explode and implode valex> (empty? #e) valex> (explode "foobar") #t (list 'f' 'o' 'o' 'b' 'a' 'r') valex> (empty? (list 7 #t "foo")) valex> (implode (list 'C' 'S' '2' '5' '1')) #f "CS251" valex> (nth 1 (list 7 #t "foo")) 7 valex> (nth 2 (list 7 #t "foo")) #t valex> (nth 3 (list 7 #t "foo")) "foo" valex> (nth 0 (list 7 #t "foo")) EvalError: nth -- out-of-bounds index 0 valex> (nth 4 (list 7 #t "foo")) EvalError: nth -- out-of-bounds index 4 11 12

  4. Type Predicates General Equality valex> (string? 'a') #f valex> (int? 3) valex> (string? "a") valex> (equal? 3 3) #t #t #t valex> (int? #t) valex> (string? (sym a)) valex> (equal? 3 (+ 1 2)) #f #f #t valex> (bool? #t) valex> (sym? 'a') valex> (equal? (> 2 3) (< 6 5)) #t #f #t valex> (bool? 3) valex> (sym? "a") #f #f valex> (equal? (> 2 3) (< 5 6)) #f valex> (char? ’a’) valex> (sym? (sym a)) #t #t valex> (equal? (+ 1 2) (< 1 2)) #f valex> (char? "a") valex> (list? #e) #f #t valex> (equal? (list 5 6) (list (+ 2 3) (* 2 3))) valex> (char? (sym a)) #t valex> (list? (list 7 #f "foobar")) #f #t valex> (equal? (list #t) (list (< 1 2) (> 1 2))) valex> (string? 'a') valex> (list? "foo") #f #f #f 13 14 User-signaled errors Racket-like quote The Valex error operator takes a string message and any value and valex> (quote CS251) halts computaCon with an error message including this value: (sym CS251) valex> (quote 42) valex> (bind x 3 (if (< x 0) 42 (error "negative!" x) (* x x))) valex> (quote #t) #t 9 valex> (quote "bunny") valex> (bind x -3 (if (< x 0) "bunny" (error "negative!" x) Valex> (quote 'c') (* x x))) 'c' EvalError: Valex Error -- negative!: -3 valex> (quote (CS251 42 #t "bunny" 'c' (just like Racket!))) (list (sym CS251) 42 #t "bunny" 'c' (list (sym just) (sym like) (sym Racket!))) 15 16

  5. ImplementaCon Strategy bind vs. bindpar vs. bindseq In addiCon to bind , Valex also has a bindpar construct similar to Racket’s let and a bindseq construct similar to Racket’s let* . valex> (#args (a 2) (b 3)) kernel valex> (bindpar ((a (+ a b)) (b (* a b))) (list a b)) primitive (list 5 6) values/datatypes valex> (bindseq ((a (+ a b)) (b (* a b))) (list a b)) (list 5 15) syntactic sugar system libraries user libraries 17 18 Valex datatypes Valex has a Small Kernel type var = string Kernel has only 5 kinds of expressions! datatype pgm = Valex of var list * exp (* param names, body *) 1. Literals: integers, booleans, strings, characters, symbols and exp = 2. variable references, Lit of value 3. primiCve applicaCons (unlike in Bindex these can have | Var of var (* variable reference *) any number of operands of any type), | PrimApp of primop * exp list (* prim application with rator, rands *) | Bind of var * exp * exp (* bind name to value of defn in body *) 4. single-variable local variable declaraCons (i.e., bind ), | If of exp * exp * exp (* conditional with test, then, else *) 5. condiConal expressions (i.e., if ). and value = (* use value rather than val because val is an SML keyword *) Unlike Bindex, where the only expression values are integers, Int of int | Bool of bool Valex has 6 kinds of expression values: | Char of char | String of string 1. Integers | Symbol of string 2. Booleans | List of value list (* Recursively defined value *) 3. Strings  and primop = Primop of var * (value list -> value) 4. Characters (* Valex bakes the primop meaning function into the syntax! *) 5. Symbols 6. Lists of values (recursively defined) fun primopName (Primop(name,_)) = name fun primopFunction (Primop(_,fcn)) = fcn 19 20

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend