Re: [Haskell-cafe] ANNOUNCE: TKYProf

2011-08-16 Thread Michael Snoyman
The blaze-textual issue is intentional: There's a bug in GHC that
prevents C++ code from working correctly with Template Haskell, and
newer versions of blaze-textual use a C++ library for parsing doubles.
I'm hoping that the defaults change on blaze-textual to use the native
code instead so that I can relax this dependency.

On Wed, Aug 17, 2011 at 7:31 AM, Sebastian Fischer  wrote:
>> I'm glad to announce the alpha release of TKYProf.
>
> This looks useful, thanks! I'll try it out and let you know if I have
> problems.
> Installing with GHC 7.2, I needed to relax some upper bounds in cabal files
> of dependencies (maintainers CC'ed).
>   - email-validate and ranges specify base < 4.4 but also seem to work with
> base < 5.
>   - yesod-json specifies blaze-textual < 0.2 but also seems to work with
> blaze-textual < 0.3
> Additionally, I linked /usr/lib/libstdc++.so.6 to /usr/lib/libstdc++.so
> before I could successfully install tkyprof. Not sure about the
> consequences..
> Cheers,
> Sebastian
>>
>>  http://hackage.haskell.org/package/tkyprof
>>  https://github.com/maoe/tkyprof
>>
>> TKYprof is a web-based interacitve visualizer for GHC time and allocation
>> profiling reports. It helps you to find the bottlenecks in your code
>> quickly!
>>
>> Here is a blog post:
>>
>>   http://blog.foldr.in/tkyprof-a-web-based-interactive-visualizer-fo
>>
>> It is still alpha and it have some bugs. I'm happy to hear your feedback.
>>
>> Thanks,
>>
>> --
>> Mitsutoshi Aoe
>> m...@foldr.in
>>
>>
>>
>>
>>
>> ___
>> Haskell-Cafe mailing list
>> Haskell-Cafe@haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] ANNOUNCE: TKYProf

2011-08-16 Thread Sebastian Fischer
>
> I'm glad to announce the alpha release of TKYProf.
>

This looks useful, thanks! I'll try it out and let you know if I have
problems.

Installing with GHC 7.2, I needed to relax some upper bounds in cabal files
of dependencies (maintainers CC'ed).

  - email-validate and ranges specify base < 4.4 but also seem to work with
base < 5.
  - yesod-json specifies blaze-textual < 0.2 but also seems to work with
blaze-textual < 0.3

Additionally, I linked /usr/lib/libstdc++.so.6 to /usr/lib/libstdc++.so
before I could successfully install tkyprof. Not sure about the
consequences..

Cheers,
Sebastian

 http://hackage.haskell.org/package/tkyprof
>  https://github.com/maoe/tkyprof
>
> TKYprof is a web-based interacitve visualizer for GHC time and allocation
> profiling reports. It helps you to find the bottlenecks in your code
> quickly!
>
> Here is a blog post:
>
>   http://blog.foldr.in/tkyprof-a-web-based-interactive-visualizer-fo
>
> It is still alpha and it have some bugs. I'm happy to hear your feedback.
>
> Thanks,
>
> --
> Mitsutoshi Aoe
> m...@foldr.in
>
>
>
>
>
> ___
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] ANNOUNCE: TKYProf

2011-08-16 Thread Mitsutoshi Aoe
Hi, forks

I'm glad to announce the alpha release of TKYProf.

  http://hackage.haskell.org/package/tkyprof
  https://github.com/maoe/tkyprof

TKYprof is a web-based interacitve visualizer for GHC time and allocation
profiling reports. It helps you to find the bottlenecks in your code quickly!

Here is a blog post:

   http://blog.foldr.in/tkyprof-a-web-based-interactive-visualizer-fo

It is still alpha and it have some bugs. I'm happy to hear your feedback.

Thanks,

-- 
Mitsutoshi Aoe
m...@foldr.in





___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] why is Random in System?

2011-08-16 Thread Niklas Larsson
I don't like the idea of Data.Random because random numbers use
ordinary number types, and the generator itself is not the object of
interest, the numbers are. I'd much prefer Math.Random. As the Math
prefix isn't used in the core libraries maybe Control.Random is the
least unpalatable alternative.

Regards,
Niklas

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] why is Random in System?

