12

There is series of functions like print_int, print_endline and Printf in OCaml. I can't do something like:

let n = 10 in
print n;; (* And I haven't to change `print` in case type of `n` changed *)

That is polymorphic print like in Java, C#, Python and others. Instead, we have C-like with type explicitly defined by programmer. So I think that OCaml losing type information during compilation and doesn't have it at runtime, right? And this is the reason why we need mli files also?

EDIT: I'm tired to write function like *print_listi*, *print_list_tuple2i* and so on. How can I do it better?

1
  • 1
    Well there's the Printf module and all the stuff that goes with it. At least it's an improvement and is somewhat type safe. I don't do enough ocaml to know if there's any other builtin alternatives. Commented Sep 16, 2011 at 9:16

1 Answer 1

22

You are right: OCaml throws away types at run time and therefore no way to distinguish your 10 is really an int or 10th 0-ary variant constructor. Constructor names are neither available, so it is impossible to print data.

Moreover, OCaml's polymorphism is parametric. You cannot define functions that work differently depending on types.

One partial workaround of this is to use CamlP4 to auto-generate printer functions for data types. But still, you cannot have " polymagical" print which works for everything. You have to combine printers by hand, like print_list (print_option print_int).

I have extended OCaml to have such polymorphic print (and other nice things) years ago. It's called GCaml. But not maintained for long.

mli files do not relate to this. They are for writing module signatures, for hiding implementations for simpler interfaces for module users.

4
  • Good answer! Clarify please what do you mean by 'parametric polymorphism'? And don't agree with mli: image you have complied your library. Types are lost. How do you get function signatures without mli?
    – demi
    Commented Sep 16, 2011 at 9:59
  • 1
    types are not lost - the compiled interface is always available - if you don't supply mli file ocaml infers one and compiles it automatically (with all types and values exposed). Moreover this info is used at compile-time, not run-time. This has nothing to do with the essence of your first question.
    – ygrek
    Commented Sep 16, 2011 at 10:17
  • 2
    ygrek's answer is clearer. OCaml is typeful language at compilation but at run-time, it peacefully forgets the types, since ``well-typed programs never go wrong''. Commented Sep 16, 2011 at 12:17
  • 1
    OCaml function type can be polymorphic only if the part is purely a parameter and never accessed by the function. For example, the only possible polymorphic function of type 'a -> unit is let ignore x = (), modulo some trivial side effects like let ignore' x = print_string "I've got something!"; (). If the function accesses the argument, for example, let not_ignore x = x + 1; () then it can no longer be polymorphic. Your print function must touch the argument, therefore it cannot be parametric polymorphic. Non-parametric polymorphism can handle such functions but OCaml does not have it. Commented Sep 16, 2011 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.