Why flambda API differs from original ocaml

Hi, I am working on a project which uses flambda as default.

However, when I try to use Hashtbl.find_all, I realized that there is no such function in 4.10.0+flambda package. The conflict is that OCaml manual 4.10 indeed has this function. I also checked other function like Hashtbl.add, the return value from flambda version and original ocaml also differ. So that API inconsistence between flambda and native ocaml should not be a coincidence.

I thought flambda is only an optimization for execution performance which should not interfere the API, so why the API differs here?

Any help would be appreciated :slight_smile:

1 Like

There is something else going on, that can’t be flambda. We need more detail on your setup and the error you see.

I there possibly a confusion here between the ocaml standard library and the Jane Street libraries’ Base and Core modules?

1 Like

Thanks for your reply, here is my environment for ocaml

root@bcc1953f3def:/workspace/compiler# ocaml --version
The OCaml toplevel, version 4.10.0

and corresponding library.

root@bcc1953f3def:/workspace/compiler# opam list
[WARNING] Running as root is not recommended
# Packages matching: installed
# Name                  # Installed    # Synopsis
astring                 0.8.5          Alternative String module for OCaml
base                    v0.14.1        Full standard library replacement for OCaml
base-bigarray           base
base-bytes              base           Bytes library distributed with the OCaml compiler
base-threads            base
base-unix               base
base_bigstring          v0.14.0        String type based on [Bigarray], for use in I/O and C-bindi
base_quickcheck         v0.14.1        Randomized testing framework, designed for compatibility wi
batteries               3.3.0          A community-maintained standard library extension
bin_prot                v0.14.0        A binary protocol generator
biniou                  1.2.1          Binary data format designed for speed, safety, ease of use
bitv                    1.6            A bit vector library for OCaml
camomile                1.0.2          A Unicode library
charInfo_width          1.1.0          Determine column width for a character
cmdliner                1.0.4          Declarative definition of command line interfaces for OCaml
core                    v0.14.0        Industrial strength alternative to OCaml's standard library
core_kernel             v0.14.2        Industrial strength alternative to OCaml's standard library
cppo                    1.6.7          Code preprocessor like cpp for OCaml
csexp                   1.5.1          Parsing and printing of S-expressions in Canonical form
dot-merlin-reader       4.1            Reads config files for merlin
dune                    2.9.1          Fast, portable, and opinionated build system
dune-build-info         2.9.1          Embed build informations inside executable
dune-configurator       2.9.1          Helper library for gathering system configuration
easy-format             1.3.2          High-level and functional interface to the Format module of
fieldslib               v0.14.0        Syntax extension to define first class values representing
fix                     20201120       Facilities for memoization and fixed points
fpath                   0.7.3          File system paths for OCaml
jane-street-headers     v0.14.0        Jane Street C header files
jst-config              v0.14.1        Compile-time configuration for Jane Street libraries
lambda-term             3.1.0          Terminal manipulation library for OCaml
lwt                     5.4.2          Promises and event-driven I/O
lwt_log                 1.1.1          Lwt logging library (deprecated)
lwt_react               1.1.4          Helpers for using React with Lwt
menhir                  20210419       An LR(1) parser generator
menhirLib               20210419       Runtime support library for parsers generated by Menhir
menhirSdk               20210419       Compile-time library for auxiliary tools related to Menhir
merlin                  3.6.1          Editor helper, provides completion, typing and source brows
mew                     0.1.0          Modal editing witch
mew_vi                  0.5.0          Modal editing witch, VI interpreter
mmap                    1.1.0          File mapping functionality
num                     1.4            The legacy Num library for arbitrary-precision integer and
ocaml                   4.10.0         The OCaml compiler (virtual package)
ocaml-compiler-libs     v0.12.4        OCaml compiler libraries repackaged
ocaml-config            1              OCaml Switch Configuration
ocaml-migrate-parsetree 2.2.0          Convert OCaml parsetrees between different versions
ocaml-variants          4.10.0+flambda Official release 4.10.0, with flambda activated
ocamlbuild              0.14.0         OCamlbuild is a build system with builtin rules to easily b
ocamlfind               1.9.1          A library manager for OCaml
ocamlformat             0.19.0         Auto-formatter for OCaml code
ocamlgraph              2.0.0          A generic graph library for OCaml
ocp-indent              1.8.1          A simple tool to indent OCaml programs
ocplib-endian           1.1            Optimised functions to read and write int16/32/64 from stri
octavius                1.2.2          Ocamldoc comment syntax parser
odoc-parser             0.9.0          Parser for ocaml documentation comments
parsexp                 v0.14.1        S-expression parsing library
ppx_assert              v0.14.0        Assert-like extension nodes that raise useful errors on fai
ppx_base                v0.14.0        Base set of ppx rewriters
ppx_bench               v0.14.1        Syntax extension for writing in-line benchmarks in ocaml co
ppx_bin_prot            v0.14.0        Generation of bin_prot readers and writers from types
ppx_cold                v0.14.0        Expands [@cold] into [@inline never][@specialise never][@lo
ppx_compare             v0.14.0        Generation of comparison functions from types
ppx_custom_printf       v0.14.1        Printf-style format-strings for user-defined string convers
ppx_derivers            1.2.1          Shared [@@deriving] plugin registry
ppx_deriving            5.2.1          Type-driven code generation for OCaml
ppx_enumerate           v0.14.0        Generate a list containing all values of a finite type
ppx_expect              v0.14.1        Cram like framework for OCaml
ppx_fields_conv         v0.14.2        Generation of accessor and iteration functions for ocaml re
ppx_fixed_literal       v0.14.0        Simpler notation for fixed point literals
ppx_hash                v0.14.0        A ppx rewriter that generates hash functions from type expr
ppx_here                v0.14.0        Expands [%here] into its location
ppx_inline_test         v0.14.1        Syntax extension for writing in-line tests in ocaml code
ppx_jane                v0.14.0        Standard Jane Street ppx rewriters
ppx_js_style            v0.14.1        Code style checker for Jane Street Packages
ppx_let                 v0.14.0        Monadic let-bindings
ppx_module_timer        v0.14.0        Ppx rewriter that records top-level module startup times
ppx_optcomp             v0.14.3        Optional compilation for OCaml
ppx_optional            v0.14.0        Pattern matching on flat options
ppx_pipebang            v0.14.0        A ppx rewriter that inlines reverse application operators `
ppx_sexp_conv           v0.14.3        [@@deriving] plugin to generate S-expression conversion fun
ppx_sexp_message        v0.14.1        A ppx rewriter for easy construction of s-expressions
ppx_sexp_value          v0.14.0        A ppx rewriter that simplifies building s-expressions from
ppx_stable              v0.14.1        Stable types conversions generator
ppx_string              v0.14.1        Ppx extension for string interpolation
ppx_typerep_conv        v0.14.2        Generation of runtime types from type declarations
ppx_variants_conv       v0.14.1        Generation of accessor and iteration functions for ocaml va
ppxlib                  0.22.2         Standard library for ppx rewriters
re                      1.9.0          RE is a regular expression library for OCaml
react                   1.2.1          Declarative events and signals for OCaml
result                  1.5            Compatibility Result module
seq                     base           Compatibility package for OCaml's standard iterator type st
sexplib                 v0.14.0        Library for serializing OCaml values to and from S-expressi
sexplib0                v0.14.0        Library containing the definition of S-expressions and some
spawn                   v0.14.0        Spawning sub-processes
splittable_random       v0.14.0        PRNG that can be split into independent streams
stdio                   v0.14.0        Standard IO library for OCaml
stdlib-shims            0.3.0          Backport some of the new stdlib features to older compiler
time_now                v0.14.0        Reports the current time
timezone                v0.14.0        Time-zone handling
topkg                   1.0.3          The transitory OCaml software packager
trie                    1.0.0          Strict impure trie tree
typerep                 v0.14.0        Typerep is a library for runtime types
uchar                   0.0.2          Compatibility library for OCaml's Uchar module
utop                    2.8.0          Universal toplevel for OCaml
uucp                    13.0.0         Unicode character properties for OCaml
uuseg                   13.0.0         Unicode text segmentation for OCaml
uutf                    1.0.2          Non-blocking streaming Unicode codec for OCaml
variantslib             v0.14.0        Part of Jane Street's Core library
yojson                  1.7.0          Yojson is an optimized parsing and printing library for the
zed                     3.1.0          Abstract engine for text edition in OCaml

As for my system (It’s in a container).

root@bcc1953f3def:/workspace/compiler# cat /etc/issue
Ubuntu 20.04.3 LTS \n \l

Here is a code snippet

let rec build_adj_hash (prog : program) hash = 
  match prog with 
    [] -> hash
  | h :: t -> 
    List.iter t ~f:(fun x -> (Hashtbl.add hash ~key:h.define ~data:x));
    build_adj_hash t hash;

When I try to dune build, the error is

List.iter t ~f:(fun x -> (Hashtbl.add hash ~key:h.define ~data:x));
Error: This expression has type [ `Duplicate | `Ok ]
       but an expression was expected of type unit

The [Duplicate | Ok] is apparently not return value for Hashtbl.add by OCaml Hashtbl document.

Thank you for your help, please let me know if there is any other useful information I can provide :slight_smile:

Yeah… I watched several tutorials for OCaml standard library, but did not know much about Jane Street libraries’ Base and Core modules.

Are you saying that I am not using the standard library? If so, how can I check it?

Thank you for your help!

From the presence of labels in your call to list, it looks like you have an open Base or open Core at the top of your .ml file.

Both Core and Base are alternative standard library that shadows the module of the standard library. Consequently, we need to look at the documentation of base: Base · base v0.15.0 · OCaml Packages rather than the documentation of the original standard library.

1 Like

Also, in case you’re using the book ‘Real World Ocaml’, beware that they are using Base (and Core?) throughout. So while it’s an excellent book, it does describe a particular flavor of Ocaml. Nowadays, it’s more doable to get by using just the standard library distributed with the language than when RWO was first published.

I recommend following one of the first books on this page listed before Real World OCaml, due to the standard library issues.

By looking at your project definition. If you’re using dune, then look at the dune file and check if base or core library dependencies are present. Also check if there is a top-level -open Base (or -open Core) is defined in the dune file or the equivalent in the relevant source code file.

Ohhhhh, that’s correct. I opened Core at the top of the .ml file. I didn’t realize there Base and Core are alternative standard library. Really appreciate for pointing out!

Thank you! I am indeed using RWO, and I didn’t realize there is a distinction between the library it uses and the standard library. It really helps!

Sure! I will check it!

Thank you for the comprehensive guidance! I checked my .ml file and yes I am using Core. Cheers!