2011-08-16 Thread Evan Laforge
Yeah, fair enough about getting the seed.  I think I like the idea of
breaking them into System.Entropy and then Random or Data.Random.  It
feels odd to stick pure algorithm packages, which simply accept a
random seed or stream from elsewhere, under System.Random.

There are a fair number of alternate implementations on hackage which
suggests people aren't satisfied with the stdlib one but haven't quite
settled down on a standard alternative.

James Cook, good point about Data.  I suppose that's also why it seems
to be the catch all for everything in haskell.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] why is Random in System?

2011-08-16 Thread Brandon Allbery
On Tue, Aug 16, 2011 at 17:07, James Cook  wrote:

> On Aug 16, 2011, at 4:04 PM, Evan Laforge wrote:
> > I've noticed there's a convention to put modules having to deal with
> > randomness into System.Random.  I thought System was for OS
> > interaction?  Granted getting a random seed usually means going to the
> > OS, but isn't the rest of it, like generating random sequences,
> > distributions, selecting based on probability, shuffling, etc. all
> > non-OS related algorithms?
>
> System definitely does seem like an odd choice.  In most cases the only
> interaction any PRNG, even when accessed via the FFI, has with the "system"
> is - as you say - to get an initial seed value for a global instance.
>

I'd be tempted to guess that the whole reason it's under System is the IO
component.

-- 
brandon s allbery  allber...@gmail.com
wandering unix systems administrator (available) (412) 475-9364 vm/sms
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] why is Random in System?

2011-08-16 Thread James Cook
On Aug 16, 2011, at 4:04 PM, Evan Laforge wrote:

> I've noticed there's a convention to put modules having to deal with
> randomness into System.Random.  I thought System was for OS
> interaction?  Granted getting a random seed usually means going to the
> OS, but isn't the rest of it, like generating random sequences,
> distributions, selecting based on probability, shuffling, etc. all
> non-OS related algorithms?
> 
> I'm not sure where I would expect Random to go, perhaps Math or maybe
> the toplevel, but under System seems, well, random...
> 
> I notice random-fu puts it under Data, which is also not where I'd
> look, except that you always look in Data because everything goes into
> Data... but algorithms dealing with random numbers aren't really data
> structures either, are they?

System definitely does seem like an odd choice.  In most cases the only 
interaction any PRNG, even when accessed via the FFI, has with the "system" is 
- as you say - to get an initial seed value for a global instance.

When I wrote random-fu I chose to use Data.Random based on the perspective is 
that a random variable or process _is_ just a mathematical object, and can be 
represented by an abstract data structure.  I'm sure there's a case to be made 
against that view too, and if someone were to present a good argument for 
something better I'd even consider changing it. It seems to me, though, that 
the line between data and not-data is pretty fuzzy in a functional language 
(which is one of the many things that makes them great), and for me it seems 
quite natural to think of random variables as data.  At least, in practice it 
"feels" a lot more like manipulating data than anything else.  But then I'm one 
of those weirdos who thinks of "IO t" as "just a data structure" too.

-- James
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] why is Random in System?

2011-08-16 Thread Thomas DuBuisson
I think of it as natural for exactly the reason you stated (the data
comes from the OS).  It seems even more natural to me in the entropy
package module 'System.Entropy' as I am accustom to the phrase system
entropy.  Equally, I would fine a 'Network.Entropy' module acceptable
under the assumption it connects to one of the public random number
servers for it's data.

Perhaps a top level "Random." should be used, but that too can be
questioned.  For example, when I import the module "Random" or perhaps
"Random.Generators" would I get fast prngs?  Cryptographic prngs?
Both?  Something else (both slow and weak, like what we have now ;-)
)?

Cheers,
Thomas

On Tue, Aug 16, 2011 at 1:04 PM, Evan Laforge  wrote:
> I've noticed there's a convention to put modules having to deal with
> randomness into System.Random.  I thought System was for OS
> interaction?  Granted getting a random seed usually means going to the
> OS, but isn't the rest of it, like generating random sequences,
> distributions, selecting based on probability, shuffling, etc. all
> non-OS related algorithms?
>
> I'm not sure where I would expect Random to go, perhaps Math or maybe
> the toplevel, but under System seems, well, random...
>
> I notice random-fu puts it under Data, which is also not where I'd
> look, except that you always look in Data because everything goes into
> Data... but algorithms dealing with random numbers aren't really data
> structures either, are they?
>
> ___
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] why is Random in System?

