Sono nuovo di Haskell e ho giocato con Arrows. Mi piacerebbe scrivere uno strumento che possa "disassemblare" a livello di programmazione una freccia costruita in precedenza. Come un potenziale applicazione, immaginare una funzione che prende in una freccia e restituisce un grafo orientato che rappresenta tutte le concatenazioni, spaccature, fan-out, eccDeconstructing Frecce in Haskell
EG, (f & & & g) >>> h rese qualcosa come
----- f ----
---| |--- h -----
----- g ----
inizialmente ho pensato che potrei essere in grado di fare questo tramite il pattern matching, come nel semplice sotto (adattato da haskell.org Freccia tutorial), ma non ha funzionato.
module Main(main) where
import Control.Arrow
import Control.Category
import Prelude hiding (id, (.))
newtype SimpleFunc a b = SimpleFunc {runF :: (a -> b)}
instance Arrow SimpleFunc where
arr f = SimpleFunc f
first (SimpleFunc f) = SimpleFunc (mapFst f) where
mapFst g (a,b) = (g a, b)
second (SimpleFunc f) = SimpleFunc (mapSnd f) where
mapSnd g (a,b) = (a, g b)
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 a b -> Bool
is_split (a1 >>> a2) = False
is_split (a1 &&& a2) = True
....
is_split h2 -- evaluates to True
is_split h1 -- evaluates to False
Tutti i miei tentativi di fare questo definendo i miei tipi (vale a dire, un tipo parametrico che include i tipi di bambini costituenti pure) hanno anche fallito.
C'è un modo per "separare" i componenti di una freccia una volta che è stata costruita?
Grazie per il suggerimento. Non ho molta esperienza con le monadi libere (molto meno frecce libere), ma ciò che suggerisci sembra promettente. – jadaska