Returning a set from function - coq

I'd like to change the function boolfunction by adding an extra case for the true,true match case. Now the function returns true for the case true,true and I want to code that this case can be false as well. I wonder if there is a way to code this in Coq.
Definition boolfunction (c d : bool) : bool :=
match c,d with
| _, false=> true
|false, true => false
| true,true => true
end.

A function can produce a different type instead of bool. If you want to produce more than one value, one way is to produce a list of values. That is one way of representing a nondeterministic function.
Require Import List.
Import ListNotations.
Definition boolfunction (c d : bool) : list bool :=
match c, d with
| _, false => [true]
| false, true => [false]
| true, true => [true, false]
end.
Without more details about what you are looking for it's hard to be more accurate.

Related

define a map over boolean scala

I am trying to change the value of an array based on the value of a different array. In particular these are the arrays I am working with:
val inpoly: Array[Boolean]=Array(false, true, true, false)
val idx1: Array[Boolean]=Array(true, false, false, false)
I would like to check the array idx1 and where it is true I would like to assign to a new vector the opposite value of the array inpoly in that specific position, otherwise, just leave the value that inpoly already has.
My expectation would be to have this array:
final_vector= true, true, true, false
since the first value of idx1 is true, change the first value of inpoly. All the other values of idx1 are false, so leave the inpoly as it is
I tried with the following code:
idx1.map({
case true => !inpoly
case false => inpoly}
)
However i get the following error:
<console>:73: error: value unary_! is not a member of Array[Boolean]
case true => !inpoly
^
Can anyone help?
You are doing an element-by-element comparison of two collections. The best way to do this is to zip them together and then map the two values to give the answer:
inpoly.zip(idx1).map{ case (poly, idx) => poly ^ idx }
In this case the two values are xor-ed, but clearly you can use any combination of the two values to generate the result.

How can do a tuple comparison in coffeescript similar to that in python?