2011-08-16 Thread Evan Laforge
I've noticed there's a convention to put modules having to deal with
randomness into System.Random.  I thought System was for OS
interaction?  Granted getting a random seed usually means going to the
OS, but isn't the rest of it, like generating random sequences,
distributions, selecting based on probability, shuffling, etc. all
non-OS related algorithms?

I'm not sure where I would expect Random to go, perhaps Math or maybe
the toplevel, but under System seems, well, random...

I notice random-fu puts it under Data, which is also not where I'd
look, except that you always look in Data because everything goes into
Data... but algorithms dealing with random numbers aren't really data
structures either, are they?

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Getting segmentation fault when profiling, despite `-K100M'.

2011-08-16 Thread Christopher Wilson
On Tue, Aug 16, 2011 at 12:42 PM, Niklas Larsson  wrote:
> If you want to call a Haskell function from C you should do a "foreign
> export" of the function, that will create a stub function with C
> calling convention that you can call.


I put an example of how to do this on Rosetta Code:

http://rosettacode.org/wiki/Use_another_language_to_call_a_function#Haskell

-- 
Chris Wilson 

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Getting segmentation fault when profiling, despite `-K100M'.

2011-08-16 Thread Niklas Larsson
Reading a bit closer I assume you have already done that. Sorry for that.

Regards,
Niklas

