Standard ML- Part I Compiler Baojian Hua

Slides:



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

Programming Languages Section 1 1 Programming Languages Section 1. SML Fundamentals Xiaojuan Cai Spring 2015.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
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
Introduction to ML – Part 1 Kenny Zhu. Assignment 2 chive/fall07/cos441/assignments/a2.ht m
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.
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.
Standard ML- Part II Compiler Baojian Hua
Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML.
Programming Language Theory Leif Grönqvist The national Graduate School of Language Technology (GSLT) MSI.
CS 320: Compiling Techniques David Walker. People David Walker (Professor) 412 Computer Science Building office hours: after each.
Guide To UNIX Using Linux Third Edition
Closure and Environment Compiler Baojian Hua
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.
CSE 425: Intro to Programming Languages and their Design A Few Key Ideas No particular language is a prerequisite for this course –However you should be.
0 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Imperative Programming
CS235 Languages and Automata Department of Computer Science Wellesley College Introduction to Standard ML Wednesday, September 23, 2009 Reading: Beginning.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
Programming Languages –14 David Watt (Glasgow) Steven Wong (Singapore) Moodle : Computing Science → Level 3 → Programming Languages 3 © 2012 David.
Chapter 1 - Introduction
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
ISBN Chapter 15 Functional Programming Languages.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
1 Functional Programming & Standard ML Hossein Hojjat et al.
Introduction to Functional Programming and ML CS 331 Principles of Programming Languages.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Functional Programming Language OCaml Tutorial 科大 - 耶鲁联合研究中心
TIVDM2Functional Programming Language Concepts 1 Concepts from Functional Programming Languages Peter Gorm Larsen.
1 Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002 ML (Meta Language) a brief introduction Alex Proctor and Brian Lee For CSCI 431 at the University.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
CMSC 330: Organization of Programming Languages
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 1 Overview A good programming language is.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
1 Objective Caml (Ocaml) Aaron Bloomfield CS 415 Fall 2005.
CSE-321 Programming Languages Introduction to Functional Programming POSTECH March 8, 2006 박성우.
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Language Paradigms CS655.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Programming Languages 2nd edition Tucker and Noonan
Functional Programming
Programming Languages and Compilers (CS 421)
CSE341: Programming Languages Lecture 11 Type Inference
Zuse’s Plankalkül – 1945 Never implemented Problems Zuse Solved
Functional Programming
Principles of programming languages 12: Functional programming
Types for Programs and Proofs
ML: a quasi-functional language with strong typing
CSE 341: Programming Langs
FP Foundations, Scheme In Text: Chapter 14.
CSE341: Programming Languages Lecture 11 Type Inference
Programming Languages 2nd edition Tucker and Noonan
CSE341: Programming Languages Lecture 11 Type Inference
CSE-321 Programming Languages Introduction to Functional Programming
CSE341: Programming Languages Lecture 11 Type Inference
Functional Programming and Haskell
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
Functional Programming and Haskell
Presentation transcript:

Standard ML- Part I Compiler Baojian Hua

Standard ML is a Functional Language Small, orthogonal core based on the lambda-calculus. Control is based on (recursive) functions. Instead of for-loops, while-loops, do-loops, iterators, etc. rather, we can define these as library (functions). Makes it easy to define semantics. Supports higher-order, lexically-scoped, first-class functions. a.k.a. closures or lambdas. first-class: can be passed to other procedures as arguments, returned from procedures as results, placed in data structures etc. lexically-scoped: meaning of variables determined statically. higher-order: compositional, programs that build programs. These aspects are in common with other functional languages such as Common Lisp, Scheme, Haskell, Ocaml, F#, and Erlang.

Standard ML is mostly Pure As opposed to mostly imperative languages. C/C++, C#, Java, Python, Basic, Fortran, … Emphasis on values, not variables or state: “ value-oriented programming ” build new values out of old, instead of destructively updating the old. leads to a simple analytic, computational model increasingly important in age of concurrency Still provides for mutable data structures and I/O. for efficiency of some algorithms but less frequently than you might first suspect.

Standard ML is Strongly, Statically Typed Statically typed: compiler catches many silly errors before you can run the code. e.g., calling a function with the wrong types of arguments Strong typed: compiler enforces type abstraction. cannot cast an integer to a record, function, string, etc. so we can utilize types as capabilities.

