Objective%20Caml%20(Ocaml) - PowerPoint PPT Presentation

About This Presentation
Title:

Objective%20Caml%20(Ocaml)

Description:

Has functions with side effects, imperative programming capabilities. Haskell is a pure functional language. Uses Hindley-Milner type inference algorithm ... – PowerPoint PPT presentation

Number of Views:101
Avg rating:3.0/5.0
Slides: 32
Provided by: csVir
Category:

less

Transcript and Presenter's Notes

Title: Objective%20Caml%20(Ocaml)


1
Objective Caml (Ocaml)
  • Aaron Bloomfield
  • CS 415
  • Fall 2005

2
ML history
  • ML first developed in late 1970s
  • Stands for Meta Langauage
  • Not a pure function language
  • Has functions with side effects, imperative
    programming capabilities
  • Haskell is a pure functional language
  • Uses Hindley-Milner type inference algorithm
  • Standard ML (SML) developed in 1990
  • Most common version is njsml from Bell Labs
  • Caml developed in 1987
  • Categorical Abstract Machine ML or CAML-ML or
    CAML
  • Ocaml came along in 1996
  • Adds objects (which we wont be using)

3
Invoking Ocaml
  • From Cygwin
  • Aaron_at_Eridanus
  • ocaml
  • Objective Caml version 3.08.1
  • 5
  • - int 5
  • Ctrl-D
  • Aaron_at_Eridanus
  • Can install Ocaml on Linux as well

4
Defining variables
  • All variables (and functions) are defined with
    the let command
  • let a 5
  • val a int 5
  • let b 5.5
  • val b float 5.5
  • let c '5'
  • val c char '5'
  • let d "5"
  • val d string "5"
  • Ocaml automatically determines the type of the
    identifier from the definition
  • It does this for functions as well more on this
    later

5
Operators
  • The standard operators with the standard
    precedences
  • Note that
  • is integer multiplication
  • . is floating point multiplication
  • Same for the other operators (/ -)
  • Thus, 4.5 - 3.2 wont work
  • Use 4.5 -. 3.2
  • And Ocaml wont allow you to mix types
  • You have to convert from one type to another
    yourself

6
Lists
  • The main workhorse in Ocaml
  • 123
  • - int list 1 2 3
  • let e "a""b""c"
  • val e string list "a" "b" "c"
  • let f 1,2,3
  • val f (int int int) list (1, 2, 3)
  • let g 1'a'"b"2.0
  • This expression has type char but is here used
    with type int
  • You dont have to declare a variable to declare a
    list
  • A comma separates parts of a tuple, not parts of
    a list
  • Lists must be homogenous all parts of the same
    type

7
vs ,
  • separates elements of a list
  • , separates elements of a tuple
  • let g 123
  • val g int list 1 2 3
  • let h 1,2,3
  • val h (int int int) list (1, 2, 3)
  • let i 1,23,4
  • val i (int int) list (1, 2) (3, 4)
  • let j (1,2)(3,4)
  • val j (int int) list (1, 2) (3, 4)
  • Parenthesis are just for human convenience

8
List operators
  • List.hd
  • Calls the hd (head) function from the List module
  • Returns the first element of a list
  • Raises an exception on an empty list
  • List.tl
  • Returns the list without the head element
  • Raises an exception on an empty list
  • Takes a head element and a list and glues them
    together
  • _at_
  • Takes two lists and glues them together

9
List operators
  • Examples
  • let l 123
  • val l int list 1 2 3
  • List.hd l
  • - int 1
  • List.tl l
  • - int list 2 3
  • 0 l
  • - int list 0 1 2 3
  • 0 _at_ l
  • - int list 0 1 2 3
  • Note that takes an element and a list
  • Note that _at_ takes two lists

10
More complex types
  • From the HW
  • let dfa1 ("0",
  • ("0", "0", "2") ("0", "1", "1") ("1", "0",
    "3")
  • ("1", "1", "0") ("2", "0", "0") ("2", "1",
    "3")
  • ("3", "0", "1") ("3", "1", "2"),
  • "0")
  • The type is val dfa1 string (string string
    string) list string list

11
Defining functions
  • let f x x5
  • val f int -gt int ltfungt
  • let g x x6
  • val g int -gt int ltfungt
  • f 1
  • - int 6
  • f(1)
  • - int 6
  • f(g(7))
  • - int 18
  • Ocaml automatically determines the type of the
    function
  • Called type inference

12
Functions with multiple parameters
  • A function that takes two int parameters is
    different than a function that takes a int int
    tuple as a parameter
  • let h x y xy
  • val h int -gt int -gt int ltfungt
  • let i x,y xy
  • Syntax error
  • let i (x,y) xy
  • val i int int -gt int ltfungt
  • h 1
  • - int -gt int ltfungt
  • let j h 1
  • val j int -gt int ltfungt
  • j 2
  • - int 3
  • Note the required use of parenthesis when the
    parameter is a tuple
  • A function that takes two parameters can have
    just one passed to it
  • This then returns another function

13
if then syntax
  • Consider
  • let x 3
  • val x int 3
  • if x 3 then 3
  • This expression has type int but is here used
    with type unit
  • if x 3 then 3 else 4
  • - int 3
  • Note that parenthesis are not needed for the if
    condition
  • There must be an else clause

14
A bad function
  • let rec foo x if ( x lt 0 ) then 1 else 2.0
  • Ocamls type inference cant determine the return
    type (int or float?)

