[m-users.] Factored out disjunction no longer compiles, generates determinism error.

Tomas By tomas at basun.net
Sun May 9 08:50:00 AEST 2021


On Sat, 08 May 2021 23:30:53 +0200, Sean Charles (emacstheviking) wrote:
> [...] I thought I was getting somewhere with it but now I am faced
> with yet another round of battling determinism errors [...]

On Sat, 08 May 2021 23:54:04 +0200, Zoltan Somogyi wrote:
> [...] I think you need to read section 6.2 of the reference manual,
> and judging by earlier emails, the Prolog to Mercury transition
> guide as well.


Also, using nondet is easier.

|main(!IO) :-
|  io.write_string(">",!IO),
|  io.read_line(Res,!IO),
|  %io.print(string(Res)++"\n",!IO),
|  ( Res = io.ok(Cs0),
|    ( remove_suffix(Cs0,['\n'],New) ->
|      Cs = New
|    ; Cs = Cs0 ),
|    solutions((pred(X::out) is nondet :-
|      parse_cmd(Cmd,Cs,Rs), X = Cmd-Rs),Ps),
|    ( Ps = [C-_R] ->
|      io.format("Command: %s\n",[s(string(C))],!IO)
|    ; io.write_string("Syntax error\n",!IO) )
|  ; Res = io.eof,
|    io.write_string("No input\n",!IO)
|  ; Res = io.error(Err),
|    io.write_string(io.error_message(Err)++"\n",!IO) ).
|
|:- type command ---> help ; lex ; ast.
|
|:- pred parse_cmd(command,list(char),list(char)).
|:- mode parse_cmd(out,in,out) is nondet.
|
|parse_cmd(Cmd) -->
|  ( help_cmd(Cmd) % ['h','e','l','p'], skip_ws, eol, { Cmd = help }
|  ; ['l','e','x'], skip_ws, eol, { Cmd = lex }
|  ; ['a','s','t'], skip_ws, eol, { Cmd = ast } ).
|
|:- pred help_cmd(command,list(char),list(char)).
|:- mode help_cmd(out,in,out) is nondet.
|
|help_cmd(help) -->
|  ['h','e','l','p'],
|  skip_ws,
|  eol.
|
|:- pred skip_ws(list(char),list(char)).
|:- mode skip_ws(in,out) is nondet.
|
|skip_ws(In,Out) :-
|  ( [C|Cs] = In ->
|    ( list.member(C,[' ','\t']) ->
|      skip_ws(Cs, Out)
|    ; Out = In )
|  ; Out = In ).
|
|:- pred eol(list(char),list(char)).
|:- mode eol(in,out) is nondet.
|
|eol([],[]).


/Tomas


More information about the users mailing list