2014-12-03

Published on 2014-12-03


OCaml Core

Basics

  1. Phrases are terminated by ;;
  2. Basic data types: booleans, characters, and character strings
  3. + - * / are for integers, and +. -. *. /. are for floats. For example, 1 + 2, and 1.2 +. 3.4
  4. Comments are in (* and *) pairs
  5. Predifined data structures: tuples, arrays, and lists

    (* list example *)
    let l = ["is"; "a"; "mystery."];;
    "Life" :: l;; (* the :: operater will add an element in the front of the list *)
    
    (* array example *)
    let add_vect v1 v2 =
    let len = min (Array.length v1) (Array.length v2) in
    let res = Array.create len 0.0 in
    for i = 0 to len - 1 do
    res.(i) <- v1.(i) +. v2.(i)
    done;
    res;;
    add_vect [| 1.0; 2.0 |] [| 3.0; 4.0 |];;
    
    (* tuple example *)
    let t = (1, "is", 'o');;
    t;;
  6. Immutable: most OCaml data structures are immutable, but a few are mutable such as arrays.

  7. Functional: functions in the full mathematical sense are supported and can be passed around freely just as any other piece of data. For example:

    let inc = function x -> x + 1;;
    let compose f g = function x -> f (g x);; (* a function whose arguments are also functions *)
    let inc2 = compose inc inc;;
    inc2 1;; (* should get 3 *)
  8. Stand alone programs: write in a .ml format file, such as a.ml, and can be compiled by

    ocamlc -o a.out a.ml
    

User-defined data structures

Include records and variants.

  • Record example:
type ratio = {num: int; denom: int};;
let add_ratio r1 r2 = {
    num = r1.num * r2.denom + r2.num * r1.denom;
    denom = r1.denom * r2.denom
};;
add_ratio {num=1; denom=3} {num=2; denom=5};;
  • Variant example
type number = Int of int | Float of float | Error;;
type sign = Positive | Negative;;
let sign_int n = if n >= 0 then Positive else Negative;;
let add_num n1 n2 =
    match (n1, n2) with
        (Int i1, Int i2) ->
            (* Check for overflow of integer addition *)
            if sign_int i1 = sign_int i2 && sign_int (i1 + i2) <> sign_int i1
                then Float(float i1 +. float i2)
            else Int(i1 + i2)
        | (Int i1, Float f2) -> Float(float i1 +. f2)
        | (Float f1, Int i2) -> Float(f1 +. float i2)
        | (Float f1, Float f2) -> Float(f1 +. f2)
        | (Error, _) -> Error
        | (_, Error) -> Error;;
add_num (Int 123) (Float 3.14159);;

Imperative

OCaml is also equipped with full imperative features. This includes the usual while and for loops, as well as mutable data structures such as arrays.

type mutable_point = { mutable x: float; mutable y: float };;
let translate p dx dy =  p.x <- p.x +. dx; p.y <- p.y +. dy;;
let mypoint = { x = 0.0; y = 0.0 };;
translate mypoint 1.0 2.0;;
mypoint;;

There are also References provided by the standard library, which are mutable indirection cells (or one-element arrays), with operators ! to fetch the current contents of the reference and := to assign the contents.

Exceptions

  • exception: declare an exceotion type.
  • raise: signal an exception.
  • try...with: trap exceptions.

Symbolic processing

OCaml for symbolic processing: formal manipulations of arithmetic expressions containing variables.

type expression =
    Const of float
    | Var of string
    | Sum of expression * expression (* e1 + e2 *)
    | Diff of expression * expression (* e1 - e2 *)
    | Prod of expression * expression (* e1 * e2 *)
    | Quot of expression * expression (* e1 / e2 *)
;;

exception Unbound_variable of string;;

let rec eval env exp =
    match exp with
        Const c -> c
        | Var v -> (try List.assoc v env with Not_found -> raise (Unbound_variable v))
        | Sum(f, g) -> eval env f +. eval env g
        | Diff(f, g) -> eval env f -. eval env g
        | Prod(f, g) -> eval env f *. eval env g
        | Quot(f, g) -> eval env f /. eval env g
;;

eval [("x", 1.0); ("y", 3.14)] (Prod(Sum(Var "x", Const 2.0), Var "y"));;

About Robots

Tools and Frameworks

  • OpenCog and its source: for Artificial General Intelligence, haven't been finished, coded in C/C++ and Python, have already cost over $3000,000. See Demo Video.
  • aHuman: aimed to create alive being existing by means of software program, is still developing, written in C.
  • NuPIC: for Intelligent Computing, written in C++ and Python, mainly implements HTM.
  • ACT-R and its source: written in LISP, a cognitive architecture: a theory for simulating and understanding human cognition.
  • PAL, strong but heavy, mainly in Java, all downloads are deprecated. It is to integrate many artificial intelligence approaches (natural language processing, speech recognition, machine vision, probabilistic logic, planning, reasoning, many forms of machine learning) into an AI assistant that learns to help manage your office environment. Excellently, Siri is derived from it.