<<

Language Paradigms Introduction to SML Amtoft from Hatcliff from Leavens

Paradigms

Motivation Different ways of expressing computation; Statements vs. Expressions I imperative Basics functional I Typing I logic Environment Tuples and Lists I ...object-oriented Others: dataflow, coordination, algebraic, graph-based, etc Note: distinction is sometimes fuzzy! Imperative Paradigm Introduction to SML Amtoft from Hatcliff from Leavens

Example: compute mn (n ≥ 0) Paradigms Motivation

r e s u l t := 1 ; Statements vs. while n > 0 do Expressions result := result ∗ m; Basics n := n − 1 Typing end while ; Environment Tuples and Lists Assessment:

I computation is expressed by repeated modification of an implicit store (i.e., components command a store modification),

I intermediate results are held in store

I iteration (loop)-based control Functional Paradigm Introduction to SML Amtoft from Hatcliff from Leavens

Example: compute mn (n ≥ 0) Paradigms Motivation

fun power (m,n) = Statements vs. i f ( n = 0) Expressions then 1 Basics e l s e m ∗ power (m, n −1); Typing Environment Assessment: Tuples and Lists

I computation is expressed by function application and composition

I no implicit store

I intermediate results (function outputs) are passed directly into other functions

I recursion-based control Logic Paradigm Introduction to SML Amtoft n from Hatcliff Example: compute m (n ≥ 0) from Leavens /∗ define predicate power(m,n,result) ∗/ Paradigms Motivation

Statements vs. power(m,0 ,1). Expressions

power(m,n, result) Basics

<− minus(n,1,n sub1 ) , Typing power (m, n sub1 , t e m p r e s u l t ) , Environment times(m, temp result ,result). Tuples and Lists Assessment:

I computation is expressed by proof search, or alternatively, by recursively defining relations

I no implicit store I all intermediate results (i.e., function outputs) are stored in variables

I recursion-based control Introduction to SML Introduction to SML Amtoft from Hatcliff from Leavens

Paradigms

Motivation SML is an expression-based (functional) language. Statements vs. 1. why SML in CIS505? Expressions Basics

2. statements vs. expressions Typing 3. basic SML expressions Environment

I literals, variable references, function calls, Tuples and Lists conditionals, ... 4. typing issues 5. variables and bindings 6. tuples and lists Why SML? Introduction to SML Amtoft from Hatcliff from Leavens

Paradigms I Well-understood foundations: This is a course Motivation about the foundations of programming languages, Statements vs. and the theory/foundations of SML have been Expressions Basics studied more in recent years than almost any other Typing language. Environment I Well-designed: Robin Milner, the principal designer Tuples and Lists of SML received the Turing Award, in part, because of his work on SML.

I Advanced features: Many of the features of SML, such as parametric polymorhism, pattern matching, and advanced modules are very elegant and do not appear in other languages like Java, ++, etc. Why SML? (continued) Introduction to SML Amtoft from Hatcliff from Leavens I Very high-level: Using SML lets us describe Paradigms language processors very succinctly (much more Motivation

concisely than any imperative language). Statements vs. Expressions I Clean: SML is useful for various critical applications where programs need to be proven correct Basics Typing

I It’s different than Java: At some point in your Environment

career, you will have to learn a new language. This Tuples and Lists course prepares you for that by forcing you to learn a new language (SML) quickly. In addition, compared to Java, C, etc., SML uses a totally different style to describe computation. This forces you to think more deeply (mental pushups!).

I There’s more! There are also several different concurrent versions of SML, object-oriented extensions, libraries for various applications, etc. Statement Introduction to SML Amtoft from Hatcliff from Leavens

I construct evaluated only for its effect Paradigms Motivation

Examples: Statements vs. Expressions

m := 5 ; Basics

n := 2 ; Typing

r e s u l t := 1 ; Environment

while n > 0 do Tuples and Lists result := result ∗ m; n := n − 1 end while ; write r e s u l t ; Statement-oriented/imperative languages:

I Pascal, C, C++, Ada, FORTRAN, COBOL, etc Expression Introduction to SML Amtoft from Hatcliff from Leavens

Paradigms

Motivation I construct evaluated to yield value Statements vs. Examples: Expressions Basics

A := 2 + 3 ; /∗ rhs is expression ∗/ Typing

