Implicit module initialization in SICStus Prolog - module

When I use a previously unused module f, the module is initialized and is known afterwards:
| ?- current_module(f).
no
| ?- assert(f:x).
yes
| ?- current_module(f).
yes
This also happens if I try to use a non-existent procedure:
| ?- current_module(f).
no
| ?- f:x.
! Existence error in f:x/0
! procedure f:x/0 does not exist
! goal: f:x
| ?- current_module(f).
yes
As far as I can tell, two predicates behave differently: current_predicate and predicate_property.
| ?- current_module(f).
no
| ?- current_predicate(f:X).
no
| ?- findall(X-P, predicate_property(f:X,P), XPs).
XPs = [call(_A)-(meta_predicate call(0)),
call(_B)-built_in,
...|...] ? ;
no
| ?- current_module(f).
no
So here's my actual question:
Do other SICStus Prolog predicates behave like current_predicate and predicate_property? If so, which ones?

Related

Creating a context-free grammar for a specific language

I am trying to create a context-free grammar for the language
L = {u2v; u,v E {a,b}*; |u| >= |v|}
however, I can't really understand how to pick up from here.
My idea is that for every a/b character that I generate in u, I should generate another a/b character in the string v. My biggest problem is the symbol 2 there, as I don't know how to add it after doing all of this or how to write a rule saying that it should be skipped.
How can this grammar be constructed?
A Context Free Grammar would be:
G = ({S,T},{a,b,2,},S,P)
P:
S-> aSa | aSb | bSa | bSb | T
T-> aT | bT | 2

Addition function in Prolog

Is there a way I can create a function for addition/3 which does that:
add(1,2,X) - X is 3.
add(1,X,3) - X is 2.
add(X,2,3) - X is 1.
As a beginner, the best is to use library(clpfd) which provides all that functionality ; and even more than that. With
?- use_module(library(clpfd)).
We start, in SICStus you have now to tell assert(clpfd:full_answer), then we have:
?- 1+2#=Z.
Z = 3.
?- 1+Y#=3.
Y = 2.
as you expected it. But even more than that!
?- X+X#=Z.
2*X#=Z.
?- X+X#=X.
X = 0.
?- X+Y#=Z.
X+Y#=Z.

Is my solution correct for this context free grammar?

I'm trying to solve this problem (I think I might have solved it): http://d.pr/i/L5Qm
L = {a3nb2n | n >= 0}
Basically the problem is saying l is not equal to m or m is not equal to n
Rules that I've generated:
S -> aaaSbb | A
A -> a | ^
A few tests:
Test one: S --> aaaSbb -> aaaAbb -> aaabb
Test two: S --> A -> a
Test three: S --> A -> ^
I'm sure there's a lot more that I could have tested but I'm not quite sure how to test for the majority of problems as I'm quite new to these. I'm thankful for any help.
Your test two:
Test two: S --> A -> a
Clearly shows that your grammar is wrong!
In your language:
L = {a3nb2n | n >= 0}
You always have three a for each two b, and a is not in language L.
The correct productions for your language is:
S ---> aaaSbb | ^
Where ^ is null symbols. notice ^ is also in language because n can be 0.
Edit:
you grammar:
S -> aaaSbb | A
A -> a | ^
produce union of two languages, that is:
{a3nb2n | n >= 0} U {a3n+1b2n | n >= 0}
The extra part a3n+1b2n is due to A--> a production.

BNF Grammar Ambiguity

I was recently thinking of the following BNF
A -> x | yA | yAzA
where x,y,z are terminals.
I'm pretty sure this grammar is ambiguous, but how would one make it unambiguous?
A grammar is ambiguous if a particular string can have more than one parse tree. In your language the string yyxzx can have either of these two parse trees:
A A
/ \ /|\`\
y A y A z A
/|\`\ / \ \
y A z A y A x
| | |
x x x
Therefore the grammar is ambiguous.
This actually is equivalent to the notorious "if/then/else" ambiguity in C-like languages, where y=if, z=else, and x=statement. http://en.wikipedia.org/wiki/Dangling_else. I would recommend checking out that page for ideas on how to get around this problem.

What's the -> operator in Prolog and how can I use it?

I've read about it in a book but it wasn't explained at all. I also never saw it in a program. Is part of Prolog syntax? What's it for? Do you use it?
It represents implication. The righthand side is only executed if the lefthand side is true. Thus, if you have this code,
implication(X) :-
(X = a ->
write('Argument a received.'), nl
; X = b ->
write('Argument b received.'), nl
;
write('Received unknown argument.'), nl
).
Then it will write different things depending on it argument:
?- implication(a).
Argument a received.
true.
?- implication(b).
Argument b received.
true.
?- implication(c).
Received unknown argument.
true.
(link to documentation.)
It's a local version of the cut, see for example the section on control predicated in the SWI manual.
It is mostly used to implement if-then-else by (condition -> true-branch ; false-branch). Once the condition succeeds there is no backtracking from the true branch back into the condition or into the false branch, but backtracking out of the if-then-else is still possible:
?- member(X,[1,2,3]), (X=1 -> Y=a ; X=2 -> Y=b ; Y=c).
X = 1,
Y = a ;
X = 2,
Y = b ;
X = 3,
Y = c.
?- member(X,[1,2,3]), (X=1, !, Y=a ; X=2 -> Y=b ; Y=c).
X = 1,
Y = a.
Therefore it is called a local cut.
It is possible to avoid using it by writing something more wordy. If I rewrite Stephan's predicate:
implication(X) :-
(
X = a,
write('Argument a received.'), nl
;
X = b,
write('Argument b received.'), nl
;
X \= a,
X \= b,
write('Received unknown argument.'), nl
).
(Yeah I don't think there is any problem with using it, but my boss was paranoid about it for some reason, so we always used the above approach.)
With either version, you need to be careful that you are covering all cases you intend to cover, especially if you have many branches.
ETA: I am not sure if this is completely equivalent to Stephan's, because of backtracking if you have implication(X). But I don't have a Prolog interpreter right now to check.