Branching on a range of integers in K? - kframework

I read in the documentation that one can write a rule
syntax Exp ::= randBounded(Int, Int)
rule randBounded(M, N) => I
requires M <=Int I andBool I <=Int N
[unboundVariables(I)]
I would like randBounded(4,6) to return some random integer. However, when I try it, it returns an integer V0 and sets constraints V0 <=Int 10 ==K true.
Is there a way, instead, to generate explicit branches for every integer in a range?
e.g. I would like to have --search produce 3 configurations:
One for each Integer 4, Integer 5 and Integer 6.

Unfortunately, the expansion you suggest is not possible with --search so you cannot get the exact three configurations. However, if you don't actually need those precise configurations, but instead want to be able to reason on their properties, performing (symbolic) execution (using the haskell backend) using this rule somehow comprises all three configurations.

Related

Summation iterated over a variable length

I have written an optimization problem in pyomo and need a constraint, which contains a summation that has a variable length:
u_i_t[i, t]*T_min_run - sum (tnewnew in (t-T_min_run+1)..t-1) u_i_t[i,tnewnew] <= sum (tnew in t..(t+T_min_run-1)) u_i_t[i,tnew]
T is my actual timeline and N my machines
usually I iterate over t, but I need to guarantee the machines are turned on for certain amount of time.
def HP_on_rule(model, i, t):
return model.u_i_t[i, t]*T_min_run - sum(model.u_i_t[i, tnewnew] for tnewnew in range((t-T_min_run+1), (t-1))) <= sum(model.u_i_t[i, tnew] for tnew in range(t, (t+T_min_run-1)))
model.HP_on_rule = Constraint(N, rule=HP_on_rule)
I hope you can provide me with the correct formulation in pyomo/python.
The problem is that t is a running variable and I do not know how to implement this in Python. tnew is only a help variable. E.g. t=6 (variable), T_min_run=3 (constant) and u_i_t is binary [00001111100000...] then I get:
1*3 - 1 <= 3
As I said, I do not know how to implement this in my code and the current version is not running.
TypeError: HP_on_rule() missing 1 required positional argument: 't'
It seems like you didn't provide all your arguments to the function rule.
Since t is a parameter of your function, I assume that it corresponds to an element of set T (your timeline).
Then, your last line of your code example should include not only the set N, but also the set T. Try this:
model.HP_on_rule = Constraint(N, T, rule=HP_on_rule)
Please note: Building a Constraint with a "for each" part, you must provide the Pyomo Sets that you want to iterate over at the begining of the call for Constraint construction. As a rule of thumb, your constraint rule function should have 1 more argument than the number of Pyomo Sets specified in the Constraint initilization line.

I want to make vhdl divider

i'm now using FPGA spartan3,
i want to calculate 'result' which is represented below formula.
and the 'result' should be returned as integer type. So i set up all the variables with integer type but it doesn't work.
result <=((a*b*7894*7)/(w*temp_constant));
i've set a,b,c,w,temp_constant as variables
variable a : integer range 0 to 99;
variable b : integer range 0 to 9999;
variable w : integer range 0 to 200;
variable temp_constant : integer range 0 to 99;
but the operator '/' doesn't work at this synthesis. the error msg was
'Operator '/' must have constant operands or first operand must be power of 2"'
The error message is almost (see the note below) 100% clear: divisions are not supported by your synthesis tool, except with constant operands (the result is computed by the synthesizer in the constant propagation phase) or with divisors that are powers of 2 (the division is a simple right shift).
One possible reason for this limitation of your synthesis tool is that there are many of ways to compute integer divisions in hardware and typing just / in a VHDL code is not enough to chose among them. There may be other reasons.
In your case where operands are not constants, and the divisor is not a power of 2, you must design this divider yourself at a lower level. If you have no idea about hardware implementations of integer dividers you will have to search a bit. This is a very classical topic, it should be easy to find good resources. Just a hint: pre-computing all inverses in fixed point representation, storing them in a read-only memory and using multiplications instead of divisions is an option.
Note: I find the error message you got (first operand must be power of 2) a bit surprising. Unless the term first operand is supposed to designate the divisor, which is not that common, it is probably a bug and the correct error message should be: second operand must be power of 2. Or, even better: divisor must be power of 2.

Perl6 log method returns a Num not a Rat

