| Safe Haskell | None |
|---|---|
| Language | GHC2024 |
Proarrow.Tools.CCC
Description
Cartesian terms, using the syntax and binders of Proarrow.Tools.SMC in PROD k.
Its tensor is the product of k, independently of any monoidal structure on k itself.
F embeds a base object, a is a product, and :** ba is the exponential
of :-> bPROD k. toCCC compiles a function on terms and unwraps the resulting arrow into k.
Each operation asks only for the structure it uses: products for pairing and binding,
Closed ( for PROD k)lam and (!), coproducts for inl and inr, and
Distributive ( for PROD k)caseOf. Copying and
discarding are supplied by products. The patterns, contexts and QualifiedDo notation are
those of SMC; see Proarrow.Tools.SMC for their use, including recast across binding depths.
import Prelude hiding ((**)) import Proarrow.Tools.CCC (SYN (..), type F, toCCC, (**)) swapProduct = toCCC @(F a :** F b) @(F b :** F a) (\(a, b) -> (b, a))
Synopsis
- data SYN k
- type F (a :: k) = 'F ('PR a)
- type Interp (a :: SYN (PROD k)) = UN ('PR :: k -> PROD k) (Interp a)
- class CategoryOf k => KnownObj (s :: SYN k)
- type Term (d :: Nat) (g :: Ctx k) (a :: SYN k) = TermP ((~>) :: CAT k) d g a
- class ToTerm (p :: CAT k) t (d :: Nat) (g :: Ctx k) (a :: SYN k) | p t -> g a
- toTerm :: ToTerm p t d g a => t -> TermP p d g a
- toCCC :: forall {k} (a :: SYN (PROD k)) (b :: SYN (PROD k)) t cont. (HasProducts k, Binds ((~>) :: CAT (PROD k)) 0 ('[] :: [(Nat, SYN (PROD k))]) a b t cont) => (t -> cont) -> Interp a ~> Interp b
- toCCC1 :: forall {k} (a :: k) (b :: k) t cont. (HasProducts k, Binds ((~>) :: CAT (PROD k)) 0 ('[] :: [(Nat, SYN (PROD k))]) (F a) (F b) t cont) => (t -> cont) -> a ~> b
- lift :: forall {k} (a :: SYN (PROD k)) (b :: SYN (PROD k)) (d :: Nat) (g :: Ctx (PROD k)) t. (CategoryOf k, Is ('PR :: k -> PROD k) (Interp a), Is ('PR :: k -> PROD k) (Interp b), ToTerm (Hom (PROD k)) t d g a) => (Interp a ~> Interp b) -> t -> Term d g b
- liftF :: forall {k} (a :: SYN (PROD k)) (b :: k) t (d :: Nat) (g :: Ctx (PROD k)). (CategoryOf k, Is ('PR :: k -> PROD k) (Interp a), BuildTuple (Hom (PROD k)) t g a) => (Interp a ~> b) -> t -> Term d g (F b)
- (-<) :: forall {k} (a :: SYN (PROD k)) (b :: k) t (d :: Nat) (g :: Ctx (PROD k)). (CategoryOf k, Is ('PR :: k -> PROD k) (Interp a), BuildTuple (Hom (PROD k)) t g a) => (Interp a ~> b) -> t -> Term d g (F b)
- returnA :: forall {k} p (a :: k). (Promonad p, Ob a) => p a a
- unit :: forall {k} (d :: Nat) (p :: CAT k). MonoidalProfunctor p => TermP p d ('[] :: [(Nat, SYN k)]) ('I :: SYN k)
- (**) :: forall {k} (d :: Nat) (g1 :: Ctx k) (g2 :: Ctx k) (a :: SYN k) (b :: SYN k) (p :: CAT k). (MonoidalProfunctor p, Merge g1 g2) => TermP p d g1 a -> TermP p d g2 b -> TermP p d (Union g1 g2) (a ':** b)
- split :: forall {k} (d :: Natural) (g :: Ctx k) (g' :: Ctx k) (r1 :: Ctx k) (r :: Ctx k) (a :: SYN k) (b :: SYN k) (c :: SYN k) (da :: Nat) (db :: Nat) (p :: CAT k). (Promonad p, MonoidalProfunctor p, KnownObj a, KnownObj b, BindVar (d + 1) b g' r1, BindVar d a r1 r, Merge r g) => TermP p d g (a ':** b) -> (TermP p da '['(d, a)] a -> TermP p db '['(d + 1, b)] b -> TermP p (d + 2) g' c) -> TermP p d (Union r g) c
- type Tuple (p :: CAT k) t (a :: SYN k) = BuildTuple p t (CtxOf t :: Ctx k) a
- class BuildTuple (p :: CAT k) t (g :: Ctx k) (a :: SYN k)
- recast :: forall {k} (a :: SYN k) (b :: SYN k) (d :: Nat) (d' :: Nat) (g :: Ctx k) (p :: CAT k). Interp a ~ Interp b => TermP p d g a -> TermP p d' g b
- with :: forall {k} (a :: SYN k) (b :: SYN k) (d :: Nat) (g1 :: Ctx k) (g2 :: Ctx k). (HasBinaryProducts k, Thin (Union g1 g2) g1, Thin (Union g1 g2) g2) => Term d g1 a -> Term d g2 b -> Term d (Union g1 g2) (a ':&& b)
- exl :: forall {k} {p :: k +-> k} (a :: SYN k) (b :: SYN k) (d :: Nat) (g :: Ctx k). (HasBinaryProducts k, Profunctor p, KnownObj a, KnownObj b) => TermP p d g (a ':&& b) -> TermP p d g a
- exr :: forall {k} {p :: k +-> k} (a :: SYN k) (b :: SYN k) (d :: Nat) (g :: Ctx k). (HasBinaryProducts k, Profunctor p, KnownObj a, KnownObj b) => TermP p d g (a ':&& b) -> TermP p d g b
- absorb :: forall {k} {p :: k +-> k} (s :: SYN k) (d :: Nat) (g :: Ctx k). (HasTerminalObject k, Profunctor p, KnownObj s) => TermP p d g s -> TermP p d g ('Top :: SYN k)
- lam :: forall {k} (d :: Nat) (r :: Ctx k) (a :: SYN k) (b :: SYN k) t cont. (Closed k, Binds ((~>) :: CAT k) d r a b t cont) => (t -> cont) -> Term d r (a ':-> b)
- class Apply (p :: CAT k) f x r | f -> p where
- (!) :: f -> x -> r
- inl :: forall {k} (a :: SYN k) (b :: SYN k) (d :: Nat) (g :: Ctx k) (p :: k +-> k). (Profunctor p, HasBinaryCoproducts k, KnownObj a, KnownObj b) => TermP p d g a -> TermP p d g (a ':|| b)
- inr :: forall {k} (a :: SYN k) (b :: SYN k) (d :: Nat) (g :: Ctx k) (p :: k +-> k). (Profunctor p, HasBinaryCoproducts k, KnownObj a, KnownObj b) => TermP p d g b -> TermP p d g (a ':|| b)
- caseOf :: forall {k} (a :: SYN k) (b :: SYN k) (c :: SYN k) (d :: Nat) (g :: Ctx k) (r1 :: Ctx k) (r2 :: Ctx k) t1 cont1 t2 cont2 (s :: SYN k) (p :: CAT k). (Distributive k, Promonad p, DistributiveProfunctor p, Sum s a b, KnownObj a, KnownObj b, Binds p d r1 a c t1 cont1, Binds p d r2 b c t2 cont2, Thin (Union r1 r2) r1, Thin (Union r1 r2) r2, Merge (Union r1 r2) g) => TermP p d g s -> (t1 -> cont1) -> (t2 -> cont2) -> TermP p d (Union (Union r1 r2) g) c
- ifThenElse :: forall {k} (a :: SYN k) (b :: SYN k) (c :: SYN k) (d :: Nat) (g :: Ctx k) (r1 :: Ctx k) (r2 :: Ctx k) t1 cont1 t2 cont2 (s :: SYN k) (p :: CAT k). (Distributive k, Promonad p, DistributiveProfunctor p, Sum s a b, KnownObj a, KnownObj b, Binds p d r1 a c t1 cont1, Binds p d r2 b c t2 cont2, Thin (Union r1 r2) r1, Thin (Union r1 r2) r2, Merge (Union r1 r2) g) => TermP p d g s -> (t1 -> cont1) -> (t2 -> cont2) -> TermP p d (Union (Union r1 r2) g) c
- class Interp s ~ Interp (a ':|| b) => Sum (s :: SYN k) (a :: SYN k) (b :: SYN k)
- absurd :: forall {k} {p :: k +-> k} (s :: SYN k) (c :: SYN k) (d :: Nat) (g1 :: Ctx k) (g2 :: Ctx k). (Distributive k, MonoidalProfunctor p, KnownObj s, KnownObj c, Merge g1 g2) => TermP p d g1 s -> TermP p d g2 ('Zero :: SYN k) -> TermP p d (Union g1 g2) c
- type Ctx k = [(Nat, SYN k)]
- class KnownCtx (g :: Ctx k)
- type family Union (g1 :: Ctx k) (g2 :: Ctx k) :: Ctx k where ...
- class (KnownCtx g1, KnownCtx g2) => Merge (g1 :: Ctx k) (g2 :: Ctx k)
- class (KnownCtx g, KnownCtx h) => Thin (g :: Ctx k) (h :: Ctx k)
- class KnownCtx r => BindVar (n :: Nat) (a :: SYN k) (g :: Ctx k) (r :: Ctx k) | n g -> r
- (>>=) :: Bind p m t cont r => m -> (t -> cont) -> r
- return :: forall k t (d :: Nat) (p :: CAT k). (Monoidal k, Promonad p, RecVars k t, KnownCtx (Vars k t)) => t -> Ret t (TermP p d (Vars k t) (Mul (Vars k t)))
- fail :: a
- class Bind (p :: CAT k) m t cont r | m -> p
- class (KnownObj a, KnownCtx g) => Binds (p :: CAT k) (n :: Nat) (g :: Ctx k) (a :: SYN k) (b :: SYN k) t cont
- injectRight :: forall {k} (a :: k) (b :: k). (HasProducts k, HasBinaryCoproducts k, Ob a, Ob b) => a ~> (b || a)
- swapProduct :: forall {k} (a :: k) (b :: k). (HasProducts k, Ob a, Ob b) => (a && b) ~> (b && a)
- applyPair :: forall {k} (a :: k) (b :: k). (HasProducts k, Closed (PROD k), Ob a, Ob b) => Interp ((F a ':-> F b) ':** F a) ~> b
- curryPair :: forall {k} (a :: k) (b :: k). (HasProducts k, Closed (PROD k), Ob a, Ob b) => a ~> Interp (F b ':-> (F a ':** F b))
- flipCurried3 :: forall {k} (a :: k) (b :: k) (c :: k). (HasProducts k, Closed (PROD k), Ob a, Ob b, Ob c) => Interp (F b ':-> (F a ':-> (F b ':-> F c))) ~> Interp (F a ':-> (F b ':-> F c))
- swapSum :: forall {k} (a :: k) (b :: k). (HasProducts k, Distributive (PROD k), Ob a, Ob b) => (a || b) ~> (b || a)
- caseEither :: forall {k} (a :: k) (b :: k) (c :: k). (HasProducts k, Closed (PROD k), Distributive (PROD k), Ob a, Ob b, Ob c) => Interp ((F a ':|| F b) ':** ((F a ':-> F c) ':** (F b ':-> F c))) ~> c
Types
Type expressions over the objects of k: an object of k, the unit, the tensor, the
internal hom and the negation, and the additives: the product and its unit Top, and the
coproduct and its unit Zero.
The negation gives the types a polarity: a type is negative when it is a Not, and positive
otherwise. A term of a positive type is a value, and a term of a negative type is a consumer
of what it negates. Up shifts a positive type to a negative one, and Dn a negative type to
a positive one, standing for the same object: a term of is a stored term of Dn nn.
Constructors
| I | |
| (SYN k) :** (SYN k) infixl 7 | |
| (SYN k) :-> (SYN k) infixr 5 | |
| (SYN k) :&& (SYN k) infixl 6 | |
| Top | |
| (SYN k) :|| (SYN k) infixl 6 | |
| Zero |
Instances
| (Monoidal k, KnownObj a, a ~ b, KnownCtx g) => BindVarBy 'EQ n (a :: SYN k) ('(m, b) ': g :: [(Nat, SYN k)]) (g :: Ctx k) Source Github # | |
| (Monoidal k, Comonoid (Interp a), KnownObj a, KnownObj b, KnownCtx g) => BindVarBy 'GT n (a :: SYN k) ('(m, b) ': g :: [(Nat, SYN k)]) ('(m, b) ': g :: [(Nat, SYN k)]) Source Github # | |
| (Monoidal k, Comonoid (Interp a), KnownObj a) => BindVar n (a :: SYN k) ('[] :: [(Nat, SYN k)]) ('[] :: [(Nat, SYN k)]) Source Github # | |
| (KnownCtx r, BindVarBy (CmpNat n m) n a ('(m, b) ': g) r) => BindVar n (a :: SYN k) ('(m, b) ': g :: [(Natural, SYN k)]) (r :: Ctx k) Source Github # | |
| (SymMonoidal k, CocommutativeComonoid (Interp a), KnownObj a, a ~ b, Merge g1 g2, KnownCtx (Union g1 g2)) => MergeBy 'EQ ('(n, a) ': g1 :: [(Nat, SYN k)]) ('(m, b) ': g2 :: [(Nat, SYN k)]) Source Github # | A variable both contexts have is copied, one copy for each side. |
| (SymMonoidal k, Merge g1 ('(m, b) ': g2), KnownObj a, KnownObj b, Mul ('(n, a) ': Union g1 ('(m, b) ': g2)) ~ (Mul (Union g1 ('(m, b) ': g2)) ':** a)) => MergeBy 'GT ('(n, a) ': g1 :: [(Nat, SYN k)]) ('(m, b) ': g2 :: [(Nat, SYN k)]) Source Github # | |
| (Monoidal k, Merge ('(n, a) ': g1) g2, KnownObj a, KnownObj b, Mul ('(m, b) ': Union ('(n, a) ': g1) g2) ~ (Mul (Union ('(n, a) ': g1) g2) ':** b)) => MergeBy 'LT ('(n, a) ': g1 :: [(Nat, SYN k)]) ('(m, b) ': g2 :: [(Nat, SYN k)]) Source Github # | |
| (Monoidal k, KnownObj a, a ~ b, Thin g h) => ThinBy 'EQ ('(n, a) ': g :: [(Nat, SYN k)]) ('(m, b) ': h :: [(Nat, SYN k)]) Source Github # | |
| (Monoidal k, Comonoid (Interp a), KnownObj a, KnownObj b, Thin g ('(m, b) ': h)) => ThinBy 'GT ('(n, a) ': g :: [(Nat, SYN k)]) ('(m, b) ': h :: [(Nat, SYN k)]) Source Github # | |
| (SymMonoidal k, Unmerge g1 ('(m, b) ': g2), KnownObj a, KnownObj b, Mul ('(n, a) ': Union g1 ('(m, b) ': g2)) ~ (Mul (Union g1 ('(m, b) ': g2)) ':** a)) => UnmergeBy 'GT ('(n, a) ': g1 :: [(Nat, SYN k)]) ('(m, b) ': g2 :: [(Nat, SYN k)]) Source Github # | |
| (Monoidal k, Unmerge ('(n, a) ': g1) g2, KnownObj a, KnownObj b, Mul ('(m, b) ': Union ('(n, a) ': g1) g2) ~ (Mul (Union ('(n, a) ': g1) g2) ':** b)) => UnmergeBy 'LT ('(n, a) ': g1 :: [(Nat, SYN k)]) ('(m, b) ': g2 :: [(Nat, SYN k)]) Source Github # | |
| KnownCtx ('[] :: [(Nat, SYN k)]) Source Github # | |
Defined in Proarrow.Tools.SMC.Internal.Context Methods ctxCase :: (('[] :: [(Nat, SYN k)]) ~ ('[] :: [(Nat, SYN k)]) => r) -> (forall (n :: Nat) (a :: SYN k) (g' :: [(Nat, SYN k)]). (('[] :: [(Nat, SYN k)]) ~ ('(n, a) ': g'), KnownObj a, KnownCtx g') => r) -> r Source Github # withCtxOb :: Monoidal k => (Ob (Interp (Mul ('[] :: [(Nat, SYN k)]))) => r) -> r Source Github # | |
| (Monoidal k, KnownCtx g2) => Merge ('[] :: [(Nat, SYN k)]) (g2 :: Ctx k) Source Github # | |
| (Monoidal k, KnownCtx g2) => Unmerge ('[] :: [(Nat, SYN k)]) (g2 :: Ctx k) Source Github # | |
| Monoidal k => Thin ('[] :: [(Nat, SYN k)]) ('[] :: [(Nat, SYN k)]) Source Github # | |
| (KnownObj a, KnownCtx g) => KnownCtx ('(n, a) ': g :: [(Nat, SYN k)]) Source Github # | |
Defined in Proarrow.Tools.SMC.Internal.Context Methods ctxCase :: (('(n, a) ': g) ~ ('[] :: [(Nat, SYN k)]) => r) -> (forall (n0 :: Nat) (a0 :: SYN k) (g' :: [(Nat, SYN k)]). (('(n, a) ': g) ~ ('(n0, a0) ': g'), KnownObj a0, KnownCtx g') => r) -> r Source Github # withCtxOb :: Monoidal k => (Ob (Interp (Mul ('(n, a) ': g))) => r) -> r Source Github # | |
| (Monoidal k, KnownCtx ('(n, a) ': g1)) => Merge ('(n, a) ': g1 :: [(Nat, SYN k)]) ('[] :: [(Nat, SYN k)]) Source Github # | |
| (Monoidal k, Comonoid (Interp a), KnownObj a, Thin g ('[] :: [(Nat, SYN k)])) => Thin ('(n, a) ': g :: [(Nat, SYN k)]) ('[] :: [(Nat, SYN k)]) Source Github # | |
| (Monoidal k, KnownCtx ('(n, a) ': g1)) => Unmerge ('(n, a) ': g1 :: [(Nat, SYN k)]) ('[] :: [(Nat, SYN k)]) Source Github # | |
| (Monoidal k, KnownObj a, KnownObj b, KnownCtx g1, KnownCtx g2, MergeBy (CmpNat n m) ('(n, a) ': g1) ('(m, b) ': g2)) => Merge ('(n, a) ': g1 :: [(Natural, SYN k)]) ('(m, b) ': g2 :: [(Natural, SYN k)]) Source Github # | |
| (KnownObj a, KnownObj b, KnownCtx g, KnownCtx h, ThinBy (CmpNat n m) ('(n, a) ': g) ('(m, b) ': h)) => Thin ('(n, a) ': g :: [(Natural, SYN k)]) ('(m, b) ': h :: [(Natural, SYN k)]) Source Github # | |
| (Monoidal k, KnownObj a, KnownObj b, KnownCtx g1, KnownCtx g2, UnmergeBy (CmpNat n m) ('(n, a) ': g1) ('(m, b) ': g2)) => Unmerge ('(n, a) ': g1 :: [(Natural, SYN k)]) ('(m, b) ': g2 :: [(Natural, SYN k)]) Source Github # | |
| (Promonad p, KnownObj a) => RecVars k (TermP p d '['(n, a)] a) Source Github # | |
| type Vars k (TermP p d '['(n, a)] a) Source Github # | |
Defined in Proarrow.Tools.SMC.Internal.Do | |
type Interp (a :: SYN (PROD k)) = UN ('PR :: k -> PROD k) (Interp a) Source Github #
The base-category object a cartesian type expression stands for.
class CategoryOf k => KnownObj (s :: SYN k) Source Github #
Type expressions whose Interp is an object, given that their leaves are.
Minimal complete definition
Instances
| Monoidal k => KnownObj ('I :: SYN k) Source Github # | |
| HasTerminalObject k => KnownObj ('Top :: SYN k) Source Github # | |
| HasInitialObject k => KnownObj ('Zero :: SYN k) Source Github # | |
| KnownObj a => KnownObj ('Dn a :: SYN k) Source Github # | |
| (CategoryOf k, Ob a) => KnownObj ('F a :: SYN k) Source Github # | |
| (Dialogue k, KnownObj a) => KnownObj ('Not a :: SYN k) Source Github # | |
| (HasBinaryProducts k, KnownObj a, KnownObj b) => KnownObj (a ':&& b :: SYN k) Source Github # | |
| (Monoidal k, KnownObj a, KnownObj b) => KnownObj (a ':** b :: SYN k) Source Github # | |
| (Closed k, KnownObj a, KnownObj b) => KnownObj (a ':-> b :: SYN k) Source Github # | |
| (HasBinaryCoproducts k, KnownObj a, KnownObj b) => KnownObj (a ':|| b :: SYN k) Source Github # | |
Terms
Products
type Term (d :: Nat) (g :: Ctx k) (a :: SYN k) = TermP ((~>) :: CAT k) d g a Source Github #
A term whose computations are the morphisms of the category itself.
class ToTerm (p :: CAT k) t (d :: Nat) (g :: Ctx k) (a :: SYN k) | p t -> g a Source Github #
A term, a constant arrow from the monoidal unit, or a tuple of these.
Constants and the empty tuple use no variables; pairs merge their components' contexts.
Base-category constants enter a promonad through arr; arrows already in it are kept.
Converting a term here keeps its binding depth, unlike tuple construction.
Minimal complete definition
Instances
| (MonoidalProfunctor p, g ~ ('[] :: [(Nat, SYN k)]), a ~ ('I :: SYN k)) => ToTerm (p :: k +-> k) () d (g :: [(Nat, SYN k)]) (a :: SYN k) Source Github # | The empty tuple is the monoidal unit. |
| t ~ TermP p d g a => ToTerm (p :: CAT k) t d (g :: Ctx k) (a :: SYN k) Source Github # | |
| (MonoidalProfunctor p, ToTerm p x d g1 a1, ToTerm p y d g2 a2, Merge g1 g2, g ~ Union g1 g2, a ~ (a1 ':** a2)) => ToTerm (p :: k +-> k) (x, y) d (g :: Ctx k) (a :: SYN k) Source Github # | A pair combines its components as a tensor, merging their contexts. |
| (q x y ~ r (Unit :: k3) b, LiftArrow p r, g ~ ('[] :: [(Nat, SYN k3)]), a ~ 'F b) => ToTerm (p :: CAT k3) (q x y) d (g :: [(Nat, SYN k3)]) (a :: SYN k3) Source Github # | |
| ToTerm p ((x, y), z) d g a => ToTerm (p :: CAT k) (x, y, z) d (g :: Ctx k) (a :: SYN k) Source Github # | |
| ToTerm p ((w, x, y), z) d g a => ToTerm (p :: CAT k) (w, x, y, z) d (g :: Ctx k) (a :: SYN k) Source Github # | |
| ToTerm p ((v, w, x, y), z) d g a => ToTerm (p :: CAT k) (v, w, x, y, z) d (g :: Ctx k) (a :: SYN k) Source Github # | |
| TermP q d' g' a' ~ TermP p d g a => ToTerm (p :: CAT k2) (TermP q d' g' a') d (g :: Ctx k2) (a :: SYN k2) Source Github # | |
| ToTerm p ((u, v, w, x, y), z) d g a => ToTerm (p :: CAT k) (u, v, w, x, y, z) d (g :: Ctx k) (a :: SYN k) Source Github # | |
toTerm :: ToTerm p t d g a => t -> TermP p d g a Source Github #
Explicit conversion for helpers whose result must be a concrete term.
toCCC :: forall {k} (a :: SYN (PROD k)) (b :: SYN (PROD k)) t cont. (HasProducts k, Binds ((~>) :: CAT (PROD k)) 0 ('[] :: [(Nat, SYN (PROD k))]) a b t cont) => (t -> cont) -> Interp a ~> Interp b Source Github #
Compile a function on terms into an arrow of k. The input arrives through an SMC pattern,
a variable, (), or a tuple, and the body's operations determine the additional structure needed.
toCCC1 :: forall {k} (a :: k) (b :: k) t cont. (HasProducts k, Binds ((~>) :: CAT (PROD k)) 0 ('[] :: [(Nat, SYN (PROD k))]) (F a) (F b) t cont) => (t -> cont) -> a ~> b Source Github #
lift :: forall {k} (a :: SYN (PROD k)) (b :: SYN (PROD k)) (d :: Nat) (g :: Ctx (PROD k)) t. (CategoryOf k, Is ('PR :: k -> PROD k) (Interp a), Is ('PR :: k -> PROD k) (Interp b), ToTerm (Hom (PROD k)) t d g a) => (Interp a ~> Interp b) -> t -> Term d g b Source Github #
Lift a base-category arrow onto terms. The type expressions can be composite, as in SMC's
lift; use type applications when their interpretations do not determine them.
liftF :: forall {k} (a :: SYN (PROD k)) (b :: k) t (d :: Nat) (g :: Ctx (PROD k)). (CategoryOf k, Is ('PR :: k -> PROD k) (Interp a), BuildTuple (Hom (PROD k)) t g a) => (Interp a ~> b) -> t -> Term d g (F b) Source Github #
(-<) :: forall {k} (a :: SYN (PROD k)) (b :: k) t (d :: Nat) (g :: Ctx (PROD k)). (CategoryOf k, Is ('PR :: k -> PROD k) (Interp a), BuildTuple (Hom (PROD k)) t g a) => (Interp a ~> b) -> t -> Term d g (F b) infixr 0 Source Github #
unit :: forall {k} (d :: Nat) (p :: CAT k). MonoidalProfunctor p => TermP p d ('[] :: [(Nat, SYN k)]) ('I :: SYN k) Source Github #
The unit, which uses no variables.
(**) :: forall {k} (d :: Nat) (g1 :: Ctx k) (g2 :: Ctx k) (a :: SYN k) (b :: SYN k) (p :: CAT k). (MonoidalProfunctor p, Merge g1 g2) => TermP p d g1 a -> TermP p d g2 b -> TermP p d (Union g1 g2) (a ':** b) infixl 7 Source Github #
Two concrete terms side by side. A variable both use is copied. With effects, the left
term runs first. Use tuples to combine components through ToTerm.
split :: forall {k} (d :: Natural) (g :: Ctx k) (g' :: Ctx k) (r1 :: Ctx k) (r :: Ctx k) (a :: SYN k) (b :: SYN k) (c :: SYN k) (da :: Nat) (db :: Nat) (p :: CAT k). (Promonad p, MonoidalProfunctor p, KnownObj a, KnownObj b, BindVar (d + 1) b g' r1, BindVar d a r1 r, Merge r g) => TermP p d g (a ':** b) -> (TermP p da '['(d, a)] a -> TermP p db '['(d + 1, b)] b -> TermP p (d + 2) g' c) -> TermP p d (Union r g) c Source Github #
Take a tensor apart: the continuation gets a variable for each side.
type Tuple (p :: CAT k) t (a :: SYN k) = BuildTuple p t (CtxOf t :: Ctx k) a Source Github #
A term built from a tuple of terms, by the type it is expected to have: a term is itself,
() is the monoidal unit, a pair at a tensor is the tensor of its parts, and a pair at a
computation is the computation of the pair at Up aa. A tuple of three to six stands for pairs nested to the left, as in
patterns. The parts may come from different binding depths; the result adopts the depth
where it is used, and their contexts are merged.
class BuildTuple (p :: CAT k) t (g :: Ctx k) (a :: SYN k) Source Github #
Build a tuple with its context inferred alongside its type. Keeping the context a parameter allows constant arrows of an abstract category to use the empty context.
Minimal complete definition
Instances
| (MonoidalProfunctor p, a ~ ('I :: SYN k), g ~ ('[] :: [(Nat, SYN k)])) => BuildTuple (p :: k +-> k) () (g :: [(Nat, SYN k)]) (a :: SYN k) Source Github # | The empty tuple is the monoidal unit and uses no variables. |
| ToTerm p t (DepthOf t) g a => BuildTuple (p :: CAT k) t (g :: Ctx k) (a :: SYN k) Source Github # | A term keeps its context and type, adapting to the depth where it is used. Variable ids in the context are unchanged. |
| (MonoidalProfunctor p, a ~ (a1 ':** a2), BuildTuple p x g1 a1, BuildTuple p y g2 a2, Merge g1 g2, g ~ Union g1 g2) => BuildTuple (p :: k +-> k) (x, y) (g :: Ctx k) (a :: SYN k) Source Github # | A pair at a tensor is the tensor of its parts. |
| (Dialogue k, Profunctor p, KnownObj a, BuildTuple p (x, y) g a) => BuildTuple (p :: k +-> k) (x, y) (g :: Ctx k) ('Not ('Not a) :: SYN k) Source Github # | A pair at a computation is the computation of the pair at the value. |
| BuildTuple p ((x, y), z) g a => BuildTuple (p :: CAT k) (x, y, z) (g :: Ctx k) (a :: SYN k) Source Github # | |
| BuildTuple p ((w, x, y), z) g a => BuildTuple (p :: CAT k) (w, x, y, z) (g :: Ctx k) (a :: SYN k) Source Github # | |
| BuildTuple p ((v, w, x, y), z) g a => BuildTuple (p :: CAT k) (v, w, x, y, z) (g :: Ctx k) (a :: SYN k) Source Github # | |
| BuildTuple p ((u, v, w, x, y), z) g a => BuildTuple (p :: CAT k) (u, v, w, x, y, z) (g :: Ctx k) (a :: SYN k) Source Github # | |
recast :: forall {k} (a :: SYN k) (b :: SYN k) (d :: Nat) (d' :: Nat) (g :: Ctx k) (p :: CAT k). Interp a ~ Interp b => TermP p d g a -> TermP p d' g b Source Github #
with :: forall {k} (a :: SYN k) (b :: SYN k) (d :: Nat) (g1 :: Ctx k) (g2 :: Ctx k). (HasBinaryProducts k, Thin (Union g1 g2) g1, Thin (Union g1 g2) g2) => Term d g1 a -> Term d g2 b -> Term d (Union g1 g2) (a ':&& b) Source Github #
Both of two alternatives over the same variables: the product. This needs products.
exl :: forall {k} {p :: k +-> k} (a :: SYN k) (b :: SYN k) (d :: Nat) (g :: Ctx k). (HasBinaryProducts k, Profunctor p, KnownObj a, KnownObj b) => TermP p d g (a ':&& b) -> TermP p d g a Source Github #
The first alternative of a product.
exr :: forall {k} {p :: k +-> k} (a :: SYN k) (b :: SYN k) (d :: Nat) (g :: Ctx k). (HasBinaryProducts k, Profunctor p, KnownObj a, KnownObj b) => TermP p d g (a ':&& b) -> TermP p d g b Source Github #
The second alternative of a product.
absorb :: forall {k} {p :: k +-> k} (s :: SYN k) (d :: Nat) (g :: Ctx k). (HasTerminalObject k, Profunctor p, KnownObj s) => TermP p d g s -> TermP p d g ('Top :: SYN k) Source Github #
Use up a term into the unit of the product.
Product exponentials
lam :: forall {k} (d :: Nat) (r :: Ctx k) (a :: SYN k) (b :: SYN k) t cont. (Closed k, Binds ((~>) :: CAT k) d r a b t cont) => (t -> cont) -> Term d r (a ':-> b) Source Github #
A function: the body receives the argument through a pattern (see Patterns). This needs the category to be closed.
class Apply (p :: CAT k) f x r | f -> p where Source Github #
Function application. A variable both the function and its argument use is copied.
Explicit exponentials determine their result syntax. Embedded function objects, such as
results of (-<), take their result syntax from the surrounding term.
Instances
| (KnownObj a, KnownObj b, Merge g1 g2, ToTerm p x d g2 a, r ~ TermP p d (Union g1 g2) b) => Apply (p :: CAT k) (TermP p d g1 (a ':-> b)) x r Source Github # | |
| (KnownObj a, KnownObj b, Merge g1 g2, f ~ (Interp a ~~> Interp b), ToTerm p x d g2 a, r ~ TermP p d (Union g1 g2) b) => Apply (p :: CAT k) (TermP p d g1 ('F f)) x r Source Github # | |
Coproducts
inl :: forall {k} (a :: SYN k) (b :: SYN k) (d :: Nat) (g :: Ctx k) (p :: k +-> k). (Profunctor p, HasBinaryCoproducts k, KnownObj a, KnownObj b) => TermP p d g a -> TermP p d g (a ':|| b) Source Github #
The left injection into a coproduct.
inr :: forall {k} (a :: SYN k) (b :: SYN k) (d :: Nat) (g :: Ctx k) (p :: k +-> k). (Profunctor p, HasBinaryCoproducts k, KnownObj a, KnownObj b) => TermP p d g b -> TermP p d g (a ':|| b) Source Github #
The right injection into a coproduct.
caseOf :: forall {k} (a :: SYN k) (b :: SYN k) (c :: SYN k) (d :: Nat) (g :: Ctx k) (r1 :: Ctx k) (r2 :: Ctx k) t1 cont1 t2 cont2 (s :: SYN k) (p :: CAT k). (Distributive k, Promonad p, DistributiveProfunctor p, Sum s a b, KnownObj a, KnownObj b, Binds p d r1 a c t1 cont1, Binds p d r2 b c t2 cont2, Thin (Union r1 r2) r1, Thin (Union r1 r2) r2, Merge (Union r1 r2) g) => TermP p d g s -> (t1 -> cont1) -> (t2 -> cont2) -> TermP p d (Union (Union r1 r2) g) c Source Github #
Case analysis on a coproduct. Each branch receives the contents of its alternative through a
pattern (see Patterns), and the variables of the term around it are shared between the
branches. This needs the tensor to distribute over the coproduct.
The sum may be written with (:||) or embedded by F, as in the result of (-<).
For Arr, this needs ArrowChoice
and runs only the selected branch.
ifThenElse :: forall {k} (a :: SYN k) (b :: SYN k) (c :: SYN k) (d :: Nat) (g :: Ctx k) (r1 :: Ctx k) (r2 :: Ctx k) t1 cont1 t2 cont2 (s :: SYN k) (p :: CAT k). (Distributive k, Promonad p, DistributiveProfunctor p, Sum s a b, KnownObj a, KnownObj b, Binds p d r1 a c t1 cont1, Binds p d r2 b c t2 cont2, Thin (Union r1 r2) r1, Thin (Union r1 r2) r2, Merge (Union r1 r2) g) => TermP p d g s -> (t1 -> cont1) -> (t2 -> cont2) -> TermP p d (Union (Union r1 r2) g) c Source Github #
caseOf as overloaded if syntax with RebindableSyntax. Import ifThenElse
unqualified and write if x then l -> ... else r -> .... The then branch handles
the left alternative and the else branch handles the right; each receives its payload.
absurd :: forall {k} {p :: k +-> k} (s :: SYN k) (c :: SYN k) (d :: Nat) (g1 :: Ctx k) (g2 :: Ctx k). (Distributive k, MonoidalProfunctor p, KnownObj s, KnownObj c, Merge g1 g2) => TermP p d g1 s -> TermP p d g2 ('Zero :: SYN k) -> TermP p d (Union g1 g2) c Source Github #
There is no term of Zero, so from one, together with the rest of the context, anything
follows.
Contexts
type Ctx k = [(Nat, SYN k)] Source Github #
A context: the variables a term uses, each with its id and type, by descending id.
class KnownCtx (g :: Ctx k) Source Github #
A context that is known to be empty or not, all the way down.
Instances
| KnownCtx ('[] :: [(Nat, SYN k)]) Source Github # | |
Defined in Proarrow.Tools.SMC.Internal.Context Methods ctxCase :: (('[] :: [(Nat, SYN k)]) ~ ('[] :: [(Nat, SYN k)]) => r) -> (forall (n :: Nat) (a :: SYN k) (g' :: [(Nat, SYN k)]). (('[] :: [(Nat, SYN k)]) ~ ('(n, a) ': g'), KnownObj a, KnownCtx g') => r) -> r Source Github # withCtxOb :: Monoidal k => (Ob (Interp (Mul ('[] :: [(Nat, SYN k)]))) => r) -> r Source Github # | |
| (KnownObj a, KnownCtx g) => KnownCtx ('(n, a) ': g :: [(Nat, SYN k)]) Source Github # | |
Defined in Proarrow.Tools.SMC.Internal.Context Methods ctxCase :: (('(n, a) ': g) ~ ('[] :: [(Nat, SYN k)]) => r) -> (forall (n0 :: Nat) (a0 :: SYN k) (g' :: [(Nat, SYN k)]). (('(n, a) ': g) ~ ('(n0, a0) ': g'), KnownObj a0, KnownCtx g') => r) -> r Source Github # withCtxOb :: Monoidal k => (Ob (Interp (Mul ('(n, a) ': g))) => r) -> r Source Github # | |
type family Union (g1 :: Ctx k) (g2 :: Ctx k) :: Ctx k where ... Source Github #
The context of two terms used side by side.
class (KnownCtx g1, KnownCtx g2) => Merge (g1 :: Ctx k) (g2 :: Ctx k) Source Github #
Split the tensor of the union of two contexts into the tensors of the two contexts. This is
where the wires are reordered, the only place swap is used, and where a variable that both
contexts have is copied.
Minimal complete definition
Instances
| (Monoidal k, KnownCtx g2) => Merge ('[] :: [(Nat, SYN k)]) (g2 :: Ctx k) Source Github # | |
| (Monoidal k, KnownCtx ('(n, a) ': g1)) => Merge ('(n, a) ': g1 :: [(Nat, SYN k)]) ('[] :: [(Nat, SYN k)]) Source Github # | |
| (Monoidal k, KnownObj a, KnownObj b, KnownCtx g1, KnownCtx g2, MergeBy (CmpNat n m) ('(n, a) ': g1) ('(m, b) ': g2)) => Merge ('(n, a) ': g1 :: [(Natural, SYN k)]) ('(m, b) ': g2 :: [(Natural, SYN k)]) Source Github # | |
class (KnownCtx g, KnownCtx h) => Thin (g :: Ctx k) (h :: Ctx k) Source Github #
Keep the variables of g that h has and discard the others, for an alternative of the
additives that does not use all the variables of the other. Every variable of h must be in
g.
Minimal complete definition
Instances
| Monoidal k => Thin ('[] :: [(Nat, SYN k)]) ('[] :: [(Nat, SYN k)]) Source Github # | |
| (Monoidal k, Comonoid (Interp a), KnownObj a, Thin g ('[] :: [(Nat, SYN k)])) => Thin ('(n, a) ': g :: [(Nat, SYN k)]) ('[] :: [(Nat, SYN k)]) Source Github # | |
| (KnownObj a, KnownObj b, KnownCtx g, KnownCtx h, ThinBy (CmpNat n m) ('(n, a) ': g) ('(m, b) ': h)) => Thin ('(n, a) ': g :: [(Natural, SYN k)]) ('(m, b) ': h :: [(Natural, SYN k)]) Source Github # | |
class KnownCtx r => BindVar (n :: Nat) (a :: SYN k) (g :: Ctx k) (r :: Ctx k) | n g -> r Source Github #
A binder's variable n on the right of r, what the body uses besides it, given the body's
context g: snoc if the body uses the variable, and its counit otherwise. The binder's
variable is the newest in scope, so it can only be at the head of g. The functional
dependency, rather than a type family, gives r, so that a binder costs one comparison of ids.
Minimal complete definition
Do notation
(>>=) :: Bind p m t cont r => m -> (t -> cont) -> r Source Github #
Bind the right hand side to the pattern of the continuation.
return :: forall k t (d :: Nat) (p :: CAT k). (Monoidal k, Promonad p, RecVars k t, KnownCtx (Vars k t)) => t -> Ret t (TermP p d (Vars k t) (Mul (Vars k t))) Source Github #
The end of a rec block: all its variables, as the tensor of their context.
GHC's translation of rec refers to fail, but pairs of variables always match.
class Bind (p :: CAT k) m t cont r | m -> p Source Github #
A bind in a do block: a term, constant or tuple taken apart by a pattern, or the variables
of a rec block.
Minimal complete definition
Instances
| (TupleProfunctor '[x, y] p, ToTerm p (x, y) d g a, Bind p (TermP p d g a) t cont r) => Bind (p :: CAT k) (x, y) t cont r Source Github # | Convert a tuple before binding it. At least one component must identify the promonad. |
Defined in Proarrow.Tools.SMC.Internal.Do | |
| (p ~ q, a ~ (Unit :: k), Bind p (TermP p d ('[] :: [(Nat, SYN k)]) ('F b)) t cont r) => Bind (p :: CAT k) (q a b) t cont r Source Github # | Bind a constant arrow, computing it once before the continuation. |
Defined in Proarrow.Tools.SMC.Internal.Do | |
| Bind p ((x, y), z) t cont r => Bind (p :: CAT k) (x, y, z) t cont r Source Github # | |
Defined in Proarrow.Tools.SMC.Internal.Do | |
| Bind p ((w, x, y), z) t cont r => Bind (p :: CAT k) (w, x, y, z) t cont r Source Github # | |
Defined in Proarrow.Tools.SMC.Internal.Do | |
| (Promonad p, MonoidalProfunctor p, Costrong (Tensor :: k -> (k, k) -> Type) p, SymMonoidal k, RecVars k t, AnyDepth t t', AnyDepth t' t, RecVars k t', ToTerm p cont (RecDepth d (Vars k t)) gc c, r ~ TermP p d (Union (Minus gc (Vars k t)) (Minus g (Vars k t))) c, Unmerge (Inter g (Vars k t)) (Minus g (Vars k t)), Union (Inter g (Vars k t)) (Minus g (Vars k t)) ~ g, Thin (Vars k t) (Union (Inter g (Vars k t)) (Inter gc (Vars k t))), Merge (Inter g (Vars k t)) (Inter gc (Vars k t)), Merge (Minus gc (Vars k t)) (Minus g (Vars k t)), Unmerge (Minus gc (Vars k t)) (Inter gc (Vars k t)), Union (Minus gc (Vars k t)) (Inter gc (Vars k t)) ~ gc) => Bind (p :: CAT k) (Rec p d t g) t' cont r Source Github # | The rest of the |
| (p ~ Hom k, Dialogue k, KnownObj y, Merge g r, Negative (TyOf cont :: SYN k) y, Binds p d r a (TyOf cont :: SYN k) t cont, r' ~ TermP p d (Union g r) ('Not y)) => Bind (p :: CAT k) (TermP p d g ('Not ('Not a))) t cont r' Source Github # | A computation on the right hand side runs first, and the rest of the block is negative. |
| BindTerm p t (TermP p d g a) cont r => Bind (p :: CAT k) (TermP p d g a) t cont r Source Github # | A term on the right hand side is taken apart by the pattern. Incoherent, so that it is chosen as soon as the right hand side is known, unless the right hand side is a computation. |
| Bind p ((v, w, x, y), z) t cont r => Bind (p :: CAT k) (v, w, x, y, z) t cont r Source Github # | |
Defined in Proarrow.Tools.SMC.Internal.Do | |
| (Bind p (TermP p d g a) t cont r', r ~ Ret tt r') => Bind (p :: CAT k) (TermP p d g a) t (Ret tt cont) r Source Github # | The statement of a |
| Bind p ((u, v, w, x, y), z) t cont r => Bind (p :: CAT k) (u, v, w, x, y, z) t cont r Source Github # | |
Defined in Proarrow.Tools.SMC.Internal.Do | |
class (KnownObj a, KnownCtx g) => Binds (p :: CAT k) (n :: Nat) (g :: Ctx k) (a :: SYN k) (b :: SYN k) t cont Source Github #
The pattern t of a binder: it takes apart the variable (n, a), and the binder's body
cont then gives a term at depth n + 1 with type b, whose context is g and possibly that
variable. Both the variable's type and the rest of the context must be known.
Minimal complete definition
Examples
injectRight :: forall {k} (a :: k) (b :: k). (HasProducts k, HasBinaryCoproducts k, Ob a, Ob b) => a ~> (b || a) Source Github #
Inject as the right element of a sum.
>>>import Prelude (Bool (..))>>>injectRight @Bool @Bool TrueRight True
swapProduct :: forall {k} (a :: k) (b :: k). (HasProducts k, Ob a, Ob b) => (a && b) ~> (b && a) Source Github #
Swap a product. Only products are needed.
>>>import Prelude (Bool (..))>>>swapProduct @Bool @Bool (True, False)(False,True)
applyPair :: forall {k} (a :: k) (b :: k). (HasProducts k, Closed (PROD k), Ob a, Ob b) => Interp ((F a ':-> F b) ':** F a) ~> b Source Github #
Apply a function to an argument, both bundled in a product.
>>>import Prelude (Bool (..), not)>>>applyPair @Bool @Bool (not, True)False
curryPair :: forall {k} (a :: k) (b :: k). (HasProducts k, Closed (PROD k), Ob a, Ob b) => a ~> Interp (F b ':-> (F a ':** F b)) Source Github #
Curry a pairing function.
>>>import Prelude (Bool (..))>>>curryPair @Bool @Bool True False(True,False)
flipCurried3 :: forall {k} (a :: k) (b :: k) (c :: k). (HasProducts k, Closed (PROD k), Ob a, Ob b, Ob c) => Interp (F b ':-> (F a ':-> (F b ':-> F c))) ~> Interp (F a ':-> (F b ':-> F c)) Source Github #
Flip the first two arguments of a curried function, supplying its third argument from the second as well. Nested binders and a repeated variable use SMC's contexts.
>>>import Prelude (Bool (..))>>>flipCurried3 @Bool @Bool @Bool (\_ a _ -> a) True FalseTrue
swapSum :: forall {k} (a :: k) (b :: k). (HasProducts k, Distributive (PROD k), Ob a, Ob b) => (a || b) ~> (b || a) Source Github #
Swap a sum by case analysis. No exponentials are needed.
>>>import Prelude (Bool (..), Either (..))>>>swapSum @Bool @Bool (Left True)Right True>>>swapSum @Bool @Bool (Right False)Left False
caseEither :: forall {k} (a :: k) (b :: k) (c :: k). (HasProducts k, Closed (PROD k), Distributive (PROD k), Ob a, Ob b, Ob c) => Interp ((F a ':|| F b) ':** ((F a ':-> F c) ':** (F b ':-> F c))) ~> c Source Github #
Apply whichever function matches the branch of a sum. The functions are shared with the selected branch through the context.
>>>import Prelude (Bool (..), Either (..), id, not)>>>caseEither @Bool @Bool @Bool (Left True, (not, id))False>>>caseEither @Bool @Bool @Bool (Right True, (not, id))True