Ist es möglich, freie Monadendolmetscher zu erweitern?

Bei einer kostenlosen DSL-Monade wie:

data FooF x = Foo String x
            | Bar Int    x
  deriving (Functor)

type Foo = Free FooF

Und ein zufälliger Dolmetscher fürFoo:

printFoo :: Foo -> IO ()
printFoo (Free (Foo s n)) = print s >> printFoo n
printFoo (Free (Bar i n)) = print i >> printFoo n

Es scheint mir, dass es möglich sein sollte, etwas in jede Iteration von printFoo einzufügen, ohne manuell vorgehen zu müssen:

printFoo' :: Foo -> IO ()
printFoo' (Free (Foo s n)) = print s >> print "extra info" >> printFoo' n
printFoo' (Free (Bar i n)) = print i >> print "extra info" >> printFoo' n

Ist das irgendwie möglich, indem man das Original einwickelt?printFoo?

Motivation: Ich schreibe eine kleine DSL, die in ein Binärformat 'kompiliert' wird. Das Binärformat enthält nach jedem Benutzerbefehl einige zusätzliche Informationen. Es muss da sein, ist aber in meinem Anwendungsfall völlig irrelevant.

Antworten auf die Frage(5)

Ihre Antwort auf die Frage