Environment power 5 2 /∗ SML function call ∗/ Tuples and Lists

a = (b= c++)+ 1; /∗ C , C++, Java ∗/ Pure expressions: no side-effects Expression-oriented/functional languages:

I Scheme, ML, Lisp, Haskell, Miranda, FP, etc Basic SML Expressions Introduction to SML Amtoft from Hatcliff from Leavens

Paradigms

Motivation

Statements vs. Expressions

Basics

I constants (i.e., literals) Typing I variable references Environment Tuples and Lists I function application

I conditional expressions Constants Introduction to SML Amtoft from Hatcliff from Leavens

Paradigms

Motivation

Statements vs. Expressions

I Integers: 0, 22, 353,... Basics Typing I Reals: 12.0, 3E-2, 3.14e12 Environment

I Booleans: true, false Tuples and Lists

I Strings: ”KSU”, ”foo\n”

I Characters: #”x”, #”A”, #”\n” Example Session Introduction to SML Amtoft from Hatcliff from Leavens − 2 ; v a l i t = 2 : i n t Paradigms

− i t + 1 ; Motivation v a l i t = 3 : i n t Statements vs. − i t ; Expressions v a l i t = 3 : i n t Basics

− ˜234 + 2 ; Typing v a l it = ˜232 : int Environment − 1 2 . 0 ; v a l it = 12.0 : real Tuples and Lists − 1 2 . + 3 . 1 ; stdIn:16.1 Error: syntax error found at DOT − ”KSU” ; v a l it = ”KSU” : string − ” foo \n” ; v a l i t = ” foo \n” : s t r i n g − #”x” ; v a l it =#”x” : char − #”gh” ; ... Error: character constant not length 1 Arithmetic Operators Introduction to SML Amtoft from Hatcliff from Leavens

Precedence: lowest to highest Paradigms

Motivation I +, − Statements vs. I ∗, /, div, mod Expressions Basics I ˜ Typing Also: Environment I ML is case sensitive (cf. mod) Tuples and Lists

I associativity and precedence as in other languages

I operators associate to the left I parentheses are

I needed only to enforce evaluation order, as in x * (y + z) I but may be freely added to improve clarity, as in x + (y * z) String Operators Introduction to SML Amtoft from Hatcliff from Leavens

Paradigms : Motivation − ”abra” ˆ ”cadabra”; Statements vs. Expressions

v a l it = ”abracadabra” : string Basics

Typing

− ”abra” ˆ ”” ˆ ”cadabra” ˆ ””; Environment

v a l it = ”abracadabra” : string Tuples and Lists

− ”abra” ˆ (”” ˆ ”cadabra”) ˆ ””; v a l it = ”abracadabra” : string

I ”” (empty string) is identity element

I ˆ is associative Comparison Operators Introduction to SML Amtoft from Hatcliff from Leavens

=, <, >, <=, >=, <> Paradigms Note: Motivation Statements vs. I cannot use = or <> on reals Expressions

I to avoid problems with rounding Basics I use e.g., <= and >= for = Typing

I < means “lexicographically procedes” for characters Environment and strings Tuples and Lists − ”a” < ”b” ; v a l it = true : bool − ”c” < ”b” ; v a l it = false : bool − ” abc ” < ” acb ” ; v a l it = true : bool − ” s t u v ” < ” s t u ” ; v a l it = false : bool Boolean Operators Introduction to SML Amtoft from Hatcliff from Leavens

Paradigms

Motivation

Statements vs. not, andalso, orelse Expressions

Basics

I behave like C’s !, &&, || — not like Pascal Typing

I not commutative, as “short-circuit” operation Environment Tuples and Lists − (1 < 4) o r e l s e ( ( 5 d i v 0) < 2 ) ; v a l it = true : bool − ( ( 5 d i v 0) < 2) o r e l s e (1 < 4 ) ; ∗∗ e r r o r ∗∗ If-then-else Expressions Introduction to SML Amtoft from Hatcliff from Leavens Examples: Paradigms − i f 4 < 3 then ”a” e l s e ” bcd ” ; Motivation v a l it = ”bcd” : string Statements vs. Expressions − v a l t = t r u e ; Basics v a l t = true : bool Typing − v a l f = f a l s e ; v a l f = false : bool Environment Tuples and Lists − i f t = f then (5 d i v 0) e l s e 6 ; v a l i t = 6 : i n t

