Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does % mean in an OCaml external declaration?

Many external declarations in the OCaml standard library have a % at the beginning of the function name, such as the definition of int_of_float:

external int_of_float : float -> int = "%intoffloat"

What does the '%' mean?

like image 966
Michael Ekstrand Avatar asked Dec 16 '09 17:12

Michael Ekstrand


People also ask

What does :: do in OCaml?

Regarding the :: symbol - as already mentioned, it is used to create lists from a single element and a list ( 1::[2;3] creates a list [1;2;3] ).

What does arrow mean in OCaml?

The operator is usually called type arrow where T1 -> T2 represents functions from type T1 to type T2 . For instance, the type of + is int -> (int -> int) because it takes two integers and returns another one.

Does OCaml use C?

The OCaml runtime system comprises three main parts: the bytecode interpreter, the memory manager, and a set of C functions that implement the primitive operations.

WHAT IS A in OCaml?

The type 'a is a type variable, and stands for any given type. The reason why sort can apply to lists of any type is that the comparisons (=, <=, etc.) are polymorphic in OCaml: they operate between any two values of the same type. This makes sort itself polymorphic over all list types.


2 Answers

There's a lot of %foo special primitives hiding in the compiler. I think the best list is available in bytecomp/translcore.ml, in the ocaml compiler sources. Let's see how many I can list here:

  • Comparisons: %equal, %notequal, %lessthan, %greaterthan, %lessequal, %greaterequal, %compare

These comparisons have specialized versions for int, float, string, nativeint, int32 and int64, and will auto-specialize if the types are known at compile-time.

  • Other primitives: %identity, %ignore, %field0, %field1, %setfield0, %makeblock, %makemutable, %raise, %incr, %decr, %seqand, %seqor, %boolnot
  • Int ops: %negint, %succint, %predint, %addint, %subint, %mulint, %divint, %modint, %andint, %orint, %xorint, %lslint, %lsrint, %asrint
  • Int comparators (??): %eq, %noteq, %ltint, %leint, %gtint, %geint
  • Float ops: %intoffloat, %floatofint, %negfloat, %absfloat, %addfloat, %subfloat, %mulfloat, %divfloat
  • Float comparators: %eqfloat, %noteqfloat, %ltfloat, %lefloat, %gtfloat, %gefloat
  • String ops: %string_length, %string_safe_get, %string_safe_set, %string_unsafe_get, %string_unsafe_set
  • Array ops: %array_length, %array_safe_get, %array_safe_set, %array_unsafe_get, %array_unsafe_set
  • Object manipulation: %obj_size, %obj_field, %obj_set_field, %obj_is_int
  • Lazy: %lazy_force
  • Nativeint,int32,int64 ops: %{nativeint,int32,int64}: _of_int, _to_int, _neg, _add, _sub, _mul, _div, _mod, _and, _or, _xor, _lsl, _lsr, _asr
  • Int conversions: %nativeint_{of,to}_int32, int64_{of,to}_int32, int64_{of,to}_nativeint
  • Bigarray operations: %caml_ba_ref_{1,2,3}, %caml_ba_set_{1,2,3}, %caml_ba_unsafe_ref_{1,2,3}, %caml_ba_unsafe_set_{1,2,3}
  • Object Oriented: %send, %sendself, %sendcache

That's all I can find.

like image 66
Thelema Avatar answered Oct 19 '22 09:10

Thelema


external with % are special external, that will be handled specially by the compiler. For example, with int_of_float, ocamlc will compile it into a call of some C function, but with ocamlopt, it will compile it into some special assembler opcode that transform double to integer.

like image 25
Rémi Avatar answered Oct 19 '22 08:10

Rémi