Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
sway
ocaml source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
x86-64 ocamlopt 4.04.2
x86-64 ocamlopt 4.06.1
x86-64 ocamlopt 4.07.1
x86-64 ocamlopt 4.07.1-flambda
x86-64 ocamlopt 4.08.1
x86-64 ocamlopt 4.08.1-flambda
x86-64 ocamlopt 4.09.0
x86-64 ocamlopt 4.09.0-flambda
x86-64 ocamlopt 4.09.1
x86-64 ocamlopt 4.09.1-flambda
x86-64 ocamlopt 4.10.0
x86-64 ocamlopt 4.10.0-flambda
x86-64 ocamlopt 4.10.1
x86-64 ocamlopt 4.10.1-flambda
x86-64 ocamlopt 4.10.2
x86-64 ocamlopt 4.10.2-flambda
x86-64 ocamlopt 4.11.1
x86-64 ocamlopt 4.11.1-flambda
x86-64 ocamlopt 4.11.2
x86-64 ocamlopt 4.11.2-flambda
x86-64 ocamlopt 4.12.0
x86-64 ocamlopt 4.12.0-flambda
x86-64 ocamlopt 4.13.1
x86-64 ocamlopt 4.13.1-flambda
x86-64 ocamlopt 4.14.0
x86-64 ocamlopt 4.14.0-flambda
x86-64 ocamlopt 4.14.2
x86-64 ocamlopt 4.14.2-flambda
x86-64 ocamlopt 5.0.0
x86-64 ocamlopt 5.0.0-flambda
x86-64 ocamlopt 5.1.0
x86-64 ocamlopt 5.1.1
x86-64 ocamlopt 5.2.0
Options
Source code
module Region : sig type region val ( #: ) : region -> ('a -> unit) -> 'a -> unit val ( let& ) : (region -> 'a) -> ('a -> 'b) -> 'b end = struct type finalizer = unit -> unit type region = finalizer list ref let make () = ref [] let ( #: ) r f v = r := (fun () -> f v) :: !r let rec exit r = match !r with | [] -> () | f::fs -> begin f () ; r := fs ; exit r end let ( let& ) : (region -> 'a) -> ('a -> 'b) -> 'b = fun constr proc -> let region = make () in Fun.protect ~finally:(fun () -> exit region) begin fun () -> let res = constr region in proc res end end open Region module File : sig type t val open_rw : string -> region -> t val read_line : t -> string option val write_line : t -> string -> unit end = struct type t = { path : string ; mutable data : string list } let rec open_rw path region = Printf.printf "++++ OPEN %s\n" path ; let f = { path ; data = ["foo";"bar";"gee";"qux"] } in region #: close f ; f and close f = f.data <- [] ; Printf.printf "---- CLOSE %s\n" f.path let read_line f = match f.data with | [] -> begin Printf.printf "EOF\n" ; None end | line::rest -> begin f.data <- rest ; Printf.printf "READ %s FROM %s\n" line f.path ; Some line end let write_line f line = Printf.printf "WRITE %s TO %s\n" line f.path ; f.data <- List.append f.data [line] end module SQL_DB : sig type t val connect : string -> region -> t val select : t -> string list val insert : t -> string -> unit end = struct type t = { table : string ; mutable rows : string list } let rec connect table region = Printf.printf "++++ CONNECT TO %s\n" table ; let c = { table ; rows = ["foo";"bar"] } in region #: disconnect c ; c and disconnect conn = conn.rows <- [] ; Printf.printf "---- DISCONNECT FROM %s\n" conn.table let select conn = Printf.printf "SELECT * FROM %s\n" conn.table ; conn.rows let insert conn value = Printf.printf "INSERT INTO %s VALUE \"%s\"\n" conn.table value ; conn.rows <- value :: conn.rows end module Transfer : sig type t val make : db:string -> file:string -> region -> t val upload : t -> unit val download : t -> unit end = struct type t = { db : SQL_DB.t ; fd : File.t } let make ~db:conn_str ~file:path region = let db = SQL_DB.connect conn_str region in let fd = File.open_rw path region in { db : SQL_DB.t ; fd : File.t } let upload { db ; fd } = let rec loop () = match File.read_line fd with | None -> () | Some line -> begin SQL_DB.insert db line ; loop () end in loop () let download { db ; fd } = let lines = SQL_DB.select db in List.iter (File.write_line fd) lines end let _ = ( let& users = Transfer.make ~db:"users" ~file:"/tmp/users" in Transfer.upload users ; Transfer.download users ); print_endline "--------------------------------" ; ( let& posts = Transfer.make ~db:"posts" ~file:"/tmp/posts" in Transfer.download posts ; Transfer.upload posts ); print_endline "--------------------------------" ; ( let& users = Transfer.make ~db:"users" ~file:"/tmp/users" in let& posts = Transfer.make ~db:"posts" ~file:"/tmp/posts" in Transfer.download users ; Transfer.download posts )
Become a Patron
Sponsor on GitHub
Donate via PayPal
Compiler Explorer Shop
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
Statistics
Changelog
Version tree