Specman e: Is there a way to know how many values there is in an enumerated type? - specman

I need to know how many values there is in an enumerated type in my verification environment. E.g.:
type my_type: [a, b, c, d];
I there a way to check on the fly that there 4 different values in the my_type?
Thank you for your help

There's an all_values(...) pseudo-routine that returns all possible values of a scalar type. You can use this to get the number of enum literals:
assert all_values(my_type).size() == 4;

Besides what Tudor suggested, another way is to use set_of_values() pseudo-routine that returns a set (rather than a list) of all values:
set_of_values(my_type).uint_size()
In a way, using set_of_values() is better because all_values() creates a new list, which usually consumes more memory than a set.
uint_size() returns the size of the set as uint. There is also size() but it returns int(bits: *), so it's good enough to use uint_size() in this case, because there can never be more than MAX_UINT items in an enumerated type.

also - set_of_values() return 'set', which you can inquire for the type smallest/largest value, and its range.
For example:
var x := set_of_values(my_type).uint_max();
keep y == set_of_values(my_type).uint_max().as_a(my_type).as_a(my_type);
print set_of_values(my_type).uint_min().as_a(my_type);

Related

Extracting default field value from an object inside Option

I have a class A, which contains an Option of class B(say b), which can be None. B has a string value(say str)inside it which I need to extract and its default value is an empty string. So basically if b is None, I need to get empty string.
a.b.getOrElse(B).str
So, here I need to create an instance of B. Can this be avoided?
Yes
val res = a.b.map(_.str).getOrElse("")
The cleanest and most efficient solution is to use fold:
a.b.fold("")(_.str)
The default value "" could equally well be computed based on other values in a.
Using map then getOrElse means testing the Option value twice, whereas fold only tests once.

What is the use of minizinc fix function?

i see that fix documentation says:
http://www.minizinc.org/doc-lib/doc-builtins-reflect.html#Ifunction-dd-T-cl-fix-po-var-opt-dd-T-cl-x-pc
function array [$U] of $T: fix(array [$U] of var opt $T: x)
Check if the value of every element of the array x is fixedat this point in evaluation. If all are fixed, return an array of their values, otherwise abort.
I am thinking it can be used to coerce a var to a par.
Here is the code.
array [1..num] of var int: value ;
%% generate random numbers from 0..num-1, this should fix the value of the var "value" or so i think
constraint forall(i in index_set(value))(let {int:temp_value=discrete_distribution([1|i in index_set(value)]); } in value[i]=trace(show(temp_value)++"\n", temp_value));
%%% this i was expecting to work, as "value" elements are fixed above
array [1..num] of int:value__ =[ trace(show(fix(value[i])), fix(value[i])) | i in index_set(value)] ;
But i get:
MiniZinc: evaluation error:
with i = 1
in call 'trace'
in call 'fix'
expression is not fixed
My questions are:
1) I think i should expect this error as minizinc is not sequential execution language?
2) Examples of fix in user guide is only where output statement is used. Is it the only place to use fix?
3) How would i coerce a var to a par?
By the way I am trying this var to par conversion because i am having problem with array generator expression. Here is the code
int:num__=200;
int:seed=134;
int: two_m=2097184;
%% prepare weights for generating numbers form 1..(two_m div 64), basically same weight
array [1..(two_m div 64)] of int: value_6_wt= [seed+1 | i in 1..(two_m div 64)] ;
%% generate numbers. this dose not work gives out
%% in variable declaration for 'value6'
%% parameter value out of range
array [1..num__] of int : value6 = [ discrete_distribution(value_6_wt) | j in 1..num__];
In the MiniZinc language the difference between a parameter and a variable is only the fact that a parameter must have a value at compile time. Within the compiler we turn as many variables into parameters as we can. This saves the solver from having to do some work. When we know that a variable has been turned into a parameter, then we can use the fix function to convince the type system that we really can use this variable as a parameter and see its value.
The problem here however is that fix is defined to abort when the variable is not fixed to one value. If no testing is done, this requires some (magic/)knowledge about the compilation process. In your case it seems that the second array is evaluated before the optimisation stage, in which all aliasing is resolved. This is the reason why it does not work. (This is indeed one of the things that is a consequence of a declarative language)
Although fix might only be used in the output statements in the examples (where it's guaranteed to work), it is used in many locations in the MiniZinc libraries. If we for example look at the library that is used for MIP solvers, there are many constraints that can be encoded more efficiently if one of the arguments is a parameter. Therefore, you will often see that the a constraint in this library first tests its arguments with is_fixed, and then use a better encoding if this returns true.
The output statement and when is_fixed returns true will both give the guarantee that a variable is fixed and ensure that the compilation doesn't abort. There is no other way to coerce a variable to a parameter, but unless you are dealing with dependant predicate definitions, you can just trust the MiniZinc compiler to ensure that the resulting FlatZinc will contain a parameter instead of a variable.

How to test if a variable is referencing a number in coffeescript?

Simple Question: How to test if a variable is referencing a number in coffeescript? Could not find an answer in the docs.
Strictly speaking, you can test variable type (which seems to be what you're asking) with
typeof n is 'number' and isFinite n
Note that this doesn't convert strings, etc., just checks straight up whether it's already a finite number.
If you're not against using libraries, underscore/lodash provide great utility functions.
_.isNumber
or
_.isFinite
(depending on if you want Infinity, and NaN to be categorized as numbers)
You can have a global function:
isNumber: (n) ->
return not isNaN(parseFloat(n)) and isFinite(n)
and use it:
is_number = isNumber('123')
it returns true if argument is not NaN and is not a infinity. Otherwise returns false

Lisp Function Interpretation

I am reading a book and I am confused on what the following code does:
(defmethod execute ((o ORDER) (l SIMUL) (e MARKETUPDATE))
(values
(list (make-TRADE :timestamp (timestamp e)
:price (price e)
:quantity (orderquantity o)))
NIL))
The source to which I got this function says that it returns two values. My question is what the body does. From my understanding, the line 3-5 creates a list with :timestamp, :price, :quantity. Am I correct? What about values, the second line? Does it return this variable too? Any summary would help. Thanks
This is a method for a generic function, specializing on arguments of types order, simul, and marketupdate.
It returns 2 values:
A list of length 1 created by the eponymous function list, which contains a single object of, presumably, type trade (probably - but not necessarily - created by a defstruct), which has slots timestamp, price, and quantity.
Symbol nil.
You can probably access the slots of the trade using functions trade-timestamp &c (unless the defstruct form is non-trivial or trade is not defined by a defstruct at all).
Why the result of make-trade is wrapped in a list is hard to guess without more context, but I'd guess that an execute can be split into N trades in some scenarios.
I suspect your confusion arises almost entire because this is the first time you have encountered a use of values. Common Lisp allows functions to return multiple values. That's slightly similar to how any language allows functions to receive multiple parameters.
These multiple return values are quite efficiently implemented. Most newbies encounter multiple values for the first time on the integer division functions, which will return a remainder as their second value. Hash table look ups will return a second value to indicate if the key was actually in the table, since the value stored for the key might be nil.
In your example the second value is NIL, presumably other execute methods might return something more interesting - for example where in the update Q the order was place, or an error code if something goes wrong. Of course checking out the manual for values will be fraught with educational values(sic).
This function is a method returning two values by using the keyword values. Have a look at CLOS to better understand object orientation and at "values" for the way of returning more than one value.

Assign a scala.collection.immutable.Set variable to other variable

I have a Set variable like this
val setValues= Set (10,20,30,40)
I want to assign one of them, for example 30 to other variable. how can i do that?
for example in other languages when we have array, we can write
int a=array(2)
and it will assign the value of position 3 of array to variable 'a'
But how can I assign the value of position 2 of a 'Set' of values to other variable or check that whether it is equal to 80 or not?
I tried for example setValues(2), setValues->2 but the result is not 30 in this example
Set = unordered set. Seq = indexable sequence
val seqValues = Seq(10,20,30,40)
seqValues(2)
For better performance, use IndexedSeq which has fast access. seqValues(2) is short for seqValues.apply(2).
Sets are unordered, so you need some way to pick out a particular element--but if you already have that exact element you don't need to get it from the set (you've already got it!). Maybe
val x = Set((0,"fish"),(1,"dish"),(2,"wish")).find(_._1 == 1).get
is what you want (i.e. find an element that obeys some condition other than perfect equality; this returns an Option which you can get if it is found)?
Trivial answer?
val value = Set (10,20,30,40)
val variable = 30
You need to be more specific about the criteria you would use to pick which item would be set to the variable. With a Set, you can't, for example, choose the first or last item because Sets are unordered. You can, however, do something like setting the variable to be the max or min:
val variable = value.max