Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Num instnace for [Double] (Nathan H?sken)
2. Re: Num instnace for [Double] (Ben Gamari)
3. Re: confusing type signature with sections (Karl Voelker)
4. Re: Num instnace for [Double] (Nathan H?sken)
5. My own State Monad and State Arrow (Thiago Negri)
----------------------------------------------------------------------
Message: 1
Date: Wed, 02 Oct 2013 14:47:10 +0200
From: Nathan H?sken <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Num instnace for [Double]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Hey,
I am working a lot with timeseries (with instances at discrete moments)
of doubles.
So I use [Double].
Now I want to be able to do stuff like
timeSeries3 = timeSeries1 + timeSeries2
So I was thinking, I create a newtype
newtype TimeSeries a = TimeSeries [a]
Now, can I somehow autoderive List, Monad, MonadPlus for TimeSeries?
Also I would like to derive from Num. Most of the things can be done
pointwise!
What about fromInteger??? Should that just be a list with one element?
Or does it simple not make sense?
Thanks!
Nathan
------------------------------
Message: 2
Date: Wed, 02 Oct 2013 12:27:32 -0400
From: Ben Gamari <[email protected]>
To: Nathan H?sken <[email protected]>, The Haskell-Beginners
Mailing List - Discussion of primarily beginner-level topics related
to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Num instnace for [Double]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Nathan H?sken <[email protected]> writes:
> Hey,
>
> I am working a lot with timeseries (with instances at discrete moments)
> of doubles.
> So I use [Double].
>
You may want to consider using a more efficient type than [Double] if
your data is at all large. Unless you really want the laziness that
lists offer you might be better off using vectors.
> Now I want to be able to do stuff like
>
> timeSeries3 = timeSeries1 + timeSeries2
>
> So I was thinking, I create a newtype
>
> newtype TimeSeries a = TimeSeries [a]
>
> Now, can I somehow autoderive List, Monad, MonadPlus for TimeSeries?
>
As far as I'm aware, List is not a typeclass. You can conveniently derive
newtype instances with GeneralizedNewtypeDeriving[1] in most cases. That
being said you need to decide what sort of semantics you want your type
to have. In the case of Appplicative, you have a choice between ZipList
(which would make point-wise operations straightforward) and standard
list (making non-determinism straightforward).
Applicative is tricky in the case of vector-like types as you
don't know what length you want your timeseries to be. In the
list case you can simply produce a non-terminating series. One way
around this (although perhaps not the best way) would be to introduce a
constructor to represent a homogenous timeseries. This might look
something like,
import qualified Data.Vector as V
import Control.Applicative
data TimeSeries a = Pure a
| Moments (V.Vector a)
deriving (Show)
instance Functor TimeSeries where
fmap f (Pure a) = Pure (f a)
fmap f (Moments as) = Moments (fmap f as)
instance Applicative TimeSeries where
pure = Pure
Pure a <*> Pure b = Pure (a b)
Pure a <*> Moments bs = Moments $ fmap a bs
Moments as <*> Pure b = Moments $ fmap ($ b) as
Moments as <*> Moments bs = Moments $ V.zipWith ($) as bs
fromList :: [a] -> TimeSeries a
fromList = Moments . V.fromList
times :: TimeSeries Int
times = fromList [1,5,9,2,5]
main = do
print $ (+) <$> pure 4 <*> times
Of course, this presents the possibility of trying to perform a
point-wise operation on differently sizes series. Zip-like functions
will typically truncate their output to the shortest of their arguments,
opening the possibility for silent data loss. One (hacky) way around
this would be to accept a partial Applicative instance, returning bottom
in the event of incompatible series.
Another approach would be to parametrize the type on the length of the
series with type-level naturals[3]. This still leaves open the
possibility of performing actions on series of incompatible stride or
offset, but at least you can be certain your series are of compatible
shapes.
> Also I would like to derive from Num. Most of the things can be done
> pointwise!
>
> What about fromInteger??? Should that just be a list with one element?
> Or does it simple not make sense?
I probably wouldn't use the Num typeclass here for this reason, among
others. If you give you type the right Applicative instance the
operations you are after should be easily performed.
Cheers,
- Ben
[1] http://www.haskell.org/ghc/docs/7.4.1/html/users_guide/deriving.html
[2] http://hackage.haskell.org/package/linear
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 489 bytes
Desc: not available
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20131002/ffe7f8af/attachment-0001.sig>
------------------------------
Message: 3
Date: Wed, 2 Oct 2013 10:25:23 -0700
From: Karl Voelker <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] confusing type signature with
sections
Message-ID:
<caffow0xfkvzq9ss6+zjo5vblvkdoeuujj7c0lozqgozuk7j...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
On Tue, Oct 1, 2013 at 5:25 PM, Patrick Redmond <[email protected]> wrote:
> Prelude> :t ((+ 1) / 2)
> ((+ 1) / 2) :: (Fractional (a -> a), Num a) => a -> a
>
The key is that typeclasses are open. You could write a Fractional instance
for (a -> a), in which case it would be possible to do _something_ with
this code. Would it be useful? Even Haskell can't guarantee that.
-Karl
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20131002/669d9aee/attachment-0001.html>
------------------------------
Message: 4
Date: Wed, 02 Oct 2013 21:08:13 +0200
From: Nathan H?sken <[email protected]>
To: Ben Gamari <[email protected]>
Cc: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Num instnace for [Double]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
Hey
Thanks for the answer one thing to discuss remains for me:
With applicative I could add TimeSeries like this:
print $ (+) <$> pure 4 <*> times
(+) <$> times1 <*> times2
ok .. but actually I find that (for this case) clumpsy.
I would much prefer to write times1 + times2 (especially in complex cases):
(times1 + times2)/(times3 * times4)
Don't you agree? Or do I oversee something?
Thanks!
Nathan
Am 10/2/2013 6:27 PM, schrieb Ben Gamari:
> Nathan H?sken <[email protected]> writes:
>
>> Hey,
>>
>> I am working a lot with timeseries (with instances at discrete moments)
>> of doubles.
>> So I use [Double].
>>
> You may want to consider using a more efficient type than [Double] if
> your data is at all large. Unless you really want the laziness that
> lists offer you might be better off using vectors.
>
>> Now I want to be able to do stuff like
>>
>> timeSeries3 = timeSeries1 + timeSeries2
>>
>> So I was thinking, I create a newtype
>>
>> newtype TimeSeries a = TimeSeries [a]
>>
>> Now, can I somehow autoderive List, Monad, MonadPlus for TimeSeries?
>>
> As far as I'm aware, List is not a typeclass. You can conveniently derive
> newtype instances with GeneralizedNewtypeDeriving[1] in most cases. That
> being said you need to decide what sort of semantics you want your type
> to have. In the case of Appplicative, you have a choice between ZipList
> (which would make point-wise operations straightforward) and standard
> list (making non-determinism straightforward).
>
> Applicative is tricky in the case of vector-like types as you
> don't know what length you want your timeseries to be. In the
> list case you can simply produce a non-terminating series. One way
> around this (although perhaps not the best way) would be to introduce a
> constructor to represent a homogenous timeseries. This might look
> something like,
>
> import qualified Data.Vector as V
> import Control.Applicative
>
> data TimeSeries a = Pure a
> | Moments (V.Vector a)
> deriving (Show)
>
> instance Functor TimeSeries where
> fmap f (Pure a) = Pure (f a)
> fmap f (Moments as) = Moments (fmap f as)
>
> instance Applicative TimeSeries where
> pure = Pure
> Pure a <*> Pure b = Pure (a b)
> Pure a <*> Moments bs = Moments $ fmap a bs
> Moments as <*> Pure b = Moments $ fmap ($ b) as
> Moments as <*> Moments bs = Moments $ V.zipWith ($) as bs
>
> fromList :: [a] -> TimeSeries a
> fromList = Moments . V.fromList
>
> times :: TimeSeries Int
> times = fromList [1,5,9,2,5]
>
> main = do
> print $ (+) <$> pure 4 <*> times
>
> Of course, this presents the possibility of trying to perform a
> point-wise operation on differently sizes series. Zip-like functions
> will typically truncate their output to the shortest of their arguments,
> opening the possibility for silent data loss. One (hacky) way around
> this would be to accept a partial Applicative instance, returning bottom
> in the event of incompatible series.
>
> Another approach would be to parametrize the type on the length of the
> series with type-level naturals[3]. This still leaves open the
> possibility of performing actions on series of incompatible stride or
> offset, but at least you can be certain your series are of compatible
> shapes.
>
>> Also I would like to derive from Num. Most of the things can be done
>> pointwise!
>>
>> What about fromInteger??? Should that just be a list with one element?
>> Or does it simple not make sense?
> I probably wouldn't use the Num typeclass here for this reason, among
> others. If you give you type the right Applicative instance the
> operations you are after should be easily performed.
>
> Cheers,
>
> - Ben
>
>
> [1] http://www.haskell.org/ghc/docs/7.4.1/html/users_guide/deriving.html
> [2] http://hackage.haskell.org/package/linear
>
------------------------------
Message: 5
Date: Wed, 2 Oct 2013 22:34:23 -0300
From: Thiago Negri <[email protected]>
To: Haskell Beginners <[email protected]>
Subject: [Haskell-beginners] My own State Monad and State Arrow
Message-ID:
<cablnezuau2inu7rbykycwxuoc6yt3x6zmszfirw-zumpksh...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
I think I did understand Monads and Arrows.
So I put it to the test: I tried to write my own State Monad and an State
Arrow without looking at the real implementation.
This is what I got: http://lpaste.net/93801
Please take a look on line 146 where I try to define a State Monad that is
represented with a full function (state+result -to- state+result -- like my
Arrow implementation).
Things got hairy with the bind operator (line 153) and I just hit the
keyboard until I hit the correct types and got it to compile.
Yet, I can't fully understand my own bind implementation.
Using the result "a" twice seems to be wrong, but I couldn't write a test
to prove it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20131002/69277cc6/attachment-0001.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 64, Issue 6
****************************************