3

If I have a value a: Free[Op, A], is it possible to "flatten" the structure of a so that two Ops that are bound together by the free monad may be collapsed into one?

Context: I'd like to perform this as an optimization step before interpretation because a semantic of Op is that its operations are idempotent. So if two appear "in a row", the second can be eliminated at no cost to the semantics of the program.

beefyhalo
  • 1,691
  • 2
  • 21
  • 33
  • Maybe this can only be done at the interpreter level? It seems like a useful generalization to have. – beefyhalo Oct 05 '16 at 00:09
  • 1
    It's an Scalaz/cats implementation constraint stemming from Scala's eager evaluation strategy rather than fundamental Free limitation. Reason is Gosub/FlatMapped constructor used by `Free`'s `flatMap` to prevent stack overflow: it makes further introspection progress impossible by wrapping a computation into trampoline function. – P. Frolov Sep 21 '17 at 10:04

1 Answers1

4

As far as I understand there is no way for this kind introspection of Free Monad program as it represents sequential computation where each step depends on the result of another.

There is a great talk by John de Goes about the pros and cons of Free Monad vs Free Applicative (https://www.youtube.com/watch?v=H28QqxO7Ihc). The latter gives power of introspection.

Denis Mikhaylov
  • 2,035
  • 21
  • 24