15
Defining recursive functions
  • Consider the Fibonacci function
  • let fib n if n1 or n2 then 1 else fib(n-1)
    fib(n-2)
  • Unbound value fib
  • As fib has not been defined yet, Ocaml doesnt
    know what the fib identifier means in the
    recursive call
  • let rec fib n if n1 or n2 then 1 else
    fib(n-1) fib(n-2)
  • val fib int -gt int ltfungt
  • All recursive functions in Ocaml need the rec
    keyword

16
Binding issues
  • Consider
  • let foo x x5
  • val foo int -gt int ltfungt
  • let bar foo
  • val bar int -gt int ltfungt
  • let foo x x6
  • val foo int -gt int ltfungt
  • foo 1
  • - int 7
  • bar 1
  • - int 6
  • Note that although foo has been redefined, bar
    still uses the old foo

17
Mutually recursive functions
  • When two functions call each other recursively
  • let rec even x if x 0 then true else odd
    (x-1)
  • and
  • odd x if x 0 then false else even (x-1)
  • I needed mutually recursive functions for the
    nfasimulate function

18
let in syntax
  • Allows you to declare local values
  • let fun foo x
  • let y 5 in
  • let z square x in
  • xyz
  • Can nest them as many as desired
  • Can also declare local functions this way
  • let quad x
  • let square x x x in
  • (square x) (square x)

19
match keyword
  • Allows you to do both case statements and
    breaking apart of complex data types
  • let rec countlist list
  • match list with
  • -gt 0
  • _ tail -gt 1 (countlist tail)
  • The _ is used when you want to match something,
    but dont plan on using it in the match

20
The match keyword, continued
  • Consider the transitions part of the FAs
  • Type is (string string string) list
  • let fun foo trans
  • match trans with
  • -gt ...
  • (a, b, a) tail -gt ...
  • (a, b, c) tail -gt ...
  • Most of my functions used the match keyword to
    break apart the parts of the FA parameter

21
User defined types
  • User can define custom types
  • type btree Node of int btree btree Leaf
  • let mytree Node (1, Node(2, Leaf, Leaf),
    Leaf)
  • let rec countnodes tree
  • match tree with
  • Leaf -gt 0
  • Node (_, lefttree, righttree) -gt 1
    (countnodes lefttree) (countnodes righttree)
  • Type name (here btree) must be all lower case

22
More on user defined types
  • If you want a function to return a float or an
    int or a string
  • type multi Int of int Float of float
    String of string
  • type multi Int of int Float of float String
    of string
  • Int(7)
  • - multi Int 7
  • Float(3.4)
  • - multi Float 3.4
  • String "st"
  • - multi String "st"
  • let foo x if x 0 then Int 0
  • else if x lt 0 then Float 0.0
  • else String "0"
  • val foo int -gt multi ltfungt

23
The use command
  • To read in a file into Ocaml use use
  • use "fa.ml"
  • This will read in (and evaluate) the file fa.ml
  • You can then use the functions in the interpreter
  • This is probably the only function with
    side-effects that youll be using

24
Generics
  • Consider
  • let rec countlist list
  • match list with
  • -gt 0
  • _ tail -gt 1 (countlist tail)
  • val countlist 'a list -gt int ltfungt
  • This function works on any type of list
  • the a (read as alpha) means that any type can be
    substituted for alpha
  • Including (string list char) list, for example
  • The List.hd function has type a list -gt a
  • Meaning the type returned is the type of list
    read in

25
Using modules
  • For the homework, you are allowed to use any
    functions in the List or String modules
  • You can refer to them as List.hd, List.tl, etc.
  • If you do anopen Listyou can then refer to
    them just as hd or tl
  • Note if you open the String module, it defines a
    new compare method
  • Which will cause issues if you are using sort
    compare from the List module (see next slide)

26
List module functions
  • map
  • Applies the passed function to each element in
    the list
  • Usage map square 1234
  • Result 14916
  • sort
  • Allows you to sort elements in a list
  • First parameter must be the comparitor
  • The function that tells how to order individual
    elements
  • Use compare for this
  • Note that if you open the String module, there is
    a different compare function, which will prevent
    the following from working
  • Usage sort compare 4321
  • Etc.
  • See the module documentation for more List module
    functions

27
Debugging Ocaml functions
  • So how to debug the functions?
  • You cant really use print statements
  • There is an ocaml debugger
  • Called ocamldebug
  • Installs with the Cygwin ocaml package
  • Build in little steps
  • And test each step to make sure it works
  • Make sure the types of your functions match what
    you expect them to be
  • Do a lot of hand-tracing
  • Its a pain, but it will find your bug
  • You can use the interpreter for this

28
Ocaml gotchas
  • Using to terminate functions (not a single )
  • vs , for lists vs tuples
  • use fa.ml to read in a file
  • ( Comments are enclosed in star-parenthesis )
  • The difference between and _at_
  • String.compare overwriting the default compare
  • vs . (and similar for / -)

29
Function example uniq
  • This method takes in a sorted list, and removes
    any duplicates
  • As the list is sorted, any duplicates would be
    next to each other
  • Name is short-hand for unique
  • Operates like the Unx command of the same name
  • let rec uniq list
  • match list with
  • -gt
  • h1 -gt h1
  • h1 h2 tail -gt if h1 h2
  • then uniq (h2 tail)
  • else h1 uniq (h2 tail)

30
NFA to DFA conversion
31
Homework hints
  • For the NFA to DFA conversion
  • When dealing with the DFA states, I found it
    easier to have the state names be sets (lists)
    for the intermediate functions
  • Use uniq and List.sort to ensure that the sets
    (lists) are equivalent
  • You will have to write intermediate steps for
    each part of the conversion algorithm
Write a Comment
User Comments (0)
About PowerShow.com