Giter Club home page Giter Club logo

Comments (1)

pchiusano avatar pchiusano commented on May 14, 2024

@refried this was trickier than I thought. :) I got a compiling, working version after about 2 hours. Probably should have done that sooner rather than sending you on a wild goose chase. My bad. :( I hope this was still a good learning exercise.

I fiddled a bit with different representations and found it was pretty easy to express with the following:

{-# Language ScopedTypeVariables #-}
{-# Language BangPatterns #-}
{-# Language ExistentialQuantification #-}
{-# Language Rank2Types #-}
{-# Language GADTs #-}

module Stream where

import Control.Applicative
import Control.Monad

data Stream f a = forall a0 . Stream (forall b . Stack f a b -> Stack f a0 b)

data Stack f a b where
  Empty :: Stack f a a
  Cons :: [Segment f a] -> Stack f a b -> Stack f a b
  Bind :: (a -> Stream f b) -> Stack f b c -> Stack f a c

data Segment f a where
  Emit :: a -> Segment f a
  Eval :: f a -> Segment f a
  Append :: Stream f a -> Segment f a

data ViewL f a b where
  Unbound :: [Segment f a] -> ViewL f a a
  Bound :: [Segment f a] -> (a -> Stream f b) -> Stack f b c -> ViewL f a c

And some helper functions:

unbind :: Stack f a b -> ViewL f a b
bindSegment :: (x -> Stream f y) -> Segment f x -> Segment f y

From there the implementations of uncons and run became a lot more inevitable - you just unbind the stack, pattern match, and handle all the cases. It might be possible to implement in terms of the simpler representation I gave earlier.

With the earlier representation, implementing everything up through MonadPlus should still be trivial, it's just the two interpreters, uncons and run, that are much trickier.

If you'd still like to give this a try, start with the representation I just gave and go from there. Otherwise we can go over my 'answer' tomorrow.

from unison.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.