Skip to content

Instantly share code, notes, and snippets.

let test b x y =
match if b then x, y else y, x with | 0, 1 -> 0 | _, _ -> 1
let test2 b x y =
let c, d = if b then x, y else y, x in
match c, d with | 0, 1 -> 0 | _, _ -> 1
type test =
| Int of int
| Float of float
| Num of Num.num [@printer fun fmt num -> fprintf fmt "%s" (Num.string_of_num num)]
[@@deriving show]
type arith_op = Add | Sub | Mul | Div
type rel_op = And | Or
type _ term =
| Bool : bool -> bool term
| Int : int -> int term
| Arith : arith_op * int term * int term -> int term
| Rel : rel_op * bool term * bool term -> bool term
| If : bool term * 'a term * 'a term -> 'a term
type arith_op = Add | Sub | Mul | Div
type rel_op = And | Or
type _ term =
| Bool : bool -> bool term
| Int : int -> int term
| Arith : arith_op * int term * int term -> int term
| Rel : rel_op * bool term * bool term -> bool term
| If : bool term * 'a term * 'a term -> 'a term
let test x y =
let () = () in
(fun () -> ignore x; ignore y; ())
(* ggole@Stross:~/src/ocaml❯ ocamlopt -c -dcmm test.ml *)
(* (data int 1792 global "camlTest" "camlTest": int 1) *)
(* (data *)
(* int 4087 *)
(* "camlTest__1": *)
(* addr "caml_curry2" *)
type foo = [`B | `C | `D]
let f (x : [`A | foo]) =
match x with
| `A -> true
| #foo as x -> ignore (x :> foo); true
let refine x : [`B | `C] option =
match x with
| `A -> None
| (`B | `C) as x -> Some x
let f (x : [`A | `B]) =
match refine x with
| None -> assert false
| Some x -> x
let test n list =
let cons = ref list in
for i = 0 to n - 1 do
match !cons with
| [] -> ()
| _::xs -> cons := xs
done;
!cons
let test2 observe n list =
(*
* Structural equality of objects in OO langauges like Java is
* typically implemented using downcasts, guarded by instanceof
* checks. The (dynamically checked) downcast provides the
* necessary access to the structure of the other object.
*
* OCaml lacks downcasts, but similar access can be granted in a
* typesafe way with equality witnesses.
*)
type (_, _) eq =
| Eq : ('a, 'a) eq
| NEq : (_, _) eq
module type Eq = sig
type 'a key
type 'a value
val eq : 'a key -> 'b key -> ('a, 'b) eq
end