{-# LANGUAGE AllowAmbiguousTypes #-}

-- | Instantiate named categorical laws as hypergraph rewrite rules.
module Proarrow.Tools.HypergraphRewrite.Laws
  ( Direction (..)
  , ruleWith
  , ruleWithDirection
  , parameterRuleWith
  , parameterRuleWithDirection
  , parameterRulesWith
  , subgraphRuleWithDirection
  , rewritesWith
  , rewritesWithDirection
  ) where

import Data.Typeable (Typeable)
import Prelude

import Proarrow.Category.Instance.OpenHypergraph (OPENHG, WireSorts, Wires)
import Proarrow.Core (CategoryOf (..))
import Proarrow.Optic.Iso (DecidableIso)
import Proarrow.Tools.HypergraphRewrite (Parameter (..), Rule, parameterRule, rewrites, rule, subgraphRule)
import Proarrow.Tools.Laws (Equation, Law (..), LawBody, Laws (..), lawName, withSides)

-- | Orient an equation as written in its 'Law' body, or in reverse.
data Direction = LeftToRight | RightToLeft
  deriving (Direction -> Direction -> Bool
(Direction -> Direction -> Bool)
-> (Direction -> Direction -> Bool) -> Eq Direction
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Direction -> Direction -> Bool
== :: Direction -> Direction -> Bool
$c/= :: Direction -> Direction -> Bool
/= :: Direction -> Direction -> Bool
Eq, Int -> Direction -> ShowS
[Direction] -> ShowS
Direction -> String
(Int -> Direction -> ShowS)
-> (Direction -> String)
-> ([Direction] -> ShowS)
-> Show Direction
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Direction -> ShowS
showsPrec :: Int -> Direction -> ShowS
$cshow :: Direction -> String
show :: Direction -> String
$cshowList :: [Direction] -> ShowS
showList :: [Direction] -> ShowS
Show)

-- | Select a law left-to-right, using an explicit interpretation to choose its
-- object variables, supplied arrows, and representation of the structures.
-- Unknown and ambiguous law names are errors. The interpreted equation must
-- satisfy 'rule'\'s restriction on the oriented left boundary.
--
-- The interpretation receives the polymorphic body. Instantiate it in a
-- diagram category supporting the requested structures, then use 'withSides'
-- to lower both sides to parallel open hypergraphs. Supplied arrows are concrete;
-- this does not introduce variables matching arbitrary subdiagrams.
ruleWith
  :: forall cs s l
   . (Laws cs)
  => String
  -> (LawBody cs -> Equation (OPENHG s l))
  -> Either String (Rule s l)
ruleWith :: forall (cs :: [Type -> Constraint]) s l.
Laws cs =>
String
-> (LawBody cs -> Equation (OPENHG s l))
-> Either String (Rule s l)
ruleWith String
name = forall (cs :: [Type -> Constraint]) s l.
Laws cs =>
String
-> Direction
-> (LawBody cs -> Equation (OPENHG s l))
-> Either String (Rule s l)
ruleWithDirection @cs String
name Direction
LeftToRight

-- | Like 'ruleWith', with an explicit choice of direction.
ruleWithDirection
  :: forall cs s l
   . (Laws cs)
  => String
  -> Direction
  -> (LawBody cs -> Equation (OPENHG s l))
  -> Either String (Rule s l)
ruleWithDirection :: forall (cs :: [Type -> Constraint]) s l.
Laws cs =>
String
-> Direction
-> (LawBody cs -> Equation (OPENHG s l))
-> Either String (Rule s l)
ruleWithDirection String
name Direction
direction LawBody cs -> Equation (OPENHG s l)
interpret = do
  equation <- forall (cs :: [Type -> Constraint]) k.
Laws cs =>
String -> (LawBody cs -> Equation k) -> Either String (Equation k)
interpretLaw @cs String
name LawBody cs -> Equation (OPENHG s l)
interpret
  withSides equation $ \a ~> b
lhs a ~> b
rhs -> case Direction
direction of
    Direction
LeftToRight -> (a ~> b) -> (a ~> b) -> Either String (Rule s l)
forall s l (a :: OPENHG s l) (b :: OPENHG s l).
(a ~> b) -> (a ~> b) -> Either String (Rule s l)
rule a ~> b
lhs a ~> b
rhs
    Direction
RightToLeft -> (a ~> b) -> (a ~> b) -> Either String (Rule s l)
forall s l (a :: OPENHG s l) (b :: OPENHG s l).
(a ~> b) -> (a ~> b) -> Either String (Rule s l)
rule a ~> b
rhs a ~> b
lhs

-- | Select a law left-to-right with the label parameters of 'parameterRule'.
-- The interpretation supplies named arrows as boxes labelled with 'Variable',
-- and structural operations as fixed constructors or 'Literal' fields.
-- Repeated names must bind equal values; the right side reuses those bindings.
-- Object choices and box arities remain fixed, and variables do not match subdiagrams.
-- Compile once, then apply with 'rewrites' or include in 'Proarrow.Tools.HypergraphRewrite.compileRules'.
parameterRuleWith
  :: forall cs s f a
   . (Laws cs, Traversable f, Eq (f ()), Typeable f, Eq a)
  => String
  -> (LawBody cs -> Equation (OPENHG s (f (Parameter a))))
  -> Either String (Rule s (f a))
parameterRuleWith :: forall (cs :: [Type -> Constraint]) s (f :: Type -> Type) a.
(Laws cs, Traversable f, Eq (f ()), Typeable f, Eq a) =>
String
-> (LawBody cs -> Equation (OPENHG s (f (Parameter a))))
-> Either String (Rule s (f a))
parameterRuleWith String
name = forall (cs :: [Type -> Constraint]) s (f :: Type -> Type) a.
(Laws cs, Traversable f, Eq (f ()), Typeable f, Eq a) =>
String
-> Direction
-> (LawBody cs -> Equation (OPENHG s (f (Parameter a))))
-> Either String (Rule s (f a))
parameterRuleWithDirection @cs String
name Direction
LeftToRight

-- | Like 'parameterRuleWith', with an explicit choice of direction.
-- Parameters on the oriented right side must be bound on the oriented left side.
parameterRuleWithDirection
  :: forall cs s f a
   . (Laws cs, Traversable f, Eq (f ()), Typeable f, Eq a)
  => String
  -> Direction
  -> (LawBody cs -> Equation (OPENHG s (f (Parameter a))))
  -> Either String (Rule s (f a))
parameterRuleWithDirection :: forall (cs :: [Type -> Constraint]) s (f :: Type -> Type) a.
(Laws cs, Traversable f, Eq (f ()), Typeable f, Eq a) =>
String
-> Direction
-> (LawBody cs -> Equation (OPENHG s (f (Parameter a))))
-> Either String (Rule s (f a))
parameterRuleWithDirection String
name Direction
direction LawBody cs -> Equation (OPENHG s (f (Parameter a)))
interpret = do
  equation <- forall (cs :: [Type -> Constraint]) k.
Laws cs =>
String -> (LawBody cs -> Equation k) -> Either String (Equation k)
interpretLaw @cs String
name LawBody cs -> Equation (OPENHG s (f (Parameter a)))
interpret
  withSides equation $ \a ~> b
lhs a ~> b
rhs -> case Direction
direction of
    Direction
LeftToRight -> (a ~> b) -> (a ~> b) -> Either String (Rule s (f a))
forall (f :: Type -> Type) a s (x :: OPENHG s (f (Parameter a)))
       (y :: OPENHG s (f (Parameter a))).
(Traversable f, Eq (f ()), Typeable f, Eq a) =>
(x ~> y) -> (x ~> y) -> Either String (Rule s (f a))
parameterRule a ~> b
lhs a ~> b
rhs
    Direction
RightToLeft -> (a ~> b) -> (a ~> b) -> Either String (Rule s (f a))
forall (f :: Type -> Type) a s (x :: OPENHG s (f (Parameter a)))
       (y :: OPENHG s (f (Parameter a))).
(Traversable f, Eq (f ()), Typeable f, Eq a) =>
(x ~> y) -> (x ~> y) -> Either String (Rule s (f a))
parameterRule a ~> b
rhs a ~> b
lhs

-- | Instantiate every law left-to-right with 'parameterRuleWith', retaining its
-- name for 'Proarrow.Tools.HypergraphRewrite.compileRules'. Duplicate law names and invalid rules are errors.
parameterRulesWith
  :: forall cs s f a
   . (Laws cs, Traversable f, Eq (f ()), Typeable f, Eq a)
  => (LawBody cs -> Equation (OPENHG s (f (Parameter a))))
  -> Either String [(String, Rule s (f a))]
parameterRulesWith :: forall (cs :: [Type -> Constraint]) s (f :: Type -> Type) a.
(Laws cs, Traversable f, Eq (f ()), Typeable f, Eq a) =>
(LawBody cs -> Equation (OPENHG s (f (Parameter a))))
-> Either String [(String, Rule s (f a))]
parameterRulesWith LawBody cs -> Equation (OPENHG s (f (Parameter a)))
interpret = (Law cs -> Either String (String, Rule s (f a)))
-> [Law cs] -> Either String [(String, Rule s (f a))]
forall (t :: Type -> Type) (f :: Type -> Type) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: Type -> Type) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse Law cs -> Either String (String, Rule s (f a))
instantiate (forall (cs :: [Type -> Constraint]). Laws cs => [Law cs]
laws @cs)
  where
    instantiate :: Law cs -> Either String (String, Rule s (f a))
