Functional Programming Language OCaml Tutorial 科大 - 耶鲁联合研究中心

Slides:



Advertisements
Similar presentations
Closures & Environments CS153: Compilers Greg Morrisett.
Advertisements

Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
ML Declarations.1 Standard ML Declarations. ML Declarations.2  Area of a circle: - val pi = ; val pi = : real - fun area (r) = pi*r*r;
Tim Sheard Oregon Graduate Institute Lecture 8: Operational Semantics of MetaML CSE 510 Section FSC Winter 2005 Winter 2005.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
0 PROGRAMMING IN HASKELL Chapter 10 - Declaring Types and Classes.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Standard ML- Part I Compiler Baojian Hua
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
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.
Cse321, Programming Languages and Compilers 1 6/23/2015 Lecture #15, March. 5, 2007 Judgments for mini-Java Multiple type environments Class Hierarchy.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
Programovací jazyky F# a OCaml Chapter 5. Hiding recursion using function-as-values.
CS235 Languages and Automata Department of Computer Science Wellesley College Introduction to Standard ML Wednesday, September 23, 2009 Reading: Beginning.
COMS W4115 Programming Languages & Translators Maria Ayako Taku COMS W PLT Columbia University 1 April 24, 2013 Functional Programming.
Functional Programming in Haskell Motivation through Concrete Examples Adapted from Lectures by Simon Thompson.
F28PL1 Programming Languages Lecture 15: Standard ML 5.
Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types.
September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 1 Functional Programming Symbolic AI is based.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Chapter 9: Functional Programming in a Typed Language.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
CMSC 330: Organization of Programming Languages
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Chapter Fifteen: Functional Programming Languages Lesson 12.
Going Functional Primož Gabrijelčič. Functional programming.
© M. Winter COSC 4P41 – Functional Programming Programming with actions Why is I/O an issue? I/O is a kind of side-effect. Example: Suppose there.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
CS5205Haskell1 CS5205: Foundations in Programming Languages FP with Haskell A pure lazy functional language that embodies many innovative ideas in language.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
Introduction to Objective Caml. General comments ML is a purely functional language--there are (almost) no side effects There are two basic dialects of.
1 Objective Caml (Ocaml) Aaron Bloomfield CS 415 Fall 2005.
CSE-321 Programming Languages Introduction to Functional Programming POSTECH March 8, 2006 박성우.
An Introduction to Programming with C++1 More on the Selection Structure Tutorial 7.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
CSE-321 Programming Languages Introduction to Functional Programming POSTECH 박성우.
String is a synonym for the type [Char].
Principles of programming languages 12: Functional programming
dr Robert Kowalczyk WMiI UŁ
ML: a quasi-functional language with strong typing
Tutorial On Lex & Yacc.
PROGRAMMING IN HASKELL
Haskell programming language.
Haskell.
CSE 3302 Programming Languages
CMSC 330: Organization of Programming Languages
Introduction to Functional Programming in Racket
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.
CSE 341 Section 5 Winter 2018.
CSE-321 Programming Languages Introduction to Functional Programming
Types and Classes in Haskell
PROGRAMMING IN HASKELL
Fundamentals of Functional Programming
Introduction to Functional Programming in Racket
CSE 3302 Programming Languages
CSE-321 Programming Languages Introduction to Functional Programming
Programming Languages and Compilers (CS 421) #3: Closures, evaluation of function applications, order of evaluation #4: Evaluation and Application.
Madhusudan Parthasarathy (madhu) 3112 Siebel Center
CSE-321 Programming Languages Introduction to Functional Programming
Functional Programming
Functions and patterns
Presentation transcript:

Functional Programming Language OCaml Tutorial 科大 - 耶鲁联合研究中心

Outline  Introduction  Get Started  OCaml Basis  Basic Concepts  Examples  Discussion

Introduction Functional programming(FP) emphasizes application of functions and has its roots in lambda calculus The difference between function in FP and imperative languages is side effects FP can also be accomplished in imperative languages.

History 1950s LISP 1960s APL -> J -> K 1970s ML -> Objective Caml, Standard ML 1980s Dependent Types -> Coq, Agda, Epigram 1987 Haskell

Get Started Install win-msvc.exe win-msvc.exe Interactive Run “ocaml” Objective Caml version # 1+2;; - : int =3 Use any text editor Emacs/Vim Notepad++ …

Get Started Run Emacs Visist New File -> “test.ml” Short Keys C-c C-s “start ocaml” C-c C-b “eval whole file” C-c C-r “eval selected code” Tab “Ident” Demo

OCaml Declaring Variables Variables in OCaml are immutable! Declaring Functions OCaml functions don’t have explicit “return”. The return value is the last statement.

OCaml All functions and values in OCaml have a datatype let add x y = (x + y);; OCaml will report val add : int -> int -> int = which means function “add” takes two “int” inputs and return a “int” Generic Function let fst x y = x - val fst : 'a -> 'b -> 'a = # fst 1 2 ;; # fst "a" "b";; # fst "a" 1;;

Recursion and Nested Function

Pattern Match Example Patterns can be chained together

High-order Functions A high-order function is a functions that takes another function as a parameter or a function returns another function or both. Function can be partial applied add : int -> int -> int add 1 : int -> int add 1 2 : int let f = add 1 let result = f 2

High-order Functions Composition let double x = x * 2 let power x = x * x let compose f g x = g (f x) let result = (compose double power) 4 Pipeline let pipe x f = f x let result = (pipe (pipe 4 double) power)

Anonymous Function Example fun x -> x * 2 We can rewrite examples from Page 11 compose (fun x->x*2) (fun x->x*x) 4 Much use of anonymous functions

Data Structures Option Type Tuples List

Examples Fibonacci number List filter, map, fold

Lab Write a calculator Provided parser combinators built-in parsers integer, alpha, ident, char, string combinators many, sepBy, paren, chainl infix >> >>= #use “parsec.ml”;;

Lab Grammar Exp :: Term [+-] Term Term :: Factor [*/] Factor Factor :: int | ( Exp ) Get Start let add = (char ‘+’) >> (return (+)) “+” Receive a char ‘+’ and return function (+) with possible error message “+” let factor = (paren exp) (integer “integer”) either a parened “exp” or an integer let term = chainl factor (mul div) a list of factor chained with “mul” or “div” let expr = chainl term (add sub)

Lab Write a parser for first-order logic formula Formula :: Clause \/ Clause Clause :: Term /\ Term Term :: Literal | Literal Comp Literal Literal :: id | int | bool Eval the formula with given environment (x:true;y:false;….)

TODO Spec# ESC/Java Compcert CComp