Is there a way to print polymorphic values in Standard ML (SML/NJ specifically)? I have a polymorphic function that is not doing what I want and due to the abysmal state that is debugging in SML (see Any real world experience debugging a production functional program?), I would like to see what it is doing with some good-ol' print
's. A simple example would be (at a prompt):
fun justThisOnce(x : 'a) : 'a = (print(x); x);
justThisOnce(42);
Other suggestions are appreciated. In the meantime I'll keep staring the offending code into submission.
I was able to find the bug but the question still stands in the hopes of preventing future pain and suffering.
No, there is no way to print a polymorphic value. You have two choices:
Specialize your function to integers or strings, which are readily printed. Then when the bug is slain, make it polymorphic again.
If the bug manifests only with some other instantiation, pass show
as an additional argument to your function. So for example, if your polymorphic function has type
'a list -> 'a list
you extend the type to
('a -> string) -> 'a list -> 'a list
You use show
internally to print, and then by partially applying the function to a suitable show
, you can get a version you can use in the original context.
It's very tedious but it does help. (But be warned: it may drive you to try Haskell.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With