Just wondering if anyone knows why Perl6's log function returns a Num type and not a Rat type.
say (e*e).log.WHAT;
> (Num)
say (2/3).WHAT;
> (Rat)
In mathematics Log is a Continuous function therefore it has mathematically-real values. Num type describes mathematically-real numbers in Perl 6. Rat type describes mathematically-rational numbers.
It's because no one has done the work to make it do anything else yet. It's a situation that the language could handle (not that it's special to Perl 6) but also a situation that you might not want it to handle.
There's no object that represents the natural base e and maintains it as such until it can't any longer (just as Rats don't turn into Nums unless they have to). That's possible and would also allow us to decide how to treat it. Maybe we want a Rat, or FatRat, or even a certain number of decimal places in a Num. But it doesn't do that.
It's not that e is special though. It doesn't work with powers of 10 either:
> 100.log10
2
> 100.log10.^name
Num
The code behind .log10 could check that the operand is a power of 10 and decide to return an Int in that case. But it would have to check every number for that and most numbers aren't a power of 10. Checking all of those would slow down the process. It's easier to make it a little "incorrect".
But you can use .narrow to get a more constrained type possibly:
> 100.log10.narrow.^name
Int
This is different from asking for a particular type and maybe getting a different number:
> (10/3).Int
3
> (10/3).narrow.^name
Rat
And for fun:
> i*i
-1+0i
> (i*i).^name
Complex
> (i*i).narrow.^name
Int
Perl6 is not a computer algebra system, so it treats e*e like any other Num - and once you've got a floating point number, only explicit operations such as rounding should change the type to something like Int or Rat: The computer cannot know if the return value 2e0 of (e*e).log actually represents 2, or some 2+ε.

Return highest or lowest value Z notation , formal method

I am new to Z notation,
Lets say I have a function f defined as X |--> Y ,
where X is string and Y is number.
How can I get highest Y value in this function? Does 'loop' exist in formal method so I can solve it using loop?
I know there is recursion in Z notation, but based on the material provided, I only found it apply in multiset or bag, can it apply in function?
Any extra reference application of 'loop' or recursion application will be appreciated. Sorry for my English.
You can just use the predefined function max that takes a set of integers as input and returns the maximum number. The input values here are the range (the set of all values) of the function:
max(ran(f))
Please note that the maximum is not defined for empty sets.
Regarding your question about recursion or loops: You can actually define a function recursively but I think your question aims more at a way to compute something. This is not easily expressed in Z and this is IMO a good thing because it is used for specifications and it is not a programming language. Even if there wouldn't be a max or ran function, you could still specify the number m you are looking for by:
\exists s:String # (s,m):f /\
\forall s2:String, i2:Z # (s2,i2):f ==> i2 <= m
("m is a value of f, belonging to an s and all other values i2 of f are smaller or equal")
After getting used to the style it is usually far better to understand than any programming language (except your are trying to describe an algorithm itself and not its expected outcome).#
Just for reference: An example of a recursive definition (let's call it rmax) for the maximum would consist of a base case:
\forall e:Z # rmax({e}) = e
and a recursive case:
\forall e:Z; S:\pow(Z) #
S \noteq {} \land
rmax({e} \cup S) = \IF e > rmax(S) \THEN e \ELSE rmax(S)
But note that this is still not a "computation rule" of rmax because e in the second rule can be an arbitrary element of S. In more complex scenarios it might even be not obvious that the defined relation is a function at all because depending on the chosen elements different results could be computed.

fitting a number within two bounds

I'm working on a program that generates pseudorandom numbers for a user based on their inputted seed, start and end range. I've written my own modulus based generator based on Lehmer's random number generator algorithm. YES I KNOW modulus based random calculations are biased, but for it's use this method is more than adequate.
Anyway, whilst I can generate a string of random numbers from the given seed in VBA, I can't find anything online with a formula or code showing how that number can be scaled down to fit within the supplied upper and lower bound. I'm hoping someone here knows a formula for this, or knows of a website I've missed that covers this sort of process (I don't even know what it would be called - scaling?)
Thanks for your time! In case it's useful or anyone's interested, here's my VBA code generating the seed-based number:
random = ((CDec(1664525) * t1) * seed + 1013904223) 't1 is the incremental count for each requested number
random = random - (Int(random / 2 ^ 23) * 2 ^ 21)
Thanks for your help!
EDIT: Just to point out, the 'scaling' cannot use the rand function, which I've seen done before, since the final numbers need to be the same each time that seed is used!
#Kevin is right I just need to add:
Linear interpolation for range change
so if you have number x on interval <x0,x1>
and want to change it to y on interval <y0,y1>
then use this formula:
y=y0+((x-x0)*(y1-y0)/(x1-x0));
it is the formula for 2D line and also base for DDA algorithms ...
What if your x range is unknown ?
then simply bound it to something known
for example x&65535 will change the x range to <0,65535>
of coarse only if the original x range was higher then that ...
What if dynamic x range is smaller then dynamic y range ?
ie |x1-x0|<|y1-y0|
the equation still works but you will be missing certain numbers in y range
so the interval will have gaps
to avoid that you have to increase effective range of x
for example like this x=(rand()&255)|((rand()&255)<<8)
so you will use more random numbers per each call
do not worry the seed stuff will be still working ...