I found that sometimes I must give types explicitly for pattern variables, otherwise Rascal would not work as expected. The following session in the Console says it all:
rascal>data foo = bar(int);
ok
rascal>int x = 1;
int: 1
rascal>[x | bar(x) <- [bar(2), bar(3)]];
list[void]: []
rascal>[x | bar(int x) <- [bar(2), bar(3)]];
list[int]: [2,3]
Why did this happen?
In the current version of Rascal it is such that variables in patterns that exist in the surrounding scope are not matched and shadowed, but rather checked for equality.
So:
<int x, x> := <2,2> => true // x is first introduced and then checked for equality
<int x, x> := <2,3> => false // x is first introduced and then checked for equality
{ int x = 1; if (x := 2) println("true"); else println("false"); // false!
And this holds for all places where we use pattern matching.
We have had several complaints about this particular design of "non-linear matching", and we intend to add an operator soon ($) to identify the intention of taking something from the surround scope. If the operator is not used, then shadowing will occur:
<int x, $x> := <2,2> => true // x is first introduced and then checked for equality
<int x, $x> := <2,3> => false // x is first introduced and then checked for equality
<int x, x> := <2,3> // static error due to illegal shadowing
<int x, y> := <2,3> => true // x and y are both introduced
{ int x = 1; if ($x := 2) println("true"); else println("false"); // false!
{ int x = 1; if (x := 2) println("true <x>"); else println("false"); // true, prints 2! or perhaps a static error.
Might also add some additional power to get expressions into patterns as in:
<1, ${1 + 2 + 3}> := <1,6> // true
Related
I am trying to prove a property in Dafny, which makes use of powers.
Concretely, this one: forall x,y in Reals : 2xy <= x^2+y^2. I implemented this idea in the following lemma:
lemma product2_lessEqual_powProduct (x:real, y:real)
requires 0.0<x<=1.0 && 0.0<y<=1.0
ensures 2.0*x*y <= (x*x)+(y*y)
{}
Which is verified with no problem (I guess some automatic induction is performed below).
However, I would like to use an own power function in order to make power(x,2) instead of x*x. Thus, I took a power function from https://github.com/bor0/dafny-tutorial/blob/master/pow.dfy, which is as follows:
function method power(A:int, N:nat):int
{
if (N==0) then 1 else A * power(A,N-1)
}
method pow(A:int, N:int) returns (x:int)
requires N >= 0
ensures x == power(A, N)
{
x := 1;
var i := N;
while i != 0
invariant x == power(A, (N-i))
{
x := x * A;
i := i - 1;
}
}
However, since I am using real values for the basis of the exponential, I modified it a bit so that it works for exponentials:
function method power(A:real, N:nat):real
{
if (N==0) then 1.0 else A * power(A,N-1)
}
method pow(A:real, N:int) returns (x:real)
requires N >= 0
ensures x == power(A, N)
{
x := 1.0;
var i := N;
while i != 0
invariant x == power(A, (N-i))
{
x := x * A;
i := i - 1;
}
}
Thus, I wanted to test it with the previous lemma:
lemma product2_lessEqual_powProduct (x:real, y:real)
requires 0.0<x<=1.0 && 0.0<y<=1.0
ensures 2.0*x*y <= power(x,2)+power(y,2)
{}
Surprisingly, it tells me the typical A postcondition might not hold on this return path.Verifier.
Can anyone explain why this happens? Why is it verifying with primitive operations of Dafny, but not when I define them functions? And how could I prove this lemma now?
Even though second parameter of power is concrete and small, Dafny is not doing enough unrolling to prove desired fact. Adding {:fuel 2} to power makes proof go through. You can read more about fuel here https://dafny.org/dafny/DafnyRef/DafnyRef.html#sec-fuel
function method {:fuel 2} power(A:real, N:nat):real
{
if (N==0) then 1.0 else A * power(A,N-1)
}
method pow(A:real, N:int) returns (x:real)
requires N >= 0
ensures x == power(A, N)
{
x := 1.0;
var i := N;
while i != 0
invariant x == power(A, (N-i))
{
x := x * A;
i := i - 1;
}
}
lemma product2_lessEqual_powProduct (x:real, y:real)
requires 0.0<x<=1.0 && 0.0<y<=1.0
ensures 2.0*x*y <= power(x,2)+power(y,2)
{}
It's surprising until you realize that there is a mathematical theory for A*A, but power(A, 2) requires two unfolding of power to have a meaning.
If you want your function to work seamlessly with the theory and prove your last lemma, you can give it precise postconditions:
function method power(A:real, N:nat): (result: real)
ensures N == 1 ==> result == A
ensures N == 2 ==> result == A*A
{
if (N==0) then 1.0 else A * power(A,N-1)
}
I tested it, your second lemma verifies.
I have the following functions:
P[t_] := P[t] = P[t-1] +a*ED[t-1];
ED[t_] := ED[t] = DF[t] + DC[t];
DF[t_] := DF[t] = b (F - P[t]);
DC[t_] := DC[t] = c (P[t] - F);
And the following parameters:
a=1;
c=0.2;
b = 0.75;
F=100;
In Mathematica I use the function "ListLinePlot" in order to plot P[t] and F:
ListLinePlot[{Table[P[t], {t, 0, 25}], Table[F, {t, 0, 25}]}, PlotStyle → {Black, Red},Frame → True, FrameLabel → {"time", "price"}, AspectRatio → 0.4, PlotRange → All]
How can I do this in wxMaxima? Is there a similar function or an alternative to ListLinePlot?
This is my attempt in wxMaxima:
P[t] := P[t-1] + a * ED[t-1];
ED[t] := DF[t] + DC[t];
DF[t] := b*[F-P[t]];
DC[t] := c*[P[t]-F];
a=1;
c=0.2;
b=0.75;
F=100;
And then I tried:
draw2d(points(P[t], [t,0,25]))
The plotted function should look like this:
OK, I've adapted the code you showed above. This works for me. I'm working with Maxima 5.44 on macOS.
P[t] := P[t-1] + a * ED[t-1];
ED[t] := DF[t] + DC[t];
DF[t] := b*(F-P[t]);
DC[t] := c*(P[t]-F);
a:1;
c:0.2;
b:0.75;
F:100;
P[0]: F + 1;
Pt_list: makelist (P[t], t, 0, 25);
load (draw);
set_draw_defaults (terminal = qt);
draw2d (points_joined = true, points(Pt_list));
Notes. (1) There needs to be a base case for the recursion on P. I put P[0]: F + 1. (2) Assignments are : instead of =. Note that x = y is a symbolic equation instead of an assignment. (3) Square brackets [ ] are only for subscripts and lists. Use parentheses ( ) for grouping expressions. (4) Syntax for draw2d is a little different, I fixed it up. (I put a default for terminal since the built-in value is incorrect for Maxima on macOS; if you are working on Linux or Windows, you can omit that.)
EDIT: Try this to draw a horizontal line as well.
draw2d (points_joined = true, points(Pt_list),
color = red, points([[0, F], [25, F]]),
yrange = [F - 1, P[0] + 1]);
I am trying to return a pair of sums using the let construct in sml. Every way I have tried will only return one value. I have tried creating a list by using cons (::) and then returning the list, but that gives an error as well.
val t = [(3,4), (4,5), (5,6)];
fun sumPairs(nil) = 0
| sumPairs((x,y)::zs) =
let
val sumFirst = x + sumPairs(zs)
val sumSecond = y + sumPairs(zs)
in
(sumFirst, sumSecond) <how would I return this as a tuple or list?>
end;
sumPairs(t);
The problem is not with (sumFirst, sumSecond) or with let specifically, but with the rest of your code.
The base case and the recursions say that sumPairs produces an int, not a pair of ints.
Because of this, there is a conflict when you try produce a pair.
Your base case should be (0,0), not 0, since it must be a pair.
You also need to deconstruct the result from the recursion since that produces a pair, not an integer.
Like this
fun sumPairs nil = (0, 0)
| sumPairs ((x,y)::zs) =
let
val (sumFirst, sumSecond) = sumPairs zs
in
(x + sumFirst, y + sumSecond)
end;
I'm attempting to solve AoC problem 12 part 2 in Smalltalk. The specific aren't super relevant, but here's the relevant portion of a single-dimensional version of the model I'm using for the problem:
Object subclass: Moon [
| X XX Xin |
init [
X := 0. XX := 0. Xin := 0.
]
Moon class >> new [ | moon | moon := super new. moon init. ^moon]
x: xx [
X := xx. XX := 0. Xin := xx.
]
kinetic [ ^ (XX abs) ]
initialState [ | killmeNow |
killmeNow := ((self kinetic) == 0).
killmeNow := killmeNow and: (Xin == X).
^killmeNow
]
]
moonA := Moon new.
moonA x: 1.
moonA initialState printNl.
As the variable names just might suggest, I'm failing at the highly complex task of checking if a number equals zero and a second number equals a third number. No amount of refactoring of this statement seems to change the ultimate outcome:
Object: true error: did not understand #value
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
True(Object)>>doesNotUnderstand: #value (SysExcept.st:1448)
True>>and: (True.st:116)
Moon>>initialState (omg.st:15)
UndefinedObject>>executeStatements (omg.st:22)
What is the #Value that (self kin == 0) is receiving, why is it not True, and how do I fix this?
The argument of the and: message must be a Block (and you are passing a Boolean). Just change your code like this
killmeNow := killmeNow and: [Xin == X]. "squared brackets rather that parentheses"
You could also have written the same more succinctly as
initialState [
^self kinetic = 0 and: [Xin = X]
]
but, of course, this is secondary. Note that I've used = instead of == because both have a slightly different semantics and you don't need object identity in these tests (even though == will work as well in your case).
For the sake of completeness, let me mention that there is way to and Booleans: the binary message & does that. So, you could have written
^(self kinetic = 0) & (Xin = X).
The precedence rules of the Smalltalk syntax allow you to remove the parentheses around self kinetic = 0 because here the main message is = which is also binary. The parantheses aroung Xin = X cannot be removed, otherwise & would take precedence over the last occurrence of =.
I have an example of a code and not sure what way is the best to use.
For example I have
if (x = 1) and (y = 2) and (if abc = false then check if z = 3) then
begin
...
check only
if x = 1
if y = 2
if abc = false check z = 3. if abc = true then dont check z = 3
i am not sure if i am explaining the best but hopefuly people will understand.
I want to know if this is possible or the best way to do it. Keeping in mind that rather than in example where its x, y, z and abc. there can be more in my use.
I currently have structure as...which i dont think is practical, and think theres a better way but i am not sure
if (abc = false) then
begin
if (x = 1) and (y = 2) and (z = 3) then
begin
...
end
else
begin
if (x = 1) and (y = 2) then
begin
...
Thanks in advance
I think you're looking for or. Now you will check that x must be 1, y must be 2, and if abc is false, z must be 3.
If abc = true, z can still be three, but it won't be checked.
Note that I just wrote abc instead of abc = true. Since it's a Boolean (true/false) already, that's allowed.
Also note how the operations are grouped using parentheses. The total sub-expression abc or (z=3) must return true for the total expression to return true.
Furthermore the sequence of the terms is significant - they are evaluated left-to-right. If the term (abc or (z=3)) is replaced by the logically-equivalent term ((z=3) or abc) then z=3 will be evaluated.
if (x = 1) and (y = 2) and (abc or (z = 3)) then
// Your magic goes here
Test program body to prove sequence is important
function z : Integer;
begin
writeln('Z being evaluated');
result := x + y;
end;
begin
x := 1;y := 2;
abc := true;
if (x=1) and (y=2) and (abc or (z=3)) then
writeln ('evaluated true')
else
writeln ('evaluated false');
writeln('done');
readln;
end.
Neither of your code samples compile, because neither is using the proper syntax.
This should get you started:
if (x = 1) and (y = 2) then
begin
if (abc) then
// Handle abc = True
else
begin
if (z = 3) then
// Handle abc = false and z = 3
else
// Handle abc = false and z <> 3
end;
end;