proarrow
Safe HaskellNone
LanguageGHC2024

Proarrow.Tools.SMC.Internal.Pattern

Description

Internal module of Proarrow.Tools.SMC: patterns and binders. It exports everything, also what the public module keeps hidden.

Synopsis

Documentation

toSMC :: forall {k} (a :: SYN k) (b :: SYN k) t cont. (Monoidal k, Binds ((~>) :: CAT k) 0 ('[] :: [(Nat, SYN k)]) a b t cont) => (t -> cont) -> Interp a ~> Interp b Source Github #

Compile a function on terms to a morphism. The function receives the input through a pattern (see Patterns), so () compiles a term without inputs and a tuple takes a tensor apart.

toSMC1 :: forall {k} (a :: k) (b :: k) t cont. (Monoidal k, Binds ((~>) :: CAT k) 0 ('[] :: [(Nat, SYN k)]) ('F a) ('F b) t cont) => (t -> cont) -> a ~> b Source Github #

toSMC with both boundary objects embedded by F. The surrounding morphism type determines the input and output objects, so their type applications can be omitted. Use toSMC when a boundary needs other syntax, such as a function or computation type.

proc :: forall {k} (a :: SYN k) (b :: SYN k) t cont p. (MonoidalProfunctor p, Binds p 0 ('[] :: [(Nat, SYN k)]) a b t cont) => (t -> cont) -> p (Interp a) (Interp b) Source Github #

Compile a function on terms to an arrow of the promonad p, as toSMC does for the morphisms of the category, which are the case p = (~>). The terms' computations are arrows of p, lifted with (-<) or lift, and run in the order they are written; variables are values, which the category's structure copies and discards. This mirrors proc in Haskell's arrow notation, whose Arrows extension must be disabled.

liftF :: forall {k} (a :: SYN k) (b :: k) t p (d :: Nat) (g :: Ctx k). (Promonad p, BuildTuple p t g a) => p (Interp a) b -> t -> TermP p d g ('F b) Source Github #

Lift an arrow onto a term or tuple of terms, with its result embedded by F. The result object is inferred from the arrow. Use lift for composite result syntax.

(-<) :: forall {k} (a :: SYN k) (b :: k) t p (d :: Nat) (g :: Ctx k). (Promonad p, BuildTuple p t g a) => p (Interp a) b -> t -> TermP p d g ('F b) infixr 0 Source Github #

Arrow application, mirroring f -< x in Haskell's arrow notation. This is infix liftF: the input may be a term or tuple, and the result is embedded by F. The Arrows extension must be disabled, since it reserves this symbol.

returnA :: forall {k} p (a :: k). (Promonad p, Ob a) => p a a Source Github #

The identity morphism, named as in Haskell's arrow notation. An alias for id; returnA -< x embeds the input by F.

type Up (a :: SYN k) = 'Not ('Not a) Source Github #

The shift of a positive type to a negative one: a computation that produces an a. A term of it is a consumer of consumers of a, so in a category with Dual a = a ~~> r it is the continuation passing type (a -> r) -> r.

runUp :: forall {k} (d :: Nat) (g :: Ctx k) (r :: Ctx k) (a :: SYN k) (y :: SYN k). (Dialogue k, KnownObj a, KnownObj y, Merge g r) => Term d g (Up a) -> ((Interp (Mul r) ** Interp a) ~> Interp ('Not y)) -> Term d (Union g r) ('Not y) Source Github #

Run a computation against the rest of a block. The body is what the rest does with the value, given the context r; it becomes a consumer of the computation, whose context g joins. What the binds of a computation share, the counterpart of bound.

ret :: forall {k} {p :: k +-> k} (a :: SYN k) t (d :: Nat) (g :: Ctx k). (Dialogue k, Profunctor p, KnownObj a, BuildTuple p t g a) => t -> TermP p d g (Up a) Source Github #

A value as the computation that produces it: double negation introduction, and the return of a do block in the continuation reading. It also reads as a producer of a handed over as a consumer of Not a. The value can be given as a Tuple of terms, built by the type expected.

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 Up a is the computation of the pair at a. 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) where 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.

Methods

tuple :: forall (d :: Nat). t -> TermP p d g a Source Github #

Instances

Instances details
(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.

Instance details

Defined in Proarrow.Tools.SMC.Internal.Pattern

Methods

tuple :: forall (d :: Nat). () -> TermP p d g a Source Github #

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.

Instance details

Defined in Proarrow.Tools.SMC.Internal.Pattern

Methods

tuple :: forall (d :: Nat). t -> TermP p d g a Source Github #

(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.

Instance details

Defined in Proarrow.Tools.SMC.Internal.Pattern

Methods

tuple :: forall (d :: Nat). (x, y) -> TermP p d g a Source Github #

(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.

Instance details

Defined in Proarrow.Tools.SMC.Internal.Pattern

Methods

tuple :: forall (d :: Nat). (x, y) -> TermP p d g ('Not ('Not a)) Source Github #

BuildTuple p ((x, y), z) g a => BuildTuple (p :: CAT k) (x, y, z) (g :: Ctx k) (a :: SYN k) Source Github # 
Instance details

Defined in Proarrow.Tools.SMC.Internal.Pattern

Methods

tuple :: forall (d :: Nat). (x, y, z) -> TermP p d g a 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 # 
Instance details

Defined in Proarrow.Tools.SMC.Internal.Pattern

Methods

tuple :: forall (d :: Nat). (w, x, y, z) -> TermP p d g a 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 # 
Instance details

Defined in Proarrow.Tools.SMC.Internal.Pattern

Methods

tuple :: forall (d :: Nat). (v, w, x, y, z) -> TermP p d g a 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 # 
Instance details

Defined in Proarrow.Tools.SMC.Internal.Pattern

Methods

tuple :: forall (d :: Nat). (u, v, w, x, y, z) -> TermP p d g a Source Github #

class (KnownObj a, KnownCtx g) => Binds (p :: CAT k) (n :: Nat) (g :: Ctx k) (a :: SYN k) (b :: SYN k) t cont where 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.

Methods

bound :: (t -> cont) -> p (Interp (Mul g) ** Interp a) (Interp b) Source Github #

The body of a binder, with the pattern taking apart its new variable, as a morphism from the context around it and the variable. This is what toSMC, lam, loop, cont, caseOf, sumOver and the binds of do notation share.

Instances

Instances details
(Promonad p, KnownObj a, KnownCtx g, BindPat p (TermP p (n + 1) '['(n, a)] a) t cont (TermP p (n + 1) g' b), BindVar n a g' g) => Binds (p :: CAT k) n (g :: Ctx k) (a :: SYN k) (b :: SYN k) t cont Source Github # 
Instance details

Defined in Proarrow.Tools.SMC.Internal.Pattern

Methods

bound :: (t -> cont) -> p (Interp (Mul g) ** Interp a) (Interp b) Source Github #

class BindPat (p :: CAT k) m t cont r | m -> p where Source Github #

A term on the right hand side taken apart by the pattern of the continuation. The types of the continuation and the result are matched with equalities, so that the instance is chosen as soon as the right hand side is known.

Methods

bindPat :: m -> (t -> cont) -> r Source Github #

Instances

Instances details
(ToTerm p cont (d + PSize t) gc b, r ~ TermP p d gr b, Pat p t d g a gc b gr) => BindPat (p :: CAT k) (TermP p d g a) t cont r Source Github # 
Instance details

Defined in Proarrow.Tools.SMC.Internal.Pattern

Methods

bindPat :: TermP p d g a -> (t -> cont) -> r Source Github #

class Pat (p :: CAT k) t (d :: Nat) (g :: Ctx k) (a :: SYN k) (g' :: Ctx k) (c :: SYN k) (gr :: Ctx k) where Source Github #

A pattern: a variable, (), or a pair of patterns. A tuple of three to six stands for pairs nested to the left, as a :** b :** c is: (x, y, z) is ((x, y), z). Binding it at depth d to a term with context g and type a, with a continuation with context g' and type c, gives a term with context gr, which each instance fixes. A pair or () at a computation, Up a, runs it and matches its value, so c is then negative.

Methods

pat :: TermP p d g a -> (t -> TermP p (d + PSize t) g' c) -> TermP p d gr c Source Github #

Instances

Instances details
(MonoidalProfunctor p, a ~ ('I :: SYN k), KnownObj c, Merge g g', gr ~ Union g g') => Pat (p :: k +-> k) () d (g :: Ctx k) (a :: SYN k) (g' :: Ctx k) (c :: SYN k) (gr :: Ctx k) Source Github #

The pattern () uses up a term of the unit type.

Instance details

Defined in Proarrow.Tools.SMC.Internal.Pattern

Methods

pat :: TermP p d g a -> (() -> TermP p (d + PSize ()) g' c) -> TermP p d gr c Source Github #

(t ~ TermP p (DepthOf t) g a, gr ~ g') => Pat (p :: CAT k) t d (g :: Ctx k) (a :: SYN k) (g' :: Ctx k) (c :: SYN k) (gr :: Ctx k) Source Github #

A variable pattern names the right hand side, which is always a variable. If the body does not use it, the binder discards it.

Instance details

Defined in Proarrow.Tools.SMC.Internal.Pattern

Methods

pat :: TermP p d g a -> (t -> TermP p (d + PSize t) g' c) -> TermP p d gr c Source Github #

(p ~ Hom k, Dialogue k, KnownObj y, c ~ 'Not y, Merge g g', gr ~ Union g g') => Pat (p :: CAT k) () d (g :: Ctx k) ('Not ('Not ('I :: SYN k)) :: SYN k) (g' :: Ctx k) (c :: SYN k) (gr :: Ctx k) Source Github #

The pattern () at a computation of the unit runs it.

Instance details

Defined in Proarrow.Tools.SMC.Internal.Pattern

Methods

pat :: TermP p d g ('Not ('Not ('I :: SYN k))) -> (() -> TermP p (d + PSize ()) g' c) -> TermP p d gr c Source Github #

(Promonad p, MonoidalProfunctor p, a ~ (a1 ':** a2), KnownObj a1, KnownObj a2, Pat p y ((d + 2) + PSize x) '['(d + 1, a2)] a2 g' c gy, Pat p x (d + 2) '['(d, a1)] a1 gy c gxy, (((d + 2) + PSize x) + PSize y) ~ (d + PSize (x, y)), BindVar (d + 1) a2 gxy r1, BindVar d a1 r1 r, Merge r g, gr ~ Union r g) => Pat (p :: CAT k) (x, y) d (g :: Ctx k) (a :: SYN k) (g' :: Ctx k) (c :: SYN k) (gr :: Ctx k) Source Github # 
Instance details

Defined in Proarrow.Tools.SMC.Internal.Pattern

Methods

pat :: TermP p d g a -> ((x, y) -> TermP p (d + PSize (x, y)) g' c) -> TermP p d gr c Source Github #

(a ~ (a1 ** a2), Pat p (x, y) d g ('F a1 ':** 'F a2) g' c gr) => Pat (p :: CAT k) (x, y) d (g :: Ctx k) ('F a :: SYN k) (g' :: Ctx k) (c :: SYN k) (gr :: Ctx k) Source Github #

A pair pattern at an embedded tensor exposes its two embedded components. The tensor pattern supplies the binding and context operations.

Instance details

Defined in Proarrow.Tools.SMC.Internal.Pattern

Methods

pat :: TermP p d g ('F a) -> ((x, y) -> TermP p (d + PSize (x, y)) g' c) -> TermP p d gr c Source Github #

(p ~ Hom k, Dialogue k, KnownObj a, KnownObj y, c ~ 'Not y, Pat p (x, y') d '['(d, a)] a g' c gp, BindVar d a gp r, Merge g r, gr ~ Union g r) => Pat (p :: CAT k) (x, y') d (g :: Ctx k) ('Not ('Not a) :: SYN k) (g' :: Ctx k) (c :: SYN k) (gr :: Ctx k) Source Github #

A pair pattern at a computation runs it and takes its value apart. The value needs no variable of its own: the pattern's variables take the ids it would have taken.

Instance details

Defined in Proarrow.Tools.SMC.Internal.Pattern

Methods

pat :: TermP p d g ('Not ('Not a)) -> ((x, y') -> TermP p (d + PSize (x, y')) g' c) -> TermP p d gr c Source Github #

Pat p ((x, y), z) d g a g' c gr => Pat (p :: CAT k) (x, y, z) d (g :: Ctx k) (a :: SYN k) (g' :: Ctx k) (c :: SYN k) (gr :: Ctx k) Source Github # 
Instance details

Defined in Proarrow.Tools.SMC.Internal.Pattern

Methods

pat :: TermP p d g a -> ((x, y, z) -> TermP p (d + PSize (x, y, z)) g' c) -> TermP p d gr c Source Github #

Pat p ((w, x, y), z) d g a g' c gr => Pat (p :: CAT k) (w, x, y, z) d (g :: Ctx k) (a :: SYN k) (g' :: Ctx k) (c :: SYN k) (gr :: Ctx k) Source Github # 
Instance details

Defined in Proarrow.Tools.SMC.Internal.Pattern

Methods

pat :: TermP p d g a -> ((w, x, y, z) -> TermP p (d + PSize (w, x, y, z)) g' c) -> TermP p d gr c Source Github #

Pat p ((v, w, x, y), z) d g a g' c gr => Pat (p :: CAT k) (v, w, x, y, z) d (g :: Ctx k) (a :: SYN k) (g' :: Ctx k) (c :: SYN k) (gr :: Ctx k) Source Github # 
Instance details

Defined in Proarrow.Tools.SMC.Internal.Pattern

Methods

pat :: TermP p d g a -> ((v, w, x, y, z) -> TermP p (d + PSize (v, w, x, y, z)) g' c) -> TermP p d gr c Source Github #

Pat p ((u, v, w, x, y), z) d g a g' c gr => Pat (p :: CAT k) (u, v, w, x, y, z) d (g :: Ctx k) (a :: SYN k) (g' :: Ctx k) (c :: SYN k) (gr :: Ctx k) Source Github # 
Instance details

Defined in Proarrow.Tools.SMC.Internal.Pattern

Methods

pat :: TermP p d g a -> ((u, v, w, x, y, z) -> TermP p (d + PSize (u, v, w, x, y, z)) g' c) -> TermP p d gr c Source Github #

type family PSize t :: Nat where ... Source Github #

The number of variables a pattern binds on the way, and so the depth it adds.

Equations

PSize (x, y) = (2 + PSize x) + PSize y 
PSize (x, y, z) = PSize ((x, y), z) 
PSize (w, x, y, z) = PSize ((w, x, y), z) 
PSize (v, w, x, y, z) = PSize ((v, w, x, y), z) 
PSize (u, v, w, x, y, z) = PSize ((u, v, w, x, y), z) 
PSize t = 0