Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break in while/for #11

Open
lcpz opened this issue Aug 1, 2017 · 5 comments
Open

Break in while/for #11

lcpz opened this issue Aug 1, 2017 · 5 comments

Comments

@lcpz
Copy link

lcpz commented Aug 1, 2017

As title says, is there a way to break from while and for in Jason?

Also, it is reported here that these feature are experimental. As of version 2.2, it safe to use them, or it's better to use plan recursion?

@jomifred
Copy link
Contributor

jomifred commented Aug 1, 2017

Hi,

for, while, and if are now quite stable, at least we didn't receive bug reports related to them.

unfortunately, there is no break. Thinking on that, I guess we could explore the idea of "cut" (symbol ! in Prolog) to implement it. Since these loops are a kind of backtracking on all solutions of a query, to "cut" the backtracking seems natural.

We also miss the "cut" in the rules part of Jason....

It is certainly a good improvement for new versions of Jason.

@jomifred
Copy link
Contributor

jomifred commented Aug 1, 2017

Note however that in some cases, the logical programming can handle that. For instance, the code

for ( .member(X,[id1,id3,id4,id8,id20]) ) {
     if ( hasProperty(X) ) {
         doSomethingWIth(X);
         break; // supposing it exists
    }
}

can be recoded as

if ( .member(X,[id1,id3,id4,id8,id20]) &  hasProperty(X) ) {
     doSomethingWIth(X);
}

@lcpz
Copy link
Author

lcpz commented Aug 1, 2017

Would the recoded if still works sequentially in Jason, that is elaborating one list member at time?

@jomifred
Copy link
Contributor

jomifred commented Aug 2, 2017

Hi, I am not sure I've got the details of the question... but the semantics is: if there is a solution for the query, run the block. To find the solution for the query, jason backtracks all possibilities (sequentially and lazily).

Since .member has 5 possible answers for X, it tries the first, checks hasProperties(X), if ok, the query is finished. Otherwise, the second answer for X is considered... and so on. If no answer for X succeeds, the query fails.

@lcpz
Copy link
Author

lcpz commented Aug 2, 2017

Then the answer is yes.

Thanks for the infomations. For me this issue is closed, but I'll leave the decision to you whether to close it or leave it open as a reminder for cut implementation (which would be a good enhancement).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants