cmd_register: process (rst_n, clk)
begin
if (rst_n='0') then
cmd_r<= (others=>'0');
elsif (clk'event and clk='1') then
cmd_r<=...;
end if;
end process cmd_register;
I know "<=" specifies assignment but what is others? And what does => do?
cmd_r is defined as a std_logic_vector, or unsigned or signed signal. let's see how this signal type are defined:
type std_logic_vector is array (natural range <>) of std_logic;
type unsigned is array (natural range <>) of std_logic;
type signed is array (natural range <>) of std_logic;
Note that these 3 types have the same definition as an array of std_logic items.
The statement "Others => '0'" is a feature of the VHDL when the coder want to defined several items in an array with the same value.
In your example, all item std_logic in the array are set to '0'.
Another application of this statement is to set some items at a specific value and all others at a default value :
cmd_r <= (0 => '1',
4 => '1',
others => '0');
In this case, the bit 0 and 4 are set to '1' and all other bits are set to '0'.
One last thing, it's NOT possible to write something like this :
cmd_r <= (0 => '1',
4 downto 2 => "111", -- this line is wrong !!!
others => '0');
It just means set all bits to zero!!
( others => '0') is an expression, an aggregate of elements into a composite type.
Without seeing the declaration for cmd_r we can imagine it's an array type, an array type is a composite type (made of one or more elements).
An aggregate combines one or more values as elements into a composite type.
aggregate ::=
( element_association { , element_association } )
Notice the opening and closing parentheses are required.
Those elements can be associated positionally by name for a record type or by index value position for an array type.
element_association ::=
[ choices => ] expression
The element association is governed by choices.
choices ::= choice { | choice }
The element association can cover more than one choice.
choice ::=
simple_expression
| discrete_range
| element_simple_name
| others
A choice can represent one or more elements.
An element simple name is used for a record type or an array type with an index type that is an enumerated type.
others is always the last choice and stands for all the remaining choices for that type. The type can be discovered in an assignment from the target. In some instances the type is required to be supplied explicitly, as in a qualified expression.
The element association others => '0' stands for all other elements of the type of the aggregate. In this case the type and subtype of cmd_r, where a subtype indication specifies a range index of elements of a std_logic_vector.
The expression '0' is required to be of the element type, and the aggregate (others => '0') stands for a value of the subtype of cmd_r comprised of '0''s for each of the elements of cmd_r in this case.
The expression (others=>’O’) means that all elements are assigned to ’0’.
If cmd_r is 8 bit then it will assign 00000000 to cmd_r. If cmd_r is two dimensional then the same thing will be (others =>(others =>'0')).
Related
When working with lists in scalar context the last element is returned:
#l = qw/ a b c d /;
print scalar ( 1, #l ); # prints 4
The last element of the list is #l, its length in scalar context is 4
So the last element is #l.
Why when I do slice I do not get this element?
print (( 1, #l )[1]); # prints 'a'. expect: abcd
PS. Maybe I should ask: Why flattening is not happened in first example?
It's because of the difference in behaviour between list and scalar context, and the fact that scalar is an operator and not a subroutine call. scalar can take only a single operand, so if you pass more than one parameter then they are put into scalar context. This is unlike ordinary subroutine calls where the parameters are always in list context.
In scalar context the comma operator evaluates and discards its left-hand operand and then evaluates its right-hand operand. So scalar(1, #l) evaluates and discards 1 and then evaluates #l. As you say, that is 4 because #l has four elements
A list slice, unsurprisingly, imposes list context, and in list context commas separate the elements of a list and arrays and hashes are flattened. So (1, #l)[1] is the same as (1, qw/ a b c d /)[1] which is a
Perl array operators like push and pop behave in a similar way. push #l, 1 wouldn't be much use if it behaved the same way as an ordinary subroutine and expanded to push 'a', 'b', 'c', 'd', 1
In your first example you force scalar context by using scalar .
In scalar content , is interpreted as scalar binary operator returning right side argument.
#l = qw/ a b c d /;
#m = (1, #l); # (1,#l) is interpreted in list context
print scalar(#l); # prints 4
print scalar(#m); # prints 5
print scalar('a',#l); # ('a',#l) is interpreted in scalar context
# prints scalar(#l)
print scalar(#l,'a'); # (#l,'a') is interpreted in scalar context
# prints a
perldoc -f scalar
scalar EXPR
Forces EXPR to be interpreted in scalar context and returns the value of EXPR.
man perlop
Comma Operator
Binary "," is the comma operator. In scalar context it evaluates its
left argument, throws that value away, then evaluates its right
argument and returns that value. This is just like C's comma operator.
In list context, it's just the list argument separator, and inserts
both its arguments into the list. These arguments are also evaluated
from left to right.
I have an exercise in Haskell where I need to create various types.The first type is called Finite which is defined like this:
type Finite a = [a]
and then I need to return a singleton which is defined like this
singleF :: a -> Finite a
so I implemented it like so:
single n = [n]
Then later I create another type
type Enumeration a = Int -> Finite a
then I need to reimplement the singleton function
singleE :: a -> Enumeration a
In my understanding the type Enumeration is a synonym for a function from an Int to a list of type a, but I can't understand how exactly I can implement that.
From the exercise (the previous type 'Finite' is also referred to as a 'bucket'): An enumeration is an infinite sequence of finite buckets, indexed by natural numbers.
And the function single : I suggest for simplicity that you put the sole item in bucket 0, So I'm thinking that the int is the index of the bucket in the enumeration
Off the top of my head:
singleE :: a -> Enumeration a
singleE a 0 = singleF a
singleE _ _ = []
main :: IO ()
main = do
let s=singleE 'a'
print $ s 0
print $ s 5
Gives
"a"
""
singleE gives you a function that takes an Int and returns a Finite. If you pass 0, you get a Finite with the single element, otherwise an empty one.
I have a list of Objects (Items, in this case) which have category ids and properties (which itself is a list of custom types).
I am trying to def a function that takes a list of integers e.g. List(101, 102, 102, 103, 104) that correspond to the category ids for the Items and creates a list of tuples that include the category type (which is an Option) and each property type from a list of properties that go along with each category. So far I have the below, but I am getting an error that value _2 is not a member of Product with Serializable.
def idxToData(index: List[Int], items: Seq[Item]): List[(Option[Category], Property[_])] = {
def getId(ic: Option[Category]): Int => {
ic match {
case Some(e) => e._id
case None => 0
}
}
index.flatMap(t => items.map(i => if(t == getId(i.category)){
(i.category, i.properties.list.map(_.property).toList.sortWith(_._id < _._id))
} else {
None
}.filter(_ != None )
))
.map(x => x._2.map(d => (x._1, d)))
.toList
}
I am not sure how it is assigning that type (I am assuming at that point that I should have a list of tuples that I am trying to map).
Overall, is there a better way in scala to achieve the desired result from taking in a list of indices and using that to access the specific items in a list where a tuple of two parts of each corresponding item would "replace" the index to create the new list structure?
You should split your code, give names to things (add some vals and some defs), and when the compiler does not agree with you, write types, so that the compiler will tell you early where it disagrees (don't worry, we all did that when starting with FP)
Also, when posting such a question, you might want to give (relevant parts of) the interface of elements that are referenced but not defined. What are "is" (is that items?), Item, category, properties...., or simplify your code so that they do not appear.
Now, to the problem :
if(t == (i.category match { case Some(e) => e._id})){
(i.category, i.properties.list.map(_.property).toList.sortWith(_._id < _._id))
} else {
None
}
The first branch is the type Tuple2(Int, whatever) while the second branch is of the completely unrelated type None. Clearly, there is no common super type better than AnyRef, so that is the type of the if expression. Then the type of is.map (supposing is is some sort of Seq) will be Seq[AnyRef]. filter does not change the type, so still Seq[AnyRef], and in the map(x =>...), x is an AnyRef too, not a Tuple2, so it has no _2.
Of course, the list actually contains only tuples, because originally it had tuples and Nones and you have removed the Nones. But that was lost to the compiler when it typed that AnyRef.
(as the compiler error message tells and as noted by Imm, the compiler finds a slightly more precise type than AnyRef, Product with Serializable; however, that will not do you any good, all of the useful typing information is still lost there).
To preserve the type, in general you should do something such as
if(....) {
Some(stuff)
else
None
That would have been typed Option[type of stuff], where type of stuff is your Pair.
However, there is something simpler with routine collect.
It is a bit like match, except that it takes a partial function, and it discard elements for which the partial function is not defined.
So that would be
is.collect { case i if categoryId(i) == Some(t) =>
(i.catetory, i.properties....)
}
supposing you have defined
def categoryId(item: Item): Option[Int] = item.category.map(._id)
When you do this:
is.map(i => if(t == getId(i.category)){
(i.category, i.properties.list.map(_.property).toList.sortWith(_._id < _._id))
} else {
None
}
you get a List[Product with Serializable] (what you should probably get is a type error, but that could be a long digression), because that's the only supertype of None and (Category, List[Property[_]]) or whatever that tuple type is. The compiler isn't smart enough to carry the union type through and figure out that when you filter(_ != None) anything left in the list must be the tuple.
Try to rephrase this part. E.g. you could do is.filter(i => t == getId(i.category)) first, before the map, and then you wouldn't need to mess around with Nones in your list.
I'm new to Fortran, and sorry for this noobish question, I didn't find an answer for it.
In the code:
integer ( kind = 4 ) k
integer ( kind = 4 ) v(k)
integer ( kind = 4 ) list(*)
What does (k) and (*) do in the second, third line?
Thanks
The first integer, k is a scalar. The second integer v(k) is an array v with k elements. The last integer list(*) an assumed size array that is a dummy argument to a procedure. Its length (number of elements) will be determined by the actual argument passed to the procedure.
Note that kind = 4 is not portable and you should instead use the intrinsics kind() or selected_int_kind() to specify the size of your integers.
Complementing the answer of #casey:
The definition of
INTEGER(KIND=4) list(*)
is only valid as definition of a dummy argument. Though, you can define this list with the help of a constant as a named constant (specified by the PARAMETER keyword):
INTEGER(KIND=4), PARAMETER :: list(*) = [1,2,3,4,5]
In this case, this is called an implied-shape array (5.3.8.6) which gets its length implicitely from the constant array.
Question is simple.
How to access a tuple by using Index variable in SML?
val index = 5;
val tuple1 = (1,2,3,4,5,6,7,8,9,10);
val correctValue = #index tuple1 ??
I hope, somebody would be able to help out.
Thanks in advance!
There doesn't exist a function which takes an integer value and a tuple, and extracts that element from the tuple. There are of course the #1, #2, ... functions, but these do not take an integer argument. That is, the name of the "function" is #5, it is not the function # applied to the value 5. As such, you cannot substitute the name index instead of the 5.
If you don't know in advance at which place in the tuple the element you want will be at, you're probably using them in a way they're not intended to be used.
You might want a list of values, for which the 'a list type is more natural. You can then access the nth element using List.nth.
To clarify a bit, why you can't do that you need some more knowledge of what a tuple is in SML.
Tuples are actually represented as records in SML. Remember that records has the form {id = expr, id = expr, ..., id = expr} where each identifier is a label.
The difference of tuples and records is given away by the way you index elements in a tuple: #1, #2, ... (1, "foo", 42.0) is a derived form of (equivalent with) {1 = 1, 2 = "foo", 3 = 42.0}. This is perhaps better seen by the type that SML/NJ gives that record
- {1 = 1, 2 = "foo", 3 = 42.0};
val it = (1,"foo",42.0) : int * string * real
Note the type is not shown as a record type such as {1: int, 2: string, 3: real}. The tuple type is again a derived form of the record type.
Actually #id is not a function, and thus it can't be called with a variable as "argument". It is actually a derived form of (note the wildcard pattern row, in the record pattern match)
fn {id=var, ...} => var
So in conclusion, you won't be able to do what you wan't, since these derived forms (or syntactic sugar if you will) aren't dynamic in any ways.
One way is as Sebastian Paaske said to use lists. The drawback is that you need O(n) computations to access the nth element of a list. If you need to access an element in O(1) time, you may use arrays, which are in basic sml library.
You can find ore about arrays at:
http://sml-family.org/Basis/array.html