[Alloy]No instance found with no fact - instance

I wrote the following code in Alloy. I was wondering why it doesn't find an instance since there are no facts at all in code.
abstract sig TaskStatus {}
one sig Completed extends TaskStatus {}
one sig Waiting extends TaskStatus {}
one sig OnGoing extends TaskStatus {}
sig Capability {}
sig Task {
status: one TaskStatus,
precondition: set Task,
capability: one Capability
}
sig Agent {
tasks: set Task,
capabilities: set Capability
}
sig ToDoList {
tasks: set Task
}
pred show {
some Capability
some Agent
some ToDoList
#Task > 3
}
run show

You didn't specify a scope in your run command
the scope defaulted to 3 (meaning, the universe contains 3 atoms of every sig)
#Task > 3 is not satisfiable in that scope
If you run your original model, with verbosity set at least to "medium", you should see something like this in the console window on the right-hand side
Executing "Run show"
Sig this/Completed scope <= 1
Sig this/Waiting scope <= 1
Sig this/OnGoing scope <= 1
Sig this/TaskStatus scope <= 3
Sig this/Capability scope <= 3
Sig this/Task scope <= 3
Sig this/Agent scope <= 3
Sig this/ToDoList scope <= 3
confirming that the scope for Task was indeed by default set to 3.
To fix the problem, specify a larger scope, e.g.,
run show for 5

Related

How to use `when` with 2 sealed classes and getting the inner value?

