3

I've read two great series of articles about Autos and Free Monads and I'd like to combine this two techniques somehow.

I'd like to have something like:

data ProgramF a = Get (String -> a) | Set String a
instance Functor ProgramF where ...
type Program = Free ProgramF

get' :: Program String
get' = liftF $ Get id

set' :: String -> Program ()
set' s = liftF $ Set s ()

auto1 :: AutoM Program () String
auto1 = arrM \_ -> get'

auto2 :: AutoM Program String ()
auto2 = arrM \s -> set' s

auto3 :: AutoM Program () ()
auto3 = auto1 >>> auto2

...

But there are some problems, for example ArrowLoop requires Program to be an instance of MonadFix which is not possible as far as I understand.

So my questions are:

  • Are there ways to make Auto and Free work together?
  • And if not, maybe there are other ways to achieve the goal?

Note: I'm pretty new to functional programming, so I understand from little to nothing in theory.

Update:

In one of the comments it was mentioned that Auto is itself a form of fixpoint and I can use ProgramF directly with it. So I guess that the type of Auto should look something like this:

newtype Auto f a b = Auto (a -> f (b, (Auto f a b)))

But the problem now is that I can't figure out how to compose two Autos without f being a Monad.

My end goal is to have some composable pieces of code with internal state and a way to purify my code hiding all IO effects (like log or getLine) in some kind of interpreter.

So I guess my real question is: how can I implement something described above?

Maybe I'm doing it all wrong and there is a better way. Can someone please give a simple example or provide some links to something similar?

starper
  • 175
  • 1
  • 8
  • So far, your only actual complaint about `Free` is that you can't implement `MonadFix` or `ArrowLoop`. Do you need to? – Daniel Wagner Nov 18 '15 at 00:21
  • Related: http://stackoverflow.com/q/29161923/414413 – Cirdec Nov 18 '15 at 03:05
  • @DanielWagner Actually I don't know for sure will I need it or not, but I have some thoughts about how it can be useful. And I don't want to discover one day that I really need it, but can't have it :) @Cirdec Thanks, I'll take a look. Btw can something not exactly the same, but similar (I mean purification part) be implemented without `Free`, for example only with `Arrow`? – starper Nov 18 '15 at 11:04
  • `Auto` is itself a form of fixpoint. Why not use `ProgramF` rather than `Program` with it? – gallais Nov 18 '15 at 17:34
  • 1
    The [machines](https://hackage.haskell.org/package/machines) library builds `Process`es from the base functor of state transducers http://stackoverflow.com/q/27997155/414413 – Cirdec Nov 18 '15 at 23:55
  • @Cirdec Thanks for this links, I'll certainly take a look. It looks like a lot of info to wrap my head around. – starper Nov 19 '15 at 02:05
  • @gallais Can you please give an example of how to do it? Or maybe give links to such examples? – starper Nov 19 '15 at 02:06

0 Answers0