What is wrong with defining a composition this way?

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.

+6
source share
1 answer

. in new_g . new_f new_g . new_f is a prelude, not Control.Category . Therefore you need to use Cat..

But the usual way to use Control.Category is:

 import Prelude hiding (id, (.)) import Control.Category 
+10
source

Source: https://habr.com/ru/post/905512/


All Articles