I went through some shooting guidelines , playing with features that bring back a new version of myself, trying to maintain some state.
The new type is defined as follows:
newtype Circuit ab = Circuit {runCircuit :: a -> (b, Circuit ab)}
Since I want to be able to chart, I make it an instance of a category. When drawing up two schemes, the result should also be a scheme. (Circuit bc) . (Circuit ab) (Circuit bc) . (Circuit ab) gives a Circuit ac .
I wrote this:
import qualified Control.Category as Cat instance Cat.Category Circuit where (Circuit g) . (Circuit f) = Circuit $ \a -> let (b, new_f) = fa (c, new_g) = gb new_circ = new_g . new_f in (c, new_circ)
but he fails:
Main.hs:70:64: Couldn't match expected type `b0 -> c0' with actual type `Circuit bc' In the first argument of `(.)', namely `new_g' In the expression: new_g . new_f In an equation for `new_circ': new_circ = new_g . new_f
I searched for an answer in a tutorial, and this answer introduced an intermediate function like this one that compiles nicely:
(.) = dot where (Circuit g) `dot` (Circuit f) = Circuit $ \a -> let (b, new_f) = fa (c, new_g) = gb new_circ = new_g `dot` new_f in (c, new_circ)
I do not see the difference.
source share