Introduction to SML Amtoft from Hatcliff from Leavens

Introduction to SML Amtoft from Hatcliff from Leavens

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, C++, 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", "foonn" I Characters: #"x", #"A", #"nn" 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 nn" ; v a l i t = " foo nn" : 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 Concatenation: 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.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    30 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us