This is a my function
st3 = (x, y) ->
console.log "#{x?}, #{y?}"
if [x?, y?] is [true, true] # <- this is the line
'good'
'bad'
This is the output
true, true
bad
I want to be able to do a tuple comparison like it is in python.
In python, the if can be written roughly as
if (x, y) == (True, False):
return 'good'
The coffescript if loop is translated into javascript as such
if ([x != null, y != null] === [true, true]) {
'good';
}
That's why this will not evaluated to true.
Is there any alternative way to express it in coffeescript?
If you want to check to see if all of your arguments are not None, then I would do this:
def check_all(*args):
return all(arg is not None for arg in args)
If you wanted to check if they're all True (like literally True) then you could use
def check_all(*args):
return all(arg is True for arg in args)
If you wanted to take in a list (instead of variable number of parameters, remove the asterisk.

How would one implement OCaml / F#'s "function" construct in Scala?

A common tendency I've discovered in Scala is something like this:
def someFunction(a: SomeClass) = a match { ... }
And from there on a is never used ever again. This pattern is SO common in FP that OCaml and F# have a built-in construct to let you ditch that parameter entirely.
Instead of writing this:
let someFunction a =
match a with
| 0 -> true
| _ -> false
you can simply write this:
let someFunction =
function
| 0 -> true
| _ -> false
So my question is, is it possible to write something like this in Scala?
def someFunction = function {
case 0 => true
case _ => false
}
Saving an otherwise unnecessary parameter.
I've attempted to write it as a function that takes a call-by-name parameter, but Scala won't let me make an empty match block.
Is it possible? Or does scala perhaps already have something like this built in?
Use a function instead of a method:
val someFunction: Int => Boolean = {
case 0 => true
case _ => false
}
You have to explicitly write the type annotations, but that must not be a drawback - for API usage it is useful documentation.
You could use partial functions:
def function[A, B](mf: PartialFunction[A, B]): A => B = x => mf(x)
although this requires you to specify the type of the function on the left e.g.
def someFunction: Int => Boolean = function {
case 0 => true
case _ => false
}
I've found a (very ugly) way of doing it
It's more of a hack/workaround than it is an actual solution, but I figured I'd post it here anyway
You can do this:
def something = (_: Int) match {
case 0 => true
case _ => false
}
This works and solves the problem, but it's ugly and clunkier to write than what I tried to get away from in the first place.
I'm curious to see what you guys can come up with.

Infinite loop scala code

object Prop {
def simplify(prop : Prop) : Prop = {
prop match {
case Not(Or(a,b)) => simplify(And(Not(a),Not(b)))
case Not(And(a,b)) => simplify(Or(Not(a),Not(b)))
case Not(Not(a)) => simplify(a)
case _ => {
if (simplify(prop) == prop) prop
else prop
}
}
}
}
I'm pretty sure I've an infinite loop caused by my 'default' case. I'm using recursion in all cases. Which is meant to be, but, only if the Prop can be simplified. As soon as the Prop can't be simplified, it should return the whole thing.
I don't see how I can test for any further simplification. (I'm not allowed to use other libraries, as suggested in freenodes #scala channel).
Can someone explain whether it IS the 'case _' causing the loop, and how to solve it? How can I test for possible simplification without making a loop?
Thanks in advance!
It is pretty obvious what happens and you are right with the default case. If your input prop does not match any of the cases you are invoking:
simplify(prop)
with the same argument. Because previously it caused recursive call to simplify() and you are calling your function with the same input, it enters simplify() again. So this is not an infinite loop but rather never terminated recursive call:
...simplify(simplify(simplify(simplify(simplify(simplify(simplify(prop)))))))
However the fix (based on your code) is simple:
if (simplify(prop) == prop) prop
else prop
just replace it with...
case _ => prop
Both branches return the same value. This is actually correct if you think about if for a while. You have a set of optimizations. If none of them matched your expressions it means it can no longer be simplified. Hence you are returning it as-is.
BTW looks like you are doing boolean expressions simplification using case classes. You might by interested in my article where I do the same but with arithmetic expressions.
The problem is that you're trying to do two things in one step that need to happen in sequence—applying De Morgan's law (and removing double negation) and recursively simplifying any children. This is why just dropping a case And(a, b) => And(simplify(a), simplify(b)) into your match won't work.
Try the following:
val deMorganAndDoubleNegation: Prop => Prop = {
case Not(Or(a, b)) => And(Not(a), Not(b))
case Not(And(a, b)) => Or(Not(a), Not(b))
case Not(Not(a)) => a
case a => a
}
val simplify: Prop => Prop = deMorganAndDoubleNegation andThen {
case And(a, b) => And(simplify(a), simplify(b))
case Or(a, b) => Or(simplify(a), simplify(b))
case Not(a) => Not(simplify(a))
case a => a
}
Yes, default case is causing the loop.
if (simplify(prop) == prop) prop is problematic line.
You don't need to test if it can be simplified further because when you are in the default case all possible simplifications are tried.

Why do the following tests return false in Scala?

Both of the following print false. The desired behaviour is for the first to print true and the second to print false. I'm not sure why false always gets printed.
def myTest(filter: => Boolean) = () => {
if (filter) {
// do something here
true
}
false
}
println(myTest(5 > 3)())
println(myTest(5 > 7)())
This expression:
if (filter) {
true
}
is an expression that returns true when filter evaluates to true or Unit () otherwise.
This block:
{
expr;
false
}
is a block that evaluates expr, discards it, then returns false.
So:
{
if (filter) {
true
}
false
}
always returns false. Think of if (filter) { true } like expr;
huynhjl gives the syntactic reason. The phenomenological reason is that you forgot the else before the false.
scala> def myTest(filter: => Boolean) = () => {
|
| if (filter) {
| // do something here
| true
| }
| else false
| }
myTest: (filter: => Boolean)() => Boolean
scala> println(myTest(5 > 3)())
true
scala> println(myTest(5 > 7)())
false
The last statement is a false, so it returns false, you'd need an else in the if expression to conditionally execute that statement.
Just like to point out that your filter parameter already resolves a boolean so the if expression is entirely redundant, your construction can be rewritten simply as:
def myTest(filter: => Boolean) = () => filter
which may lead to further existential questions.