[m-users.] Correct use of solutions.

Volker Wysk post at volker-wysk.de
Sat Jul 29 19:21:01 AEST 2023


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.mercurylang.org/archives/users/attachments/20230729/cb2073bd/attachment.sig>


More information about the users mailing list