by Yu Feng
let x = 3
let y = x + 1
let square x = x * x
let add x y = x + y
let add_curried = fun x -> fun y -> x + y
let add2 = add 2 (* partial application *)
let sign x =
if x > 0 then "positive"
else if x < 0 then "negative"
else "zero"
let pair = (3, "hello")
let (x, y) = pair
let lst = [1; 2; 3]
let rec sum xs = match xs with
| [] -> 0
| x :: xs' -> x + sum xs'
let safe_div x y =
if y = 0 then None
else Some (x / y)
let b = true
let msg = "hello"
let double x = x * 2 (* inferred: int -> int *)
List.map (fun x -> x + 1) [1;2;3]
List.filter (fun x -> x mod 2 = 0) [1;2;3;4]
let rec factorial n =
if n = 0 then 1
else n * factorial (n - 1)
type student = { name: string; id: int }
type expr =
| Int of int
| Add of expr * expr
| Var of string
let rec eval e = match e with
| Int n -> n
| Add (e1, e2) -> eval e1 + eval e2
type logic_op = And | Or | Not
type result = Ok of int | Error of string
type formula = True | Not of formula | And of formula * formula
let rec size f = match f with
| True -> 1
| Not f1 -> 1 + size f1
| And (f1, f2) -> 1 + size f1 + size f2
let env = [("x", 3); ("y", 4)]
List.Assoc.find env "x" ~equal:String.equal
Option.value ~default:0 (Some 5)
Option.value_exn (Some 5)
failwith "something went wrong"
type aexp =
| Var of string | Int of int | Add of aexp * aexp
type formula =
| Eq of aexp * aexp | And of formula * formula
type var = string
type literal = Pos of var | Neg of var
type clause = literal list
type formula = clause list
let ctx = Z3.mk_context []
let x = Z3.Arithmetic.Integer.mk_const_s ctx "x"
let solver = Solver.mk_solver ctx None
let x_gt_1 = Arithmetic.mk_gt ctx x (Integer.mk_numeral_i ctx 1)
let _ = Solver.add solver [x_gt_1]
let result = Solver.check solver []
match Solver.get_model solver with
| Some model -> Model.to_string model
| None -> "unsat"
let rec vcgen stmt post =
match stmt with
| Skip -> post
| Assign (x, e) -> substitute x e post
utop
# let x = 2 + 3;;
# List.map (fun x -> x * 2) [1;2;3];;
proj/
├── dune-project
├── src/
│ ├── main.ml
│ └── dune
(executable (name main))
dune build
dune exec ./src/main.exe