CMSC 330: Organization of Programming Languages. Rust Basics

Similar documents
CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Ownership, References, and Lifetimes in Rust

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Common Programming. Variables and Mutability

Introduction to Programming Using Java (98-388)

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Introduction to OCaml

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

Functions. Nate Foster Spring 2018

CMSC 330: Organization of Programming Languages. Lets, Tuples, Records

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Lexical Considerations

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

COMP520 - GoLite Type Checking Specification

CSCI 2041: Basic OCaml Syntax and Features

1 Lexical Considerations

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

COMP520 - GoLite Type Checking Specification

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Programmazione Avanzata

Lexical Considerations

1: /////////////// 2: // 1. Basics // 3: /////////////// 4: 5: // Functions. i32 is the type for 32-bit signed integers 6: fn add2(x: i32, y: i32) ->

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Begin at the beginning

Chapter 4 Defining Classes I

CS 251 Intermediate Programming Java Basics

COMP520 - GoLite Type Checking Specification

CS 11 Haskell track: lecture 1

Intermediate Code Generation

Type Conversion. and. Statements

CMSC 330: Organization of Programming Languages

Overloading, Type Classes, and Algebraic Datatypes

CMSC 330: Organization of Programming Languages. Operational Semantics

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

CPSC 3740 Programming Languages University of Lethbridge. Control Structures

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

CMSC 330: Organization of Programming Languages. Strings, Slices, Vectors, HashMaps in Rust

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

Exercises on ML. Programming Languages. Chanseok Oh

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

CSE 130 [Winter 2014] Programming Languages

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Lecture #23: Conversion and Type Inference

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CSE 341: Programming Languages

Type Soundness. Type soundness is a theorem of the form If e : τ, then running e never produces an error

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Programming Languages

Typed Racket: Racket with Static Types

IC Language Specification

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CMSC 330: Organization of Programming Languages

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Absolute C++ Walter Savitch

The Typed Racket Guide

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

CSCI-GA Scripting Languages

Recap: Functions as first-class values

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Language Proposal: tail

CMSC330. Objects, Functional Programming, and lambda calculus

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

SML A F unctional Functional Language Language Lecture 19

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

CS558 Programming Languages

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Chapter 8. Statement-Level Control Structures

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Scala : an LLVM-targeted Scala compiler

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

CSE 341 Section 5. Winter 2018

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

Programming Languages, Summary CSC419; Odelia Schwartz

CSC Java Programming, Fall Java Data Types and Control Constructs

CMSC 330, Fall 2013, Practice Problem 3 Solutions

Pace University. Fundamental Concepts of CS121 1

Haskell Overview III (3A) Young Won Lim 10/4/16

Problem Solving with C++

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Principles of Programming Languages

CMSC330 Spring 2016 Midterm #1 9:30am/12:30pm/3:30pm

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

The Warhol Language Reference Manual

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

Transcription:

CMSC 330: Organization of Programming Languages Rust Basics CMSC330 Spring 2018 1

Organization It turns out that a lot of Rust has direct analogues in OCaml So we will introduce its elements with comparisons let rec fact n = let rec fact n = if n = 0 then 1 else let x = fact (n-1) in let rec fact n = n * x let x = fact (n-1) in n * x if n = 0 then 1 else let x = fact (n-1) in n * x 2

