[Haskell-cafe] Announcing OneTuple-0.1.0
dons at galois.com
Fri Oct 3 17:25:19 EDT 2008
> On Fri, 2008-10-03 at 15:38 -0400, David Menendez wrote:
> > On Fri, Oct 3, 2008 at 3:17 AM, Jason Dusek <jason.dusek at gmail.com> wrote:
> > > Perhaps I am lacking in imagination, but I still can't see the
> > > value of one tuples.
> > You can use them to defeat seq.
> > undefined `seq` x == undefined
> > OneTuple undefined `seq` x == x
> > That might be useful if a polymorphic function is using seq to force
> > evaluation, and you don't want it to. But I can't imagine that coming
> > up much in practice.
> Think element strict polymorphic containers, e.g.
> data HeadStrictList a = Nil | Cons !a (HeadStrictList a)
> type LazyList a = HeadStrictList (OneTuple a)
Used in practice to prevent strict state components in list fusion
leaking into user's lazy code,
data L a = L a -- lazy / lifted
newtype S a = S a -- strict / unlifted
class Unlifted a where
instance Unlifted (L a) where
expose (L _) s = s
instance Unlifted (S a) where
expose (S a) s = seq a s
data Stream a = forall s. Unlifted s =>
Stream !(s -> Step a s) -- ^ a stepper function
!s -- ^ an initial state
So we can then ensure
stream :: [a] -> Stream a
stream xs0 = Stream next (L xs0)
next (L ) = Done
next (L (x:xs)) = Yield x (L xs)
Has the appropriate strictness properties.
More information about the Haskell-Cafe