[m-users.] Converting types to printable form

Julien Fischer jfischer at opturion.com
Wed May 12 12:15:49 AEST 2021


Hi Sean,

On Tue, 11 May 2021, Sean Charles (emacstheviking) wrote:

> How do you convert a discriminated union to a printable string? I have this:
>     % Character classification types.
> :- type char_type ---> ws; chr; op; cp; ol; cl; om; cm; sq; dq; tk.
> :- type tokstate
>         --->    skip_ws
>         ;       eat_token.
> 
> Do I have to explicitly code a predicate for each type I define or
> not? In Haskell I can just add (show) to the definition and it does
> the write(!) thing.

Mercury provides two families of predicates in its standard library
that do the above via RTTI.  The print family (io.print, io.print_line
etc) are intended to output values in a "human readable" way.
The write family (io.write, io.write_line) output values in a way that
the result is valid Mercury syntax.

The string module provides the function string/1 which does the
same thing as io.write but converts the value into its string
representation rather than printing the representation to a file stream.

> Can Mercury do that other than using io.print_line, sometimes I don’t
> want a new line!


Use io.print, which does the same thing minus the final newline.

Julien.


More information about the users mailing list