In alloy consider
sig Queue{
link : Queue,
elem: Int
}
consider that I have some predicate predicate-1, How would I define scope when I run predicate-1 for Queue <=1 , int ={-3,-2,0,2}. I have not listed the predicate here
run predicate-1 for 1 Queue, int scope here
don't know what would be the syntax for int scope
The syntax is
run predicate1 for 1 Queue, 3 Int
The scope for integers is always a bitwidth, so you can't specify that the Int set contains exactly {-3. -2. 0. 2}; you can only specify a bitwidth, and all integers within that bitwidth will be used. In my example above, the Int set will contain all integers from -4 to 3.
Related
The use of * is all so confusing especially for us new comers to C. I mean, how do you easily differentiate the use of * as a pointer, * as a de-reference operator, * as a multiplication operator? I see alot of different use of * online and it's quite confusing, especially regarding different positions of the * e.g. as in the sample code below. What positions of * are correct and advisable to use to avoid confusion with pointers, de-referencing and multiplication?
Code is just sample and may...infact will not compile.
int main(void){
int *size, length, width;
*size1= malloc(sizeof(int) * length);
*size2= (int *) malloc(width * sizeof(int) * width);
*size3= malloc(sizeof(int) *length);
printf("%d\n%d\n%d\n", size1,size2,size3);
return (0);
}
N.B: I'm new in C and pls don't refer me to useless links and ("supposedly") duplicates that doesn't answer exactly same question as this...pls allow others to air their views so we get maximum answers, none knows it all, u will be surprised that something new can pop-up, even for the so called C old-timers.
When you read C code, you must be able to distinguish definitions and expressions. A definition starts with a type optionally preceded by a storage class such as extern, static, typedef... and/or type qualifiers such as const and, volatile. A star in front of the identifier being defined indicates that this variable is a pointer, multiple stars indicate multiple levels of indirection.
In an expression, a star can occur as a binary operator, between operands, or as unary operator, in front of its operand. The binary operator * is the multiplication operator, whereas the unary * is the dereference operator.
As a rule of thumb, a * that follows an identifier or a postfix operator is a multiplication operator. Postfix operators are ++, --, indexing with [] and calling a function with optional arguments inside ().
The confusion comes from the conciseness of the C language, which allows the programmer to combine these different uses at will:
int x=0,*p=&x,n=*p**p;
This ugly line is indeed confusing. Let's first insert meaningful spaces to improve readability:
int x = 0, *p = &x, n = *p * *p;
Spacing is mostly unnecessary for the C compiler, but following simple rules increases readability for humans:
add a space on both sides of binary operators
add a space after , and ; except at the end of a line
do not insert a space between unary operators and their operand, except for sizeof.
indent code in a consistent manner, typically 4 spaces per level
The above line of code defines 3 variables x, p and n.
* p is a pointer to int, while x and n are int variables.
* x is initialized to 0
* p is initialized to the address of x.
* the initializer for n is the result of multiplying of the value pointed to by p by itself.
Defining multiple variables with different levels of indirection in the same definition is frowned upon because it is confusing and error prone. Rewriting the above definition on 3 lines is highly advisable:
int x = 0;
int *p = &x;
int n = *p * *p;
Is there a way to introspect a variable to directly find out what subset it was declared with? Here I create a subset, but introspection points me to its base type:
> subset Prime of Int where .is-prime
(Prime)
> my Prime $x = 23
23
> $x.WHICH
Int|23
I know it has to store the information somewhere, because if I try to reassign a value that doesn't match the subset, it fails:
> $x = 24
Type check failed in assignment to $x; expected Prime but got Int (24)
in block <unit> at <unknown file> line 1
I tried searching through the code, but I quickly get down into files like container.c and perl6_ops.c where the C code makes my eyes glaze over. I thought that X::TypeCheck::Assignment might help (see core/Exception.pm), but it's not clear to me where the expected value comes from. (see also this commit)
I feel like I'm missing something obvious.
I can check that something matches a subset, but that doesn't tell me if it were declared with a particular subset:
> my Int $y = 43;
43
> $y ~~ Prime;
True
I'm using Rakudo Star 2017.01
Inspired by a Zoffix's use of subsets in a recent post.
The value that you stored in $x is an Int. It is acceptable to the container (which you typed to Prime) because Prime is a subtype of Int.
So what you're interested in, is not the value in the container, but the type of the container. To get at the container, Perl 6 has the .VAR method. And that has an .of method to get at the type:
$ 6 'subset Prime of Int where .is-prime; my Prime $x; dd $x.VAR.of'
Prime
Here's the alloy code for the example we're going over in my class:
abstract sig Airport {
flight: set Airport
}
one sig CMX, DTW, MSP, BRD, CDG extends Airport {}
fact {
-- flight = CMX->DTW + DTW->MSP + MSP->CMX + DTW->CDG + CDG->DTW + MSP->BRD + BRD->MSP
}
fun flight2: Airport->Airport {flight.flight}
fun flight3: Airport->Airport {flight.flight2}
fun flight4: Airport->Airport {flight.flight3}
fun flight5: Airport->Airport {flight.flight4}
pred show {
flight5 in flight2
flight2 not in flight5
-- #flight = 10
}
run show for 5 Airport, 6 Int
I want to know what the last line means. Specifically, what does "6 Int" mean?
The last line defines an upperbound (called a scope) to the number of atoms conforming to the given concepts in any satisfying instance to be found when running this command.
In your example, you will look for instances which contains at most 5 Airport atoms. Int is a built-in type in Alloy. As you didn't declare any Int-typed field, the part 6 Int won't have big impacts on the instance finding process.
The number associated to Int in the scope do not correspond to the maximum number of Int atom in instances to be found but to the bit-width to be used to represent Integers.
With a bit-width of 6, your instance will contains integers from -32 to 31.
I was experimenting with alloy and wrote this code.
one sig s1{
vals: some Int
}{
#vals = 4
}
one sig s2{
vals: some Int
}{
#vals = 4
}
fact {
all a : s1.vals | a > 2
all i : s2.vals | i < 15
s1.vals = s2.vals
}
pred p{}
run p
It seems to me that {3,4,5,6} at least is a solution however Alloy says no instance found. When I comment s1.vals = s2.vals or change i < 15 to i > 2, it finds instances.
Can anyone please explain me why? Thanks.
Alloy's relationship with integers is sometimes mildly strained; it's not designed for heavily numeric applications, and many uses of integers in conventional programming are better handled in Alloy by other signatures.
The default bit width for integers is 4 bits, and Alloy uses twos-complement integers, so your run p is asking for a world in which integers range in value from -8 to 7. In that world, the constraint i < 15 is subject to integer overflow, and turns out to mean, in effect, i < -1. (To see this, comment out both of your constraints so that you get some instances. Then (a) leaf through the instances produced by the Analylzer and look at the integers that appear in them; you'll see their range is as I describe. Also, (b) open the Evaluator and type the numeral "15"; you'll see that its value in this universe is -1.)
If you change your run command to provide an appropriate bit width for integers (e.g. run p for 5 int), you'll get instances which are probably more like what you were expecting.
An alternative change, however, which leads to a more idiomatic Alloy model, is to abstract away from the specific kind of value by defining a sig for values:
sig value {}
Then change the declaration for vals in s1 and s2 from some Int to some value, and comment out the numeric constraints on them (or substitute some other interesting constraints for them). And then run p in a suitable scope (e.g. run p for 8 value).
i saw the following Alloy definition:
one sig Number { i:Int[3]}
//what does Int[3] mean. I mean what is the meaning of the above field 'i'
It means i is a relation between the singleton set Number and the singleton subset of Int with the atom 3. In object notation it's like Number.i = 3