SML basics Aslan Askarov aslan@cs.au.dk Revised from slides by E. - - PowerPoint PPT Presentation

sml basics
SMART_READER_LITE
LIVE PREVIEW

SML basics Aslan Askarov aslan@cs.au.dk Revised from slides by E. - - PowerPoint PPT Presentation

Compilation 2014 SML basics Aslan Askarov aslan@cs.au.dk Revised from slides by E. Ernst ML Functional programming language Development initiated by Robin Milner at Edinburgh in 1970s First implementation in 1974 on PDP-10


slide-1
SLIDE 1

Compilation 2014

SML basics

Aslan Askarov aslan@cs.au.dk

Revised from slides by E. Ernst

slide-2
SLIDE 2

ML

  • Functional programming language
  • Development initiated by Robin Milner at Edinburgh

in 1970s

  • First implementation in 1974 on PDP-10
  • Evolved over a period of 20+ years
  • Fusion of many ideas from many people
  • Initial application: DSL for manipulating logical proofs
  • ML = Meta Language
  • Tremendous influence on the design of modern

programming languages

  • Type inference, polymorphism, safety, …
slide-3
SLIDE 3

Robin Milner

“In 1956 […] I regarded programming as really rather

  • inelegant. You’d write one instruction after the other, and

it was all rather arbitrary. It seemed to me that programming was not a very beautiful thing. So I resolved that I would never go near a computer in my life!”

  • – R. Milner (interview by M. Bergen, 2003)

ACM Turing Award, 1991

Honorary Doctorate, Aarhus University, 1998

“For three distinct and complete achievements: 1) LCF , the mechanization of Scott's Logic of Computable Functions, probably the first theoretically based yet practical tool for machine assisted proof construction; 2) ML, the first language to include polymorphic type inference together with a type-safe exception-handling mechanism; 3) CCS, a general theory of concurrency. In addition, he formulated and strongly advanced full abstraction, the study of the relationship between

  • perational and denotational semantics.”
slide-4
SLIDE 4

Standard ML

  • SML is a dialect of ML
  • Functional language
  • Functions are first class (like in Scheme)
  • The core syntactic category is expressions (not statements)
  • Assignments are possible, but rare
  • SML is statically and strongly typed
  • No NullPointerExceptions, no ClassCastExceptions
  • no casts, but many usages of pattern matching
  • Since everything is an expression, everything has a type
  • The interpreter (and the compiler) infers many types

automatically

  • Variables are often declared w/o an explicit type
slide-5
SLIDE 5

Using SML

  • SML comes with a read-eval-print loop (like Python

and Scheme)

  • All interactions must end with semicolon

> sml Standard ML of New Jersey v110.76 [built: Fri Aug 1 00:28:43 2014]

  • print "hello, world!\n";

hello, world! val it = () : unit

slide-6
SLIDE 6

Basic types (1/4)

  • SML comes with a number of base types
  • int, char, bool, string, real, …
  • Integers are 31-bit: ~1, 0, 1, 42, Int.maxInt, …

all have type int

  • int/real types come with usual operations: 


+, -, *, div, /, mod, …

  • Note that unary negation is tilde, not munis
  • Check out the associated libraries:

  • pen Int; ..Char, Bool, String, Real
slide-7
SLIDE 7

Basic types (2/4)

SML comes with

  • Booleans: true and false both have type bool
  • Boolean negation is not, conjunction is andalso,

and disjunction is orelse

  • Usual comparison operations also produces bools:

=, <>, <, <=, >, >=, …

  • Characters: #”a”, #”X”, #”\n”, #”\\”, #”\012”,

all have type char

  • Conversions are done with chr: int -> char and
  • rd : char -> int
slide-8
SLIDE 8

Basic types (3/4)

SML comes with

  • Strings: ””, ”hello, world!” both have type

string

  • String concatenation is ^: ”d” ^ ”Ovs”
  • One can inspect and manipulate strings:

String.size, String.substring, String.translate

  • And convert to and from strings:

Int.fromString, Int.toString, Bool.fromString, Bool.toString, …

slide-9
SLIDE 9

Basic types (4/4)

  • () has type unit
  • Notice how print returned unit
  • unit serves the purpose of void in C and Java
  • and doubles as the “empty argument (list)”:


SMLofNJ.SysInfo.getOSName()

  • Technically it is not the “empty type” because one

value has the type unit, namely ()

  • (* This is a (* nested *) comment in SML *)
slide-10
SLIDE 10

Conditionals

Conditionals in SML are expressions and hence have a types

  • y + (if x < 0 then ~x else x)
  • Both then and else branch are required, and must have the

same type

  • if x > 0 then ”hello” else ()
  • Error: types of if branches do not agree [tycon mismatch]

then branch: string else branch: unit in expression: …

slide-11
SLIDE 11

SML by example