Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Category theory/Natural transformation
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Mixed === The “mixed” operations described below will be important also in understanding the definition of “monad” concept in category theory. ==== Functor and natural transformation ==== Let us imagine a parser library, which contains functions for parsing a form. There are two kinds of cells: * containing data which are optional (e.g. name of spouse) * containing data which consist of an enumeration of items (e.g. names of acquired languages) <haskell> spouse :: Parser (Maybe String) languages :: Parser [String] </haskell> Let us imagine we have any processing (storing, archiving etc.) function which processes lists (or any other reason which forces us to convert our results to list format and exclude any Maybe's). (Perhaps, all this example is impractical and exaggerated, because in real life we should solve the whole thing in other ways.) Thus, we want to build a parser combinator (we could notate it graphically with something like <math>\mathrm?\!\!\to\!\!\mathrm*</math>) which converts a “zero-ore-one-occurrence” like parser to a “zero-or-one-or-many-occurrences” like parser. We can convert <hask>Maybe</hask> to list with <hask>maybeToList</hask> But if we want to do something similar with a ''parser'' on Maybe's to achieve a ''parser'' on list, then <hask>maybeToList</hask> is not enough alone, we must <hask>fmap</hask> it. E.g. if we want to convert a parser like <hask>spouse</hask> to be of the same type as <hask>languages</hask>: <haskell> fmap maybeToList spouse </haskell> Let us see the types: We start with <haskell> spouse :: Parser (Maybe String) </haskell> :<math>\Lambda(\Phi(X))</math> or using notion of composing functors :<math>(\Lambda \Phi)(X)</math> We want to achieve <haskell> fmap maybeToList spouse :: Parser [String] </haskell> :<math>\Lambda(\Psi(X))</math> :<math>(\Lambda \Psi)(X)</math> thus we can infer <haskell> fmap maybeToList :: Parser (Maybe [String]) -> Parser [String] </haskell> :<math>(\Lambda\eta)_X \in \mathrm{Hom}_{\mathcal D}((\Lambda\Phi)(X),\;(\Lambda\Psi)(X))</math> In fact, we have a new “datatype converter”: converting not Maybe's to lists, but parser on Maybe to Parser on list. Let us notate the corresponding natural transformation with <math>\Lambda\eta</math>: :To each <math>X \in \mathbf{Ob}(\mathcal C)</math> we associate <math>(\Lambda\eta)_X \in \mathrm{Hom}_{\mathcal D}((\Lambda\Phi)(X),\;(\Lambda\Psi)(X))</math> :<math>\Lambda\eta : \Lambda\Phi \to \Lambda\Psi</math> :<math>(\Lambda\eta)_X = \Lambda(\eta_X)</math> Summary: :Let <math>\mathcal C, \mathcal D, \mathcal E</math> be categories :<math>\Phi, \Psi : \mathcal C \to \mathcal D</math> functors :<math>\Lambda : \mathcal D \to \mathcal E</math> functor :<math>\eta : \Phi \to \Psi</math> natural transformation Then let us define a new natural transformation: :<math>\Lambda\eta : \Lambda\Phi \to \Lambda\Psi</math> :<math>(\Lambda\eta)_X = \Lambda(\eta_X)</math> ==== Natural transformation and functor ==== :Let <math>\mathcal C, \mathcal D, \mathcal E</math> be categories :<math>\Delta : \mathcal C \to \mathcal D</math> functor :<math>\Phi, \Psi : \mathcal D \to \mathcal E</math> functors :<math>\eta : \Phi \to \Psi</math> natural transformation Then let us define a new natural transformation: :<math>\eta\Delta : \Phi\Delta \to \Psi\Delta</math> :<math>(\eta\Delta)_X = \eta_{\Delta(X)}</math> It can be illustrated by Haskell examples, too. Understanding it is made harder (easier?) by the fact that Haskell's type inference “(dis)solves” the main point, thus there is no “materialized” manifestation of it. <haskell> convert :: Maybe (Term a) -> [Term a] </haskell> Unlike [[#Functor and natural transformation|seen at <math>\mathrm?\!\!\to\!\!\mathrm*</math>]], the definition of this converter will not show much novelty: <haskell> convert = maybeToList </haskell> the most interesting thing is done automatically by type inference.
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width