How to print polymorphic values ​​in standard ML?

Is there a way to print polymorphic values ​​in standard ML (specifically for SML / NJ)? I have a polymorphic function that does not do what I want, and because of the absurd state that debugs in SML (see Any real-world experience debugging a production functional program? ), I would like to see what it does with some good print tins. A simple example would be (at the tip):

 fun justThisOnce(x : 'a) : 'a = (print(x); x); justThisOnce(42); 

Other suggestions are welcome. In the meantime, I will continue to look at breaking code in submission.

Update

I could find a mistake, but the question still stands in the hope of preventing future pain and suffering.

+6
polymorphism functional-programming printing sml
source share
2 answers

No, it is not possible to print a polymorphic value. You have two options:

  • Specialize your function with integers or strings that are easy to print. Then, when the error is killed, make it polymorphic again.

  • If the error only appears with some other instance, pass show as an additional argument to your function. For example, if your polymorphic function is of type

     'a list -> 'a list 

    you expand the type to

     ('a -> string) -> 'a list -> 'a list 

    You use show internally for printing, and then, partially applying this function to a suitable show , you can get a version that you can use in the original context.

    It is very tiring, but it helps. (But be careful: this may make you try Haskell.)

+8
source share

Only in MOSML . For debugging purposes, use the printVal function. Please note that this function is only available in top-level mode, this will lead to an error when trying to compile your program.

Change In this case, I am afraid that there is no general solution, you need to explicitly translate your values ​​into strings and print them. See Other Answers for Useful Suggestions.

0
source share

All Articles