instantiate Law cs
law = do
      let name :: String
name = Law cs -> String
forall (cs :: [Type -> Constraint]). Law cs -> String
lawName Law cs
law
      r <- forall (cs :: [Type -> Constraint]) s (f :: Type -> Type) a.
(Laws cs, Traversable f, Eq (f ()), Typeable f, Eq a) =>
String
-> (LawBody cs -> Equation (OPENHG s (f (Parameter a))))
-> Either String (Rule s (f a))
parameterRuleWith @cs String
name LawBody cs -> Equation (OPENHG s (f (Parameter a)))
interpret
      pure (name, r)

interpretLaw
  :: forall cs k
   . (Laws cs)
  => String -> (LawBody cs -> Equation k) -> Either String (Equation k)
interpretLaw :: forall (cs :: [Type -> Constraint]) k.
Laws cs =>
String -> (LawBody cs -> Equation k) -> Either String (Equation k)
interpretLaw String
name LawBody cs -> Equation k
interpret = case (Law cs -> Bool) -> [Law cs] -> [Law cs]
forall a. (a -> Bool) -> [a] -> [a]
filter ((String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
name) (String -> Bool) -> (Law cs -> String) -> Law cs -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Law cs -> String
forall (cs :: [Type -> Constraint]). Law cs -> String
lawName) (forall (cs :: [Type -> Constraint]). Laws cs => [Law cs]
laws @cs) of
  [] -> String -> Either String (Equation k)