− i f t = t r u e then 7 e l s e ” foo ” ; ... Error: types of rules don’t agree... earlier rule(s): bool −> i n t this rule: bool −> s t r i n g i n r u l e : f a l s e => ” foo ” Typing Issues Introduction to SML Amtoft from Hatcliff from Leavens

ML has strong typing: Paradigms Motivation (strong/weak = how much) Statements vs. Expressions I each value has exactly one type Basics for example, 12 is but not I int real Typing I explicit coercions therefore necessary Environment ML has static typing: Tuples and Lists (static/dynamic = when)

I type-checking occurs before programs are run

I thus if x = y then 7 else "foo" is an error I but it wouldn’t be in a dynamically typed language These concepts are too often mixed up, even in the Ullman textbook (pages 3 and 143) Coercions Introduction to SML Amtoft from Hatcliff From integers to reals: from Leavens − r e a l ( 1 1 ) ; Paradigms v a l it = 11.0 : real − 5 . 0 + 1 1 ; Motivation Statements vs. ... Error: operator and operand mismatch Expressions operator domain: real ∗ r e a l Basics operand: real ∗ i n t i n expression: Typing 5 . 0 + 11 Environment − 5.0 + real(11); Tuples and Lists v a l it = 16.0 : real From reals to integers: − floor (5.4); v a l i t = 5 : i n t − c e i l ( 5 . 4 ) ; v a l i t = 6 : i n t − round(5.5); v a l i t = 6 : i n t − trunc(˜5.4); v a l i t = ˜5 : i n t Coercions Introduction to SML Amtoft from Hatcliff from Leavens

