Hacker News new | ask | show | jobs
by vidarh 4229 days ago
You're thinking imperatively. It is not "do this" but "generate the set of possible solutions".

The "soft cut" part I'm presuming is similar to Prolog, where a "cut" indicates that once you've gotten past it, you can't backtrack (roughly - my knowledge of Prolog is rather limited) to try other arms - so "choose" is forced down one path by the guard, and then not allowed to also evaluate the other arm, while "if" can generate both alternatives.

1 comments

I am somewhat familiar with declarative programming and Prolog and still don't get it.

The problem is there even without else.

    if (s='a')
        x=2
Should it be

    (s='a') and (x=2)
or

    ((s='a') and (x=2)) or (s!='a')
Because IMHO it should be the second one (it's also basicaly the logical definition of implication, which IMHO should be consistent with "if").

The first interpretation of if without else just give you another way of writing:

    s='a'
    x=2
so why "if" at all?

And if we take the second interpretation of if without else, then when there's else - it should also get the !if_condition implicitly.

"if" here isn't a perfect analog to a traditional imperative language's if statement.

A better set of keywords to describe the logic might be "when ... allow". Because there is an implication here, it's just in reverse.

Because the relation finds all possible solutions that meet the relationship, this code really reads "when s is the letter a, allow x to be 0, and call that a solution. Otherwise, any pairing where x is 2 is a solution."

So if you were to call p(s, 0), you would find that you only get one solution, where s must be 'a'. From an imperative-ish POV, that "sets" (constrains) s to 'a'.

OP is using if, combined with the notions that you take only the first of all relation solutions, and that you treat the rightmost relation member as an output, to achieve an imperative-like behavior in a non-deterministic logic language. Which is mentioned in the paragraph following the example.