Good day,
Consider the following:
In[1]:= HoldComplete[With[{line=a},Null]]
Names["`*"]
Attributes/#Names["`*"]
Remove/#Names["`*"]
Out[1]= HoldComplete[With[{line=a},Null]]
Out[2]= {a,line,line$}
Out[3]= {{},{},{Temporary}}
During evaluation of In[1]:= Remove::rmnsm: There are no symbols matching
"line$". >>
Out[4]= {Null,Null,Null}
One can see that Remove::rmnsm message appear although the temporary
Symbol line$ still exists up the that moment. Why this happens?
P.S. I am using Mathematica 7.01. In v.5.2 this message does not appear.
I think what is happening is when you remove the Symbol line then there are no further references to the temporary variable line$ and so it is automagically removed.
In[1]:= HoldComplete[With[{line=a},Null]]
Names["`*"]
Attributes/#Names["`*"]
Out[1]= HoldComplete[With[{line=a},Null]]
Out[2]= {a,line,line$}
Out[3]= {{},{},{Temporary}}
In[4]:= Remove["line"]
In[5]:= Names["`*"]
Out[5]= {a}
This was tested in Mma v8. So maybe the reference counting (or the implementation of localization) has changed slightly since v5?
Note that if you try to remove the temporary symbol first, you get quite an informative warning:
In[6]:= HoldComplete[With[{line=a},Null]]
Names["`*"]
Attributes/#Names["`*"]
Remove/#Reverse#Names["`*"]
Out[6]= HoldComplete[With[{line=a},Null]]
Out[7]= {a,line,line$}
Out[8]= {{},{},{Temporary}}
During evaluation of In[6]:= Remove::relex: Cannot remove lexical symbol
line$ except automatically (when line is removed). >>
Out[9]= {Null,Null,Null}
Related
I'm writing bnf notation with Jison and getting an reduce/reduce conflict:
Conflict at state: 26, token: SIMPLE_ASSIGN
reduce by rule: PrimaryExpression -> Identifier
reduce by rule: LeftHandSideExpression -> Identifier
My State 26 looks like this:
item set 24
AssignmentExpression -> LeftHandSideExpression .AssignmentOperator AssignmentExpression
AssignmentOperator -> .SIMPLE_ASSIGN
AssignmentOperator -> .COMPLEX_ASSIGN
transitions -> {"AssignmentOperator":61,"SIMPLE_ASSIGN":62,"COMPLEX_ASSIGN":63}
item set 25
LogicalORExpression -> LogicalANDExpression .
LogicalANDExpression -> LogicalANDExpression .LOGICAL_AND EqualityExpression
transitions -> {"LOGICAL_AND":64}
item set 26
LeftHandSideExpression -> Identifier .
PrimaryExpression -> Identifier .
transitions -> {}
item set 27
LogicalANDExpression -> EqualityExpression .
EqualityExpression -> EqualityExpression .EQUALITY_OPERATOR RelationalExpression
transitions -> {"EQUALITY_OPERATOR":65}
How would I figure out the problem here and fix it?
I am using Jison, and I am running:
jison -p lalr -t grammer/noa.bnf -t > log
Interestingly I am also using syntax-cli which doesn't produce a reduce/reduce conflict, but still has trouble parsing.
What that is telling you is that there is some grammatical production in which the non-terminal PrimaryExpression (or some non-terminal whose expansion ends with PrimaryExpression) can be followed by a SIMPLE_ASSIGN (or some non-terminal whose expansion starts with SIMPLE_ASSIGN, although that seems unlikely). In that context, Identifier (whose expansion isn't shown) is ambiguous, because both PrimaryExpression and LeftHandSideExpression can expand to Identifier.
It's hard to guess what context that might be, but it should be reasonably simple to track down: just look at every non-terminal which can precede SIMPLE_ASSIGN until you find one which can end with PrimaryExpression. (Not all such productions are errors, though. It's really highly dependent on the grammar.)
What is the difference \= and \+?
because
?- 15\=14.
?- \+ 15=14.<--- this gives an error while the above does not.
Why?Aren't they the same?
Edit: here's the error:
Compiling the file:
D:\Program Files\Strawberry Prolog Beta\Games\WarCraft.pro
Warning 4: The string \+ is not an operator. (line 1, before the first clause)
Error 16: Instead of the integer 15 what is expected here is something like an infix operator or a full stop. (line 1, before the first clause)
1 error, 1 warning.
Also I'm using Strawberry prolog I also tried it on SWI prolog still the same.
I think you are putting queries into Prolog source files. That is not where they should go:
predicate definitions go into Prolog source files
queries are typed into the interactive Prolog toplevel
Try running the SWI-Prolog program without an input file. You should get a window with some informational messages about the SWI-Prolog version and then a prompt ?-. That is the toplevel. Try typing your query there. All queries should go there.
I don't know about Strawberry Prolog, but I suspect it's the same there.
I want to test this prolog program:
#
binary_tree(void).
binary_tree(tree(_Element,Left,Right)):-binary_tree(Left),binary_tree(Right).
test(tree(a,tree(b,tree(d,void,void),void),tree(c,void,void))).
#
When I execute it with:
test(X), binary_tree(X).
I get
X = tree(a, tree(b, tree(d, void, void), void), tree(c, void, void)).
(and I should get "true" instead)
What am I doing wrong?
Thanks
Who says you should just get true instead? Calls to Prolog predicates may succeed or fail. On success, they answer with a variable substitution, as in your case. Your Prolog system's answer says that your test succeeded, as you expected.
You will usually only get true if no variable substitution can be given because the query does not contain any variables. If the query fails, you will get false or fail but no variable substitution.
I'm just trying to figure out constraint programming in SWI-Prolog, looking at this tutorial : http://en.wikibooks.org/wiki/Prolog/Constraint_Logic_Programming
However I seem to be falling at the first hurdle.
?- use_module(library(clpfd)).
true.
?- X #> Y, X in 1..3, Y=2.
ERROR: Syntax error: Operator expected
ERROR: X
ERROR: ** here **
ERROR: #> Y, X in 1..3, Y=2 .
?-
What's going wrong here? I seem to have included the library, but the first example line from the tutorial throws a syntax error.
All the tutorials I can find seem to use operators like #=, #< etc. But my SWI-Prolog baulks at them. Are they an extra syntax which comes with that constraint library? (And am I failing to load it?)
Or am I misreading the tutorial examples?
Update : Trying to understand things from Horsh's reply below. I can get this to work if I use the library and run the line in the interactive terminal. But if I try to import the library and use these operators in a source file, then it throws the error again. What am I not understanding?
Update 2 :
OK. If, in my source file, I invoke the library and then write a rule which contains a #>. Then I try to consult it from the command-line. It will throw an error and the #> syntax is un-recognised. If import the library to the command line before trying to consult the program, it works. Can this be right?
Building on Horsh's answer, you should be importing the library in your source code, remembering to put ?- at the beginning of the line like so:
?- use_module(library(clpfd)).
The ?- tells SWI-Prolog to execute the line as if it were typed into the interpreter directly, instead of trying to declare it as a predicate in your program.
Don't be concerned about SWI-Prolog importing the library more than once, it knows to check if the library was modified and only reloads it if the library was changed since the last time it was loaded.
For anyone else that finds this in the future, if you want to import a library in an SWI-Prolog source file, the following will also work:
:- use_module(library(clpfd)).
Note the :- and not ?-.
The is all in the manual here and there.
?- [library(clpfd)].
% library(error) compiled into error 0.00 sec, 10,128 bytes
% library(apply) compiled into apply 0.00 sec, 16,840 bytes
% library(assoc) compiled into assoc 0.00 sec, 13,132 bytes
% library(lists) compiled into lists 0.00 sec, 14,332 bytes
% library(pairs) compiled into pairs 0.00 sec, 5,372 bytes
% library(clpfd) compiled into clpfd 0.05 sec, 392,604 bytes
true.
?- X #> Y, X in 1..3, Y=2.
X = 3,
Y = 2.
The following gives some strange "Part" warnings
Clear[f];
f = Function[{x}, x[[1]] == x[[2]]]
However if I execute the second line again, it works without warnings, any idea what's going on?
Are you sure? I don't see any warnings in version 7.0.1:
In[1]:= Clear[f];
f=Function[{x},x[[1]]==x[[2]]]
Out[2]= Function[{x},x[[1]]==x[[2]]]