Paradigms Between characters and integers: Motivation Statements vs. − ord(#”0” ) ; Expressions v a l i t = 48 : i n t Basics Typing − chr ( 4 8 ) ; Environment v a l it =#”0” : char Tuples and Lists Between strings and characters: − s t r (#”a” ) ; v a l it = ”a” : string What about from string to character? Identifiers Introduction to SML Amtoft from Hatcliff from Leavens

SML has two classes of identifiers: Paradigms Motivation alphanumeric (e.g., , , ) I abc abc’ A 1 Statements vs. Expressions I symbolic (e.g., +, $$$, %-%) Basics

Alphanumeric Identifiers: strings formed by Typing I An upper or lower case letter or the character ’ Environment (called apostrophe or “prime”), followed by Tuples and Lists

I Zero or more additional characters from the set given in (1) plus the digits and the character (underscore). Symbolic Identifiers: strings composed of

+-/*<>=!@#$%^&‘~\|?: Variables Introduction to SML Amtoft from Hatcliff Consider from Pascal: A := B + 2; from Leavens

I B is a variable reference (contrast with A) Paradigms I a memory location is associated with A Motivation Statements vs. I a stored value (e.g., 5) is associated Expressions

with B Basics Pascal, C, Java, Fortran, etc: Typing memory c e l l Environment +−−−−−−−−−−−−−+ Tuples and Lists == | | +−−−−−−−−−−−−−+

I variables bind to locations

I there is a level of indirection

I two mappings

I environment: maps variables to locations

I store: maps locations to values Variables Introduction to SML Amtoft from Hatcliff from Leavens

Paradigms

Motivation SML: variables bound to values Statements vs. Expressions == Basics

Typing variables bind directly to values I Environment I there is no indirection Tuples and Lists

I a binding cannot be modified (!!)

I no assignment (!!) I one mapping

I environment: maps variables to values Top-level Environment Introduction to SML Amtoft from Hatcliff from Leavens

Paradigms

Motivation v a r v a l u e Statements vs. − v a l a = 2 ; +−−−−−−−+−−−−−−−+ Expressions

v a l a = 2 : i n t | a | 2 | Basics − v a l b = 3 ; +−−−−−−−+−−−−−−−+ Typing v a l b = 3 : i n t | b | 3 | Environment − v a l c = a + b ; +−−−−−−−+−−−−−−−+ v a l c = 5 : i n t | c | 5 | Tuples and Lists − v a l a = c + 2 ; +−−−−−−−+−−−−−−−+ v a l a = 7 : i n t | a | 7 | − v a l c = c + 2 ; +−−−−−−−+−−−−−−−+ v a l c = 7 : i n t | c | 7 | +−−−−−−−+−−−−−−−+ Tuples Introduction to SML Amtoft from Hatcliff from Leavens

Tuple: fixed-size ordered collection of two or more values. Paradigms Motivation

Statements vs. − v a l t = (1, ”a”, true); Expressions v a l t = (1,”a”,true) : int ∗ s t r i n g ∗ b o o l Basics

− #3( t ) ; Typing v a l it = true : bool Environment − v a l s = ( 4 , t ) ; v a l s = (4,(1,”a”,true)) : Tuples and Lists i n t ∗ ( i n t ∗ s t r i n g ∗ b o o l ) − #2(#2( s ) ) ; v a l it = ”a” : string − ( 4 ) ; v a l i t = 4 : i n t − (); v a l i t = ( ) : u n i t − #2 t ; v a l it = ”a” : string − #4( t ) ; s t d I n :16.1 −16.6 Error: ... Lists Introduction to SML Amtoft from Hatcliff from Leavens

ML lists are lists of values of the same type. Paradigms Motivation

Statements vs. Example session: Expressions Basics − [ 1 , 2 , 3 ] ; Typing v a l it = [1,2,3] : int list − [(1,2),(2,3),(3,4)]; Environment v a l it = [(1,2),(2,3),(3,4)] : Tuples and Lists ( i n t ∗ i n t ) l i s t − [ ”a” ] ; v a l it = [”a”] : string list − [ ”a” , 2 ] ; ... Error: operator and operand don’t agree... − [[1] ,[2] ,[3]]; v a l it = [[1],[2],[3]] : int list list − []; v a l it = [] : ’a list Polymorphic List Operations Introduction to SML Amtoft from Hatcliff empty list [] : ’a list from Leavens head hd : ’a list → ’a Paradigms tail tl : ’a list → ’a list Motivation

append @ : ’a list * ’a list → ’a list Statements vs. cons :: : ’a * ’a list → ’a list Expressions Example session: Basics − v a l ls = [1,2,3]; Typing v a l ls = [1,2,3] : int list Environment − hd ( l s ) ; Tuples and Lists v a l i t = 1 : i n t − hd([”a”,”b”,”c”]); v a l it = ”a” : string − tl(tl(ls)); v a l it = [3] : int list − tl(tl(ls)) @ ls; v a l it = [3,1,2,3] : int list − 3 @ l s ; ... Error: operator and operand don’t agree − 3 : : l s ; v a l it = [3,1,2,3] : int list Strings ↔ Lists Introduction to SML Amtoft from Hatcliff from Leavens

Paradigms

Motivation Example session: Statements vs. Expressions − explode(”abcd”); Basics v a l it = [#”a”,#”b”,#”c”,#”d”] : char list Typing − implode([#”f”,#”o”,#”o”]); Environment

v a l it = ”foo” : string Tuples and Lists − implode(explode(”abcd”)); v a l it = ”abcd” : string − explode(implode([#”f”,#”o”,#”o”])); v a l it = [#”f”,#”o”,#”o”] : char list Examples Introduction to SML Amtoft from Hatcliff from Leavens

− ”abc” ˆ implode([#”f”,#”o”,#”o”]) ˆ ”bar”; Paradigms v a l it = ”abcfoobar” : string Motivation

− ([4 ,5] ,[2] ,[ord(#”c”)]); Statements vs. v a l it = ([4,5],[2],[99]) : Expressions i n t l i s t ∗ i n t l i s t ∗ i n t l i s t Basics − ” abc ” > ” foo ” ; Typing

v a l it = false : bool Environment − 7 : : 5 ; Tuples and Lists s t d I n :37.1 −37.7 E r r o r : o p e r a t o r and operand don’t agree [literal] − [”a”,”b”,#”c”,”d”]; s t d I n :1.1 −30.2 Error: operator and operand don’t agree [tycon mismatch] − 20 + ( i f #”c ” < #”C” then 5 e l s e 1 0 ) ; v a l i t = 30 : i n t − ((),(),[()],([])); . . . : u n i t ∗ u n i t ∗ u n i t l i s t ∗ ’ a l i s t Summary Introduction to SML Amtoft from Hatcliff from Leavens

Paradigms

Motivation

Statements vs. Expressions

Basics

ML is an expression-based (functional) language with Typing

strong static typing. Environment

Tuples and Lists Next lecture: user-defined functions