VHDL error: Ambiguous type in infix expression; reg_buff or data_buff - type-conversion

i get the following error in ModelSim when doing a type conversion
Ambiguous type in infix expression; reg_buff or data_buff.
Error: (vcom-1583) Illegal type converson from 'unknown' to
'ieee.NUMERIC_STD.UNSIGNED' (operand type is not known).
--declaration of types:
type packet_ipg_array is
array (1 downto 0) of integer;
type packet_buffer_array is
array (1 downto 0, packet_buffer_size-1 downto 0) of std_logic_vector(8 downto 0);
variable packet_ipg : packet_ipg_array;
variable packet_buffers : packet_buffer_array;
--conversion:
packet_ipg(i) :=
to_integer(unsigned(
packet_buffers(i,packet_buffer_bytes_sent(i))(7 downto 0)
& packet_buffers(i,packet_buffer_bytes_sent(i)+1)(7 downto 0)
& packet_buffers(i,packet_buffer_bytes_sent(i)+2)(7 downto 0)
& packet_buffers(i,packet_buffer_bytes_sent(i)+3)(7 downto 0)
));
Could anyone help me out please?

Related

coq Basics: bin_to_nat function

I am passing Logical Foundations course and became stuck upon the last excersize of Basics:
Having binary number write a converter to it's unary representation:
Inductive bin : Type :=
| Z
| A (n : bin)
| B (n : bin).
Fixpoint bin_to_nat (m:bin) : nat :=
(* What to do here? *)
I solved the problem with a recursive function in C. The only thing, I used "0" istead of "A" and "1" instead of "B".
#include <stdio.h>
unsigned int pow2(unsigned int power)
{
if(power != 0)
return 2 << (power - 1);
else
return 1;
}
void rec_converter(char str[], size_t i)
{
if(str[i] == 'Z')
printf("%c", 'Z');
else if(str[i] == '0')
rec_converter(str, ++i);
else if(str[i] == '1')
{
unsigned int n = pow2(i);
for (size_t j = 0; j < n; j++)
{
printf("%c", 'S');
}
rec_converter(str, ++i);
}
}
int main(void)
{
char str[] = "11Z";
rec_converter(str, 0);
printf("\n");
return 0;
}
My problem now is how to write this code in coq:
unsigned int n = pow2(i);
for (size_t j = 0; j < n; j++)
{
printf("%c", 'S');
}
rec_converter(str, ++i);
The main difference between your code and the Coq code is that the Coq code ought to return the natural number, rather than printing it. That means we'll need to keep track of everything that your solution printed and return the result all at once.
Since printing an S means that the answer is the successor of whatever else is printed, we'll need a function that can take the 2^(n)th successor of a natural number. There are various ways to do this, but I'd suggest recursion on n and noting that the 2^(n + 1)th successor of x is the 2^(n)th successor of the 2^(n)th successor of x.
That should be enough to get what you want.
unsigned int n = pow2(i);
for (size_t j = 0; j < n; j++)
{
printf("%c", 'S');
}
rec_converter(str, ++i);
can be written (in pseudo-Coq) as
pow2_succ i (rec_converter str (S i)).
However, one other thing to note: you may not be able to directly access the ith "character" of the input, but this shouldn't be a problem. When you write your function as a Fixpoint
Fixpoint rec_converter (n: bin) (i: nat): nat :=
match n with
| Z => 0
| A m => ...
| B m => ...
end.
the first "character" of m will be the second "character" of the original input. So you'll just need to access the first "character", which is exactly what a Fixpoint does.
For the question on computing powers of 2, you should look at the following file, provided in the Coq libraries (at least up to version 8.9):
https://coq.inria.fr/distrib/current/stdlib/Coq.Init.Nat.html
This file contains a host of functions around the natural numbers, they could all be used as illustrations about how to program with Coq and this datatype.
Fixpoint bin_to_nat (m:bin) : nat :=
match m with
| Z => O
| A n =>2 * (bin_to_nat n)
| B n =>2 * (bin_to_nat n) + 1
end.
see: coq art's 2004. P167-P168. ( How to understand 'positive' type in Coq)

Use of the forall construct in Stainless

