Introduction to ML You will be responsible for learning ML on your own. Today I will cover some basics Read Robert Harper’s notes on “an introduction to.

Slides:



Advertisements
Similar presentations
More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
Advertisements

Modern Programming Languages, 2nd ed.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
ML Declarations.1 Standard ML Declarations. ML Declarations.2 Declarations o Re-declaration of names in ML o The reserved words "val rec" o Pattern Matching.
ML Declarations.1 Standard ML Declarations. ML Declarations.2  Area of a circle: - val pi = ; val pi = : real - fun area (r) = pi*r*r;
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Data  Consists of constructions that can be inspected, taken apart, or joined to form.
Programming Languages Section 1 1 Programming Languages Section 1. SML Fundamentals Xiaojuan Cai Spring 2015.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
CS 320: Compiling Techniques David Walker. People David Walker (Professor) 412 Computer Science Building office hours: after each.
Introduction to ML – Part 1 Frances Spalding. Assignment 1 chive/fall05/cos441/assignments/a1.ht m
Standard ML- Part I Compiler Baojian Hua
Introduction to ML – Part 1 Kenny Zhu. Assignment 2 chive/fall07/cos441/assignments/a2.ht m
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
Functional Design and Programming Lecture 1: Functional modeling, design and programming.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Chapter 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax.
CS 320: Compiling Techniques David Walker. People David Walker (Professor) 412 Computer Science Building office hours: after each.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded.
CS 320: Compiling Techniques David Walker. People David Walker (Professor) 412 Computer Science Building office hours: after each.
CS 320: Compiling Techniques David Walker. People David Walker (Professor) 412 Computer Science Building office hours: after each.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
CS235 Languages and Automata Department of Computer Science Wellesley College Introduction to Standard ML Wednesday, September 23, 2009 Reading: Beginning.
Standard ML1 Standard ML: A Quick Tutorial Hossein Hojjat University of Tehran Formal Methods Laboratory.
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
F28PL1 Programming Languages Lecture 13: Standard ML 3.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Chapter SevenModern Programming Languages1 A Second Look At ML.
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
CSE-321 Programming Languages Introduction to Functional Programming POSTECH March 8, 2006 박성우.
Programming Languages and Compilers (CS 421)
CSE341: Programming Languages Lecture 11 Type Inference
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
FP Foundations, Scheme In Text: Chapter 14.
CSE341: Programming Languages Lecture 11 Type Inference
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
CSE 341 Section 5 Winter 2018.
CSE S. Tanimoto Introduction to ML
CSE341: Programming Languages Lecture 11 Type Inference
ML’s Type Inference and Polymorphism
CSE-321 Programming Languages Introduction to Functional Programming
CSE S. Tanimoto Introduction to ML
CSE 341 Section 3 Nick Mooney Spring 2017.
ML’s Type Inference and Polymorphism
CSE-321 Programming Languages Introduction to Functional Programming
Madhusudan Parthasarathy (madhu) 3112 Siebel Center
CSE341: Programming Languages Lecture 11 Type Inference
CSE S. Tanimoto Introduction to ML
ML’s Type Inference and Polymorphism
ML’s Type Inference and Polymorphism
CSE S. Tanimoto Introduction to ML
CSE341: Programming Languages Lecture 11 Type Inference
Programming Languages Dan Grossman 2013
Presentation transcript:

Introduction to ML You will be responsible for learning ML on your own. Today I will cover some basics Read Robert Harper’s notes on “an introduction to ML” See course webpage for pointers and info about how to get the software

Intro to ML Highlights Functional Language Functions are pervasive: First-class values Storable, arguments, results, nested Strongly-typed language Every expression has a type Certain errors cannot occur Polymorphic types provide flexibility Flexible Module System Abstract Types Higher-order modules (functors)

Interactive Language Type in expressions Evaluate and print type and result Statically Scoped Language Compile-time resolution of values Call-by-value language f(e) High-level programming features Data types Pattern matching Exceptions Mutable data discouraged

Preliminaries Read – Eval – Print – Loop ; > 5: int - it + 7; > 12 : int - it – 3; > 9 : int true; Type clash in : 3 + true Looking for a : intType I have found a : boolError - 3 div 0 Failure : Div- run-time error

Basic Values - (); > () : unit=> like “void” in c (sort of) => the uninteresting value/type - true; > : bool - False; > : bool - if it then 3+2 else 7;always necessary > 7 : int - false and also loop_Forever; > False : booland also, or else short-circuit eval

Basic Values Integers j > 5:int (if not true then 5 else 7); > 10 : intNo division between expressions and statements Strings - “Dave” ^ “ “ ^ “Walker”; > “Dave Walker”:string - size “foo”; > 3 : int Reals ; > 3.14 : real

Structured Values Tuples - (true, 17, “Stuff”); > (true, 17, “Stuff”) : bool * int * string - (if 3 < 2 then “x” else “y”, false); >(“y”, false) : string * bool Records -{name = “Dave”, ssn = } >_____________ : {name = string, ssn : int} -#name it ; >“Dave” : string -#1 (true, False); -true : bool

Parametric Polymorphism Functions like compose work on objects of many different types In ML, we write compose: (‘a -> ‘b) -> (‘c -> ‘a) -> type variables are written with ‘ (‘c -> ‘b) compose not : compose not not : compose (op + 1) not : compose (fn x => x) :

What is the type of compose? Fun compose = Fn f => (fn g => Fn x => f (g x)) T3 Compose : T2 -> (T2 -> (T5 -> T6) : T2 -> (( T7 -> T8) -> (T5 -> T6)) : (T9 -> TA) -> (T7 -> T9) -> (T7 -> TA) : (‘a -> ‘b) -> (‘c -> ‘a) -> (‘a -> ‘c) Compose (fn x => x + 1) (fn y => y * 3)7;

Declarations -val x = 4 * 5;Declare new name x and bind it to value >val x = 20 : int -val y = x + 1; >val y = 21 : int -val x = x + y;reuse name >val x = 41 : int -x; >41 : int static scoping -y; local declaration >21 : intscope -(let val x = 10 in (x + x, y + x) and, x); >((20, 31), 41) : (int * int) * int

Functions (the good stuff) - not;functions are values >Not = fn : bool -> bool we can’t write out a function so ML just says this -not true;function application is a space >False : bool -fun twice x = 2 * x >Val twice = fn : int -> int -fun fact x = if x = 0 then 1 else (fact (x-1)) * x -fact (twice (twice 3));

functions - fn s => x ^ “!”;an anonymous function like writing >Fn : string -> string-3 -val exclaim = rather than - val x = 3; fn : => s ^ “!” >… -val compose = fn f => fn g => fn x => f(g x); >… - ((compose twice) twice) 3; >12 -four = compose twice twice >… -four 7; >…