Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
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
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
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 Type_equal = struct type ('a, 'b) t = T : ('a, 'a) t end module List = struct include List let map ~f l = List.map f l end module Tag = struct type 'a t = | Int : int t | Bool : bool t | String : string t (* Given two tags, returns a "proof" (a value of [(a, b) Type.equal.t]) that their types are equal, or None if they are not. *) let type_equal (type a b) (fst : a t) (snd : b t) : (a, b) Type_equal.t option = match fst, snd with | Int, Int -> Some T (* a = int = b *) | Bool, Bool -> Some T (* a = bool = b *) | String, String -> Some T (* a = string = b *) | _ -> None end module List_with_tag = struct type 'a t = { data : 'a list ; tag : 'a Tag.t } module Packed = struct type 'a unpacked = 'a t type t = T : 'a unpacked -> t end end module Rpc = struct (* call returns a Packed to simulate what an RPC call would likely return (the type information is lost in serialization/deserialization) *) let call (type a) ({ data; tag } : a List_with_tag.t) : List_with_tag.Packed.t = match tag with | Int -> T { tag; data = (List.map ~f:(fun x -> x * 2) data) } | Bool -> T { tag; data = (List.map ~f:(fun b -> not b) data) } | String -> T { tag; data = (List.map ~f:(fun s -> s ^ s) data) } end let call_rpc (type a) (lst : a List_with_tag.t) : a List_with_tag.t = let T result = Rpc.call lst in match Tag.type_equal lst.tag result.tag with | None -> failwith "tag returned from rpc call does not match!" | Some T -> result
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree