简体   繁体   中英

Visual Prolog error c502: The expression does not produce a value

I am trying to translate a simple Turbo Prolog problem to Visual Prolog 7.1

The original Turbo Prolog code is the following.

    DOMAINS
            s=string   sl=s*  sll=sl*

    PREDICATES
            select(sl,s,sl)
            solve(sll)

    CLAUSES

            select([A|B],A,B).
            select([A|B],C,[A|D]):- select(B,C,D).

            solve([["Anna",A,A],["Kate",Vp,Vt],                ["Natasha",Np,"green"]]):-
                    select(["white","green","blue"],A,ColPl),
                    select(["white","blue"],A,[Vt]), Vt<>"white",
                    select(ColPl,Vp,[Np]), Vp<>"white", Np<>"green".

And its resulting list is outputted with solve(Out) with provides a correct result to the Turbo Prolog console.

When trying to translate this to Visual Prolog, I get error c502 in line 33.

    implement main
        open core

    constants
        className = "main".
        classVersion = "".
    domains
    s=string.
    sl=s*.
    sll=sl*.
    %
    class predicates
    select:(sl,s,sl) nondeterm anyflow.
    solve:(sll) nondeterm anyflow.
    %
    clauses
    %
    select([A|B],A,B).
    select([A|B],C,[A|D]):- select(B,C,D).
    %
    solve([["Anna",A,A],["Kate",Vp,Vt],["Natasha",Np,"green"]]):-
    select(["white","green","blue"],A,ColPl),
    select(["white","blue"],A,[Vt]), Vt<>"white",
    select(ColPl,Vp,[Np]), Vp<>"white", Np<>"green".

    clauses
        classInfo(className, classVersion).

    clauses
        run():-
            console::init(),
            %ERROR AFTER THIS LINE
            stdIO::writef("%", solve(Out)),fail().

    end implement main

    goal
        mainExe::run(main::run).

What I get from this error is that solve(Out) does not give anything to print. What I do not know is how to change the code to produce something to print.

I am a beginner in Prolog and I cannot figure out how to fix this problem and Google is not much of a help either, this seems to be very obscure problem.

Thank you!

I'm not familiar with Visual Prolog, but could you rewrite the offending line as:

solve(Out), stdIO::writef("%", Out),fail().

and try again?

Remember that predicates are not functions like in other programming languages; they do not have a return value.

EDIT to answer comment : a procedure predicate should succeed exactly one time. Here, main is calling your solve function which my fail or succeeds several time. To ensure that, you can try to wrap the call to solve into another predicate:

wrap_solve(S) :- solve(S), !.
wrap_solve([]).

The cut after the call to solve should ensure that you get only one solution if it succeeds. If there's no solution (ie, the call to solve fails), then the second clause will give a default value (an empty list in that case).

In main , you should call wrap_solve instead of solve .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM