O’Caml by Pavan Krishnamurthy and Qiyam TungTable of Contents Introduction......................................................................................................................... 3 History of ML/OCaml......................................................................................................... 3 Audience ............................................................................................................................. 3 Types................................................................................................................................... 3 • Type Inference ........................................................................................................ 3 • Simple Types .......................................................................................................... 4 Labels.................................................................................................................................. 4 Variants............................................................................................................................... 5 • Recursive variants (trees)........................................................................................ 5 • Parameterized variants............................................................................................ 5 Aliases (Currying)............................................................................................................... 6 Pattern Matching................................................................................................................. 6 Object Oriented Features .................................................................................................... 7 • Classes & Objects ................................................................................................... 7 • Immediate Objects .................................................................................................. 7 • Functional Objects .................................................................................................. 7 • Cloning Objects ...................................................................................................... 8 Inheritance........................................................................................................................... 8 Coercions ............................................................................................................................ 8 Parametric Polymorphism................................................................................................... 9 Exception Handling .......................................................................................................... 10 Garbage Collector............................................................................................................. 10 OCaml Intrepreter............................................................................................................. 11Introduction OCaml is the most popular variant of the ML branch Caml (Categorical Abstract Machine Language). It is a functional programming language with an added advantage of type inference and static typing, which means that most types are safe and there are very less chances of run time failures in OCaml. So it is considered a safe language. There are lot of intresting features in OCaml which attracts users and makes it an efficient functional language. History of ML/OCaml ML was designed by Robin Milner to allow programmers to develop theorem proving techniques. A series of events led to the development of Caml, which was based on a branch of mathematics developed by Pierre-Louis Curien that allowed for the techniques to be proven in a simple way. In the 90’s, object-oriented programming and type systems were a hot research area. The hype led to the development of OCaml, which is object-oriented but elegantly done, unlike the patched C++. According to the website, OCaml “was the first language (and still is the only language) that combines the full power of object-oriented programming with ML-style static typing and type inference.” Audience While OCaml is an impure functional programming language, it distinguishes itself from most other functional languages by being fast. Based on benchmarks, on average, OCaml performs at least 50% the speed of equivalent C programs, which is fairly good. One of the reasons for this performance is that it was built to be fast from the beginning; furthermore, it supports imperative features in cases where functional programming cannot provide the speed necessary for the application. A distinct feature of OCaml and of the ML programming languages is their use of static types and type inference. This combination allows the compiler to prevent programming errors one usually encounters in Java and C/C++. That is, it guarantees runtime safety without runtime checks (except for cases such as array bounds checking). It is an attractive programming language in that it is has the elegance and readability of functional languages, acceptable execution time, as well as the ability to write large programs thanks to its object-oriented features. It is more tailored towards more numerical programming and in creating programs the assist in proofs. For example, Coq Proof Assistant, as its name implies, helps to verify mathematical proofs. The following are features of OCaml are: Types • Type Inference OCaml is a static-typed language. This means it picks up the type mismatch at the compile time. This makes the interpreter work easier when executing it. Static typing also makes the programs safer , since everything is checked at compile time they are proven to run safe. A dynamically typed language has the advantage of not needing to define its type during compile-time, but potential type errors could occur during runtime. Along with static typing OCaml is also type inferred language which means the user need not specify what type of variable it is , it’s the compiler job to detect it. For example. let multiply2 x = x * 2;; Since 2 is multiplied with x , compiler infers that x has to be a number and it later verifies by the operator specified. Because of the potential ambiguity that arises from floatingpoint and integers, *. is used to denote floating point multiplication. Hence if instead of 2 it is 2.0
View Full Document