I'm trying to proof in Stainless that if two lists have the same contents and one list is bounded by x then the other list is also bounded by x. For doing so, I'm told to use the construct:
forall(x => list.content.contains(x) ==> p(x))
The lemma would be written (in a verbose way) as:
def lowerBoundLemma(l1: List[BigInt],l2: List[BigInt],x:BigInt) : Boolean = {
require(l1.content == l2.content && forall(y => l1.content.contains(y) ==> y <= x))
forall(z => l2.content.contains(z) ==> z <= x) because{
forall(z => l2.content.contains(z) ==> z <= x) ==| l1.content == l2.content |
forall(z => l1.content.contains(z) ==> z <= x) ==| trivial |
forall(y => l1.content.contains(z) ==> y <= x)
}
}.holds
The problem is that I get the following errors:
exercise.scala:12:48: error: missing parameter type
require(l1.content == l2.content && forall(y => l1.content.contains(y) ==> y <= x))
Once I add the type to y I get this error (pointing to the left brace of the contains parentheses):
exercise.scala:12:81: error: ')' expected but '(' found.
require(l1.content == l2.content && forall(y : BigInt => l1.content.contains(y) ==> y <= x))
Any idea why this is happening?
I also tried the syntax l.forall(_ <= x) but I get errors when combining with constructs like because and ==| of the type: because is not a member of Boolean.
The issues you are facing are coming from the Scala compiler frontend to Stainless. In Scala, the syntax for a closure (with specified parameter type) is (x: Type) => body (note the extra parentheses!)
If you want to use because and ==|, you'll have to add import stainless.proof._ at the beginning of your source file.

Do if else statements in Scala always require an else?

I have the following function which works fine:
def power(x: Double, n: Int) : Double = {
if (n > 0 && n % 2 == 0) power(x, n/2) * power(x, n/2)
else if (n > 0 && n % 2 == 1) x * power(x, n-1)
else if (n < 0) 1 / power(x, -n)
else 1
}
If I change it to be:
def power(x: Double, n: Int) : Double = {
if (n > 0 && n % 2 == 0) power(x, n/2) * power(x, n/2)
else if (n > 0 && n % 2 == 1) x * power(x, n-1)
else if (n < 0) 1 / power(x, -n)
else if (n==0 ) 1
}
I.e. change the final else statement to be an else if, then I get the following error trying to call the function:
> <console>:8: error: not found: value power
power(2, 1)
^
I'm guessing this is because there is a possible return type of Unit because the value of n could meet none of the conditions?
In Java "if-else" is a statement. In Scala it is an expression (think of Java's ternary ?:), meaning that it always produces some value. As mentioned by som-snytt in comments, in case of a missing else block the compiler supplies else (), which is of type Unit, which obviously conflicts with the expected type Double in your example.
Some valid examples of missing else are provided in Chris's answer.
No - In general, an if expression does not require an else clause.
Examples:
if (true) println("a")
// prints "a"
if (false) println("a") else if (true) println("b")
// prints "b"
As Nikita Volkov's answer says, though, it is necessary if you need the expression to have some type other than Unit.
Your guess is correct. The method must return a Double, yours would return Unit if non of the "if.." cases match. Actually, when you paste the second definition into the repl, you should get
<console>:11: error: type mismatch;
found : Unit
required: Double

Is mixing types allowed in ST (Structured Text)

I wonder if it is allowed by the standard (IEC 1131-3) to mix different data types in an expression.
Example
VAR A : BOOL;
B : INT;
(* ... *)
IF (B AND C) THEN
...
END_IF
You must use the explicit type conversion functions when converting "down" in types. "up" conversion is done implicitly.
VAR A : BOOL;
B : INT;
(* ... *)
IF (INT_TO_BOOL(B) AND C) THEN
...
END_IF
There are all forms of these type conversion in the form of TYPEA_TO_TYPEB()
It will not compile. Type conversion is needed since ST is type strict as Pascal.

Type mismatch with If statement containing bitwise operators

I am new to scala but I am having the problem with the following code:
var c:Int = 0
var j:Int = 0
for( c <- 0 to 100){
for( j <- 0 to 100){
/* Check if jth bit in c is set,
if( (c & (1<<j)) ) // this line is the line where i get the error
xs :+ (ys(j)) // this is copying element j from list ys to list xs
}
}
The error I get is:
type mismatch; found : Int required: Boolean
the code (c & (1<<j)) should be shifting 1 left j bits and then bitwise anding the result to the int in the variable c to get a boolean result.
It is entirely possible that I am doing something wrong.. I have been learning Scala fro 3 days and I am very rusty on my java.
Any help would be appreicated
Bitwise operations in Scala (in fact in any language) return a result of type Int, your if expression requires a type of Boolean. Scala doesn't treat Boolean values like C, where you code would work fine.
You can make your expression return a Boolean by testing explicitly for 1:
if((c & (1 << j)) != 0)
Unlike in C (or C++), Scala's if statement (just like Java) only accepts a Boolean expression, and there is no implicit type promotion from integral types to Boolean. So you need to be explicit about what you want, and replace if( (c & (1<<j)) ) with if( (c & (1<<j)) != 0)