forall a b. a -> Either a b
Left (String
"ruleWith: unknown law " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ShowS
forall a. Show a => a -> String
show String
name)
  [Law String
_ LawBody cs
body] -> Equation k -> Either String (Equation k)
forall a b. b -> Either a b
Right (LawBody cs -> Equation k
interpret (\ @a @b @c @d @e forall (x :: k) (y :: k). (Ob x, Ob y) => String -> m (x ~> y)
mor -> forall (a :: k) (b :: k) (c :: k) (d :: k) (e :: k)
       (m :: Type -> Type).
(Labelled k, All cs k, Monad m, Ob a, Ob b, Ob c, Ob d, Ob e) =>
(forall (x :: k) (y :: k). (Ob x, Ob y) => String -> m (x ~> y))
-> m (Equation k)
LawBody cs
body @a @b @c @d @e String -> m (x ~> y)
forall (x :: k) (y :: k). (Ob x, Ob y) => String -> m (x ~> y)
mor))
  [Law cs]
_ -> String -> Either String (Equation k)
forall a b. a -> Either a b
Left (String
"ruleWith: ambiguous law " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ShowS
forall a. Show a => a -> String
show String
name)

-- | Select an explicitly instantiated law left-to-right and enumerate its single-step rewrites.
-- Compile once with 'ruleWith' when applying the same instantiation to several hosts.
rewritesWith
  :: forall cs s l a b
   . (Laws cs, Eq l, DecidableIso s)
  => String
  -> (LawBody cs -> Equation (OPENHG s l))
  -> (a :: OPENHG s l) ~> b
  -> Either String [Wires (WireSorts a) ~> (Wires (WireSorts b) :: OPENHG s l)]
rewritesWith :: forall (cs :: [Type -> Constraint]) s l (a :: OPENHG s l)
       (b :: OPENHG s l).
(Laws cs, Eq l, DecidableIso s) =>
String
-> (LawBody cs -> Equation (OPENHG s l))
-> (a ~> b)
-> Either String [Wires (WireSorts a) ~> Wires (WireSorts b)]
rewritesWith String
name = forall (cs :: [Type -> Constraint]) s l (a :: OPENHG s l)
       (b :: OPENHG s l).
