Suppose
keyset == {"k1", "k2"}
valueset == {"v1", "v2"}
I want to generate a set of all 4 possible record
{
[k1 |-> v1, k2 |-> v1],
[k1 |-> v1, k2 |-> v2],
[k1 |-> v2, k2 |-> v1],
[k1 |-> v2, k2 |-> v2]
}
Use a function set:
AllKVs == [keyset -> valueset]
Related
I'm trying to rewrite the cryptomath code from here and I'm getting this when I try to compile to JS;
Hint: used config file '/home/*******/.choosenim/toolchains/nim-1.6.8/config/nim.cfg' [Conf]
Hint: used config file '/home/*******/.choosenim/toolchains/nim-1.6.8/config/config.nims' [Conf]
...........................................................
/home/*******/nim/cryptomath.nim(6, 9) Error: expression 'a' is of type 'int' and has to be used (or discarded)
This is my code:
import std/random
import std/math
randomize()
proc gcd*(a: int, b: int): int =
while a != 0:
a, b = floorMod(b,a), a # fails here apparently
return (b+a)-a
proc find_mod_inverse*(a: int, m: int): int =
if gcd(a,m) != 1:
return -1
var
u1 = 1
u2 = 0
u3 = a
v1 = 0
v2 = 1
v3 = m
q = -1
while v3 != 0:
q = floorDiv(u3,v3)
v1 = (u1 - q * v1)
v2 = (u2 - q * v2)
v3 = (u3 - q * v3)
u1 = v1
u2 = v2
u3 = v3
return floorMod(u1,m)
I tried adding this, but it did nothing
discard a
before the end of the function
Your code has two issues:
Function arguments in Nim are immutable by default, so if you want to overwrite them locally, you need to shadow them or use var
Nim syntax for multiple variable assignment is different from Python and is done with tuple-like syntax
Fixed code would look like this:
import std/random
import std/math
randomize()
proc gcd*(a: int, b: int): int =
var (a, b) = (a, b)
while a != 0:
(a, b) = (floorMod(b,a), a)
return (b+a)-a
proc find_mod_inverse*(a: int, m: int): int =
if gcd(a,m) != 1:
return -1
var
u1 = 1
u2 = 0
u3 = a
v1 = 0
v2 = 1
v3 = m
q = -1
while v3 != 0:
q = floorDiv(u3,v3)
v1 = (u1 - q * v1)
v2 = (u2 - q * v2)
v3 = (u3 - q * v3)
u1 = v1
u2 = v2
u3 = v3
return floorMod(u1,m)
The compiler error is unclear, I agree.
Also, just a tip - keep in mind that standard Nim integers are limited by the architecture's native integer size, so if you want to operate on big numbers, you need to use a separate library.
when I use NuSMV tools to verify if my CTL is right, I encounter a problem that make me so confused.
My model is
and here's the NuSMV code:
MODULE main
VAR
state : {ROOT, A1, B1, C1, D1, F1, M1};
ASSIGN
init(state) := ROOT;
next(state) := case
state = ROOT : A1;
state = A1 : {B1, C1};
state = B1 : D1;
state = D1 : F1;
TRUE : state;
esac;
CTLSPEC
AG( state=A1 -> AX ( A [ state=B1 U ( state=D1 -> EX state=F1 ) ] ) );
CTLSPEC
AG( state=A1 -> AX ( A [ state=B1 U ( state=F1 -> EX state=C1 ) ] ) );
CTLSPEC
AG( state=A1 -> AX ( A [ state=M1 U ( state=F1 -> EX state=C1 ) ] ) );
My CTL formula is as follows:
"AG( A1 -> AX ( A [ B1 U ( D1 -> EX ( F1) ) ] ) )"
"AG( A1 -> AX ( A [ B1 U ( F1 -> EX ( C1) ) ] ) )"
"AG( A1 -> AX ( A [ M1 U ( F1 -> EX ( C1) ) ] ) )"
NuSMV verified the above three formulas all of which turns out to be true .
So my question is that why the formula 2 and formula 3 turn out to be true?
This question is old, but I think it's still worth an answer, as the issue might be misleading for other people.
M, s ⊨ A[ϕUψ] iff for all paths (s, s2,s3, s4, . ..) s.t. si Rt si+1 there is a state sj s.t. M, sj ⊨ ψ and M, si ⊨ ϕ for all i < j.
So, for the property to be verified, ϕ must be true up until when ψ fires.
Notice that if ψ fires immediately then the value of ϕ is not relevant.
It is easy to see that all three formulas are trivially verified because ψ is true in the first state of each path starting from B1 and C1.
This is the case because ψ is an implication which, in states B1 and C1, has a false premise.
Since we know that A [ ANYTHING U TRUE ] is verified for any state, we conclude that all three properties are satisfiable.
I have the following def. in Alloy:
sig A {b : set B}
sig B{}
sig Q {s: A , t: B}
I want to add a set of constraints such that for each relation b1:b there exists one and only one Q1:Q where Q1.s and Q1.t refers to the source and target of b1, respectively. For example, if I have an instance which contains A1 and B1 and b1 connects them (i.e., b1: A1->B1), then I also would like to have a Q1 where Q1.s=A1 and Q1.t=B1.
Obviously number (cardinality) of Q is equal to number (cardinality) of b relation.
I managed to write such a constraint as bellow:
t in s.b
all q1,q2:Q | q1.s=q2.s and q1.t=q2.t => q1=q2
all a1:A,b1:B | a1->b1 in b => some q:Q | q.s=a1 and q.t=b1
I am wondering if anyone has a bit more concise way to express my intentions in terms of an alloy fact. I am open to use Alloy util package if it makes life easier.
Thanks
sig A { b : set B }
sig B {}
sig Q { ab : A -> B }{ one ab }
fact { b = Q.ab and #Q = #b }
I would complete the #user1513683 answer by adding two relations s and t to make it the complete answer to the question:
sig A { b : set B }
sig B {}
sig Q { ab : A -> B , s:A, t:B}{ one ab and t=ab[s]}
fact { b = Q.ab and #Q = #b }
i must to create a structure of Kripke in NuSMV and i must to check some properties.
Anybody help me? The structure and the properties(LTL, CTL and CTL*) are in the pictures.
Here there is a structure and properties:
http://cl.ly/image/1x0b1v3E0P0D/Screen%20Shot%202014-10-16%20at%2016.52.34.png
I found a simpler and seemingly more reliable NuSMV code for your Kripke Structure. Thanks to dejvuth for his answer to my question. The code is as follows
MODULE main
VAR
state : {s0,s1,s2,s3,s4};
ASSIGN
init(state) := s0;
next(state):=
case
state = s0 : {s1,s2};
state = s1 : {s1,s2};
state = s2 : {s1,s2,s3};
state = s3 : {s1,s4};
state = s4 : {s4};
esac;
DEFINE
p := state = s1 | state = s2 | state = s3 | state = s4;
q := state = s1 | state = s2;
r := state = s3;
SPEC
EG p;
SPEC
AG p;
SPEC
EF (AG p);
As far as I know NuSMV only handles LTL and CTL formulas (see NuSMV in Wikipedia). The formulas in problem 1-3 are CTL formulas, hence it can be model-checked by NuSMV. However the formulas in problem 4 & 5 are CTL* formulas, and thus we cannot straightforwardly use them as an input to NuSMV. You also need to understand that the set of all CTL* formulas is the proper superset of the union of all LTL and CTL formulas. This conditions implies that some CTL* formulas do not have their equivalent LTL or CTL formulas (see CTL* in Wikipedia). Your Kripke structure can be defined in NuSMV by following code:
MODULE main
VAR
p : boolean;
q : boolean;
r : boolean;
state : {s0,s1,s2,s3,s4};
ASSIGN
init (state) := s0;
next (state) :=
case
state = s0 : {s1, s2};
state = s1 : {s1, s2};
state = s2 : {s1, s2, s3};
state = s3 : {s1, s4};
state = s4 : {s4};
TRUE : state;
esac;
init (p) := FALSE;
init (q) := FALSE;
init (r) := FALSE;
next(p) :=
case
state = s1 | state = s2 | state = s3 | state = s4 : TRUE;
TRUE : p;
esac;
next(q) :=
case
state = s1 | state = s2 : TRUE;
state = s3 | state = s4 : FALSE;
TRUE : q;
esac;
next(r) :=
case
state = s3 : TRUE;
state = s1 | state = s2 | state = s4 : FALSE;
TRUE : r;
esac;
SPEC
EG p;
SPEC
AG p;
SPEC
EF (AG p);
Of course, there is another way to define your Kripke structure in NuSMV, but I think this is one of the easiest. (Anyway, thanks for helping me with my problem).
As for the formulas in problem 4 & 5, here is my answer.
The formula AF [p U EG ( p -> q)] is of the form AF [\phi], where \phi is an LTL formula p U EG (p->q). Since the LTL formula \phi is satisfied in a Kripke model if for every path starting at s0 we have the satisfaction of \phi, then we translate AF [p U EG ( p -> q)] into AF A[p U EG ( p -> q)].
By similar argument, we translate EG[(( p & q ) | r) U ( r U AG p)] into EG[A(( p & q ) | r) U A( r U AG p)].
I have been given a struct to work with:
(struct Binding (id (value #:mutable)))
This struct represents a variable binding such as (set! x 3) where I would expect id = x and value = 3.
How do I create and initialize this struct? How do I get the values of id and value and to set the value of value?
> (struct Binding (id (value #:mutable)))
> (define b (Binding 'x 123))
> (Binding-id b)
'x
> (Binding-value b)
123
> (set-Binding-value! b 456)
> (Binding-value b)
456
(See also the documentation page on structs.)