Characteristic function in functional set - scala

It's a question regarding syntactic sugars in Scala functions, similar discussion could be found here and here. Both answers give great insights, but I still cannot get my head aroud.
That is, I could not understand how characteristic function works?
Characteristic function (see the code below):
s(elem)
Set(elem)
Let's see an example,
object devScript extends App {
type Set = Int => Boolean
def contains(s: Set, elem: Int): Boolean = s(elem)
def singletonSet1(elem: Int): Set = (x: Int) => x == elem
def singletonSet2(elem: Int): Set = Set(elem)
println(contains(singletonSet1(5), 5))
println(contains(singletonSet2(5), 5))
}
Returns:
true
true
singletonSet1 and singletonSet2 have given the same result, so are they just two functions that expressing the same thing but in different forms? Moreover, without explicitly define a Boolean clause, how could a function return true/false?

I think this example is a bit confusing because there are two definitions of Set in scope. You define your own type alias Set which has a type of a function that takes Int and returns Boolean: Int => Boolean. On the other hand you also have Set from Scala collections in scope which has apply function which is also of the same type: Int => Boolean. Let's look at these types in REPL:
scala> singletonSet1 _
res1: Int => Set = $$Lambda$1261/1618596377#4ed90b04
scala> singletonSet2 _
res2: Int => Set = $$Lambda$1262/224661478#14f08a97
scala> Set(1).apply _
res3: Int => Boolean = $$Lambda$1352/273821181#17d6b6e
// same as apply above
scala> Set(1)(_)
res4: Int => Boolean = $$Lambda$1377/1981148063#4ebe0e3f
scala> singletonSet1(1)
res5: Set = $$Lambda$1230/593573468#7574d30b
scala> singletonSet1(1)(1)
res6: Boolean = true
// or similarly:
scala> singletonSet2(1)(1)
res7: Boolean = true
As you can see apply, also written as () conforms to the type that you need to return from your singletonSet[1|2] function.
You are right that both of them can be used as functions which are implemented in different forms: using closures for singletonSet1 and using regular Scala Set in singletonSet2 case.
Note that singletonSet[1|2] doesn't return boolean, it returns a function that will return a Boolean provided you've given it an Int. There is one more level of indirection involved. If you write out the type of these functions entirely it will look like this: Int => Int => Boolean, or equivalently: Int => Set.
Coincidentally I wrote a blog post trying to explain how this works. You can check it out here:http://izmailoff.github.io/programming%20languages/functional%20programming/functional_sets. Hopefully it's clear enough.

The Set(elem) refers to the companion object of scala.collection.Set, which is a value, and therefore is not shadowed by your type-definition Set.
Just run this to see it:
object devScript extends App {
type Set = Int => Boolean
def contains(s: Set, elem: Int): Boolean = s(elem)
def singletonSet1(elem: Int): Set = (x: Int) => x == elem
def singletonSet2(elem: Int): Set = Set(elem)
println(contains(singletonSet1(5), 5))
println(contains(singletonSet2(5), 5))
println(singletonSet2(42).getClass)
}
It will print true, true, class scala.collection.immutable.Set$Set1,
instead of something like Int => Boolean, as you might have expected.
Even more confusingly, the contains(singletoSet2(5), 5) also works, because, scala's standard collection Set[A] also implements A => Boolean, as can be seen in the documentation.

Related

How to define a type of function with default value in Scala

This is slightly different from this question.
I want to define a function type that have default value defined.
Like this:
trait Foo {
type MyFunction = (Int, Option[Int] = 0) => Boolean
def checkInts(f: MyFunction)
}
Is it possible? If yes, how can I achieve this? If not, why?
Read here why you can't have default arguments in anonymous functions and how to make something similar - In Scala, can you make an anonymous function have a default argument?
But if you just need a way to pass a function taking 2 or 1 argument, you can always use simpler approach:
trait Foo {
type MyFunc1 = (Int) => Boolean
type MyFunc2 = (Int, Int) => Boolean
def checkInts(f: MyFunc1)
def checkInts(f: MyFunc2)
// common code of checkInts could be in some private method
}
Based on my knowledge so far you cannot define a type with default parameters. A type is a type.
What you can do is define a partially applied function.
Taking as an example the following function:
scala> def factorOf(x: Int, y: Int) = y % x == 0
factorOf: (x: Int, y: Int)Boolean
If you want a shortcut to the function without retaining any parameters, you can use the wildcard operator (_) assignment
scala> val f = factorOf _
f: (Int, Int) => Boolean = <function2>
scala> val x = f(7, 20)
x: Boolean = false
If you want to retain some of the parameters, you can partially apply the function by using the wildcard operator to take the place of one of the parameters. The wildcard operator here requires an explicit type, because it is used to generate a function value with a declared input type:
scala> val multipleOf3 = factorOf(3, _: Int)
multipleOf3: Int => Boolean = <function1>
scala> val y = multipleOf3(78)
y: Boolean = true
The new function value, multipleOf3, is a partially applied function, because it contains some but not all of the parameters for the factorOf() function.
A cleaner way to partially apply functions is to use functions with multiple parameter lists. This is a technique
known as currying the function:
scala> def factorOf(x: Int)(y: Int) = y % x == 0
factorOf: (x: Int)(y: Int)Boolean
scala> val isEven = factorOf(2) _
isEven: Int => Boolean = <function1>
scala> val z = isEven(32)
z: Boolean = true