Factorial in Rust (recursively) rec by default physical eq (no built-in structural eq) Rust fn fact(n:i32) -> i32 { if n == 0 { 1 else { let x = fact(n-1); n * x parm, return types explicit block (expression) local var type inferred let rec fact n = if n = 0 then 1 else let x = fact (n-1) in n * x OCaml 3

Running factorial Rust programs start at main As in C and Java fn main() { let res = fact(6); println!( fact(6) = {,res); on call: arg parens required Prints fact(6) = 720 format string string conversion Aside: command-line args via env::args 4

Block Expressions block Syntax { stmt* e? i.e., zero or more statements (separated by semi-colons) followed by an optional final expression Evaluation Evaluate each stmt; result is evaluation of e Or () if e is absent Type checking Must type check each stmt, extending environment of subsequent stmts with added let-bindings Final type is the type of e Or unit if e is absent 5

If Expressions (not Statements) Syntax if e block1 block2 e is the guard block1 and block2 are the true/false branches Evaluation Evaluate e to v Result is evaluation of block1 if v is true; or block2 if v is false Type checking e : bool block1 : t and block2 : t for some t 6

Functions Syntax fn f(parms) [-> t] block f is the function name parms are formal parameters, including their types Zero or more; have form x1:t1,, xn:tn t is the return type May be omitted if function returns unit value () block is the body, which is a block expression 7

Let Statements Syntax let [mut]? x[:t]? = e; Keyword is mut optional Type t is optional; often can be inferred if missing Evaluation Evaluate e to v; set x to v within the defining scope x is immutable unless mut keyword is present Type checking If type t given, then e : t required Else e should have some type t, which is inferred x:t assumed in rest of scope; immutability enforced 8

Let Statement Usage Examples { let x = 37; let y = x + 5; y //42 { let x = 37; x = x + 5;//err x { //err: let x:u32 = -1; let y = x + 5; y { let x = 37; let x = x + 5; x //42 Redefining a variable shadows it (like OCaml) { let mut x = 37; x = x + 5; x //42 Assigning to a variable only allowed if mut { let x:i16 = -1; let y:i16 = x+5; y //4 Type annotations must be consistent (may override defaults) 9

Quiz 1: What does this evaluate to? { let x = 6; let y = "hi"; if x == 5 { y else { 5 ; 7 A. 6 B. 7 C. 5 D. Error 10

Quiz 1: What does this evaluate to? { let x = 6; let y = "hi"; if x == 5 { y else { 5 ; 7 A. 6 B. 7 C. 5 D. Error if and else have incompatible types 11

Quiz 2: What does this evaluate to? { let x = 6; let y = 4; let x = 8; x == 10-y A. 6 B. true C. false D. error 12

Quiz 2: What does this evaluate to? { let x = 6; let y = 4; let x = 8; x == 10-y A. 6 B. true C. false D. error 13

Pattern: Conditional Initialization Initialization expressions in let statements are arbitrary expressions Thus can be dynamically determined fn foo(cond:bool) -> i32 { let num = if cond { 5 else { 6 ; num+1 foo(true) == 6 foo(false) == 7 14

Using Mutation Mutation is useful when performing iteration As in C and Java fn fact(n: u32) -> u32 { let mut x = n; let mut a = 1; loop { if x <= 1 { break; a = a * x; x = x - 1; a locals mutable infinite loop (break out) 15

Other Looping Constructs While loops while e block For loops for pat in e block More later e.g., for iterating through collections These (and loop) are expressions They return the final computed value unit, if none break may take an expression argument, which is the final result of the loop 16

Quiz 3: What does this evaluate to? let mut x = 1; for i in 1..6 { let x = x + 1; x A. 1 B. 6 C. 0 D. error 17

Quiz 3: What does this evaluate to? let mut x = 1; for i in 1..6 { let x = x + 1; x A. 1 B. 6 C. 0 D. error 18

Data: Scalar Types Integers i8, i16, i32, i64, isize u8, u16, u32, u64, usize Characters (unicode) char Booleans bool = { true, false Floating point numbers f32, f64 Machine word size Defaults (from inference) Note: arithmetic operators (+, -, etc.) overloaded 19

Compound Data: Tuples and Arrays Tuples n-tuple type (t1,,tn) unit () is just the 0-tuple n-tuple expression(e1,,en) Accessed by pattern matching or like a record field Arrays constant length Thus, not as useful as Vec<t> type, discussed later array type [t] And type [t;n] where n is the array s (constant) length array expression has Ruby-like syntax [e1,,en] 20

Compound Data: Tuples tuple type fn dist(s:(f64,f64),e:(f64,f64)) -> f64 { let (sx,sy) = s; pattern let ex = e.0; accessor let ey = e.1; let dx = ex - sx; let dy = ey - sy; (dx*dx + dy*dy).sqrt() method invocation Rust let dist s e = let (sx,sy) = s in let (ex,ey) = e in let dx = ex -. sx in let dy = ey -. sy in sqrt (dx *. dx +. dy *. dy) OCaml 21

Compound Data: Tuples Can include patterns in parameters directly, too fn dist2((sx,sy):(f64,f64),(ex,ey):(f64,f64)) -> f64 { let dx = ex - sx; let dy = ey - sy; (dx*dx + dy*dy).sqrt() let dist (sx,sy) (ex,ey) = let dx = ex -. sx in let dy = ey -. sy in sqrt (dx *. dx +. dy *. dy) OCaml We ll see Rust structs later. They generalize tuples. Rust 22

Arrays Standard operations Creating an array (can be mutable or not) But must be of fixed length Indexing an array Assigning at an array index let nums = [1,2,3]; let strs = ["Monday","Tuesday","Wednesday"]; let x = nums[0]; // 1 let s = strs[1]; // "Tuesday" let mut xs = [1,2,3]; xs[0] = 1; // OK, since xs mutable let i = 4; let y = nums[i]; //fails (panics) at run-time 23

Array Iteration Rust provides a way to iterate over a collection Including arrays let a = [10, 20, 30, 40, 50]; for element in a.iter() { println!("the value is: {", element); a.iter() produces an iterator, like a Java iterator This is a method call, a la Java. More about these later The special for syntax issues the.next() call until no elements are left No possibility of running out of bounds 24

Quiz 4: Will this function type check? fn f(n:[u32]) -> u32 { n[0] A. Yes B. No 25

Quiz 4: Will this function type check? fn f(n:[u32]) -> u32 { n[0] A. Yes B. No because array length not known 26

Fun Fact The original Rust compiler was written in OCaml Betrays the sentiments of the language s designers! Now the Rust compiler is written in Rust How is this possible? Through a process called bootstrapping: The first Rust compiler written in Rust is compiled by the Rust compiler written in OCaml Now we can use the binary from the Rust compiler to compile itself We discard the OCaml compiler and just keep updating the binary through self-compilation So don t lose that binary! J 27