3.3 KiB
3.3 KiB

CAML is a categorical abstract machine language. It is strongly typed. It was created by INRIA in 1984.
1.1. General idea
It's a simple language to write.
# 1+2 ;;
- # is the prompt (we don't have to write it)
- 1 + 2 is the program
- ;; is the end of the line
_: int=3 (*That a Caml answer to the previous program*)
# 1+2.2;; (*That an error*)
# 1. +. 2.2;; (*This is good*)
1.2. CAML interaction
# 1;; (*He can be a simple expression*)
_: int=1
# "Hello World";; (*He can be a string*)
_:string = "Hello World"
The general form is :
# expr ;;
_: type of the evaluation = result of the evaluation
1.3. Definitions
Global definitions
# let x=1+2;;
val x:int = 3 (*our current env is x -> 3*)
# let y=x/2;;
val y:int = 1 (*our current env is x -> 3 ; y -> 1*)
# let x = 2
val x : int = 2
# x;;
_: int = 2
# let name = expr;;
val name: type of evaluation
Local definitions
# let r = 1. in let pi = 3.14 in 2*pi*r;;
- : float = 6.28
# let x = 10 in y = 2*x in x+y;;
- : float = 30
# x;;
- : int =2 (*see global definition section*)
# pi;;
error: unbound value pi
when there is the keyword len
and in
that create a local definition. A local definition is something that only exist in our expression and then it disappear. You can define a local definition into a variable : let x = let r = 1. in let pi = 3.14 in 2*pi*r;;
(for example)
Multiple definitions
# let r = 1. and pi = 3.14 in 2 *. pi *. r;; (*local version*)
- : float = 6.28
# let x = 1.2 and y = "Hello";;
val x: float = 1.2 (*our current env is x -> 3 ; y -> 1 ; x -> 1.2 *)
val y: string = "Hello" (*our current env is x -> 3 ; y -> 1 ; x -> 1.2 ; y -> "Hello"*)
# let x = 2 and y = x + 1 in y + 2;;
Error (*because x is 1.2 in our current evaluation*)
⚠️ We have only one
let
and onein
for the multiple definitions methods
1.4 Basic types
-
Integer:
int
- Compatible operator : + ; - ; * ; / ; mod
- Same operator priority than in mathematics
# 2+3*2;; _: int = 8 # (2+3)*3;; _: int = 10 # max_int 1 073 741 823 _: int = 2^30-1 # max_int -1 073 741 824 _: int = -2^30
-
Float:
float
- Compatible operator : -. ; +. ; * . ; /.
-
Booleans:
bool
- Can be
true
orfalse
# let x = 2 and y = 1 in x/y > 1;; _: bool = true # let x = 2 and y = 0 in x/y > 1;; Exception: Division by zero # let x = 2 and y = 0 in y<>0 && x/y> 1;; _: bool = false
- Can be
-
Char:
char
- 8 bits -> extended ASCII table
# 'a'= 'A';;
_: bool = false
# 'a'
- : char = 'a'
- String:
string
# "ab" < "abc" ;;
_: bool = true (*because O chars -> 2^24 - 6 chars*)
# "Hello" ^" World"
_: string = "Hello World"
# "abc".[1]
_: char = "b"
⚠️ Les minuscules passent avant les majuscules
Comparaison
|Mathematics|$=$|$\not=$|$<$|$>$|$\eqslantgtr$|$\eqslantless$|
|:----:|:----:|:----:|:----:|:----:|:----:|
|CAML|=
|<>
|<
|>
|<=
|>=
|
CAML | Maths | English |
---|---|---|
not | \neg |
negation |
&& | \land |
and |
// (vertical bar) | \lor |
or (inclusive) |