language paradigms
play

Language Paradigms Introduction to SML Amtoft from Hatcliff from - PowerPoint PPT Presentation

Language Paradigms Introduction to SML Amtoft from Hatcliff from Leavens Paradigms Motivation Different ways of expressing computation; Statements vs. imperative Expressions Basics functional Typing logic Environment ...


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

  2. Imperative Paradigm Introduction to SML Amtoft from Hatcliff from Leavens Example: compute m n ( n ≥ 0) Paradigms Motivation r e s u l t := 1; Statements vs. while n > 0 do Expressions Basics r e s u l t := r e s u l t ∗ m; Typing n := n − 1 Environment end while ; Tuples and Lists Assessment: ◮ computation is expressed by repeated modification of an implicit store (i.e., components command a store modification), ◮ intermediate results are held in store ◮ iteration (loop)-based control

  3. Functional Paradigm Introduction to SML Amtoft from Hatcliff from Leavens Example: compute m n ( n ≥ 0) Paradigms Motivation fun power (m, n) = Statements vs. i f (n = 0) Expressions Basics then 1 Typing else m ∗ power (m, n − 1); Environment Assessment: Tuples and Lists ◮ computation is expressed by function application and composition ◮ no implicit store ◮ intermediate results (function outputs) are passed directly into other functions ◮ recursion-based control

  4. Logic Paradigm Introduction to SML Amtoft Example: compute m n ( n ≥ 0) from Hatcliff from Leavens / ∗ d e f i n e p r e d i c a t e power (m, n , r e s u l t ) ∗ / Paradigms Motivation power (m, 0 , 1 ) . Statements vs. Expressions power (m, n , r e s u l t ) 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 , r e s u l t ) . Tuples and Lists Assessment: ◮ computation is expressed by proof search, or alternatively, by recursively defining relations ◮ no implicit store ◮ all intermediate results (i.e., function outputs) are stored in variables ◮ recursion-based control

  5. Introduction to SML Introduction to SML Amtoft from Hatcliff from Leavens Paradigms Motivation SML is an expression-based ( functional ) language. Statements vs. Expressions 1. why SML in CIS505? Basics 2. statements vs. expressions Typing 3. basic SML expressions Environment ◮ literals, variable references, function calls, Tuples and Lists conditionals, ... 4. typing issues 5. variables and bindings 6. tuples and lists

  6. Why SML? Introduction to SML Amtoft from Hatcliff from Leavens Paradigms ◮ Well-understood foundations: This is a course Motivation about the foundations of programming languages, Statements vs. Expressions and the theory/foundations of SML have been Basics studied more in recent years than almost any other Typing language. Environment ◮ Well-designed: Robin Milner, the principal designer Tuples and Lists of SML received the Turing Award, in part, because of his work on SML. ◮ 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.

  7. Why SML? (continued) Introduction to SML Amtoft from Hatcliff from Leavens ◮ Very high-level: Using SML lets us describe Paradigms language processors very succinctly (much more Motivation concisely than any imperative language). Statements vs. Expressions ◮ Clean: SML is useful for various critical applications Basics where programs need to be proven correct Typing ◮ 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!). ◮ There’s more! There are also several different concurrent versions of SML, object-oriented extensions, libraries for various applications, etc.

  8. Statement Introduction to SML Amtoft from Hatcliff from Leavens ◮ 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 r e s u l t := r e s u l t ∗ m; n := n − 1 end while ; r e s u l t ; write Statement-oriented/imperative languages: ◮ Pascal, C, C++, Ada, FORTRAN, COBOL, etc

  9. Expression Introduction to SML Amtoft from Hatcliff from Leavens Paradigms ◮ construct evaluated to yield value Motivation Statements vs. Expressions Examples: Basics A := 2 + 3; / ∗ rhs i s e x p r e s s i o n ∗ / Typing Environment power 5 2 / ∗ SML f u n c t i o n c a l l ∗ / Tuples and Lists a = (b = c++) + 1; / ∗ C, C++, Java ∗ / Pure expressions: no side-effects Expression-oriented/functional languages: ◮ Scheme, ML, Lisp, Haskell, Miranda, FP, etc

  10. Basic SML Expressions Introduction to SML Amtoft from Hatcliff from Leavens Paradigms Motivation Statements vs. Expressions Basics ◮ constants (i.e., literals) Typing ◮ variable references Environment ◮ function application Tuples and Lists ◮ conditional expressions

  11. Constants Introduction to SML Amtoft from Hatcliff from Leavens Paradigms Motivation Statements vs. Expressions ◮ Integers: 0, 22, 353,... Basics Typing ◮ Reals: 12.0, 3E-2, 3.14e12 Environment ◮ Booleans: true, false Tuples and Lists ◮ Strings: ”KSU”, ”foo \ n” ◮ Characters: #”x”, #”A”, #” \ n”

  12. Example Session Introduction to SML Amtoft from Hatcliff from Leavens − 2; i t = 2 : i n t val Paradigms − i t + 1; Motivation i t = 3 : i n t val Statements vs. − i t ; Expressions val i t = 3 : i n t Basics − ˜234 + 2; Typing val i t = ˜232 : i n t Environment − 1 2 . 0 ; Tuples and Lists val i t = 12.0 : r e a l − 12. + 3 . 1 ; s t d I n : 1 6 . 1 E rror : syntax e r r o r found at DOT − ”KSU” ; val i t = ”KSU” : s t r i n g − ” foo \ n” ; val i t = ” foo \ n” : s t r i n g − #”x” ; val i t = #”x” : char − #”gh” ; . . . Error : c h a r a c t e r constant not l ength 1

  13. Arithmetic Operators Introduction to SML Amtoft from Hatcliff from Leavens Precedence: lowest to highest Paradigms ◮ +, − Motivation Statements vs. ◮ ∗ , / , div, mod Expressions ◮ ˜ Basics Typing Also: Environment ◮ ML is case sensitive (cf. mod ) Tuples and Lists ◮ associativity and precedence as in other languages ◮ operators associate to the left ◮ parentheses are ◮ needed only to enforce evaluation order, as in x * (y + z) ◮ but may be freely added to improve clarity, as in x + (y * z)

  14. String Operators Introduction to SML Amtoft from Hatcliff from Leavens Paradigms Concatenation: Motivation − ” abra ” ˆ ” cadabra ” ; Statements vs. Expressions val i t = ” abracadabra ” : s t r i n g Basics Typing − ” abra ” ˆ ”” ˆ ” cadabra ” ˆ ”” ; Environment val i t = ” abracadabra ” : s t r i n g Tuples and Lists − ” abra ” ˆ ( ”” ˆ ” cadabra ” ) ˆ ”” ; i t = ” abracadabra ” : s t r i n g val ◮ ”” (empty string) is identity element ◮ ˆ is associative

  15. Comparison Operators Introduction to SML Amtoft from Hatcliff from Leavens =, < , > , < =, > =, <> Paradigms Note: Motivation ◮ cannot use = or <> on reals Statements vs. Expressions ◮ to avoid problems with rounding Basics ◮ use e.g., < = and > = for = Typing ◮ < means “lexicographically procedes” for characters Environment and strings Tuples and Lists − ”a” < ”b” ; val i t = true : bool − ”c” < ”b” ; val i t = f a l s e : bool − ”abc” < ”acb” ; val i t = true : bool − ” stuv ” < ” stu ” ; i t = f a l s e : bool val

  16. Boolean Operators Introduction to SML Amtoft from Hatcliff from Leavens Paradigms Motivation not , andalso , o r e l s e Statements vs. Expressions Basics ◮ behave like C’s ! , && , || — not like Pascal Typing ◮ not commutative, as “short-circuit” operation Environment Tuples and Lists − (1 < 4) orelse ((5 div 0) < 2 ) ; i t = true : bool val − ((5 div 0) < 2) orelse (1 < 4 ) ; ∗∗ e r r o r ∗∗

  17. 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 val i t = ”bcd” : s t r i n g Statements vs. Expressions − val t = t r u e ; Basics val t = t r u e : bool Typing − val f = f a l s e ; Environment val f = f a l s e : bool Tuples and Lists − i f t = f then (5 div 0) e l s e 6; val i t = 6 : i n t − i f t = t r u e then 7 e l s e ” foo ” ; . . . Error : types of r u l e s don ’ t agree . . . e a r l i e r r u l e ( s ) : bool − > i n t t h i s r u l e : bool − > s t r i n g r u l e : in f a l s e = > ” foo ”

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