ErTR
ErTR

Reputation: 905

Prolog: How to find out which part of code is failing and write a specific message depending on that part?

I want to do something like this in Prolog:

if ((x>0 && x<50)||length==0){
    write("It's OK");
    return true;
}
else if (y==0){
    write("It's not OK cuz of y");
    return false;
}
else{
    write("It's not OK cuz of z");
    return false;
}

I would do so if I write a C# code. I understand concept is different with Prolog but I need to show to user which part is failing.

So I tried like this:

(
    X > 0, X < 50 ; Length = 0 -> write(its_ok) % should assert now and write msg
    ; Y = 0 -> write(not_ok_cuz_of_y)           % should NOT assert but write msg
    ; write(not_ok_cuz_of_z)                    % should NOT assert but write msg
),
assert(something). 

I guess I'm going to the wrong direction.

Upvotes: 1

Views: 42

Answers (1)

Will Ness
Will Ness

Reputation: 71065

Not at all, you just need to fail explicitly, in those branches:

    (   ( X > 0, X < 50 ; Length = 0 ) 
    ->  write(its_ok)
    ;   Y = 0 
    ->  write(not_ok_cuz_of_y),   fail
    ;   write(not_ok_cuz_of_z),   fail
    ),
    .....

Upvotes: 1

Related Questions