On 14/05/11 18:24, Reiner Pope wrote:
On 14/05/11 17:40, Nicolas Pouillard wrote:
On Sat, 14 May 2011 15:06:51 +1000, Reiner Pope<reiner.p...@gmail.com> wrote:
Hi all,

Is there a policy for strictness of datatypes in Yi, and what is it?
Specifically:

    1. When I write a datatype, which fields should I make strict? For
       instance, all fields in Editor are strict, and all fields except
       'height' and 'winRegion' in Window are strict. Is there some
       reasoning underlying this?
2. What should I do to ensure strictness of lazy datatypes like [a]?
       For instance, the 'bufAccessList :: ![BufferRef]' field of
'Window' is marked strict, but is a list. Is it the client's duty
       to ensure that this list is fully evaluated?

Would there be any objections to using the 'deepseq' package to ensure
evaluation? The 'NFData' class is supported by Derive, so it might be
possible to generate many instances automatically.
I would recommend to use a strict-by-default datastructure instead of lazy lists plus NFData. This is not against NFData which is very useful but it
traverses the whole list each time even when the list is already forced.
What about Data.Sequence or Data.FingerTree ?

Indeed, strict datastructures seem the right solution.

However, Data.Sequence and Data.FingerTree aren't strict enough. Both of them are only spine-strict, so that

singleton undefined `seq` () = ()

Likewise for most other containers, such as Data.Map.

Cheers,
Reiner

I've looked on Hackage, and there don't currently seem to be element-strict versions of these containers. However, it seems to be relatively easy to make any of these containers strict. Here's an example for Data.Sequence:

import qualified Data.Sequence as S

newtype SeqStrict a = SS (S.Seq a)

-- element-strict constructors
singleton a = a `seq` SS (S.singleton a)
a <| (SS s) = a `seq` SS (a S.<| s)
(SS s) |> a = a `seq` SS (s S.|> a)

-- all other constructors
empty = SS S.empty
(SS s) >< (SS t) = SS (s S.>< t)
fromList l = foldl (|>) empty l

The basic idea is that all functions which have elements as arguments force the elements. Provided we export the SeqStrict type abstractly, these are the only ways to insert elements into the sequence, so all SeqStricts must be strict in their elements. (Of course, we are depending on the containers being spine-strict for this to work.)

It works, too:
Main> fromList [1,2,undefined,3,4] `seq` ()
*** Exception: Prelude.undefined

Perhaps we should use an approach like this?

Cheers,
Reiner

--
Yi development mailing list
yi-devel@googlegroups.com
http://groups.google.com/group/yi-devel

Reply via email to