Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More free applicatives! #150

Open
treeowl opened this issue Mar 29, 2017 · 1 comment
Open

More free applicatives! #150

treeowl opened this issue Mar 29, 2017 · 1 comment

Comments

@treeowl
Copy link

treeowl commented Mar 29, 2017

The free applicatives I've seen have been based on the binary-operator notion of Applicative. But we can also think in terms of idiom brackets. The simplest one is slow in general:

-- A vinyl record
data Args :: (* -> *) -> [*] -> * where
  Nil :: Args f '[]
  Cons :: f a -> Args f as -> Args f (a ': as)

-- A free Applicative
data Expr f a = forall (ts :: [*]).
      Expr (Args Identity ts -> a) (Args f ts)

It's slow to consume because it will allocate lots of initial segments. That's easily fixed:

data Expr f a = forall (ts :: [*]).
      Expr (forall us. Args Identity (ts ++ us) -> (Args Identity us, a))
               (Args f ts)

It's also slow on construction, of course. I imagine a difference list would do the trick if you don't need reflection.

data Expr f a = forall (ts :: [*]).
      Expr (forall us. Args Identity (ts ++ us) -> (Args Identity us, a))
               (forall us. Args f us -> Args f (ts ++ us))
@treeowl
Copy link
Author

treeowl commented Mar 29, 2017

@ElvishJerricco's blog post on sorting Traversable containers inspired this, BTW. Using

data Mono x a where
   Mono :: x -> Mono x x

-- Just a length-indexed vector
type List x = Expr (Mono x)

gets you a List x applicative you can traverse with and then sort. Rebuilding the container separately seems much easier than stirring everything together with the standard version. I have no idea if there are other potential applications of the idiom bracket representation, but maybe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant