wondering about a MonadIO instance for a heap data type

Hi, I was wondering about creating an instance of MonadIO for a heap data. Any hints? data Heap a = E | T Int a (Heap a) (Heap a) deriving (Eq, Ord, Read, Show) The reason is that I want to use liftIO during a heapsort to print out intermediate results. Thanks. Qi Qi

Use Debug.Trace.
It does not make sense to declare that heap is a monad, as a monad is an abstraction of sequencing computations, and a heap is not an abstraction of sequencing computations at all. You don't make your String class implement the "rendering engine" interface just because you want to use it in a computer game program, equally you dont pretend that a heap is a way of sequencing computations just because you want to sequence computations related to heaps.
The actual computation in your case is the heapsort function, not the heap. If you absolutely must use IO, add IO to the functions type.
11.07.2012, в 15:19, Qi Qi
Hi,
I was wondering about creating an instance of MonadIO for a heap data. Any hints?
data Heap a = E | T Int a (Heap a) (Heap a) deriving (Eq, Ord, Read, Show)
The reason is that I want to use liftIO during a heapsort to print out intermediate results.
Thanks.
Qi Qi
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

List [] is a monad, why not for heap data. Heap data could be an instance of Monad too. I have the heapsort function, and just wanted to rewrite a verbose version of it by using liftIO. But I would look into Debug.Trace. Thanks for your hint. Qi On Wednesday, July 11, 2012 5:28:17 PM UTC-5, Eugene Kirpichov wrote:
Use Debug.Trace. It does not make sense to declare that heap is a monad, as a monad is an abstraction of sequencing computations, and a heap is not an abstraction of sequencing computations at all. You don't make your String class implement the "rendering engine" interface just because you want to use it in a computer game program, equally you dont pretend that a heap is a way of sequencing computations just because you want to sequence computations related to heaps.
The actual computation in your case is the heapsort function, not the heap. If you absolutely must use IO, add IO to the functions type.
11.07.2012, в 15:19, Qi Qi
написал(а): Hi,
I was wondering about creating an instance of MonadIO for a heap data. Any hints?
data Heap a = E | T Int a (Heap a) (Heap a) deriving (Eq, Ord, Read, Show)
The reason is that I want to use liftIO during a heapsort to print out intermediate results.
Thanks.
Qi Qi
_______________________________________________ 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

On Wed, Jul 11, 2012 at 8:00 PM, Qi Qi
List [] is a monad, why not for heap data. Heap data could be an instance of Monad too.
Only if you can define a meaningful semantics for it. Lists aren't a monad because of liftIO, but because the monad pattern means something (backtracking/multiple evaluation). What does the monad pattern mean for your heap? I'm not saying there *isn't* one; I'm saying that you must *have* one in order to make a monad. Monads are not simply some sneaky way to slipstream I/O into expressions; IO is just one possible monad, and many programs make heavier use of other monads such as state and reader. And in particular, most applications of the list monad don't involve any I/O. (Almost necessarily, since the standard ListT is actually broken but can't be fixed for backward compatibility reasons.) -- brandon s allbery allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms

List is a monad because it has an associated way of sequencing
computations: namely, "collect results of the second computation
invoked on all results of the first computation". That's not because
List is a data structure (similarly to Heap), it's because it is
associated with the computational abstraction of computations with
multiple results. The heap data structure does not have an associated
computational abstraction, at least not that one I'm aware of, and
definitely not one that would make any sense in the context of
heapsort. So it doesn't make sense to pretend that it does.
On Wed, Jul 11, 2012 at 5:00 PM, Qi Qi
List [] is a monad, why not for heap data. Heap data could be an instance of Monad too. I have the heapsort function, and just wanted to rewrite a verbose version of it by using liftIO. But I would look into Debug.Trace. Thanks for your hint.
Qi
On Wednesday, July 11, 2012 5:28:17 PM UTC-5, Eugene Kirpichov wrote:
Use Debug.Trace. It does not make sense to declare that heap is a monad, as a monad is an abstraction of sequencing computations, and a heap is not an abstraction of sequencing computations at all. You don't make your String class implement the "rendering engine" interface just because you want to use it in a computer game program, equally you dont pretend that a heap is a way of sequencing computations just because you want to sequence computations related to heaps.
The actual computation in your case is the heapsort function, not the heap. If you absolutely must use IO, add IO to the functions type.
11.07.2012, в 15:19, Qi Qi
написал(а): Hi,
I was wondering about creating an instance of MonadIO for a heap data. Any hints?
data Heap a = E | T Int a (Heap a) (Heap a) deriving (Eq, Ord, Read, Show)
The reason is that I want to use liftIO during a heapsort to print out intermediate results.
Thanks.
Qi Qi
_______________________________________________ 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 mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Eugene Kirpichov http://www.linkedin.com/in/eugenekirpichov

On 12 July 2012 06:19, Qi Qi
Hi,
I was wondering about creating an instance of MonadIO for a heap data. Any hints?
data Heap a = E | T Int a (Heap a) (Heap a) deriving (Eq, Ord, Read, Show)
The reason is that I want to use liftIO during a heapsort to print out intermediate results.
If you just want this for debugging, you're probably better off using Debug.Trace to print the intermediate results. Conrad.
participants (4)
-
Brandon Allbery
-
Conrad Parker
-
Eugene Kirpichov
-
Qi Qi