Upload
gerard-mathews
View
271
Download
0
Embed Size (px)
Citation preview
Functional Programming LanguageOCaml Tutorial
科大 - 耶鲁联合研究中心http://kdyhcs.ustcsz.edu.cn
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• 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++ …
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 = <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;;
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