typing query

Janis Voigtlaender voigt@orchid.inf.tu-dresden.de
Fri, 21 Feb 2003 09:21:29 +0100


Amit Garg wrote:
> 
> Hey all.
> 
> I am trying to declare a read-only state monad and a read-write state
> monad, so as to distinguish between methods on a data type that are
> read-only vs. read-write.
> 
> This is the best I could come up with:
> 
> newtype ST s a = ST ( s -> (s,a) ) -- read-only
> newtype SW s a = SW ( s -> (s,a) ) -- read-write
> 
> class ReadM m s a where
>     readM :: m s s
>     runM :: s -> m s a -> (a,s)
> 
> class WriteM m r s where
>     updateM :: (s -> s) -> m s ()
> 
> instance ReadM ST s a where
>     readM = ST (\s -> (s,s))
>     runM s (ST c) = c s -- Doesn't work
> 
> instance ReadM SW s a where
>     readM = SW (\s -> (s,s))
>     runM s (SW c) = c s -- Doesn't work
> 
> And later on ...
>     updateM s (SW c) = c s
> 
> Does that make sense? If not, how do I do it? If so, is there a
> simpler means of getting there? Thanks.

1. You don't need multiparameter type classes for this. "class ReadM m
where ..." and likewise for instance declarations suffices.

2. The declaration for runM in ReadM has wrong order of elements in the
result tuple, that causes your "-- Doesn't work" type problems.

3. The suggested definition for updateM doesn't make sense, not even
with respect to types.

HTH, Janis.

--
Janis Voigtlaender
http://wwwtcs.inf.tu-dresden.de/~voigt/
mailto:voigt@tcs.inf.tu-dresden.de