{-# LANGUAGE UnicodeSyntax, FlexibleContexts, MultiParamTypeClasses #-}
-- | Offers an 'activePair' pattern for convenient implementation of interaction nets.
module GraphRewriting.Pattern.InteractionNet where

import Prelude.Unicode
import Data.View
import Data.Functor
import GraphRewriting.Graph.Types
--import GraphRewriting.Graph.Read
import GraphRewriting.Pattern


-- | Index that identifies the principal port within the list of ports
class INet n where principalPort  n  Port

-- | Instead of @(,)@ to save parentheses
data Pair a = a :-: a

instance Functor Pair where fmap :: forall a b. (a -> b) -> Pair a -> Pair b
fmap a -> b
f (a
x :-: a
y) = a -> b
f a
x forall a. a -> a -> Pair a
:-: a -> b
f a
y

pair  Pair a  (a,a)
pair :: forall a. Pair a -> (a, a)
pair (a
x :-: a
y) = (a
x,a
y)

activePair  (View [Port] n, View v n, INet v)  Pattern n (Pair v)
activePair :: forall n v. (View [Port] n, View v n, INet v) => Pattern n (Pair v)
activePair = forall (m :: * -> *) n a.
Monad m =>
PatternT n m a -> PatternT n m a
linear forall a b. (a -> b) -> a -> b
$ do
	v
v1  forall (m :: * -> *) v n. (MonadFail m, View v n) => PatternT n m v
node
	let pp1 :: Port
pp1 = forall n. INet n => n -> Port
principalPort v
v1
	v
v2  forall (m :: * -> *) v n.
(MonadFail m, View v n) =>
Port -> PatternT n m v
nodeWith Port
pp1
	forall (m :: * -> *). MonadFail m => Bool -> m ()
require (Port
pp1 forall α. Eq α => α -> α -> Bool
 forall n. INet n => n -> Port
principalPort v
v2)
	forall (m :: * -> *) a. Monad m => a -> m a
return (v
v1 forall a. a -> a -> Pair a
:-: v
v2)