Haskell Arrow Destruction

I am new to Haskell and I played with Arrows. I would like to write a tool that can programmatically “parse” the previously created Arrow. As a potential application, imagine a function that takes an arrow and returns a directed graph that represents all concatenations, partitions, branches, etc.

For example, (f && g) →> h gives something like

----- f ---- ---| |--- h ----- ----- g ---- 

Initially, I thought I could do this using pattern matching, as described below (adapted from the haskell.org Arrow tutorial), but that didn't work.

 module Main(main) where import Control.Arrow import Control.Category import Prelude hiding (id, (.)) newtype SimpleFunc ab = SimpleFunc {runF :: (a -> b)} instance Arrow SimpleFunc where arr f = SimpleFunc f first (SimpleFunc f) = SimpleFunc (mapFst f) where mapFst g (a,b) = (ga, b) second (SimpleFunc f) = SimpleFunc (mapSnd f) where mapSnd g (a,b) = (a, gb) instance Category SimpleFunc where (SimpleFunc g) . (SimpleFunc f) = SimpleFunc (g . f) id = arr id f,g :: SimpleFunc Int Int f = arr (\x -> x - 5) g = arr (\x -> 3*x + 1) h1 :: SimpleFunc Int Int h1 = f >>> g h2 :: SimpleFunc Int (Int, Int) h2 = f &&& g # It would be great if I something like this worked is_split :: SimpleFunc ab -> Bool is_split (a1 >>> a2) = False is_split (a1 &&& a2) = True .... is_split h2 -- evaluates to True is_split h1 -- evaluates to False 

All my attempts to do this by defining my own types (for example, a parameterized type that includes child types) also failed.

Is there a way to “push apart” the arrow components after creating it?

+5
source share
2 answers

You can make a free arrow that looks like a tree so you can check its structure. Or lower it to the base arrow. One example is another SO question: Useful operations on free arrows

+5
source

The answer will be completely absent, because branching and composition and other arrow operators are functions, not constructors. You can “split”, say, a tree, because arranging trees from other trees saves the constructors used to execute the composition, and then Haskell can map patterns to them. But there is no guarantee that the arrow compositions will retain the splitters and compositions used to complete the composition. This is like adding 2 + 3 and then trying to split 5 later.

+1
source

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


All Articles