2011/8/16 Niklas Larsson :
> If you want to call a Haskell function from C you should do a "foreign
> export" of the function, that will create a stub function with C
> calling convention that you can call.
>
> Regards,
> Niklas
>
> 2011/8/16 David Banas :
>> Hi all,
>>
>> I'm trying to profile a mixed language program, in which Haskell is NOT
>> the top layer and does not contain the `main' function. (C is/does.)
>>
>> Is this effort doomed to fail?
>>
>> I'm getting a segmentation fault, as soon as the first Haskell function
>> is called, despite having included `-K100M' in my list of +RTS args:
>>
>>  19     int argc = 5;
>>  20     char* argv[] = {"ghcDll",
>>  21                     "+RTS",
>>  22                     "-hc",
>>  23                     "-p",
>>  24                     "-K100M",
>>  25                     NULL}; // argv must end with NULL
>>  26
>>  27     // Initialize Haskell runtime
>>  28     char** args = argv;
>>  29     hs_init(&argc, &args);
>>
>> Using gdb to trace through the C-code, up until the first call to
>> Haskell, I get to here:
>>
>>  64     // Call the Haskell function.
>>  65     res = amiInit(
>>  66         impulse_matrix,
>>  67         row_size,
>>
>> Here's the beginning of `amiInit':
>>
>>  41 -- Our Haskell implementation of `AMI_Init'.
>>  42 amiInit :: Ptr CDouble -> CInt -> CInt -> CDouble -> CDouble ->
>>  43            CString -> Ptr CString -> Ptr (StablePtr AmiModel) -> Ptr
>> CString -> IO Int
>>  44 amiInit impulse_matrix row_size aggressors sample_interval bit_time
>>  45         ami_parameters_in ami_parameters_out ami_memory_handle
>> msgHndl
>>  46     | impulse_matrix == nullPtr = return 0
>>  47     | otherwise = do
>>  48         putStrLn "I'm here."
>>
>> I never see "I'm here." printed to my console, before I get the
>> segmentation fault.
>>
>> Any thoughts?
>>
>> Thanks,
>> -db
>>
>>
>>
>> ___
>> Haskell-Cafe mailing list
>> Haskell-Cafe@haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Getting segmentation fault when profiling, despite `-K100M'.

2011-08-16 Thread Niklas Larsson
If you want to call a Haskell function from C you should do a "foreign
export" of the function, that will create a stub function with C
calling convention that you can call.

Regards,
Niklas

2011/8/16 David Banas :
> Hi all,
>
> I'm trying to profile a mixed language program, in which Haskell is NOT
> the top layer and does not contain the `main' function. (C is/does.)
>
> Is this effort doomed to fail?
>
> I'm getting a segmentation fault, as soon as the first Haskell function
> is called, despite having included `-K100M' in my list of +RTS args:
>
>  19     int argc = 5;
>  20     char* argv[] = {"ghcDll",
>  21                     "+RTS",
>  22                     "-hc",
>  23                     "-p",
>  24                     "-K100M",
>  25                     NULL}; // argv must end with NULL
>  26
>  27     // Initialize Haskell runtime
>  28     char** args = argv;
>  29     hs_init(&argc, &args);
>
> Using gdb to trace through the C-code, up until the first call to
> Haskell, I get to here:
>
>  64     // Call the Haskell function.
>  65     res = amiInit(
>  66         impulse_matrix,
>  67         row_size,
>
> Here's the beginning of `amiInit':
>
>  41 -- Our Haskell implementation of `AMI_Init'.
>  42 amiInit :: Ptr CDouble -> CInt -> CInt -> CDouble -> CDouble ->
>  43            CString -> Ptr CString -> Ptr (StablePtr AmiModel) -> Ptr
> CString -> IO Int
>  44 amiInit impulse_matrix row_size aggressors sample_interval bit_time
>  45         ami_parameters_in ami_parameters_out ami_memory_handle
> msgHndl
>  46     | impulse_matrix == nullPtr = return 0
>  47     | otherwise = do
>  48         putStrLn "I'm here."
>
> I never see "I'm here." printed to my console, before I get the
> segmentation fault.
>
> Any thoughts?
>
> Thanks,
> -db
>
>
>
> ___
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Memory management issue in notmuch-haskell bindings

2011-08-16 Thread Ben Gamari
It seems that the notmuch-haskell bindings (version 0.2.2 built against
notmuch from git master; passes notmuch-test) aren't dealing with memory
management properly. In particular, the attached test code[1] causes
talloc to abort.  Unfortunately, while the issue is consistently
reproducible, it only occurs with some queries (see source[1]). I have
been unable to establish the exact criterion for failure.

It seems that the crash is caused by an invalid access to a freed Query
object while freeing a Messages object (see Valgrind trace[3]). I've
taken a brief look at the bindings themselves but, being only minimally
familiar with the FFI, there's nothing obviously wrong (the finalizers
passed to newForeignPtr look sane). I was under the impression that
talloc was reference counted, so the Query object shouldn't have been
freed unless if there was still a Messages object holding a
reference. Any idea what might have gone wrong here?  Thanks!

Cheers,

- Ben



[1] Test case,

import Data.List
import Control.Monad
import System.Environment
import Foreign.Notmuch

dbpath = "/home/ben/.mail"

getAddresses :: Database -> String -> IO [String]
getAddresses db q = do
query <- queryCreate db q
msgs <- queryMessages query
addrs <- mapM (flip messageGetHeader $ "From") msgs
return addrs

main = do
db <- databaseOpen dbpath DatabaseModeReadOnly
--addrs2 <- getAddresses db "tag:haskell" -- This succeeds
addrs3 <- getAddresses db "to:dietz" -- This fails

--print addrs2
--print addrs3

databaseClose db



[2] Crashed session and backtrace,

[1217 ben@ben-laptop ~] $ ghc test.hs -auto-all -rtsopts -prof && gdb ./test 
GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /home/ben/test...(no debugging symbols found)...done.
(gdb) run
Starting program: /home/ben/test 
[Thread debugging using libthread_db enabled]

Program received signal SIGABRT, Aborted.
0x75979d05 in raise (sig=6) at 
../nptl/sysdeps/unix/sysv/linux/raise.c:64
64  ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
in ../nptl/sysdeps/unix/sysv/linux/raise.c
(gdb) bt
#0  0x75979d05 in raise (sig=6) at 
../nptl/sysdeps/unix/sysv/linux/raise.c:64
#1  0x7597dab6 in abort () at abort.c:92
#2  0x76de038c in talloc_abort (reason=0x76de56e8 "Bad talloc magic 
value - access after free") at ../talloc.c:210
#3  0x76de0271 in talloc_abort_access_after_free (ptr=0x769190, 
location=0x77bd4e04 "lib/messages.c:142") at ../talloc.c:229
#4  talloc_chunk_from_ptr (ptr=0x769190, location=0x77bd4e04 
"lib/messages.c:142") at ../talloc.c:250
#5  _talloc_free (ptr=0x769190, location=0x77bd4e04 "lib/messages.c:142") 
at ../talloc.c:1164
#6  0x77bc7e65 in notmuch_messages_destroy (messages=0x769190) at 
lib/messages.c:142
#7  0x004de1c9 in scheduleFinalizers ()
#8  0x004e013d in GarbageCollect ()
#9  0x004d9e40 in scheduleDoGC.clone.18 ()
#10 0x004db0e0 in exitScheduler ()
#11 0x004d9066 in hs_exit_ ()
#12 0x004d940a in shutdownHaskellAndExit ()
#13 0x004d8a91 in real_main ()
#14 0x004d8ade in hs_main ()
#15 0x75964eff in __libc_start_main (main=0x408ed0 , argc=1, 
ubp_av=0x7fffe4f8, init=, fini=, 
rtld_fini=, stack_end=0x7fffe4e8) at 
libc-start.c:226
#16 0x00407791 in _start ()
(gdb) 


[3] Valgrind output,

==25241== Memcheck, a memory error detector
==25241== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==25241== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==25241== Command: ./test
==25241== 
==25241== Conditional jump or move depends on uninitialised value(s)
==25241==at 0x52BB510: inflateReset2 (in 
/lib/x86_64-linux-gnu/libz.so.1.2.3.4)
==25241==by 0x52BB605: inflateInit2_ (in 
/lib/x86_64-linux-gnu/libz.so.1.2.3.4)
==25241==by 0x5F211BE: ChertTable::lazy_alloc_inflate_zstream() const 
(chert_table.cc:1672)
==25241==by 0x5F23B06: ChertTable::read_tag(Cursor*, std::string*, bool) 
const (chert_table.cc:1264)
==25241==by 0x5F260F9: ChertTable::get_exact_entry(std::string const&, 
std::string&) const (chert_table.cc:1210)
==25241==by 0x5F26DE2: 
ChertTermList::ChertTermList(Xapian::Internal::RefCntPtr, 
unsigned int) (chert_termlist.cc:44)
==25241==by 0x5EFF2E5: ChertDatabase::open_term_list(unsigned int) const 
(chert_database.cc:891)
==25241==by 0x5E7E7FB: Xapian::Document::termlist_begin() const 
(omdo

Re: [Haskell-cafe] Getting segmentation fault when profiling, despite `-K100M'.

2011-08-16 Thread Bas van Dijk
On 16 August 2011 15:56, David Banas  wrote:
> Any thoughts?

Just a hunch, did you specify the correct calling convention for amiInit?

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Getting segmentation fault when profiling, despite `-K100M'.

2011-08-16 Thread David Banas
Hi all,

I'm trying to profile a mixed language program, in which Haskell is NOT
the top layer and does not contain the `main' function. (C is/does.)

Is this effort doomed to fail?

I'm getting a segmentation fault, as soon as the first Haskell function
is called, despite having included `-K100M' in my list of +RTS args:

 19 int argc = 5;
 20 char* argv[] = {"ghcDll",
 21 "+RTS",
 22 "-hc",
 23 "-p",
 24 "-K100M",
 25 NULL}; // argv must end with NULL
 26 
 27 // Initialize Haskell runtime
 28 char** args = argv;
 29 hs_init(&argc, &args);

Using gdb to trace through the C-code, up until the first call to
Haskell, I get to here:

 64 // Call the Haskell function.
 65 res = amiInit(
 66 impulse_matrix,
 67 row_size,

Here's the beginning of `amiInit':

 41 -- Our Haskell implementation of `AMI_Init'.
 42 amiInit :: Ptr CDouble -> CInt -> CInt -> CDouble -> CDouble ->
 43CString -> Ptr CString -> Ptr (StablePtr AmiModel) -> Ptr
CString -> IO Int
 44 amiInit impulse_matrix row_size aggressors sample_interval bit_time
 45 ami_parameters_in ami_parameters_out ami_memory_handle
msgHndl
 46 | impulse_matrix == nullPtr = return 0
 47 | otherwise = do
 48 putStrLn "I'm here."

I never see "I'm here." printed to my console, before I get the
segmentation fault.

Any thoughts?

Thanks,
-db



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Hiding "growing" state using existentials.

2011-08-16 Thread Tom Schouten

On 08/16/2011 09:23 AM, Stephen Tetley wrote:
> {-
> I don't think parametric monads will solve your problem though, as you
> want a product of the states as the result of bind. Are you really
> sure you want this behavior?, I'd imagine it breaks the monad laws
> anyway.
> -}

It seems that the product is essential to what I want to do.
The product comes from the property that the states are independent.

Each "Sig" has a state component that is only known to the function (s
-> (a, s)) that represents a sequence as an iterated function.  This
iteration is to be seeded with an initial value stored in Sig when
unfolding the Sig (see _run).

Because this parallel state is completely in the background it thought
it might be possible to hide it.  What I don't understand is why the
usage of the existential type breaks at my definition of the Monad
instance, but not at the Applicative instance.

I.e. My _ap is ::  Sig s1 (x -> y) -> Sig s2 x -> Sig s3 y
but this doesn't hinder the construction of an Applicative instance
using the existential type which hides the s parameter (see below).

The problem seems to be to convince the type checker that it's ok to
unpack the data in the case of the Monad instance.  Or maybe it really
isn't ok, but then I don't understand why.


> {-# LANGUAGE ExistentialQuantification #-}
> import Control.Applicative
>
> data Sig s a = Sig { sigInit :: s, sigNext :: (s -> (a, s)) }
>
> _join :: (Sig s1 (Sig s2 a)) -> Sig (s1,s2) a
> _join (Sig i1 u1) = Sig (i1, i2) u12 where
>   ((Sig i2 _), _) = u1 i1
>   u12 (s1, s2) = (a, (s1', s2')) where
> ((Sig _ u2), s1') = u1 s1
> (a, s2')  = u2 s2
>
> _fmap :: (a -> b) -> Sig s a -> Sig s b
> _fmap f (Sig s0 u) = Sig s0 u' where
>   u' s = (f a, s') where
> (a, s') = u s
>
> _return x = Sig () $ \() -> (x, ())
>
> _bind :: (Sig s1 a) -> (a -> Sig s2 b) -> (Sig (s1,s2) b)
> m `_bind` g = _join ((_fmap g) m)
>
> _ap :: Sig s1 (a -> b) -> Sig s2 a -> Sig (s1, (s2, ())) b
> _ap f a = f `_bind` \f' ->
>   a `_bind` \a' ->
>   _return $ f' a'
>
> _run :: Sig s a -> [a]
> _run (Sig init next) = f init where
>   f s = (v:vs) where
> (v, s') = next s
> vs = f s'
>
> data Signal a = forall s. Signal (Sig s a)
>
> run (Signal a) = _run a

These work!

> instance Functor Signal where
>   fmap f (Signal a) = Signal $ _fmap f a
>
> instance Applicative Signal where
>   pure  = Signal . _return
>   (<*>) (Signal f) (Signal a) = Signal $ _ap f a
>
> ramp = Signal $ Sig 0 $ \s -> (s, s+1)
> disp = (take 10) . run
> test1 = disp $ pure 1   -- [1,1,1,1,1,1,1,1,1,1]
> test2 = disp $ ramp -- [0,1,2,3,4,5,6,7,8,9]
> test3 = disp $ pure (1 +) <*> ramp  -- [1,2,3,4,5,6,7,8,9,10]

So why not this one?
How to unpack the return value of f?

> {-
> instance Monad Signal where
>   return = Signal . _return
>   (>>=) (Signal ma) f = Signal $ _bind ma f' where
> f' a = case (f a) of
>   Signal mb -> mb
> -}


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Hiding "growing" state using existentials.

2011-08-16 Thread Stephen Tetley
You can't define (>>=) if your state type changes:

(>>=) :: m a -> (a -> m b) -> m c

Whereas, your bind is effectively three different parametric types:

_bind :: m1 a -> (a -> m2 b) -> m3 b

You can use parametric monads to represent state changing within a
monad. Oleg Kiselyov has tutorials on their use on his website (search
for the Monadish class). The SHE experimental extension of Haskell
also has parametric monads built in.

bindish :: m s1 a -> (a -> m s2 b) -> m s2 b

I don't think parametric monads will solve your problem though, as you
want a product of the states as the result of bind. Are you really
sure you want this behavior?, I'd imagine it breaks the monad laws
anyway.

http://okmij.org/ftp/Computation/monads.html

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe