Skip to content

Generalize and expose the conversion machienery #91

Description

@ShapeOfMatter

I've been playing around with functions to build Vec n as from infinite Streams and state-like functions (e.g.). We could certainly have a function like fromFunctor :: (Functor f1, Applicative f2) => ... -> f1 a -> f2 (Vec n a). This would be able to subsume* the existing implementations of fromListPrefix and fromList.

* fromListPrefix certainly; fromList may be a proud nail.

Here's what I've got so far:

newtypeFromFunctorf1f2na=FromFunctor{getFromFunctor:: (Functorf1, Functorf2) =>f1a->f2 (Vecna) }fromFunctor::forallf1f2na.
(SNatIn, Functorf1, Applicativef2) =>
(f1a->f2a) -> (f1a->f1a) ->f1a->f2 (Vecna)
fromFunctor mkHead mkTail = getFromFunctor $ induction1 start step
where start ::FromFunctorf1f2 'Z a
start =FromFunctor$const$pureVNilstep::forallm.FromFunctorf1f2ma->FromFunctorf1f2 ('S m) a
step (FromFunctor f) =FromFunctor$\f_1 ->(:::)<$> (mkHead f_1) <*> (f $ mkTail f_1)
-- These usage examples, while useful, would involve new dependencies: fromStream::SNatIn=>Streama->Vecna
fromStream = runIdentity . (fromFunctor (Identity.Data.Stream.head) Data.Stream.tail)
unfold::forallsna.SNatIn=>
(s-> (a, s)) ->
(s-> (Vecna, s))
unfold = runState . (fromFunctor idid) . state

Would you like me to put together a pull request?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions