[m-users.] Correct use of solutions.

Sean Charles (emacstheviking) objitsu at gmail.com
Sat Jul 29 21:08:57 AEST 2023


Thank you Julien and Volker.


I have ended up with this, which both works and has helped me increase my understanding of Mercury a little more!

show_targets(!IO) :-
    io.format("Available target languages and command line flags:\n", [], !IO),
    solutions((pred(Entry::out) is nondet :-
        target_name(_, Flag, Text),
        string.format("  %-20s -t %s", [ s(Text), s(Flag) ], Entry)
    ), Targets),
    io.write_list(Targets, "\n", io.print, !IO),
    io.nl(!IO).

    % All supported targets and their command line equivalents.
    %
:- pred target_name(supported_target, string, string).
:- mode target_name(in, out, out) is det.
:- mode target_name(out, out, out) is multi.

target_name(language_c, "c", "C").
target_name(language_python, "python", "Python - untyped").
target_name(language_pythont, "pythont", "Python - typed").
target_name(language_js, "js", "JavaScript").

When I run my tool with the '-T' option to list targets,

Available target languages and command line flags:
  C                    -t c
  JavaScript           -t js
  Python - typed       -t pythont
  Python - untyped     -t python


Perfect, thanks again!
:)
Sean


> On 29 Jul 2023, at 10:21, Volker Wysk <post at volker-wysk.de> wrote:
> 
> Am Samstag, dem 29.07.2023 um 09:27 +0100 schrieb Sean Charles
> (emacstheviking):
>> I've been trying to produce a simple list of strings to output as the
>> response to a command line argument request to list the supported targets
>> of my transpiler, given I have a type and a predicate to return the
>> printable string for the language I thought solutions/2 was my answer, but
>> not so far! Again, it's a mixture of the terminology to my untutored brain
>> and the lack of any really clear guiding examples, in Prolog this stuff is
>> trivial!
>> 
>> 
>>     % Does -T / --targets.
>>     %
>> :- type supported_target
>>     --->    language_c
>>     ;       language_python
>>     ;       language_pythont
>>     ;       language_js.
>> 
>> :- pred show_targets(io::di, io::uo) is det.
>> 
>> 300:show_targets(!IO) :-
>> 301:    io.format("Available target languages:", [], !IO),
>> 302:    solutions(
>> 303:       (pred(A::out) is nondet :-
>> 304:            target_name(_, A)
>> 305:        ),
>> 306:       Targets
>> 307:    ),
>> 308:    io.print_line(Targets, !IO).
>> 
>> 
>> 
>> :- pred target_name(supported_target, string).
>> :- mode target_name(in, out) is det.
>> 
>> target_name(language_c, "Vanilla C").
>> target_name(language_python, "Vanilla Python").
>> target_name(language_pythont, "Typed Python").
>> target_name(language_js, "Vanilla JavaScript").
> 
> 
> I don't know if there's a way to return (nondeterministically) all things of
> an enumeration type. If there isn't, define one:
> 
> :- pred get_target(supported_target::out) is multi.
> 
> get_target(language_c).
> get_target(language_python).
> get_target(language_pythont).
> get_target(language_js).
> 
> 
> Then you can do:
> 
> solutions(
>  (pred(A::out) is multi :-
>      get_target(T), 
>      target_name(T, A)
>  ),
>  Targets)
> 
> 
> Cheers,
> Volker
> _______________________________________________
> users mailing list
> users at lists.mercurylang.org <mailto:users at lists.mercurylang.org>
> https://lists.mercurylang.org/listinfo/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20230729/a2634311/attachment-0001.html>


More information about the users mailing list