{-# LANGUAGE AllowAmbiguousTypes #-}
module Proarrow.Tools.HypergraphRewrite.Free
( NamedArrows (..)
, Struct (NamedArrow)
, FREE_OB
, FreeWire
, StructureBox
, fromFreeWith
, freeLawRule
, freeLawRuleWithDirection
, freeLawRules
) where
import Control.Monad (foldM, unless)
import Control.Monad.Trans.State.Strict (State, get, put, runState)
import Data.Functor.Identity (Identity (..))
import Data.Kind (Constraint, Type)
import Data.Map.Strict qualified as M
import Data.Proxy (Proxy)
import Data.Type.Equality ((:~:) (..), (:~~:) (..))
import GHC.TypeLits (Symbol)
import Type.Reflection
( SomeTypeRep (..)
, TypeRep
, Typeable
, eqTypeRep
, typeRep
, typeRepKind
, typeRepTyCon
, withTypeable
, pattern App
, pattern Con'
, pattern Fun
)
import Type.Reflection.Unsafe (mkTrCon)
import Prelude hiding (id, (.))
import Prelude qualified as P
import Proarrow.Category.Enriched.Thin (Discrete)
import Proarrow.Category.Instance.Free
( All
, Elem (..)
, Embed
, FREE (..)
, Free (..)
, HasStructure (..)
, IsFreeOb (..)
, StructureChild (..)
, fromAll
, sameElem
, structureChildren
, withLowerOb
)
import Proarrow.Category.Instance.OpenHypergraph (Box (..), OPENHG, SomeSort, Wires, unsafeOpenHypergraph)
import Proarrow.Category.Instance.Sub (SUBCAT (..))
import Proarrow.Core (Any, CAT, CategoryOf (..), Kind, Show2, UN, type (+->))
import Proarrow.Object (SomeOf (..), pattern Objs)
import Proarrow.Optic.Iso ()
import Proarrow.Profunctor.Instance.Initial (InitialProfunctor)
import Proarrow.Profunctor.Representable (Rep)
import Proarrow.Tools.HypergraphRewrite (Rule, withSortParameters)
import Proarrow.Tools.HypergraphRewrite.Laws (Direction (..), subgraphRuleWithDirection)
import Proarrow.Tools.Laws (Equation, Labelled (..), LawBody, Laws (..), ProEquation (..), lawName, withSides)
type NamedArrows :: Kind -> Constraint
class (CategoryOf k) => NamedArrows k where
namedArrow :: (Ob (a :: k), Ob b) => String -> a ~> b
instance (NamedArrows `Elem` cs) => HasStructure cs p NamedArrows where
data Struct NamedArrows a b where
NamedArrow :: (Ob a, Ob b) => String -> Struct NamedArrows a b
sameStructure :: forall {k1 :: Kind} (a :: FREE cs p) (b :: FREE cs p)
(ds :: [Kind -> Constraint]) (q :: CAT k1) (x :: FREE ds q)
(y :: FREE ds q).
Struct NamedArrows a b -> Struct NamedArrows x y -> Bool
sameStructure (NamedArrow String
x) (NamedArrow String
y) = String
x String -> String -> Bool
forall (a :: Kind). Eq a => a -> a -> Bool
P.== String
y
foldStructure :: forall {k' :: Kind} (f :: k +-> k') (m :: Kind -> Kind)
(a :: FREE cs p) (b :: FREE cs p).
(Applicative m, NamedArrows k', All cs k', Representable f) =>
(forall (x :: FREE cs p) (y :: FREE cs p).
(x ~> y) -> m (Lower f x ~> Lower f y))
-> Struct NamedArrows a b -> m (Lower f a ~> Lower f b)
foldStructure @f forall (x :: FREE cs p) (y :: FREE cs p).
(x ~> y) -> m (Lower f x ~> Lower f y)
_ (NamedArrow @a @b String
name) = (Lower f a ~> Lower f b) -> m (Lower f a ~> Lower f b)
forall (a :: Kind). a -> m a
forall (f :: Kind -> Kind) (a :: Kind). Applicative f => a -> f a
P.pure ((Lower f a ~> Lower f b) -> m (Lower f a ~> Lower f b))
-> (Lower f a ~> Lower f b) -> m (Lower f a ~> Lower f b)
forall a b. (a -> b) -> a -> b
P.$ forall {k :: Kind} {k' :: Kind} {cs :: [Kind -> Constraint]}
{p :: CAT k} (f :: k +-> k') (a :: FREE cs p) (r :: Kind).
(IsFreeOb a, Representable f, All cs k') =>
(Ob (Lower f a) => r) -> r
forall (f :: k +-> k') (a :: FREE cs p) (r :: Kind).
(IsFreeOb a, Representable f, All cs k') =>
(Ob (Lower f a) => r) -> r
withLowerOb @f @a (forall {k :: Kind} {k' :: Kind} {cs :: [Kind -> Constraint]}
{p :: CAT k} (f :: k +-> k') (a :: FREE cs p) (r :: Kind).
(IsFreeOb a, Representable f, All cs k') =>
(Ob (Lower f a) => r) -> r
forall (f :: k +-> k') (a :: FREE cs p) (r :: Kind).
(IsFreeOb a, Representable f, All cs k') =>
(Ob (Lower f a) => r) -> r
withLowerOb @f @b (String -> Lower f a ~> Lower f b
forall (a :: k') (b :: k'). (Ob a, Ob b) => String -> a ~> b
forall (k :: Kind) (a :: k) (b :: k).
(NamedArrows k, Ob a, Ob b) =>
String -> a ~> b
namedArrow String
name))
instance Show (Struct NamedArrows a b) where
show :: Struct NamedArrows a b -> String
show (NamedArrow String
name) = ShowS
forall (a :: Kind). Show a => a -> String
show String
name
instance (NamedArrows `Elem` cs) => NamedArrows (FREE cs p) where
namedArrow :: forall (a :: FREE cs p) (b :: FREE cs p).
(Ob a, Ob b) =>
String -> a ~> b
namedArrow String
name = Struct NamedArrows a b -> Free a a -> Free a b
forall {k :: Kind} {cs :: [Kind -> Constraint]} {p :: CAT k}
(c :: Kind -> Constraint) (a1 :: FREE cs p) (b :: FREE cs p)
(a :: FREE cs p).
(HasStructure cs p c, Ob a1, Ob b) =>
Struct c a1 b -> Free a a1 -> Free a b
St (String -> Struct NamedArrows a b
forall {k :: Kind} {cs :: [Kind -> Constraint]} {p :: CAT k}
(a :: FREE cs p) (b :: FREE cs p).
(Ob a, Ob b) =>
String -> Struct NamedArrows a b
NamedArrow String
name) Free a a
forall {k :: Kind} {cs :: [Kind -> Constraint]} {p :: CAT k}
(a :: FREE cs p).
Ob a =>
Free a a
Nil
type data FREE_SHAPE = FO Type
instance CategoryOf FREE_SHAPE where
type (~>) = (:~:)
type Ob a = Any a
type FREE_OB = SUBCAT (Typeable :: FREE_SHAPE -> Constraint)
type FreeWire :: forall {k} {cs} {p :: CAT k} {l}. FREE cs p -> OPENHG FREE_OB l
type FreeWire a = Wires '[SUB (FO (Proxy a))]
type StructureBox :: [Kind -> Constraint] -> Type
data StructureBox cs where
StructureBox :: (HasStructure cs p c) => Struct c (a :: FREE cs p) b -> String -> StructureBox cs
instance Eq (StructureBox cs) where
StructureBox (Struct c a b
s :: Struct c a b) String
_ == :: StructureBox cs -> StructureBox cs -> Bool
== StructureBox (Struct c a b
t :: Struct d x y) String
_ =
case ElemEvidence c cs -> ElemEvidence c cs -> Maybe (c :~: c)
forall {k :: Kind} (c :: k) (cs :: [k]) (d :: k).
ElemEvidence c cs -> ElemEvidence d cs -> Maybe (c :~: d)
sameElem (forall (c :: Kind -> Constraint) (cs :: [Kind -> Constraint]).
Elem c cs =>
ElemEvidence c cs
elemEvidence @c @cs) (forall (c :: Kind -> Constraint) (cs :: [Kind -> Constraint]).
Elem c cs =>
ElemEvidence c cs
elemEvidence @d @cs) of
Just c :~: c
Refl -> Struct c a b -> Struct c a b -> Bool
forall (k :: Kind) (cs :: [Kind -> Constraint]) (p :: CAT k)
(c :: Kind -> Constraint) {k1 :: Kind} (a :: FREE cs p)
(b :: FREE cs p) (ds :: [Kind -> Constraint]) (q :: CAT k1)
(x :: FREE ds q) (y :: FREE ds q).
HasStructure cs p c =>
Struct c a b -> Struct c x y -> Bool
forall {k1 :: Kind} (a :: FREE cs p) (b :: FREE cs p)
(ds :: [Kind -> Constraint]) (q :: CAT k1) (x :: FREE ds q)
(y :: FREE ds q).
Struct c a b -> Struct c x y -> Bool
sameStructure Struct c a b
s Struct c a b
Struct c a b
t
Maybe (c :~: c)
Nothing -> Bool
False
instance Show (StructureBox cs) where
show :: StructureBox cs -> String
show (StructureBox Struct c a b
_ String
rendered) = String
rendered
data Building cs l = Building Int [SomeSort FREE_OB] [Box (Either (StructureBox cs) l) Int]
fromFreeWith
:: forall {k} {cs} {p :: CAT k} l (a :: FREE cs p) b
. ( Typeable k
, Typeable cs
, Typeable p
, forall (x :: k). (Ob x) => Typeable x
, Discrete k
, All cs (FREE cs p)
, Show2 p
, NamedArrows `Elem` cs
)
=> (forall x y. (Ob x, Ob y) => p x y -> l)
-> (String -> l)
-> Free a b
-> (FreeWire a :: OPENHG FREE_OB (Either (StructureBox cs) l)) ~> FreeWire b
fromFreeWith :: forall {k :: Kind} {cs :: [Kind -> Constraint]} {p :: CAT k}
(l :: Kind) (a :: FREE cs p) (b :: FREE cs p).
(Typeable k, Typeable cs, Typeable p,
forall (x :: k). Ob x => Typeable x, Discrete k,
All cs (FREE cs p), Show2 p, Elem NamedArrows cs) =>
(forall (x :: k) (y :: k). (Ob x, Ob y) => p x y -> l)
-> (String -> l) -> Free a b -> FreeWire a ~> FreeWire b
fromFreeWith forall (x :: k) (y :: k). (Ob x, Ob y) => p x y -> l
generator String -> l
named term :: Free a b
term@Free a b
Objs =
forall {k :: Kind} {cs :: [Kind -> Constraint]} {p :: CAT k}
(a :: FREE cs p) (r :: Kind).
(IsFreeOb a, Typeable k, Typeable cs, Typeable p,
forall (x :: k). Ob x => Typeable x) =>
(Typeable a => r) -> r
forall (a :: FREE cs p) (r :: Kind).
(IsFreeOb a, Typeable k, Typeable cs, Typeable p,
forall (x :: k). Ob x => Typeable x) =>
(Typeable a => r) -> r
withTypeableFreeOb @a ((Typeable a => FreeWire a ~> FreeWire b)
-> FreeWire a ~> FreeWire b)
-> (Typeable a => FreeWire a ~> FreeWire b)
-> FreeWire a ~> FreeWire b
forall a b. (a -> b) -> a -> b
$
forall {k :: Kind} {cs :: [Kind -> Constraint]} {p :: CAT k}
(a :: FREE cs p) (r :: Kind).
(IsFreeOb a, Typeable k, Typeable cs, Typeable p,
forall (x :: k). Ob x => Typeable x) =>
(Typeable a => r) -> r
forall (a :: FREE cs p) (r :: Kind).
(IsFreeOb a, Typeable k, Typeable cs, Typeable p,
forall (x :: k). Ob x => Typeable x) =>
(Typeable a => r) -> r
withTypeableFreeOb @b ((Typeable b => FreeWire a ~> FreeWire b)
-> FreeWire a ~> FreeWire b)
-> (Typeable b => FreeWire a ~> FreeWire b)
-> FreeWire a ~> FreeWire b
forall a b. (a -> b) -> a -> b
$
let ((Int
input, Int
output), Building Int
_ [SomeSort FREE_OB]
nodes [Box (Either (StructureBox cs) l) Int]
boxes) = State (Building cs l) (Int, Int)
-> Building cs l -> ((Int, Int), Building cs l)
forall (s :: Kind) (a :: Kind). State s a -> s -> (a, s)
runState State (Building cs l) (Int, Int)
build (Int
-> [SomeSort FREE_OB]
-> [Box (Either (StructureBox cs) l) Int]
-> Building cs l
forall (cs :: [Kind -> Constraint]) (l :: Kind).
Int
-> [SomeSort FREE_OB]
-> [Box (Either (StructureBox cs) l) Int]
-> Building cs l
Building Int
0 [] [])
in [SomeSort FREE_OB]
-> [Int]
-> [Int]
-> [Box (Either (StructureBox cs) l) Int]
-> FreeWire a ~> FreeWire b
forall {s :: Kind} (l :: Kind) (as :: [s]) (bs :: [s]).
(SortList as, SortList bs) =>
[SomeSort s]
-> [Int] -> [Int] -> [Box l Int] -> Wires as ~> Wires bs
unsafeOpenHypergraph ([SomeSort FREE_OB] -> [SomeSort FREE_OB]
forall (a :: Kind). [a] -> [a]
reverse [SomeSort FREE_OB]
nodes) [Int
input] [Int
output] ([Box (Either (StructureBox cs) l) Int]
-> [Box (Either (StructureBox cs) l) Int]
forall (a :: Kind). [a] -> [a]
reverse [Box (Either (StructureBox cs) l) Int]
boxes)
where
build :: State (Building cs l) (Int, Int)
build = do
input <- forall (x :: FREE cs p). Ob x => State (Building cs l) Int
fresh @a
output <- go input term
pure (input, output)
fresh :: forall (x :: FREE cs p). (Ob x) => State (Building cs l) Int
fresh :: forall (x :: FREE cs p). Ob x => State (Building cs l) Int
fresh = forall {k :: Kind} {cs :: [Kind -> Constraint]} {p :: CAT k}
(a :: FREE cs p) (r :: Kind).
(IsFreeOb a, Typeable k, Typeable cs, Typeable p,
forall (x :: k). Ob x => Typeable x) =>
(Typeable a => r) -> r
forall (a :: FREE cs p) (r :: Kind).
(IsFreeOb a, Typeable k, Typeable cs, Typeable p,
forall (x :: k). Ob x => Typeable x) =>
(Typeable a => r) -> r
withTypeableFreeOb @x ((Typeable x => State (Building cs l) Int)
-> State (Building cs l) Int)
-> (Typeable x => State (Building cs l) Int)
-> State (Building cs l) Int
forall a b. (a -> b) -> a -> b
$ do
Building next nodes boxes <- StateT (Building cs l) Identity (Building cs l)
forall (m :: Kind -> Kind) (s :: Kind). Monad m => StateT s m s
get
put (Building (next + 1) (Some @(SUB (FO (Proxy x))) : nodes) boxes)
pure next
emit :: Either (StructureBox cs) l
-> [Int] -> [Int] -> StateT (Building cs l) m ()
emit Either (StructureBox cs) l
boxLabel [Int]
inputs [Int]
outputs = do
Building next nodes boxes <- StateT (Building cs l) m (Building cs l)
forall (m :: Kind -> Kind) (s :: Kind). Monad m => StateT s m s
get
put (Building next nodes (Box boxLabel inputs outputs : boxes))
go :: forall (x :: FREE cs p) y. Int -> Free x y -> State (Building cs l) Int
go :: forall (x :: FREE cs p) (y :: FREE cs p).
Int -> Free x y -> State (Building cs l) Int
go Int
input Free x y
Nil = Int -> State (Building cs l) Int
forall (a :: Kind). a -> StateT (Building cs l) Identity a
forall (f :: Kind -> Kind) (a :: Kind). Applicative f => a -> f a
pure Int
input
go Int
input (Emb p a1 b1
p Free x (EMB a1)
previous) = do
mid <- Int -> Free x (EMB a1) -> State (Building cs l) Int
forall (x :: FREE cs p) (y :: FREE cs p).
Int -> Free x y -> State (Building cs l) Int
go Int
input Free x (EMB a1)
previous
output <- fresh @y
emit (Right (generator p)) [mid] [output]
pure output
go Int
input (St @c Struct c a1 y
s Free x a1
previous) = do
mid <- Int -> Free x a1 -> State (Building cs l) Int
forall (x :: FREE cs p) (y :: FREE cs p).
Int -> Free x y -> State (Building cs l) Int
go Int
input Free x a1
previous
output <- fresh @y
case sameElem (elemEvidence @c @cs) (elemEvidence @NamedArrows @cs) of
Just c :~: NamedArrows
Refl -> case Struct c a1 y
s of
NamedArrow String
name -> Either (StructureBox cs) l
-> [Int] -> [Int] -> StateT (Building cs l) Identity ()
forall {m :: Kind -> Kind} {cs :: [Kind -> Constraint]}
{l :: Kind}.
Monad m =>
Either (StructureBox cs) l
-> [Int] -> [Int] -> StateT (Building cs l) m ()
emit (l -> Either (StructureBox cs) l
forall (a :: Kind) (b :: Kind). b -> Either a b
Right (String -> l
named String
name)) [Int
mid] [Int
output]
Maybe (c :~: NamedArrows)
Nothing -> do
children <- (StructureChild cs p -> State (Building cs l) (Int, Int))
-> [StructureChild cs p]
-> StateT (Building cs l) Identity [(Int, Int)]
forall (t :: Kind -> Kind) (f :: Kind -> Kind) (a :: Kind)
(b :: Kind).
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: Kind -> Kind) (a :: Kind) (b :: Kind).
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse StructureChild cs p -> State (Building cs l) (Int, Int)
child (Struct c a1 y -> [StructureChild cs p]
forall {k :: Kind} {cs :: [Kind -> Constraint]} {p :: CAT k}
(c :: Kind -> Constraint) (a :: FREE cs p) (b :: FREE cs p).
(HasStructure cs p c, Discrete k, All cs (FREE cs p)) =>
Struct c a b -> [StructureChild cs p]
structureChildren Struct c a1 y
s)
let rendered =
forall (c :: Kind -> Constraint) (cs :: [Kind -> Constraint])
(k :: Kind) (r :: Kind).
(Elem c cs, All cs k) =>
(c k => r) -> r
fromAll @c @cs @(FREE cs p) ((c (FREE cs p) => String) -> String)
-> (c (FREE cs p) => String) -> String
forall a b. (a -> b) -> a -> b
$
Free a1 y -> String
forall (a :: Kind). Show a => a -> String
show
( Identity (Free a1 y) -> Free a1 y
forall (a :: Kind). Identity a -> a
runIdentity
( forall (k :: Kind) (cs :: [Kind -> Constraint]) (p :: CAT k)
(c :: Kind -> Constraint) {k' :: Kind} (f :: k +-> k')
(m :: Kind -> Kind) (a :: FREE cs p) (b :: FREE cs p).
(HasStructure cs p c, Applicative m, c k', All cs k',
Representable f) =>
(forall (x :: FREE cs p) (y :: FREE cs p).
(x ~> y) -> m (Lower f x ~> Lower f y))
-> Struct c a b -> m (Lower f a ~> Lower f b)
foldStructure @_ @_ @_ @_ @(Rep (Embed :: k +-> FREE cs p))
(\x ~> y
Free x y
Objs -> Free x y -> Identity (Free x y)
forall (a :: Kind). a -> Identity a
Identity (String -> x ~> y
forall (k :: Kind) (a :: k) (b :: k).
(NamedArrows k, Ob a, Ob b) =>
String -> a ~> b
forall (a :: FREE cs p) (b :: FREE cs p).
(Ob a, Ob b) =>
String -> a ~> b
namedArrow String
"_"))
Struct c a1 y
s
)
)
emit
(Left (StructureBox s rendered))
(mid : map snd children)
(output : map fst children)
pure output
child :: StructureChild cs p -> State (Building cs l) (Int, Int)
child :: StructureChild cs p -> State (Building cs l) (Int, Int)
child (StructureChild (f :: Free a b
f@Free a b
Objs :: Free x y)) = do
input <- forall (x :: FREE cs p). Ob x => State (Building cs l) Int
fresh @x
output <- go input f
pure (input, output)
type data OBJECT_NAMES = OBJECT_NAME Symbol
instance CategoryOf OBJECT_NAMES where
type (~>) = (:~:)
type Ob a = Any a
type OBJECT_VARIABLES = SUBCAT (Typeable :: OBJECT_NAMES -> Constraint)
type ObjectQuiver = InitialProfunctor :: CAT OBJECT_VARIABLES
type ObjectSyntax :: [Kind -> Constraint] -> Kind
type ObjectSyntax cs = FREE cs ObjectQuiver
type ObjectVariable :: forall cs. Symbol -> ObjectSyntax cs
type ObjectVariable name = EMB (SUB (OBJECT_NAME name))
instance Labelled (ObjectSyntax cs) where
label :: forall (a :: ObjectSyntax cs) (b :: ObjectSyntax cs).
String -> (a ~> b) -> a ~> b
label String
_ a ~> b
f = a ~> b
f
freeLawRule
:: forall laws cs a
. (Laws laws, Typeable cs, All cs (ObjectSyntax cs), All laws (ObjectSyntax cs), NamedArrows `Elem` cs, Eq a)
=> String -> Either String (Rule FREE_OB (Either (StructureBox cs) a))
freeLawRule :: forall (laws :: [Kind -> Constraint]) (cs :: [Kind -> Constraint])
(a :: Kind).
(Laws laws, Typeable cs, All cs (ObjectSyntax cs),
All laws (ObjectSyntax cs), Elem NamedArrows cs, Eq a) =>
String -> Either String (Rule FREE_OB (Either (StructureBox cs) a))
freeLawRule String
name = forall (laws :: [Kind -> Constraint]) (cs :: [Kind -> Constraint])
(a :: Kind).
(Laws laws, Typeable cs, All cs (ObjectSyntax cs),
All laws (ObjectSyntax cs), Elem NamedArrows cs, Eq a) =>
String
-> Direction
-> Either String (Rule FREE_OB (Either (StructureBox cs) a))
freeLawRuleWithDirection @laws @cs String
name Direction
LeftToRight
freeLawRuleWithDirection
:: forall laws cs a
. (Laws laws, Typeable cs, All cs (ObjectSyntax cs), All laws (ObjectSyntax cs), NamedArrows `Elem` cs, Eq a)
=> String -> Direction -> Either String (Rule FREE_OB (Either (StructureBox cs) a))
freeLawRuleWithDirection :: forall (laws :: [Kind -> Constraint]) (cs :: [Kind -> Constraint])
(a :: Kind).
(Laws laws, Typeable cs, All cs (ObjectSyntax cs),
All laws (ObjectSyntax cs), Elem NamedArrows cs, Eq a) =>
String
-> Direction
-> Either String (Rule FREE_OB (Either (StructureBox cs) a))
freeLawRuleWithDirection String
name Direction
direction =
([SomeSort FREE_OB]
-> [SomeSort FREE_OB]
-> [SomeSort FREE_OB]
-> Maybe [SomeSort FREE_OB])
-> Rule FREE_OB (Either (StructureBox cs) a)
-> Rule FREE_OB (Either (StructureBox cs) a)
forall (s :: Kind) (l :: Kind).
([SomeSort s]
-> [SomeSort s] -> [SomeSort s] -> Maybe [SomeSort s])
-> Rule s l -> Rule s l
withSortParameters (forall (cs :: [Kind -> Constraint]).
Typeable cs =>
[SomeSort FREE_OB]
-> [SomeSort FREE_OB]
-> [SomeSort FREE_OB]
-> Maybe [SomeSort FREE_OB]
matchObjectSorts @cs)
(Rule FREE_OB (Either (StructureBox cs) a)
-> Rule FREE_OB (Either (StructureBox cs) a))
-> Either String (Rule FREE_OB (Either (StructureBox cs) a))
-> Either String (Rule FREE_OB (Either (StructureBox cs) a))
forall (f :: Kind -> Kind) (a :: Kind) (b :: Kind).
Functor f =>
(a -> b) -> f a -> f b
<$> forall (cs :: [Kind -> Constraint]) (s :: Kind) (p :: Kind)
(l :: Kind).
Laws cs =>
String
-> Direction
-> (p -> Either l String)
-> (LawBody cs -> Equation (OPENHG s p))
-> Either String (Rule s l)
subgraphRuleWithDirection @laws String
name Direction
direction ((StructureBox cs -> Either (Either (StructureBox cs) a) String)
-> (String -> Either (Either (StructureBox cs) a) String)
-> Either (StructureBox cs) String
-> Either (Either (StructureBox cs) a) String
forall (a :: Kind) (c :: Kind) (b :: Kind).
(a -> c) -> (b -> c) -> Either a b -> c
either (Either (StructureBox cs) a
-> Either (Either (StructureBox cs) a) String
forall (a :: Kind) (b :: Kind). a -> Either a b
Left (Either (StructureBox cs) a
-> Either (Either (StructureBox cs) a) String)
-> (StructureBox cs -> Either (StructureBox cs) a)
-> StructureBox cs
-> Either (Either (StructureBox cs) a) String
forall (b :: Kind) (c :: Kind) (a :: Kind).
(b -> c) -> (a -> b) -> a -> c
P.. StructureBox cs -> Either (StructureBox cs) a
forall (a :: Kind) (b :: Kind). a -> Either a b
Left) String -> Either (Either (StructureBox cs) a) String
forall (a :: Kind) (b :: Kind). b -> Either a b
Right) (forall (laws :: [Kind -> Constraint]) (cs :: [Kind -> Constraint]).
(Typeable cs, All cs (ObjectSyntax cs), All laws (ObjectSyntax cs),
Elem NamedArrows cs) =>
LawBody laws
-> Equation (OPENHG FREE_OB (Either (StructureBox cs) String))
objectLaw @laws @cs)
freeLawRules
:: forall laws cs a
. (Laws laws, Typeable cs, All cs (ObjectSyntax cs), All laws (ObjectSyntax cs), NamedArrows `Elem` cs, Eq a)
=> Either String [(String, Rule FREE_OB (Either (StructureBox cs) a))]
freeLawRules :: forall (laws :: [Kind -> Constraint]) (cs :: [Kind -> Constraint])
(a :: Kind).
(Laws laws, Typeable cs, All cs (ObjectSyntax cs),
All laws (ObjectSyntax cs), Elem NamedArrows cs, Eq a) =>
Either String [(String, Rule FREE_OB (Either (StructureBox cs) a))]
freeLawRules =
(Law laws
-> Either
String (String, Rule FREE_OB (Either (StructureBox cs) a)))
-> [Law laws]
-> Either
String [(String, Rule FREE_OB (Either (StructureBox cs) a))]
forall (t :: Kind -> Kind) (f :: Kind -> Kind) (a :: Kind)
(b :: Kind).
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: Kind -> Kind) (a :: Kind) (b :: Kind).
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (\Law laws
law -> (Law laws -> String
forall (cs :: [Kind -> Constraint]). Law cs -> String
lawName Law laws
law,) (Rule FREE_OB (Either (StructureBox cs) a)
-> (String, Rule FREE_OB (Either (StructureBox cs) a)))
-> Either String (Rule FREE_OB (Either (StructureBox cs) a))
-> Either
String (String, Rule FREE_OB (Either (StructureBox cs) a))
forall (f :: Kind -> Kind) (a :: Kind) (b :: Kind).
Functor f =>
(a -> b) -> f a -> f b
<$> forall (laws :: [Kind -> Constraint]) (cs :: [Kind -> Constraint])
(a :: Kind).
(Laws laws, Typeable cs, All cs (ObjectSyntax cs),
All laws (ObjectSyntax cs), Elem NamedArrows cs, Eq a) =>
String -> Either String (Rule FREE_OB (Either (StructureBox cs) a))
freeLawRule @laws @cs (Law laws -> String
forall (cs :: [Kind -> Constraint]). Law cs -> String
lawName Law laws
law)) (forall (cs :: [Kind -> Constraint]). Laws cs => [Law cs]
laws @laws)
objectLaw
:: forall laws cs
. (Typeable cs, All cs (ObjectSyntax cs), All laws (ObjectSyntax cs), NamedArrows `Elem` cs)
=> LawBody laws -> Equation (OPENHG FREE_OB (Either (StructureBox cs) String))
objectLaw :: forall (laws :: [Kind -> Constraint]) (cs :: [Kind -> Constraint]).
(Typeable cs, All cs (ObjectSyntax cs), All laws (ObjectSyntax cs),
Elem NamedArrows cs) =>
LawBody laws
-> Equation (OPENHG FREE_OB (Either (StructureBox cs) String))
objectLaw LawBody laws
body =
Equation (ObjectSyntax cs)
-> (forall (a :: ObjectSyntax cs) (b :: ObjectSyntax cs).
(a ~> b) -> (a ~> b) -> ProEquation DecCospan)
-> ProEquation DecCospan
forall {k :: Kind} (r :: Kind).
Equation k
-> (forall (a :: k) (b :: k). (a ~> b) -> (a ~> b) -> r) -> r
withSides
( Identity (Equation (ObjectSyntax cs)) -> Equation (ObjectSyntax cs)
forall (a :: Kind). Identity a -> a
runIdentity
( LawBody laws
forall (a :: ObjectSyntax cs) (b :: ObjectSyntax cs)
(c :: ObjectSyntax cs) (d :: ObjectSyntax cs)
(e :: ObjectSyntax cs) (m :: Kind -> Kind).
(Labelled (ObjectSyntax cs), All laws (ObjectSyntax cs), Monad m,
Ob a, Ob b, Ob c, Ob d, Ob e) =>
(forall (x :: ObjectSyntax cs) (y :: ObjectSyntax cs).
(Ob x, Ob y) =>
String -> m (x ~> y))
-> m (Equation (ObjectSyntax cs))
body
@(ObjectVariable @cs "a")
@(ObjectVariable @cs "b")
@(ObjectVariable @cs "c")
@(ObjectVariable @cs "d")
@(ObjectVariable @cs "e")
(\String
name -> Free x y -> Identity (Free x y)
forall (a :: Kind). a -> Identity a
Identity (String -> x ~> y
forall (k :: Kind) (a :: k) (b :: k).
(NamedArrows k, Ob a, Ob b) =>
String -> a ~> b
forall (a :: ObjectSyntax cs) (b :: ObjectSyntax cs).
(Ob a, Ob b) =>
String -> a ~> b
namedArrow String
name))
)
)
(\a ~> b
lhs a ~> b
rhs -> (forall (x :: SUBCAT Typeable) (y :: SUBCAT Typeable).
(Ob x, Ob y) =>
InitialProfunctor x y -> String)
-> ShowS
-> Free a b
-> DC (SUB (FH (Port '[SUB (FO (Proxy a))])))
~> DC (SUB (FH (Port '[SUB (FO (Proxy b))])))
forall {k :: Kind} {cs :: [Kind -> Constraint]} {p :: CAT k}
(l :: Kind) (a :: FREE cs p) (b :: FREE cs p).
(Typeable k, Typeable cs, Typeable p,
forall (x :: k). Ob x => Typeable x, Discrete k,
All cs (FREE cs p), Show2 p, Elem NamedArrows cs) =>
(forall (x :: k) (y :: k). (Ob x, Ob y) => p x y -> l)
-> (String -> l) -> Free a b -> FreeWire a ~> FreeWire b
fromFreeWith (\case {}) ShowS
forall (a :: Kind). a -> a
P.id a ~> b
Free a b
lhs DecCospan
(DC (SUB (FH (Port '[SUB (FO (Proxy a))]))))
(DC (SUB (FH (Port '[SUB (FO (Proxy b))]))))
-> DecCospan
(DC (SUB (FH (Port '[SUB (FO (Proxy a))]))))
(DC (SUB (FH (Port '[SUB (FO (Proxy b))]))))
-> ProEquation DecCospan
forall {j :: Kind} {k :: Kind} (p :: j +-> k) (a :: k) (b :: j).
p a b -> p a b -> ProEquation p
:=: (forall (x :: SUBCAT Typeable) (y :: SUBCAT Typeable).
(Ob x, Ob y) =>
InitialProfunctor x y -> String)
-> ShowS
-> Free a b
-> DC (SUB (FH (Port '[SUB (FO (Proxy a))])))
~> DC (SUB (FH (Port '[SUB (FO (Proxy b))])))
forall {k :: Kind} {cs :: [Kind -> Constraint]} {p :: CAT k}
(l :: Kind) (a :: FREE cs p) (b :: FREE cs p).
(Typeable k, Typeable cs, Typeable p,
forall (x :: k). Ob x => Typeable x, Discrete k,
All cs (FREE cs p), Show2 p, Elem NamedArrows cs) =>
(forall (x :: k) (y :: k). (Ob x, Ob y) => p x y -> l)
-> (String -> l) -> Free a b -> FreeWire a ~> FreeWire b
fromFreeWith (\case {}) ShowS
forall (a :: Kind). a -> a
P.id a ~> b
Free a b
rhs)
objectType :: SomeSort FREE_OB -> Maybe SomeTypeRep
objectType :: SomeSort FREE_OB -> Maybe SomeTypeRep
objectType (Some @x) = case forall {k :: Kind} (a :: k). Typeable a => TypeRep a
forall (a :: FREE_SHAPE). Typeable a => TypeRep a
typeRep @(UN SUB x) of
App TypeRep a
outer (App TypeRep a
proxy TypeRep b
a)
| TypeRep a -> TyCon
forall {k :: Kind} (a :: k). TypeRep a -> TyCon
typeRepTyCon TypeRep a
outer TyCon -> TyCon -> Bool
forall (a :: Kind). Eq a => a -> a -> Bool
== TypeRep (FO ()) -> TyCon
forall {k :: Kind} (a :: k). TypeRep a -> TyCon
typeRepTyCon (forall {k :: Kind} (a :: k). Typeable a => TypeRep a
forall (a :: FREE_SHAPE). Typeable a => TypeRep a
typeRep @(FO ()))
, TypeRep a -> TyCon
forall {k :: Kind} (a :: k). TypeRep a -> TyCon
typeRepTyCon TypeRep a
proxy TyCon -> TyCon -> Bool
forall (a :: Kind). Eq a => a -> a -> Bool
== TypeRep (Proxy ()) -> TyCon
forall {k :: Kind} (a :: k). TypeRep a -> TyCon
typeRepTyCon (forall (a :: Kind). Typeable a => TypeRep a
forall {k :: Kind} (a :: k). Typeable a => TypeRep a
typeRep @(Proxy ())) ->
SomeTypeRep -> Maybe SomeTypeRep
forall (a :: Kind). a -> Maybe a
Just (TypeRep b -> SomeTypeRep
forall (k :: Kind) (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep TypeRep b
a)
TypeRep (UN SUB x)
_ -> Maybe SomeTypeRep
forall (a :: Kind). Maybe a
Nothing
objectSort :: SomeTypeRep -> SomeSort FREE_OB
objectSort :: SomeTypeRep -> SomeSort FREE_OB
objectSort (SomeTypeRep (TypeRep a
rep :: TypeRep a)) =
TypeRep k -> (Typeable k => SomeSort FREE_OB) -> SomeSort FREE_OB
forall (k :: Kind) (a :: k) r. TypeRep a -> (Typeable a => r) -> r
withTypeable (TypeRep a -> TypeRep k
forall (k :: Kind) (a :: k). TypeRep a -> TypeRep k
typeRepKind TypeRep a
rep) ((Typeable k => SomeSort FREE_OB) -> SomeSort FREE_OB)
-> (Typeable k => SomeSort FREE_OB) -> SomeSort FREE_OB
forall a b. (a -> b) -> a -> b
$ TypeRep a -> (Typeable a => SomeSort FREE_OB) -> SomeSort FREE_OB
forall (k :: Kind) (a :: k) r. TypeRep a -> (Typeable a => r) -> r
withTypeable TypeRep a
rep ((Typeable a => SomeSort FREE_OB) -> SomeSort FREE_OB)
-> (Typeable a => SomeSort FREE_OB) -> SomeSort FREE_OB
forall a b. (a -> b) -> a -> b
$ forall {k :: Kind} {c :: OB k} (x :: k). c x => SomeOf c
forall (x :: FREE_OB). SortOb x => SomeSort FREE_OB
Some @(SUB (FO (Proxy a)))
matchObjectSorts
:: forall (cs :: [Kind -> Constraint])
. (Typeable cs)
=> [SomeSort FREE_OB] -> [SomeSort FREE_OB] -> [SomeSort FREE_OB] -> Maybe [SomeSort FREE_OB]
matchObjectSorts :: forall (cs :: [Kind -> Constraint]).
Typeable cs =>
[SomeSort FREE_OB]
-> [SomeSort FREE_OB]
-> [SomeSort FREE_OB]
-> Maybe [SomeSort FREE_OB]
matchObjectSorts [SomeSort FREE_OB]
patterns [SomeSort FREE_OB]
actual [SomeSort FREE_OB]
replacement = do
templates <- (SomeSort FREE_OB -> Maybe SomeTypeRep)
-> [SomeSort FREE_OB] -> Maybe [SomeTypeRep]
forall (t :: Kind -> Kind) (f :: Kind -> Kind) (a :: Kind)
(b :: Kind).
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: Kind -> Kind) (a :: Kind) (b :: Kind).
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse SomeSort FREE_OB -> Maybe SomeTypeRep
objectType [SomeSort FREE_OB]
patterns
objects <- traverse objectType actual
rhs <- traverse objectType replacement
unless (length templates == length objects) Nothing
first <- case objects of
SomeTypeRep
x : [SomeTypeRep]
_ -> SomeTypeRep -> Maybe SomeTypeRep
forall (a :: Kind). a -> Maybe a
Just SomeTypeRep
x
[] -> Maybe SomeTypeRep
forall (a :: Kind). Maybe a
Nothing
(hostKind, hostBase, hostQuiver) <- sourceContext first
unless (all ((== hostKind) P.. objectKind) objects) Nothing
let context =
[(SomeTypeRep, SomeTypeRep)] -> Map SomeTypeRep SomeTypeRep
forall (k :: Kind) (a :: Kind). Ord k => [(k, a)] -> Map k a
M.fromList
[ (TypeRep (ObjectSyntax cs) -> SomeTypeRep
forall (k :: Kind) (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep (forall (a :: Kind). Typeable a => TypeRep a
forall {k :: Kind} (a :: k). Typeable a => TypeRep a
typeRep @(ObjectSyntax cs)), SomeTypeRep
hostKind)
, (TypeRep (SUBCAT Typeable) -> SomeTypeRep
forall (k :: Kind) (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep (forall (a :: Kind). Typeable a => TypeRep a
forall {k :: Kind} (a :: k). Typeable a => TypeRep a
typeRep @OBJECT_VARIABLES), SomeTypeRep
hostBase)
, (TypeRep InitialProfunctor -> SomeTypeRep
forall (k :: Kind) (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep (forall {k :: Kind} (a :: k). Typeable a => TypeRep a
forall (a :: CAT (SUBCAT Typeable)). Typeable a => TypeRep a
typeRep @ObjectQuiver), SomeTypeRep
hostQuiver)
]
bindings <- foldM (\Map SomeTypeRep SomeTypeRep
env (SomeTypeRep
template, SomeTypeRep
object) -> Map SomeTypeRep SomeTypeRep
-> SomeTypeRep
-> SomeTypeRep
-> Maybe (Map SomeTypeRep SomeTypeRep)
unify Map SomeTypeRep SomeTypeRep
env SomeTypeRep
template SomeTypeRep
object) context (zip templates objects)
specialised <- traverse (substitute bindings) rhs
unless (all ((== hostKind) P.. objectKind) specialised) Nothing
pure (map objectSort specialised)
where
variables :: [SomeTypeRep]
variables =
[ TypeRep (ObjectVariable "a") -> SomeTypeRep
forall (k :: Kind) (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep (forall {k :: Kind} (a :: k). Typeable a => TypeRep a
forall (a :: ObjectSyntax cs). Typeable a => TypeRep a
typeRep @(ObjectVariable @cs "a"))
, TypeRep (ObjectVariable "b") -> SomeTypeRep
forall (k :: Kind) (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep (forall {k :: Kind} (a :: k). Typeable a => TypeRep a
forall (a :: ObjectSyntax cs). Typeable a => TypeRep a
typeRep @(ObjectVariable @cs "b"))
, TypeRep (ObjectVariable "c") -> SomeTypeRep
forall (k :: Kind) (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep (forall {k :: Kind} (a :: k). Typeable a => TypeRep a
forall (a :: ObjectSyntax cs). Typeable a => TypeRep a
typeRep @(ObjectVariable @cs "c"))
, TypeRep (ObjectVariable "d") -> SomeTypeRep
forall (k :: Kind) (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep (forall {k :: Kind} (a :: k). Typeable a => TypeRep a
forall (a :: ObjectSyntax cs). Typeable a => TypeRep a
typeRep @(ObjectVariable @cs "d"))
, TypeRep (ObjectVariable "e") -> SomeTypeRep
forall (k :: Kind) (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep (forall {k :: Kind} (a :: k). Typeable a => TypeRep a
forall (a :: ObjectSyntax cs). Typeable a => TypeRep a
typeRep @(ObjectVariable @cs "e"))
]
objectKind :: SomeTypeRep -> SomeTypeRep
objectKind (SomeTypeRep TypeRep a
rep) = TypeRep k -> SomeTypeRep
forall (k :: Kind) (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep (TypeRep a -> TypeRep k
forall (k :: Kind) (a :: k). TypeRep a -> TypeRep k
typeRepKind TypeRep a
rep)
sourceContext :: SomeTypeRep -> Maybe (SomeTypeRep, SomeTypeRep, SomeTypeRep)
sourceContext (SomeTypeRep TypeRep a
rep) = case TypeRep a -> TypeRep k
forall (k :: Kind) (a :: k). TypeRep a -> TypeRep k
typeRepKind TypeRep a
rep of
kind :: TypeRep k
kind@(App (App (Con' TyCon
constructor [SomeTypeRep
base]) TypeRep b
structures) TypeRep b
quiver)
| TyCon
constructor TyCon -> TyCon -> Bool
forall (a :: Kind). Eq a => a -> a -> Bool
== TypeRep (ObjectSyntax '[]) -> TyCon
forall {k :: Kind} (a :: k). TypeRep a -> TyCon
typeRepTyCon (forall (a :: Kind). Typeable a => TypeRep a
forall {k :: Kind} (a :: k). Typeable a => TypeRep a
typeRep @(ObjectSyntax '[]))
, TypeRep b -> SomeTypeRep
forall (k :: Kind) (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep TypeRep b
structures SomeTypeRep -> SomeTypeRep -> Bool
forall (a :: Kind). Eq a => a -> a -> Bool
== TypeRep cs -> SomeTypeRep
forall (k :: Kind) (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep (forall (a :: [Kind -> Constraint]). Typeable a => TypeRep a
forall {k :: Kind} (a :: k). Typeable a => TypeRep a
typeRep @cs) ->
(SomeTypeRep, SomeTypeRep, SomeTypeRep)
-> Maybe (SomeTypeRep, SomeTypeRep, SomeTypeRep)
forall (a :: Kind). a -> Maybe a
Just (TypeRep k -> SomeTypeRep
forall (k :: Kind) (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep TypeRep k
kind, SomeTypeRep
base, TypeRep b -> SomeTypeRep
forall (k :: Kind) (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep TypeRep b
quiver)
TypeRep k
_ -> Maybe (SomeTypeRep, SomeTypeRep, SomeTypeRep)
forall (a :: Kind). Maybe a
Nothing
unify :: Map SomeTypeRep SomeTypeRep
-> SomeTypeRep
-> SomeTypeRep
-> Maybe (Map SomeTypeRep SomeTypeRep)
unify Map SomeTypeRep SomeTypeRep
env SomeTypeRep
template SomeTypeRep
object
| Just SomeTypeRep
expected <- SomeTypeRep -> Map SomeTypeRep SomeTypeRep -> Maybe SomeTypeRep
forall (k :: Kind) (a :: Kind). Ord k => k -> Map k a -> Maybe a
M.lookup SomeTypeRep
template Map SomeTypeRep SomeTypeRep
env = if SomeTypeRep
expected SomeTypeRep -> SomeTypeRep -> Bool
forall (a :: Kind). Eq a => a -> a -> Bool
== SomeTypeRep
object then Map SomeTypeRep SomeTypeRep -> Maybe (Map SomeTypeRep SomeTypeRep)
forall (a :: Kind). a -> Maybe a
Just Map SomeTypeRep SomeTypeRep
env else Maybe (Map SomeTypeRep SomeTypeRep)
forall (a :: Kind). Maybe a
Nothing
| SomeTypeRep
template SomeTypeRep -> [SomeTypeRep] -> Bool
forall (a :: Kind). Eq a => a -> [a] -> Bool
forall (t :: Kind -> Kind) (a :: Kind).
(Foldable t, Eq a) =>
a -> t a -> Bool
`elem` [SomeTypeRep]
variables = Map SomeTypeRep SomeTypeRep -> Maybe (Map SomeTypeRep SomeTypeRep)
forall (a :: Kind). a -> Maybe a
Just (SomeTypeRep
-> SomeTypeRep
-> Map SomeTypeRep SomeTypeRep
-> Map SomeTypeRep SomeTypeRep
forall (k :: Kind) (a :: Kind).
Ord k =>
k -> a -> Map k a -> Map k a
M.insert SomeTypeRep
template SomeTypeRep
object Map SomeTypeRep SomeTypeRep
env)
| SomeTypeRep
template SomeTypeRep -> SomeTypeRep -> Bool
forall (a :: Kind). Eq a => a -> a -> Bool
== SomeTypeRep
object = Map SomeTypeRep SomeTypeRep -> Maybe (Map SomeTypeRep SomeTypeRep)
forall (a :: Kind). a -> Maybe a
Just Map SomeTypeRep SomeTypeRep
env
unify Map SomeTypeRep SomeTypeRep
env (SomeTypeRep (App TypeRep a
f TypeRep b
x)) (SomeTypeRep (App TypeRep a
g TypeRep b
y)) =
Map SomeTypeRep SomeTypeRep
-> SomeTypeRep
-> SomeTypeRep
-> Maybe (Map SomeTypeRep SomeTypeRep)
unify Map SomeTypeRep SomeTypeRep
env (TypeRep a -> SomeTypeRep
forall (k :: Kind) (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep TypeRep a
f) (TypeRep a -> SomeTypeRep
forall (k :: Kind) (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep TypeRep a
g) Maybe (Map SomeTypeRep SomeTypeRep)
-> (Map SomeTypeRep SomeTypeRep
-> Maybe (Map SomeTypeRep SomeTypeRep))
-> Maybe (Map SomeTypeRep SomeTypeRep)
forall (a :: Kind) (b :: Kind).
Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: Kind -> Kind) (a :: Kind) (b :: Kind).
Monad m =>
m a -> (a -> m b) -> m b
>>= \Map SomeTypeRep SomeTypeRep
env' -> Map SomeTypeRep SomeTypeRep
-> SomeTypeRep
-> SomeTypeRep
-> Maybe (Map SomeTypeRep SomeTypeRep)
unify Map SomeTypeRep SomeTypeRep
env' (TypeRep b -> SomeTypeRep
forall (k :: Kind) (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep TypeRep b
x) (TypeRep b -> SomeTypeRep
forall (k :: Kind) (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep TypeRep b
y)
unify Map SomeTypeRep SomeTypeRep
env (SomeTypeRep (Con' TyCon
c [SomeTypeRep]
ks)) (SomeTypeRep (Con' TyCon
d [SomeTypeRep]
ls))
| TyCon
c TyCon -> TyCon -> Bool
forall (a :: Kind). Eq a => a -> a -> Bool
== TyCon
d, [SomeTypeRep] -> Int
forall (a :: Kind). [a] -> Int
forall (t :: Kind -> Kind) (a :: Kind). Foldable t => t a -> Int
length [SomeTypeRep]
ks Int -> Int -> Bool
forall (a :: Kind). Eq a => a -> a -> Bool
== [SomeTypeRep] -> Int
forall (a :: Kind). [a] -> Int
forall (t :: Kind -> Kind) (a :: Kind). Foldable t => t a -> Int
length [SomeTypeRep]
ls = (Map SomeTypeRep SomeTypeRep
-> (SomeTypeRep, SomeTypeRep)
-> Maybe (Map SomeTypeRep SomeTypeRep))
-> Map SomeTypeRep SomeTypeRep
-> [(SomeTypeRep, SomeTypeRep)]
-> Maybe (Map SomeTypeRep SomeTypeRep)
forall (t :: Kind -> Kind) (m :: Kind -> Kind) (b :: Kind)
(a :: Kind).
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM (\Map SomeTypeRep SomeTypeRep
e (SomeTypeRep
a, SomeTypeRep
b) -> Map SomeTypeRep SomeTypeRep
-> SomeTypeRep
-> SomeTypeRep
-> Maybe (Map SomeTypeRep SomeTypeRep)
unify Map SomeTypeRep SomeTypeRep
e SomeTypeRep
a SomeTypeRep
b) Map SomeTypeRep SomeTypeRep
env ([SomeTypeRep] -> [SomeTypeRep] -> [(SomeTypeRep, SomeTypeRep)]
forall (a :: Kind) (b :: Kind). [a] -> [b] -> [(a, b)]
zip [SomeTypeRep]
ks [SomeTypeRep]
ls)
unify Map SomeTypeRep SomeTypeRep
_ SomeTypeRep
_ SomeTypeRep
_ = Maybe (Map SomeTypeRep SomeTypeRep)
forall (a :: Kind). Maybe a
Nothing
substitute :: Map SomeTypeRep SomeTypeRep -> SomeTypeRep -> Maybe SomeTypeRep
substitute Map SomeTypeRep SomeTypeRep
env SomeTypeRep
template
| Just SomeTypeRep
object <- SomeTypeRep -> Map SomeTypeRep SomeTypeRep -> Maybe SomeTypeRep
forall (k :: Kind) (a :: Kind). Ord k => k -> Map k a -> Maybe a
M.lookup SomeTypeRep
template Map SomeTypeRep SomeTypeRep
env = SomeTypeRep -> Maybe SomeTypeRep
forall (a :: Kind). a -> Maybe a
Just SomeTypeRep
object
| SomeTypeRep
template SomeTypeRep -> [SomeTypeRep] -> Bool
forall (a :: Kind). Eq a => a -> [a] -> Bool
forall (t :: Kind -> Kind) (a :: Kind).
(Foldable t, Eq a) =>
a -> t a -> Bool
`elem` [SomeTypeRep]
variables = Maybe SomeTypeRep
forall (a :: Kind). Maybe a
Nothing
substitute Map SomeTypeRep SomeTypeRep
env (SomeTypeRep (App TypeRep a
f TypeRep b
x)) = do
f' <- Map SomeTypeRep SomeTypeRep -> SomeTypeRep -> Maybe SomeTypeRep
substitute Map SomeTypeRep SomeTypeRep
env (TypeRep a -> SomeTypeRep
forall (k :: Kind) (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep TypeRep a
f)
x' <- substitute env (SomeTypeRep x)
applyType f' x'
substitute Map SomeTypeRep SomeTypeRep
env original :: SomeTypeRep
original@(SomeTypeRep (Con' TyCon
c [SomeTypeRep]
kinds)) = do
kinds' <- (SomeTypeRep -> Maybe SomeTypeRep)
-> [SomeTypeRep] -> Maybe [SomeTypeRep]
forall (t :: Kind -> Kind) (f :: Kind -> Kind) (a :: Kind)
(b :: Kind).
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: Kind -> Kind) (a :: Kind) (b :: Kind).
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Map SomeTypeRep SomeTypeRep -> SomeTypeRep -> Maybe SomeTypeRep
substitute Map SomeTypeRep SomeTypeRep
env) [SomeTypeRep]
kinds
pure (if kinds' == kinds then original else SomeTypeRep (mkTrCon c kinds'))
applyType :: SomeTypeRep -> SomeTypeRep -> Maybe SomeTypeRep
applyType :: SomeTypeRep -> SomeTypeRep -> Maybe SomeTypeRep
applyType (SomeTypeRep TypeRep a
f) (SomeTypeRep TypeRep a
x) = case TypeRep a -> TypeRep k
forall (k :: Kind) (a :: k). TypeRep a -> TypeRep k
typeRepKind TypeRep a
f of
Fun TypeRep arg
argument TypeRep res
result -> case TypeRep (TYPE r2) -> TypeRep Kind -> Maybe (TYPE r2 :~~: Kind)
forall (k1 :: Kind) (k2 :: Kind) (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
eqTypeRep (TypeRep res -> TypeRep (TYPE r2)
forall (k :: Kind) (a :: k). TypeRep a -> TypeRep k
typeRepKind TypeRep res
result) (forall (a :: Kind). Typeable a => TypeRep a
forall {k :: Kind} (a :: k). Typeable a => TypeRep a
typeRep @Type) of
Just TYPE r2 :~~: Kind
HRefl -> case TypeRep arg -> TypeRep k -> Maybe (arg :~~: k)
forall (k1 :: Kind) (k2 :: Kind) (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
eqTypeRep TypeRep arg
argument (TypeRep a -> TypeRep k
forall (k :: Kind) (a :: k). TypeRep a -> TypeRep k
typeRepKind TypeRep a
x) of
Just arg :~~: k
HRefl -> SomeTypeRep -> Maybe SomeTypeRep
forall (a :: Kind). a -> Maybe a
Just (TypeRep (a a) -> SomeTypeRep
forall (k :: Kind) (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep (TypeRep a -> TypeRep a -> TypeRep (a a)
forall (k2 :: Kind) (t :: k2) (k1 :: Kind) (a :: k1 -> k2)
(b :: k1).
(t ~ a b) =>
TypeRep a -> TypeRep b -> TypeRep t
App TypeRep a
TypeRep a
f TypeRep a
x))
Maybe (arg :~~: k)
Nothing -> Maybe SomeTypeRep
forall (a :: Kind). Maybe a
Nothing
Maybe (TYPE r2 :~~: Kind)
Nothing -> Maybe SomeTypeRep
forall (a :: Kind). Maybe a
Nothing
TypeRep k
_ -> Maybe SomeTypeRep
forall (a :: Kind). Maybe a
Nothing