Where does set come from in "val Singleton4 : Set = set => set == 4"?

What's the verbose way for val Singleton4 : Set = set => set == 4? I am unable to understand where the set parameter is coming from.
What maybe confuses you is the fact that Set is not what one would expect from the Scala library. You should read the line as:
val Singleton4: Int => Boolean = someArbitraryName => someArbitraryName == 4
// <identifier> <type> = <argument> => <returnValue>
It's not "coming" from anywhere, Set is defined as a function from an integer to a boolean and the type of Singleton4 is exactly that, it takes an integer and returns a function which needs another integer and returns a boolean:
scala> type Set = Int => Boolean
defined type alias Set
scala> def singletonSet(elem: Int): Set = set => set == elem
singletonSet: (elem: Int)Int => Boolean
scala> val p: Set = singletonSet(3)
p: Int => Boolean = <function1>
So now p is a function which takes an integer:
scala> p(3)
res0: Boolean = true
scala> p(2)
res1: Boolean = false
An alternative way of writing that is this:
def singletonSet(elem: Int) = (i: Int) => i == elem
val p: (Int) => Boolean = singletonSet(3)
Or using a different way but achieving the same result:
def singletonSet(elem: Int)(i: Int): Boolean = i == elem
val p: (Int) => Boolean = singletonSet2(3)
By currying the function and applying only one argument you get back a function which still needs another integer to be fully evaluated.
The other answers are very good, but I thought I'd throw some additional details to help newcomers to Scala understand the notation better (as there are some hidden gems in the notation that were not uncovered/described).
Let's play the Scala compiler game!
The following val says enough about what Set the type represents.
val Singleton4: Set = set => set == 4
The type of val is given explicitly - it's Set. We don't however know much about it...yet. It has however to be given to the compiler before it can be used and given the left-hand side it's a function (mind the => two-letter string) from a type (we'll get to it in a moment) to Boolean since set == 4 will inevitably end as Boolean.
From set == 4 we can deduce that set can only be Int (so you can compare apples to apples) and hence we've got the type of set as Int.
The right-hand side of = of the val could also have been written as:
val Singleton4: Set = (set: Int) => set == 4
So, the Singleton4 val is an instance of a function Int => Boolean that's called Set. Set's declared as such somewhere above (lexicographically):
type Set = Int => Boolean
You could substitute Set to the type alias and end up with:
val Singleton4: Int => Boolean = (set: Int) => set == 4

Scala and Writing map functions

So suppose I have a function that expects a Set with definition Int => Boolean and a function f like this:
def map(s: Set, f: Int => Int): Set = {}
Now how do I apply this f to each element of this set s.
def map(s: Set, f: Int => Int): Set = { (i: Int) => f(s(i)) }
Which is ofcourse incorrect because in f(s(i)), 's(i)' returns a boolean, and thus can't apply f on it. Problem is how do I access each element of Set and apply this f on it?
This question is part of Coursera's Functional Programming with Scala course.
A goal of the course is to help you understand the functional model, and in this case, how the Set can be represented by a function (called the charasteristic function for that set).
Instead of the final solution, here's a hint on how to reason about this problem:
Given the characteristic function f: Int => Boolean that defines your set and x, an Int, if f(x) == true, then x belongs to the Set. Now, if you have a function g:Int=>Int
that you want to map over the set, what you want is to apply that function to the elements that you know that belong to the set: if (f(x)) then g(x).
Try to apply that thinking to your exercise.
If you wrote the exists function correctly:
def map(s: Set, f: Int => Int): Set = (i: Int) => exists(s, (x: Int) => i == f(x))
Bam one line!
You need to use fold to do this.
def map(s: Set[Int], f: Int => Int): Set[Int] =
s.foldLeft(Set.empty[Int])((s,i) => s+f(i))