Consider this extreme simplified code (available on https://pl.kotl.in/bb2Irv8dD):
sealed class Person {
data class A(val i: Int) :
Person()
}
fun main() {
val a = Person.A(i = 0)
val b = Person.A(i = 1)
// Compiles
when (a) {
is Person.A -> print("I have access to {$a.i}")
}
// Does not compile :(
when (a to b) {
is Person.A to is Person.A -> print("I have access to {$a.i} and b {$b.i}")
}
}
Why does the (a to b) code not work? It works for 1 variable, I was hoping I can match on both classes and get both inner values.
The error is:
Incompatible types: Person.A and Pair<Person.A, Person.A> Expecting
'->' Expecting an element Incompatible types: Person.A and
Pair<Person.A, Person.A>
Aside from that syntax not being supported (you can only use is on one thing in a when branch), by using to you're literally creating an instance of the Pair class.
Pair uses generics for the types of its two variables, so this type information is lost at runtime due to type erasure.
So although, you can do this:
when (a to b) {
is Pair<Person.A, Person.A> -> print("I have access to {$a.i} and b {$b.i}")
}
it is only allowed when both a and b are local variables whose types are declared locally, so that the generic types of the Pair are known at compile time. But this makes it mostly useless, because if a and b are local variables with known type at compile time, then you could just replace the above with true or false.
To be able to do something like this in a general way, you must either create local variables to use:
val aIsTypeA = a is Person.A
val bIsTypeA = b is Person.A
when (aIsTypeA to bIsTypeA) {
true to true -> //...
//...
}
or use when without a subject and put the full condition on each branch:
when {
a is Person.A && b is Person.A -> //...
//...
}
The (a to b) returns a Pair<Person.A,Person.A> but what you are checking is Type Person.A to Type Person.A instead of the Type Pair<Person.A,Person.A>.
What you can do instead is:
when (a to b) {
is Pair<Person.A,Person.A> -> print("I have access to {$a.i} and b {$b.i}")
}

Expecting member declaration (this happens when using a variable from another class)

The code here works:
fun main(){
val pizza = random()
print(pizza.num)
}
class random{
val num = 5
}
But the code here does not work
fun main(){
val pizza = random()
print(pizza.num)
}
class random{
val num = 5
num = 7
}
The only difference is that in the last line of code I reassign the variable num. The only thing I did was change this variable from 5 to 7.
Why is this causing errors?
Note This is the online IDE I was using: https://developer.android.com/training/kotlinplayground
2 things:
Firstly, you can't reassign vals. you need to change that to var
Secondly, you can't do assignments directly in a class body, only declarations.
However, you could put it in an init block like this to get the desired result:
class random{
var num = 5
init {
num = 7
}
}
you might want to read the documentation about kotlin classes here

Is "variable dot property" construct (var.property) just syntaxic sugar for accessors?

This is a follow up question to this one: Are accessors of class properties useful?
In this complete answer it is stated that:
"When you use the property, you are using the getter and setter methdods. The property is a convenience for language binding. Thus, we could also say that you don't need the property, only the getter and setter."
But the following experiment seems to contradict that point:
file src/PositiveInteger.gd
With a setter which prevents negative integer to be assigned to the property n
class_name PositiveInteger
var n: int
func _init() -> void:
self.n = 0
func set_n(_n: int) -> void:
if _n < 0:
n = 0
else:
n = _n
func get_n() -> int:
return n
file main.gd
Now let us consider this test, with c1 using the setter and c2 using the dot construct:
tool
extends EditorScript
tool
extends EditorScript
func _run() -> void:
var PositiveInteger = load("res://src/PositiveInteger.gd")
var c1 = PositiveInteger.new()
c1.set_n(-3)
print("c1: " + str(c1.n))
var c2 = PositiveInteger.new()
c2.n = -3
print("c2: " + str(c2.n))
The output is:
c1: 0
c2: -3
So the property assignement seems to bypass the setter, is this behaviour different with the core class of the language only?
My prior answer applies to core classes (and should apply to modules classes), which are written in C++. They do not expose anything by default. Instead they must bind their C++ methods as methods and properties to expose them.
Now, if you want something like that for a class created in GDScript, you can do it with setget:
class_name PositiveInteger
var n: int setget set_n, get_n
func _init() -> void:
self.n = 0
func set_n(_n: int) -> void:
if _n < 0:
n = 0
else:
n = _n
func get_n() -> int:
return n
With that your output should be:
0
0

"the expression is unused" when using kotest in intellij

With this kotlin test written with Kotest, IntelliJ shows the warning "The expression is unused" and syntax coloration is not working.
Also, when running the tests, the test is not found.
class TalentMatchServiceSpec : StringSpec() {
init {
"add 3 to 2 should give 5"
{
// Given integers 2 and 3
val two = 2
val three = 3
// When adding both numbers
val sum = two + three
// Then the result is 5
sum shouldBe 5
}
}
}
#Thomas Martin's answer is correct, but you asked why it makes a difference.
The SpringSpec in Kotest relies on some Kotlin DSL features to work. Let's demonstrate by building the same function from scratch.
We would start by using Kotlin's extension functions. This allows us to add functions to classes we don't control.
So,
fun String.test(test: () -> Unit)
So then we can call this test function on any string:
"this is a test".test({ 1 + 2 shouldBe 3 })
Secondly, Kotlin allows any final lambda arg to be brought outside of the parens. So foo(arg, arg2, arg3, lambda) can become foo(arg, arg2, arg3) lambda. This means we can write our test as:
"this is a test".test { 1 + 2 shouldBe 3 }
Next, we can mark functions as infix and then we do not need to use the dot to invoke them. So our test function becomes:
infix fun String.test(test: () -> Unit)
And our test now looks like:
"this is a test" test { 1 + 2 shouldBe 3 }
Finally, any function named invoke and marked as an operator function can be invoked without the function name. So operator fun invoke(a: String, b: String) inside a class Foo can be invoked as both the regular Foo.invoke(a,b) or just Foo(a, b).
So putting all this together, our final test function looks like:
operator fun String.invoke(test: () -> Unit)
And our test ends up as:
"this is a test" { 1 + 2 shouldBe 3 }
Or more likely,
"this is a test" {
1 + 2 shouldBe 3
}
If the braces are moved to the next line, as in your original question, it just looks like a String followed by an unrelated lambda block. Two different expressions.
Just put the opening curly bracket after "add 3 to 2 should give 5", like this :
class TalentMatchServiceSpec : StringSpec() {
init {
"add 3 to 2 should give 5" {
// Given integers 2 and 3
val two = 2
val three = 3
// When adding both numbers
val sum = two + three
// Then the result is 5
sum shouldBe 5
}
}
}

ocaml: create set of polymorphic type

In a module, I have defined a type that represents a graph node, which has a polymorphic data field i.e.
type 'a t = {data: 'a; adj: 'a t list}
How can I go about creating a Set of this data? I have tried the following (as per one of the suggestions here.
let cmp (g1:int Graph.t) (g2: int Graph.t) : int=
if phys_equal g1 g2 then
0
else
Int.compare g1.data g2.data
let make_set () =
let module Ord=struct
type t=int Graph.t
let compare=cmp
end
in (module Set.Make(Ord): Set.S with type elt=Ord.t)
But when I do, I get "The signature constrained by `with' has no component named elt"
I'm not sure exactly what you're trying to do, but if you just want to make sets of nodes whose data type is int, you don't need to use anything fancier than the usual OCaml module operations.
The following code works for me:
module Graph =
struct
type 'a t = {data: 'a; adj: 'a t list}
end
let cmp (g1:int Graph.t) (g2: int Graph.t) : int =
if g1 == g2 then
0
else
compare g1.Graph.data g2.Graph.data
module GSet = Set.Make(struct type t = int Graph.t let compare = cmp end)
Here's a session with the code:
$ ocaml
OCaml version 4.01.0
# #use "g.ml";;
module Graph : sig type 'a t = { data : 'a; adj : 'a t list; } end
val cmp : int Graph.t -> int Graph.t -> int = <fun>
module GSet :
sig
type elt = int Graph.t
. . .
end
# let myset = GSet.add { Graph.data = 14; adj = [] } GSet.empty;;
val myset : GSet.t = <abstr>
# GSet.is_empty myset;;
- : bool = false
I don't see a reason to constrain the module type, as Set.S is already the module type returned by Set.Make. But I am not a sophisticated user of OCaml module types.
That code works fine in the interpreter for me. Perhaps you've opened a different Set module without the elt type defined in S?
If I define the following Set in the interpreter:
# module Set = struct
module type S = sig end
end;;
module Set : sig module type S = sig end end
And then simply redefine make_set as you wrote it, I actually get the same error message. When trying out code with the interpreter, always keep in mind that you may be working with definitions you wrote previously.
As a rule of thumb, try to avoid binding values to names already in use in the libraries you wish to employ (I know it's tempting to shorten your names there, but at least add them a small distinctive prefix - for instance use ISet instead of Set in your case).
you can always run your code as a script, i.e. by launching it from the command line as follow:
$ ocaml my_script.ml
Or by the #use directive at the interpreter prompt. This let you write code snippets before testing them out with a fresh ocaml environment.
Finally, as in #Jeffrey's provided answer, an unpacked module is good enough for most purpose; your code was about instantiating a first class module, which is only interesting if you intend to pass that module around without the use of functors. See the documentation on modules (and related extensions of the language) for further explanation.