I tried the following snippet of Alloy4, and found myself confused by the behavior of the util/Natural module. The comments explain more in detail what was unexpected. I was hoping someone could explain why this happens.
module weirdNatural
private open util/natural as nat
//Somehow, using number two obtained from incrementing one works as I expect, (ie, there is no
//number greater than it in {0,1,2}. but using number two obtained from the natrual/add function
//seems to work differently. why is that?
let twoViaAdd = nat/add[nat/One, nat/One]
let twoViaInc = nat/inc[nat/One]
pred biggerAdd {
some x: nat/Natural | nat/gt[x, twoViaAdd]
}
pred biggerInc {
some y: nat/Natural | nat/gt[y, twoViaInc]
}
//run biggerAdd for 10 but 3 Natural //does not work well, it does find a number gt2 in {0,1,2}
run biggerInc for 10 but 3 Natural //works as expected, it finds a number gt2 in {0,1,2,3}, but not in {0,1,2}
Thanks for this bug report. You are absolutely right, that is a weird behavior.
Alloy4.2 introduced some changes to how integers are handled; namely, the + and - operators in Alloy4.2 are always interpreted as set union/difference, so built-in functions plus/minus have to be used to express arithmetic addition/subtraction. On the other side, the util/natural module (mistakenly) hasn't been updated to use the latest syntax, which was the root cause of the weird behavior you experienced (specifically, the nat/add function uses the old + operator instead of plus, whereas nat/inc doesn't).
To work around this issue, you can either
open the util/natural module (choose "File -> Open Sample Models" from the main menu)
edit the file and replace the two occurrences of <a> + <b> with plus[<a>, <b>]
save the new file in the same folder with your model als file (e.g., as "my_nat.als")
open it from your main module (e.g., open my_nat as nat)
or
download the latest unofficial version where this bug is fixed (you might need to manually delete the Alloy temp folder to make sure that the Alloy Analyzer is not using the old (cached) version of the util/natural library).
Related
I'm trying to make a "very simple" syntax highlight for "cottle" (which is a script language used in a text-to-speech app dedicated to Elite:Dangerous).
All i want (at least at the beginning) is to have three different colours: Comments, "non-strings", and strings.
I started trying with the ST3 wiki, youtube tutorials, questions here.... but i can't sort out how to do it, 'cause the way the language work.
I'll try to show you an example
{ everything_between_a_pair_of_brackets_is_code }
everything outside all pairs of bracket is a string {_ and this is a comment. It begins with "_" and ends at the closing bracket }
{ This_is_code("but this is a string")
This_is_still_code("this is also a string {but_this_is_code(\"and a string\")} and this the end of the string")
}
My problem is how to define this kind of "nidification" in my cottle.sublime-syntax file. I managed to get the comment, but only the first one.
- EDIT -
This is a real script:
{event.item}
{if event.repairedfully:
fully repaired
|else:
partially repaired
{Occasionally(2,
cat(
OneOf("to ", "at "),
Humanise(event.health * 100),
" percent functionality"
)
)}
}
{Occasionally(2,
cat(OneOf(", ", "and is"), " ready for re-activation")
)}.
The output of this script could be "Engine module fully repaired." or "Engine module partially repaired, and is ready for re-activation."
Please note the last dot of the phrase, which in the code is after the last bracket.
This is another sample, with strings passed to functions inside other strings:
{OneOf("{ShipName()} has", "")}
{OneOf("left supercruise", "{OneOf(\"entered\", \"returned to\", \"dropped to\")} normal space")}
My question is:
how sublime-syntax files handle this kind of nidification?
Looking at the overview of the templating language over at https://cottle.readthedocs.io/en/stable/page/01-overview.html, it seems to be an easy syntax for which to write a .sublime-syntax for, but given the utter lack of resources for knowing how syntax files works in ST, I can understand it can be sometimes difficult to start or even understand.
So, I took the liberty of creating a starter syntax definition (the result of an hour & a half of boredom on a Saturday evening), which you can take and work upon. Note that I have not used the language and as such made it by just reading the docs and looking over code snippets.
You can find a gist for it here (https://gist.github.com/Ultra-Instinct-05/96fa99e1aaeb32b12d1e62109d61fcc2)
Here is a screenshot showing it in the color scheme I use (which follows the official scope naming guidelines).
It still lacks support for user defined functions (as I came to know from the docs) (and probably a few other things), but maybe that's something you can add to it !
Note that to use it, save the file as Cottle.sublime-syntax in your User package. Right now files having a .cottle extension are highlighted (because I don't know how you create a cottle file).
The syntax definition doesn't use any new feature added in ST4, so it should work the same in both ST3 & ST4.
i am trying to print variables with indices.
The goal is to write something like:
x_1 + x_2 = 3 + 1 = 4
The problem is, that variables just like x_1 does not show an index. It shows the underscore itself.
/* works fine */
print(x_1)$
x_1;
/* Does not work */
ostream: make_string_output_stream()$
printf(ostream, string(x_1))$
get_output_stream_string(ostream);
Output of the code above
Converting "x_1" into a string destroys the underscore, but you need a string for the method printf().
Are there workarounds for this?
What to do here probably depends somewhat on what goal you need to achieve with this. Can you say more about the larger goal you are working toward?
In order to generate output which has typesetting instructions in it, you can call the tex or tex1 function to generate TeX output. If that needs to be displayed in a web page, I believe you can make use of the MathJax Javascript library. Sorry, I don't know more about MathJax.
You should probably write x[1] instead of x_1. Displaying x_1 with a subscript 1 is a convenience -- the rest of Maxima doesn't know much about it.
EDIT: There is also an add-on package to output MathML; there might be two such packages, I would have to check. If MathML could help solve your problem, I will look into it.
I am working on developing a large-scale Python (backend) project. I was working with a firm that does extensive testing, and they built the frontend and test tools. Before every deploy, all the tools (like linters) are run regularly.
I had put down the code for a while, and now it fails many tests. Some of these are deprecation warnings for features or syntax soon to be deprecated, and they note they started classifying those as warnings (to later become errors) starting January 1, 2020, so I know they make dynamic changes in the tools themselves.
My problem is a bunch of code that used to pass no longer does. And the error is always the same: if I have a line that looks like so, I get an error that says something along the lines of "error: may not use operator '-' with incompatible types; a and b are of types numpy.array and NoneType":
x = a - b
This gets fixed by making the code super-messy with this sort of fix:
x = a.astype(float) - b.astype(float)
It's even worse because in the actual code there are 3 variables, all doing addition and subtraction with a 'c' that is an integer array kicking around along with the two numpy arrays. But then the code goes from:
x = a - b - c
to:
x = a.astype(float) - b.astype(float) - c.astype(float)
And this won't work since int's don't have an astype method. The error looks like this now:
File "/home/engine.py", line 165, in Foo
lower_array[t].astype(float)) / num_values.astype(float)
AttributeError: 'NoneType' object has no attribute 'astype'
Thus, I end up with:
x = a.astype(float) - b.astype(float) - float(c)
This is all extraordinarily cumbersome and nasty casting that is required, and makes the code impossible to read.
The odd thing to me is that all three arrays were instantiated as numpy arrays, i.e.,:
a=numpy.array(_a)
b=numpy.array(_b)
c=numpy.array(_c)
When I ask the code to put output to stdout the type of all three vars, they all say . Yet, the next line of code blows up and dumps, saying "Attribute error: 'NoneType' object has no attribute 'astype'"
I can't fathom how a static code analyzer determines the types - other than as numpy.ndarray type - since Python uses duck-typing. Thus, the type could change dynamically. But that's not the case here; all three vars are identified as numpy.ndarray type, but "z = a - b - c" fails.
Anyone understand what's going on here?
After much work, the answer is to ignore the linter. Readable code is the object, not code that satisfies a linter.
I am writing documentation for a library using haddock, and, for reasons that are not really necessary to explain, I need to include a little code block in my documentation that looks like:
z(<>)
Importantly there can be no space between z and (<>). It may be a bit esoteric but
z (<>)
would make my documentation incorrect, even if it is more stylistically correct.
Now I believe that hyperlinks to both z and (<>) would be helpful. Neither has a very informative name, so a link that helps people remember their definitions and purpose is nice.
So my code without the hyperlinks looks like:
#z(<>)#
And to add hyperlinks I just use single quotes:
#'z''(<>)'#
Except that doesn't work, haddock sees 'z'' and thinks that I mean to link z' (a function that does exist in my module), and then just leaves the rest alone. The rendered output looks like
z'(<>)'
Now as an experiment I deleted the definition of z', however the only difference this makes is that the link to z' goes away. The raw text remains the same. The next thing I tried was ditching #s altogether and did
'z''(<>)'
Which also created a hyperlink to z' and left the rest untouched, the same problem as earlier except now nothing is in a code block.
How can I make a code block that links two functions without a space between?
You can separate the two functions into different code blocks. If there is no space between the code blocks, it will appear no different than a single code block. So
#'z'##'(<>)'#
will render as desired.
You can also do it in one code block by moving the 's inside of the parentheses to only surround <>.
#'z'('<>')#
This will render slightly differently with the parentheses not being part of any hyperlink, however this may be desired.
Here is an alternative solution to add to the answer you already provided:
You can mix and match ' and `. These two will also be rendered correctly by haddock:
-- | #`z`'(<>)'#
-- | #'z'`(<>)`#
At the same time I've tried your solution #'z'##'(<>)'# and for some reason it did not render for me properly, but with haddock you never know.
Here are all of the ones that I've tried:
-- * #'z'##'(<>)'#
-- * #'z'('<>')#
-- * #'z'`(<>)`#
-- * #`z`'(<>)'#
With corresponding result:
Is there any way to duplicate a ParserRule? I need a real deep copy, so copyFrom() doesn't do the trick. Or must I re-parse the code?
An alternative idea how to solve the following would also be much appreciated:
I am working on a compiler, translating old legacy code to modern programming languages, in this case EGL -> Java.
EGL has a concept called Standalone Function, which are similar to C-macros. This means that code inside the functions can reference symbols in the calling scope. So both defining and resolving of symbols and type promotion are context-dependent.
In ANTLR3, we solved this by dupTree(), and simply made a copy to work on in each calling scope.
Dynamic types is not an option.
Example (pseudo code) to illustrate:
Program A
int var = 4;
saf(); # Prints 5
end A;
Program B
String var = "abc";
saf(); # Prints abc1
end B;
function saf()
int j = 1;
print(var + j);
end saf;
As of version 4.2, ANTLR 4 does not include any API for manipulating the structure of a parse tree after the parse is complete. This is an area we are currently exploring, especially considering the possibilities created by the new pattern matching syntax.
For duplicating trees, I recommend you implement the visitor interface created when you generated your parser. This will allow you to call visit on any node in your parse tree to create a deep copy of that node.