- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Functional programming question
Wed, 2011-11-23, 16:35
Hello,
I figured the Scala group is a good place for a FP question.
I understand the concept of functions: immutable values, inputs &
output, and no side effects.
My question is: what about storage (or IO in general)? Isn't a
function that stores, and particularly updates, committing the sin of
performing a side effect? Is IO considered a necessary exception or
is there another way of classifying it?
Thanks for your thoughts
-gz
Wed, 2011-11-23, 17:07
#2
Re: Functional programming question
Hello,
This is a very legitimate question, that is quite old and had at least
4 answers:
1. Allow to have effect in any function. This is the choice of, for
example, ML, all lisps, and Scala.
In particular, in Scala, you can have part of a program that are
purely functional and other that are effectfu.
2. Effect are forbidden but the language designer creates a small
library of functions allowing to build effectful computations.
This allow an effectless program to build an effectful computation
that is executed by the top-level.
Two examples come to mind:
- the IO monad in the Haskell language,
- In Miranda (less modern language), build a pure function from an
input Stream to an output Stream.
Monads are in fact more general than IO and can protect/represent many
type of effectful computations. (reference / exception /
non-termination ...)
You can use this monadic style in Scala. The scalaz library implements
a lot of helper for that.
3. Allow effect everywhere but express them in your types. A function
Int => Int is a pure function, but a function Int => Int ! IO is a
function from Int to Int that may
do some IO. You can then restrict where effectful functions can be called.
4 Have a World variable to represent the outside World. An effectful
is a function modifying the world. Have your type system ensure that
you can not copy the world.
The language Concurrent Clean does that.
1 and the monad part of 2 are currently widely used. 3 is gaining some momentum.
Hope that helps,
Best regards,
Nicolas.
Wed, 2011-11-23, 17:27
#3
Re: Functional programming question
Yes, this is a great explanation, thanks!
I'll have to try a couple of these and see which one fits my needs
best.
-gz
On Nov 23, 9:58 am, "nicolas.o...@gmail.com"
wrote:
> Hello,
>
> This is a very legitimate question, that is quite old and had at least
> 4 answers:
>
> 1. Allow to have effect in any function. This is the choice of, for
> example, ML, all lisps, and Scala.
> In particular, in Scala, you can have part of a program that are
> purely functional and other that are effectfu.
>
> 2. Effect are forbidden but the language designer creates a small
> library of functions allowing to build effectful computations.
> This allow an effectless program to build an effectful computation
> that is executed by the top-level.
> Two examples come to mind:
> - the IO monad in the Haskell language,
> - In Miranda (less modern language), build a pure function from an
> input Stream to an output Stream.
>
> Monads are in fact more general than IO and can protect/represent many
> type of effectful computations. (reference / exception /
> non-termination ...)
> You can use this monadic style in Scala. The scalaz library implements
> a lot of helper for that.
>
> 3. Allow effect everywhere but express them in your types. A function
> Int => Int is a pure function, but a function Int => Int ! IO is a
> function from Int to Int that may
> do some IO. You can then restrict where effectful functions can be called.
>
> 4 Have a World variable to represent the outside World. An effectful
> is a function modifying the world. Have your type system ensure that
> you can not copy the world.
> The language Concurrent Clean does that.
>
> 1 and the monad part of 2 are currently widely used. 3 is gaining some momentum.
>
> Hope that helps,
> Best regards,
>
> Nicolas.
Sat, 2011-11-26, 07:57
#4
Re: Functional programming question
See, as Erik Meijer said, every language actually has an implicit monad. Monads are the tricks that allow ust to break out of so-called "pure" functions. Being "pure" is a relative notion; pure meaning using morphisms in a certain category; one category may be a subcategory of another, with a richer collection of morphisms (say, multi-valued, partial etc).
A monad naturally arises when there is a certain correspondence between a bigger and a smaller categories.
I may be wrong; does anybody know if there is some kind of "absolute purity"?
Thanks,
-Vlad
On Wed, Nov 23, 2011 at 7:35 AM, Greg Zoller <gzoller@gmail.com> wrote:
A monad naturally arises when there is a certain correspondence between a bigger and a smaller categories.
I may be wrong; does anybody know if there is some kind of "absolute purity"?
Thanks,
-Vlad
On Wed, Nov 23, 2011 at 7:35 AM, Greg Zoller <gzoller@gmail.com> wrote:
Hello,
I figured the Scala group is a good place for a FP question.
I understand the concept of functions: immutable values, inputs &
output, and no side effects.
My question is: what about storage (or IO in general)? Isn't a
function that stores, and particularly updates, committing the sin of
performing a side effect? Is IO considered a necessary exception or
is there another way of classifying it?
Thanks for your thoughts
-gz
Sun, 2011-11-27, 05:27
#5
Re: Functional programming question
hrm, oof, ugh, i guess one can talk about changing state in a
functional way w/out having to start with monads. "threading the
world" is a fine place to start, no?
(i mean, look at Racket worlds, even if that isn't pure enough, i
think that kind of thing isn't pure enough for all.
http://download.plt-scheme.org/doc/4.2.5/html/teachpack/2htdpuniverse.html)
On Fri, Nov 25, 2011 at 10:53 PM, Vlad Patryshev wrote:
> See, as Erik Meijer said, every language actually has an implicit monad.
> Monads are the tricks that allow ust to break out of so-called "pure"
> functions. Being "pure" is a relative notion; pure meaning using morphisms
> in a certain category; one category may be a subcategory of another, with a
> richer collection of morphisms (say, multi-valued, partial etc).
>
> A monad naturally arises when there is a certain correspondence between a
> bigger and a smaller categories.
>
> I may be wrong; does anybody know if there is some kind of "absolute
> purity"?
>
> Thanks,
> -Vlad
>
>
> On Wed, Nov 23, 2011 at 7:35 AM, Greg Zoller wrote:
>>
>> Hello,
>>
>> I figured the Scala group is a good place for a FP question.
>>
>> I understand the concept of functions: immutable values, inputs &
>> output, and no side effects.
>>
>> My question is: what about storage (or IO in general)? Isn't a
>> function that stores, and particularly updates, committing the sin of
>> performing a side effect? Is IO considered a necessary exception or
>> is there another way of classifying it?
>>
>> Thanks for your thoughts
>> -gz
>
>
Sun, 2011-11-27, 11:37
#6
Re: Functional programming question
Isn't a problem with explicit world passing that you can have a
reference to the state of the "world" at two different points in time?
def doComplexIO(world0:World) : World {
val world1 = doIO(world0)
val world2 = doIO(world0) // typo. this should be world1.
world2
}
There is a programming language called clean that has special
"uniqueness type" support for this scenario. In the above example, it
would be impossible to refer to world0 after calling a doIO once with
it.
But I don't think it is possible to emulate this in scala without a
compiler plugin.
http://en.wikipedia.org/wiki/Uniqueness_type
On Sun, Nov 27, 2011 at 5:19 AM, Raoul Duke wrote:
> hrm, oof, ugh, i guess one can talk about changing state in a
> functional way w/out having to start with monads. "threading the
> world" is a fine place to start, no?
>
> (i mean, look at Racket worlds, even if that isn't pure enough, i
> think that kind of thing isn't pure enough for all.
> http://download.plt-scheme.org/doc/4.2.5/html/teachpack/2htdpuniverse.html)
>
> On Fri, Nov 25, 2011 at 10:53 PM, Vlad Patryshev wrote:
>> See, as Erik Meijer said, every language actually has an implicit monad.
>> Monads are the tricks that allow ust to break out of so-called "pure"
>> functions. Being "pure" is a relative notion; pure meaning using morphisms
>> in a certain category; one category may be a subcategory of another, with a
>> richer collection of morphisms (say, multi-valued, partial etc).
>>
>> A monad naturally arises when there is a certain correspondence between a
>> bigger and a smaller categories.
>>
>> I may be wrong; does anybody know if there is some kind of "absolute
>> purity"?
>>
>> Thanks,
>> -Vlad
>>
>>
>> On Wed, Nov 23, 2011 at 7:35 AM, Greg Zoller wrote:
>>>
>>> Hello,
>>>
>>> I figured the Scala group is a good place for a FP question.
>>>
>>> I understand the concept of functions: immutable values, inputs &
>>> output, and no side effects.
>>>
>>> My question is: what about storage (or IO in general)? Isn't a
>>> function that stores, and particularly updates, committing the sin of
>>> performing a side effect? Is IO considered a necessary exception or
>>> is there another way of classifying it?
>>>
>>> Thanks for your thoughts
>>> -gz
>>
>>
>
Mon, 2011-11-28, 01:07
#7
Re: Functional programming question
On Sun, Nov 27, 2011 at 2:36 AM, Rüdiger Klaehn wrote:
> Isn't a problem with explicit world passing that you can have a
> reference to the state of the "world" at two different points in time?
good point, there are problems like that with threading the world. my
thought was more about how to think about the basic steps one would
take to get state changes happening in a functional mellieu, without
having to first go rampaging towards the term 'monad' since i think
that is too big a first jump. once the world threading is something
people can agree upon, then yes i see how one has to deal with such
issues and that leads to discussions about how monads help, uniqueness
typing helps, stm helps, etc.
thanks.
short answer:
val newdatabase = update(olddatabase, modification)
there is io (of course), but it doesn't have to break the functional programming.
-------- Original-Nachricht --------
> Datum: Wed, 23 Nov 2011 07:35:33 -0800 (PST)
> Von: Greg Zoller
> An: scala-user
> Betreff: [scala-user] Functional programming question
> Hello,
>
> I figured the Scala group is a good place for a FP question.
>
> I understand the concept of functions: immutable values, inputs &
> output, and no side effects.
>
> My question is: what about storage (or IO in general)? Isn't a
> function that stores, and particularly updates, committing the sin of
> performing a side effect? Is IO considered a necessary exception or
> is there another way of classifying it?
>
> Thanks for your thoughts
> -gz