[mercury-users] partial instantiation?

Mark Brown mark at csse.unimelb.edu.au
Tue Apr 20 17:12:44 AEST 2010


Hi,

On 20-Apr-2010, Tomas By <tomas at basun.net> wrote:
> On Mon, April 19, 2010 23:24, Mark Brown wrote:
> > Ah, I think I see what you are getting at.  Aside from picking the right
> > cmdproc for the given arguments, you also want to be able to check whether
> > there exists any implementation of the command at all (so you know whether
> > the error is "unknown command" or "funny command line").  It wasn't clear
> > that's what you wanted.
> 
> Well that's just a side effect of using nondet.

Not entirely -- doing this in a single predicate also requires Prolog
unification, or at least something more powerful than Mercury's unification.

> You would need to handle
> undefined commands somehow in your approach also.

    ( if cmd_name(Cmd, CS) then
        ( if cmd_handler(Cmd, Ss, Handler) then
            ... handle command ...
        else
            ... funny cmdline error ...
        )
    else
        ... unknown command error ...
    )

> 
> > I usually do something along the following lines:
> 
> This is what Ralph suggested. If I want the types of the arguments to
> be a factor when picking the right command, and to be able to accept
> strings consisting of only digits for some commands, then this doesn't
> work.

Yes it does:

    cmd_handler(cmd3, [Arg1], handler(N)) :-
       string.to_int(Arg1, N).   % fails if Arg1 is not an integer

Note that you may need cmd_handler to be nondet if there are ambiguities,
or if the compiler can't see that it is a switch.

> 
> > This perhaps isn't as short as you might like, but is very maintainable --
> > the detism ensures that help messages will always be added whenever a new
> > command is added, for example.
> 
> It means you will have to change things in several places, though. That
> was one thing that annoyed me with using getopt. I'd like to have just
> a single list of name/parameters-command bindings, and then the commands.

True; I know of no way to do this in Mercury with a single list.

Cheers,
Mark.

--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the users mailing list