(Laws cs, Eq l, DecidableIso s) =>
String
-> Direction
-> (LawBody cs -> Equation (OPENHG s l))
-> (a ~> b)
-> Either String [Wires (WireSorts a) ~> Wires (WireSorts b)]
rewritesWithDirection @cs String
name Direction
LeftToRight

-- | Like 'rewritesWith', with an explicit choice of direction.
rewritesWithDirection
  :: forall cs s l a b
   . (Laws cs, Eq l, DecidableIso s)
  => String
  -> Direction
  -> (LawBody cs -> Equation (OPENHG s l))
  -> (a :: OPENHG s l) ~> b
  -> Either String [Wires (WireSorts a) ~> (Wires (WireSorts b) :: OPENHG s l)]
rewritesWithDirection :: forall (cs :: [Type -> Constraint]) s l (a :: OPENHG s l)
       (b :: OPENHG s l).
(Laws cs, Eq l, DecidableIso s) =>
String
-> Direction
-> (LawBody cs -> Equation (OPENHG s l))
-> (a ~> b)
-> Either String [Wires (WireSorts a) ~> Wires (WireSorts b)]
rewritesWithDirection String
name Direction
direction LawBody cs -> Equation (OPENHG s l)
interpret a ~> b
host =
  (Rule s l
 -> [DecCospan
       (DC (SUB (FH (Port (PortSorts (UN SUB (UN DC a)))))))
       (DC (SUB (FH (Port (PortSorts (UN SUB (UN DC b)))))))])
-> Either String (Rule s l)
-> Either
     String
     [DecCospan
        (DC (SUB (FH (Port (PortSorts (UN SUB (UN DC a)))))))
        (DC (SUB (FH (Port (PortSorts (UN SUB (UN DC b)))))))]
forall a b. (a -> b) -> Either String a -> Either String b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (Rule s l
-> (a ~> b)
-> [DC (SUB (FH (Port (PortSorts (UN SUB (UN DC a))))))
    ~> DC (SUB (FH (Port (PortSorts (UN SUB (UN DC b))))))]
forall {s} l (a :: OPENHG s l) (b :: OPENHG s l).
(Eq l, DecidableIso s) =>
Rule s l
-> (a ~> b) -> [Wires (WireSorts a) ~> Wires (WireSorts b)]
`rewrites` a ~> b
host) (forall (cs :: [Type -> Constraint]) s l.
Laws cs =>
String
-> Direction
-> (LawBody cs -> Equation (OPENHG s l))
-> Either String (Rule s l)
ruleWithDirection @cs String
name Direction
direction LawBody cs -> Equation (OPENHG s l)
interpret)

-- | Select a law with the whole-arrow parameters of 'subgraphRule'.
subgraphRuleWithDirection
  :: forall cs s p l
   . (Laws cs)
  => String
  -> Direction
  -> (p -> Either l String)
  -> (LawBody cs -> Equation (OPENHG s p))
  -> Either String (Rule s l)
subgraphRuleWithDirection :: forall (cs :: [Type -> Constraint]) s p l.
Laws cs =>
String
-> Direction
-> (p -> Either l String)
-> (LawBody cs -> Equation (OPENHG s p))
-> Either String (Rule s l)
subgraphRuleWithDirection String
name Direction
direction p -> Either l String
classify LawBody cs -> Equation (OPENHG s p)
interpret = do
  equation <- forall (cs :: [Type -> Constraint]) k.
Laws cs =>
String -> (LawBody cs -> Equation k) -> Either String (Equation k)
interpretLaw @cs String
name LawBody cs -> Equation (OPENHG s p)
interpret
  withSides equation $ \a ~> b
lhs a ~> b
rhs -> case Direction
direction of
    Direction
LeftToRight -> (p -> Either l String)
-> (a ~> b) -> (a ~> b) -> Either String (Rule s l)
forall p l s (a :: OPENHG s p) (b :: OPENHG s p).
(p -> Either l String)
-> (a ~> b) -> (a ~> b) -> Either String (Rule s l)
subgraphRule p -> Either l String
classify a ~> b
lhs a ~> b
rhs
    Direction
RightToLeft -> (p -> Either l String)
-> (a ~> b) -> (a ~> b) -> Either String (Rule s l)
forall p l s (a :: OPENHG s p) (b :: OPENHG s p).
(p -> Either l String)
-> (a ~> b) -> (a ~> b) -> Either String (Rule s l)
subgraphRule p -> Either l String
classify a ~> b
rhs a ~> b
lhs