A bit of history Alonzo Church: lambda calculus 1930’s John McCarthy: LISP 1958 Guy Steele & Gerry Sussman: Scheme late 1970’s Robin Milner, Mads Tofte, & Robert Harper Standard ML 1980’s Xavier Leroy: Ocaml 1990’s Don Syme: F# 2000’s

Standard ML Standard ML is a domain-specific language for building compilers, interpreters, theorem provers, etc. Features First-class functions Memory management like Java Exception handling Module system supporting large projects Advanced type system for error detection

Introduction to SML You will be responsible for learning SML on your own. Today I will cover some basics Readings: Robert Harper’s Online book “Programming in Standard ML” is a good place to start Also see course webpage for pointers and info about how to get, install and run the software

Installation and Run We ’ d use SML/NJ and MLton, see the webpage for detailed info ’ easy installation on Linux, Windows, … Editing Notepad, Eclipse, VS, Emacs, Vi, … Next, I ’ d introduce SML via examples

Preliminaries Start sml in Unix or Windows by typing sml at a prompt: % sml Standard ML of New Jersey, v […] - (* quit SML by pressing ctrl-D; ctrl-Z *some times... *) (* just as you see, comments can be (* nested *) *)

Preliminaries Read – Eval – Print – Loop ;

Preliminaries Read – Eval – Print – Loop ; > val it = 5 : int

Preliminaries Read – Eval – Print – Loop ; > val it = 5 : int - it + 7; > val it = 12 : int

Preliminaries Read – Eval – Print – Loop ; > val it = 5 : int - it + 7; > val it = 12 : int - it – 3; > val it = 9 : int true; stdIn: Error: operator and operand don't agree [literal] operator domain: int * int operand: int * bool in expression: 4 + true

Preliminaries Read – Eval – Print – Loop - 3 div 0; uncaught exception Div [divide by zero] run-time error

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

Basic Values Integers ; > val it = 5 : int (if not true then 5 else 7); > val it = 10 : int No division between expressions and statements Strings - “Baojian” ^ “ “ ^ “Hua”; > val it = “Baojian Hua” : string - print “foo\n”; foo > val it = () : unit

Declarations (bindings) Just like in any other language, SML allows value and type declarations with key difference that values in SML don ’ t vary so it ’ s called value binding (rather than value assignment) Basic forms: val id: type = exp type id = typeExp

First-class Functions It ’ s simple, it ’ s “ fun ” fun (id: type): type = exp The power of SML functions comes from the fact that they are first-class stored in data structures passed as arguments, returned as values nested! (closure)

Algebraic Data Types A simple yet powerful feature allow user-defined data type like unions or inheritance in other languages with the key highlights of type-safety may be non-recursive or recursive natural to represent compiler intermediate languages

Pattern Matching Pattern matching for handling case analysis on algebraic Exhaustiveness Redundancy

List The ubiquitous data structure in functional programming See the accompany code for details

Reference Normally, variables in SML don ’ t vary, if you ’ d like to modify a variable, use reference think “ malloc ” or “ new ” in other languages again with type-safety assurance should be used rarely and with care

More on Using SML/NJ Interactive mode is a good way to start learning and to debug programs, but… Type in a series of declarations into a “.sml” file - use “foo.sml” [opening foo.sml] … list of declarations with their types

Larger Projects SML has its own built in interactive “make” Pros: It automatically does the dependency analysis for you No crazy makefile syntax to learn Cons: May be more difficult to interact with other languages or tools

Compilation Manager % sml - OS.FileSys.chDir “ ~/src ” ; - CM.make “ sources.cm ” ; looks for “ sources.cm ”, analyzes dependencies [compiling … ] compiles files in group [wrote … ] saves binaries in.cm/ sources.cm c.smlb.smla.sml Group is a.sml b.sml c.sml

Core Language All SML expressions produce values that have a particular type SML doesn’t have “statements” SML can do type inference (and give you hard-to-decrypt error messages) SML data types are super-cool a new type name new constructors new patterns In summary, SML has a small, elegant yet power core language Next time, module system

Summary Learning to program in SML can be tricky at first But once you get used to it, you will never want to go back to imperative languages Check out the reference materials listed on the course homepage, do reading and practicing