In Scala, can generic type parameters be used with *function* definitions?

Is there a syntax to allow generic type parameters on function literals? I know I could wrap it in a method such as:
def createLongStringFunction[T](): (T) => Boolean = {
(obj: T) => obj.toString.length > 7
}
but then I end up needing to invoke the method for every type T and getting a new function. I looked through the language reference, and while I see that the function literal syntax is translated by the compiler to an instance of a Functionn object that itself has generic input types, it looks like the compiler magic realizes those parameters at the time of creation. I haven't found any syntax that allows me to, in effect, "leave one or more of the type parameters of Functionn unbound". What I would prefer is something along the lines of:
// doesn't compile
val longStringFunction: [T](T) => Boolean = (obj: T) => obj.toString.length > 7
Does any such thing exist? Or for that matter, what is the explicit type of an eta-expansion function when the method being expanded has generic parameters?
This is a purely contrived and useless example. Of course I could just make the function use Any here.
No, type parameters only apply to methods and not function objects. For example,
def f[T](x: T) = x //> f: [T](x: T)T
val g = f _ //> g: Nothing => Nothing = <function1>
// g(2) // error
val h: Int=>Int = f _ //> h : Int => Int = <function2>
h(2) //> res0: Int = 2
The method f cannot be converted to a polymorphic function object g. As you can see, the inferred type of g is actually Function1[Nothing, Nothing], which is useless. However, with a type hint we can construct h: Function1[Int,Int] that works as expected for Int argument.
As you say, in your example all you're requiring is the toString method and so Any would be the usual solution. However, there is call for being able to use higher-rank types in situations such as applying a type constructor such as List to every element in a tuple.
As the other answers have mentioned, there's no direct support for this, but there's a relatively nice way to encode it:
trait ~>[A[_],B[_]] {
def apply[X](a : A[X]) : B[X]
}
type Id[A] = A //necessary hack
object newList extends (Id ~> List) {
def apply[X](a : Id[X]) = List(a)
}
def tupleize[A,B, F[_]](f : Id ~> F, a : A, b : B) = (f(a), f(b))
tupleize(newList, 1, "Hello") // (List(1), List(Hello))
Since longStringFunction defined as followed is a value, which must have some given type.
val longStringFunction: (T) => Boolean = (obj: T) => obj.toString.length > 7
However, you can reuse a function object with a method:
scala> val funObj: Any => Boolean = _.toString.size > 7
funObj: Any => Boolean = <function1>
scala> def typedFunction[T]: T => Boolean = funObj
typedFunction: [T]=> T => Boolean
scala> val f1 = typedFunction[String]
f1: String => Boolean = <function1>
scala> val f2 = typedFunction[Int]
f2: Int => Boolean = <function1>
scala> f1 eq f2
res0: Boolean = true
This works because trait Function1[-T1, +R] is contravariant of type T1.
In scala, Function values are parametrically monomorphic(while methods are polymorphic)
Shapeless library introduces polymorphic function values which may be mapped over HLists and many more other features.
Please consider the following refs:
http://www.chuusai.com/2012/04/27/shapeless-polymorphic-function-values-1/
http://www.chuusai.com/2012/05/10/shapeless-polymorphic-function-values-2/

Is my understanding of below scala code correct?

