19
Functional Programming Language OCaml Tutorial 科科 - 科科科科科科科科 http://kdyhcs.ustcsz.edu.cn

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

Embed Size (px)

Citation preview

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

Functional Programming LanguageOCaml Tutorial

科大 - 耶鲁联合研究中心http://kdyhcs.ustcsz.edu.cn

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

Outline Introduction Get Started OCaml Basis Basic Concepts Examples Discussion

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

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.

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

History

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

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

Get Started

• Install• http://caml.inria.fr/pub/distrib/ocaml-3.11/ocaml-3.11.0-win

-msvc.exe

• Interactive• Run “ocaml”• Objective Caml version 3.11.2• # 1+2;;• - : int =3

• Use any text editor• Emacs/Vim• Notepad++ …

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

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

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

OCaml

• Declaring Variables

• Variables in OCaml are immutable!• Declaring Functions

• OCaml functions don’t have explicit “return”. The return value is the last statement.

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

OCaml

• All functions and values in OCaml have a datatype• let add x y = (x + y);;• OCaml will report• val add : int -> int -> int = <fun>• which means• function “add” takes two “int” inputs and return a “int”

• Generic Function• let fst x y = x• - val fst : 'a -> 'b -> 'a = <fun>• # fst 1 2 ;; • # fst "a" "b";;• # fst "a" 1;;

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

Recursion and Nested Function

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

Pattern Match

• Example

• Patterns can be chained together

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

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

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

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)

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

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

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

Data Structures

• Option Type• Tuples• List

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

Examples

• Fibonacci number• List filter, map, fold

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

Lab

• Write a calculator• Provided parser combinators• built-in parsers• integer, alpha, ident, char, string

• combinators• many, sepBy, paren, chainl

• infix • >> >>= <|> <?>

• #use “parsec.ml”;;

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

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)

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

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;….)

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

TODO

• Spec#• ESC/Java• Compcert• CComp