[m-users.] Converting types to printable form

Julian Fondren jfondren at minimaltype.com
Wed May 12 06:16:41 AEST 2021


Mercury types get Haskell's Eq, Show, Read, and Ord by default.

:- module stringex.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module string, list.

:- type char_type ---> ws; chr; op; cp; ol; cl; om; cm; sq; dq; tk.
:- type tokstate
---> skip_ws
; eat_token.

main(!IO) :-
S1 = string.string(skip_ws),
X = om,
io.format("%s %s\n", [s(S1), s(string(X))], !IO).

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Tuesday, May 11, 2021 3:10 PM, Sean Charles (emacstheviking) <objitsu at gmail.com> 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. Can Mercury do that other than using io.print_line, sometimes I don’t want a new line!
>
> Thanks
> Sean
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20210511/b4c88a99/attachment.html>


More information about the users mailing list