I'm just trying to understand the below code :
Here a new type alias Set is declared which is a function that takes an Int
parameter and returns a boolean
type Set = Int => Boolean
Here a new method 'contains' is declared, which takes two parameters of type Set and Int
which returns a boolean. The boolean is set to the function declared in earlier
('type Set = Int => Boolean')
But what logic is performed to determine if the Int 'elem' is a member of Set 's'
def contains(set: Set, elem: Int): Boolean = set(elem)
Here a method is defined which returns a set which returns a function ?
def singletonSet(elem: Int): Set = set => set == elem
Complete code with comments :
/**
* We represent a set by its characteristic function, i.e.
* its `contains` predicate.
*/
type Set = Int => Boolean
/**
* Indicates whether a set contains a given element.
*/
def contains(set: Set, elem: Int): Boolean = set(elem)
/**
* Returns the set of the one given element.
*/
def singletonSet(elem: Int): Set = set => set == elem
Let's read sort of backwards, in logical order.
Say you have a finite set of integers: 0, 1, 2, 3, 5, 8 for instance
One way to describe this set of integers is through a function (its characteristic or indicator function) that, for each integer, returns true if the integer is in the set, false if it is not.
The signature for this function, as we described it, must always be Int => Boolean ("give me an integer, I will tell you if it's in the set"), while its implementation will vary depending on the specific set.
For the set in my example above you could write this function simply as:
val mySet: Int => Boolean = x => Array(0,1,2,3,5,8) contains x
or recognize that the ints in the set are the first ones of the Fibonacci sequence and define f in a slightly more sophisticated way (which I won't do here...).
Note that the "contains" I've used is defined for all scala collections.
In any case, now you have a function that tells you what is in the set and what is not.
Let's try it in the REPL.
scala> val mySet: Int => Boolean = x => Array(0,1,2,3,5,8) contains x
mySet: Int => Boolean = <function1>
scala> mySet(3)
res0: Boolean = true
scala> mySet(9)
res1: Boolean = false
Now, mySet has type Int => Boolean, which we can make more readable if we define it as a type alias.
scala> type Set = Int => Boolean
defined type alias Set
Besides readability, defining Set as an alias of Int => Boolean is making it explicit that in a way a Set is its characteristic function. We can redefine mySet in a more concise (but otherwise equivalent) way with the Set type alias:
scala> val mySet: Set = x => Array(0,1,2,3,5,8) contains x
mySet: Int => Boolean = <function1>
Now for the last piece of this long answer. Let's define a characteristic function to describe this Singleton set: 3.
Easy:
val Singleton3 : Set = set => set == 3
for a Singleton set containing only 4, it would be:
val Singleton4 : Set = set => set == 4
So, let's generalize the creation of these functions and write a method that returns a Singleton function that, for any integer, describes the set containing only that integer:
def singletonSet(elem: Int): Set = set => set == elem
APPENDIX:
I skipped this part, because it wasn't really needed: def contains(set: Set, elem: Int): Boolean = set(elem)
I think it's sort of pointless and (without more context) it looks just like a contrived example to demonstrate how you can pass a function around as an argument, just like any other type in scala. It takes the Int => Bool function and the Int and just applies the function to the Int so you can do
scala> contains(mySet, 3)
res2: Boolean = true
which is like calling mySet(3) directly.
After watching the lecture video on "Currying", I believe that Paolo's solution expressed in a more verbose manner is :
def singletonSet(elem: Int): Set = {
def innerFunction (givenElement: Int) =
if (elem == givenElement) true
else false
innerFunction
}
Plesae correct me if I am wrong!
To answer your question - But what logic is performed to determine if the Int 'elem' is a member of Set 's'
This is performed when you make the actual function call. Consider the following function call.
contains(singletonSet(1), 1)
Now singletonSet is defined as def singletonSet(elem: Int): Set = x => x == elem (I choose to use the identifier x for clarity sake). The return type of the singletonSet is the function of type Set which takes an Int argument and returns Boolean. So the above calling function's first argument singletonSet(1) equates to the function x => x == 1 as elem here is 1. So we get
contains((x => x == 1),1)
Now considering the definition of contains function def contains(f: Set, elem: Int): Boolean = f(elem). The first argument in the call above is the function x => x == 1 which substitutes formal parameter f and second argument 1 substitutes formal parameter elem. The return value of contains is the function f(elem) which equates to f(1). Since f(x) is defined as (x == 1), f(1) equates to (1 == 1) which returns true.
Going by the same logic, a function call like contains(singletonSet(1), 2) would finally equate to (1 == 2) which will return false.
I'm taking the course now, was also confused, but I think I get the point now.
def singletonSet(elem: Int): Set = (x : Int) => x == elem
here singletonSet is a function that returns a function with the type Set, which is defined as type Set = Int => Boolean
so when you call def contains(s:Set, elem:Int): Boolean = s(elem), for example: contains(singletonSet(1), 2), singletonSet(1) is returning a function with elem(in the definition of singletonSet) set to 1, and the 2(also defined as elem, but is defined in the parameter of contains) is passed in as the x in singletonSet definition, we need to get rid of java set idea, we don't need the singletonSet to persist the value we set.
For better understanding, we can change the parameter name as following:
def singletonSet(elemToStore: Int): Set = (x : Int) => x == elemToStore
def contains(s: Set, elemToCheck: Int): Boolean = s(elemToCheck)