Articulo de referencia

transformador monad

En programación funcional , un transformador de mónadas es un constructor de tipos que toma una mónada como argumento y devuelve una mónada como resultado. Los transformadores d...

En programación funcional , un transformador de mónadas es un constructor de tipos que toma una mónada como argumento y devuelve una mónada como resultado.

Los transformadores de mónadas permiten componer funcionalidades encapsuladas por mónadas —como el estado, el manejo de excepciones y la entrada/salida— de forma modular. Normalmente, un transformador de mónada se crea generalizando una mónada existente; al aplicar el transformador resultante a la mónada identidad, se obtiene una mónada equivalente a la original (sin tener en cuenta el empaquetado ni el desempaquetado necesarios).

Definición

Un transformador de mónada consta de:

  1. Un constructor de tipos tde clase(* -> *) -> * -> *
  2. Operaciones de mónadas returny bind(o una formulación equivalente) para todo t mdonde mes una mónada, que satisface las leyes de las mónadas.
  3. Una operación adicional, , que satisface las siguientes leyes: [ 1 ] (la notación que aparece a continuación indica la aplicación infija): lift :: m a -> t m a`bind`
    1. lift . return = return
    2. lift (m `bind` k) = (lift m) `bind` (lift . k)

Ejemplos

El transformador de mónada de opciones

Dado cualquier mónadaMETROA{\displaystyle \mathrm {M} \,A}, el transformador de mónada de opcionesMETRO(A¿){\displaystyle \mathrm {M} \left(A^{?}\right)}(dóndeA¿{\displaystyle A^{?}}denota el tipo de opción ) se define por:

rmitrnorte:AMETRO(A¿)armitrnorte(Jsta)binorted:METRO(A¿)(AMETRO(B¿))METRO(B¿)metroFbinortedmetro(a{devolver nadasi a=norteothinortegramoFasi a=Jsta)liFt:METRO(A)METRO(A¿)metrobinortedmetro(armitrnorte(Jsta)){\displaystyle {\begin{array}{ll}\mathrm {return} :&A\rightarrow \mathrm {M} \left(A^{?}\right)\\&a\mapsto \mathrm {return} (\mathrm {Just} \,a)\\\mathrm {bind}  :&\mathrm {M} \left(A^{?}\right)\rightarrow \left(A\rightarrow \mathrm {M} \left(B^{?}\right)\right)\rightarrow \mathrm {M} \left(B^{?}\right)\\&m\mapsto f\mapsto \mathrm {bind} \,m\,\left(a\mapsto {\begin{cases}{\mbox{devolver nada}}&{\mbox{if }}a=\mathrm {Nada} \\f\,a'&{\mbox{if }}a=\mathrm {Sólo} \,a'\end{cases}}\right)\\\mathrm {lift}  :&\mathrm {M} (A)\rightarrow \mathrm {M} \left(A^{?}\right)\\&m\mapsto \mathrm {bind} \,m\,(a\mapsto \mathrm {return} (\mathrm {Just} \,a))\end{array}}}

El transformador de mónadas de excepción

Dado cualquier mónadaMETROA{\displaystyle \mathrm {M} \,A}, el transformador de mónadas de excepciónMETRO(A+mi){\displaystyle \mathrm {M} (A+E)}(donde E es el tipo de excepciones) se define por:

rmitrnorte:AMETRO(A+mi)armitrnorte(valmia)binorted:METRO(A+mi)(AMETRO(B+mi))METRO(B+mi)metroFbinortedmetro(a{error de retorno misi a=mirrmiFasi a=valmia)liFt:METROAMETRO(A+mi)metrobinortedmetro(armitrnorte(valmia)){\displaystyle {\begin{array}{ll}\mathrm {return} :&A\rightarrow \mathrm {M} (A+E)\\&a\mapsto \mathrm {return} (\mathrm {valor} \,a)\\\mathrm {bind}  :&\mathrm {M} (A+E)\rightarrow (A\rightarrow \mathrm {M} (B+E))\rightarrow \mathrm {M} (B+E)\\&m\mapsto f\mapsto \mathrm {bind} \,m\,\left(a\mapsto {\begin{cases}{\mbox{return err }}e&{\mbox{if }}a=\mathrm {err} \,e\\f\,a'&{\mbox{if }}a=\mathrm {value} \,a'\end{cases}}\right)\\\mathrm {lift}  :&\mathrm {M} \,A\rightarrow \mathrm {M} (A+E)\\&m\mapsto \mathrm {bind} \,m\,(a\mapsto \mathrm {return} (\mathrm {valor} \,a))\\\end{array}}}

El transformador de mónadas del lector

Dado cualquier mónadaMETROA{\displaystyle \mathrm {M} \,A}, el transformador de mónadas del lectormiMETROA{\displaystyle E\rightarrow \mathrm {M} \,A}(donde E es el tipo de entorno) se define por:

rmitrnorte:AmiMETROAamirmitrnorteabinorted:(miMETROA)(AmiMETROB)miMETROBmetrokmibinorted(metromi)(akami)liFt:METROAmiMETROAamia{\displaystyle {\begin{array}{ll}\mathrm {return} :&A\rightarrow E\rightarrow \mathrm {M} \,A\\&a\mapsto e\mapsto \mathrm {return} \,a\\\mathrm {bind}  :&(E\rightarrow \mathrm {M} \,A)\rightarrow (A\rightarrow E\rightarrow \mathrm {M} \,B)\rightarrow E\rightarrow \mathrm {M} \,B\\&m\mapsto k\mapsto e\mapsto \mathrm {bind} \,(m\,e)\,(a\mapsto k\,a\,e)\\\mathrm {lift}  :&\mathrm {M} \,A\rightarrow E\rightarrow \mathrm {M} \,A\\&a\mapsto e\mapsto a\\\end{array}}}

El transformador de mónada de estado

Dado cualquier mónadaMETROA{\displaystyle \mathrm {M} \,A}, el transformador de mónada de estadoSMETRO(A×S){\displaystyle S\rightarrow \mathrm {M} (A\times S)}(donde S es el tipo de estado) se define por:

rmitrnorte:ASMETRO(A×S)asrmitrnorte(a,s)binorted:(SMETRO(A×S))(ASMETRO(B×S))SMETRO(B×S)metroksbinorted(metros)((a,s)kas)liFt:METROASMETRO(A×S)metrosbinortedmetro(armitrnorte(a,s)){\displaystyle {\begin{array}{ll}\mathrm {return} :&A\rightarrow S\rightarrow \mathrm {M} (A\times S)\\&a\mapsto s\mapsto \mathrm {return} \,(a,s)\\\mathrm {bind}  :&(S\rightarrow \mathrm {M} (A\times S))\rightarrow (A\rightarrow S\rightarrow \mathrm {M} (B\times S))\rightarrow S\rightarrow \mathrm {M} (B\times S)\\&m\mapsto k\mapsto s\mapsto \mathrm {bind} \,(m\,s)\,((a,s')\mapsto k\,a\,s')\\\mathrm {levantar}  :&\mathrm {M} \,A\rightarrow S\rightarrow \mathrm {M} (A\times S)\\&m\mapasto s\mapsto \mathrm {bind} \,m\,(a\mapsto \mathrm {return} \,(a,s))\end{array}}}

El escritor transformador de mónadas

Dado cualquier mónadaMETROA{\displaystyle \mathrm {M} \,A}, el escritor transformador de mónadasMETRO(W×A){\displaystyle \mathrm {M} (W\times A)}(donde W está dotado de una operación monoide con elemento identidadε{\displaystyle \varepsilon }) se define por:

rmitrnorte:AMETRO(W×A)armitrnorte(ε,a)binorted:METRO(W×A)(AMETRO(W×B))METRO(W×B)metroFbinortedmetro((w,a)binorted(Fa)((w,b)rmitrnorte(ww,b)))liFt:METROAMETRO(W×A)metrobinortedmetro(armitrnorte(ε,a)){\displaystyle {\begin{array}{ll}\mathrm {return} :&A\rightarrow \mathrm {M} (W\times A)\\&a\mapsto \mathrm {return} \,(\varepsilon ,a)\\\mathrm {bind}  :&\mathrm {M} (W\times A)\rightarrow (A\rightarrow \mathrm {M} (W\times B))\rightarrow \mathrm {M} (W\times B)\\&m\mapsto f\mapsto \mathrm {bind} \,m\,((w,a)\mapsto \mathrm {bind} \,(f\,a)\,((w',b)\mapsto \mathrm {return} \,(w*w',b)))\\\mathrm {lift}  :&\mathrm {M} \,A\rightarrow \mathrm {M} (W\veces A)\\&m\mapsto \mathrm {bind} \,m\,(a\mapsto \mathrm {return} \,(\varepsilon ,a))\\\end{array}}}

El transformador de mónada de continuación

Dado cualquier mónadaMETROA{\displaystyle \mathrm {M} \,A}, el transformador de mónada de continuación asigna un tipo arbitrario R a funciones de tipo(AMETROR)METROR{\displaystyle (A\rightarrow \mathrm {M} \,R)\rightarrow \mathrm {M} \,R}donde R es el tipo de resultado de la continuación. Se define por:

rmitrnorte:A(AMETROR)METRORakkabinorted:((AMETROR)METROR)(A(BMETROR)METROR)(BMETROR)METRORdoFkdo(aFak)liFt:METROA(AMETROR)METRORbinorted{\displaystyle {\begin{array}{ll}\mathrm {return} \colon &A\rightarrow \left(A\rightarrow \mathrm {M} \,R\right)\rightarrow \mathrm {M} \,R\\&a\mapsto k\mapsto k\,a\\\mathrm {bind} \colon &\left(\left(A\rightarrow \mathrm {M} \,R\right)\rightarrow \mathrm {M} \,R\right)\rightarrow \left(A\rightarrow \left(B\rightarrow \mathrm {M} \,R\right)\rightarrow \mathrm {M} \,R\right)\rightarrow \left(B\rightarrow \mathrm {M} \,R\right)\rightarrow \mathrm {M} \,R\\&c\mapsto f\mapsto k\mapsto c\,\left(a\mapsto f\,a\,k\right)\\\mathrm {lift} \colon &\mathrm {M} \,A\rightarrow (A\rightarrow \mathrm {M} \,R)\rightarrow \mathrm {M} \,R\\&\mathrm {bind} \end{array}}}

Tenga en cuenta que las transformaciones de mónadas generalmente no son conmutativas : por ejemplo, aplicar el transformador de estado a la mónada de opción produce un tipoS(A×S)¿{\displaystyle S\rightarrow \left(A\times S\right)^{?}}(un cálculo que puede fallar y no producir ningún estado final), mientras que la transformación inversa tiene tipoS(A¿×S){\displaystyle S\rightarrow \left(A^{?}\times S\right)}(un cálculo que produce un estado final y un valor de retorno opcional).

Véase también

Referencias

  1. Liang, Sheng; Hudak, Paul; Jones, Mark (1995). "Transformadores de mónadas e intérpretes modulares" (PDF) . Actas del 22.º simposio ACM SIGPLAN-SIGACT sobre principios de lenguajes de programación . Nueva York, NY: ACM. págs. 333–343 . doi : 10.1145/199448.199528 . 
  • Una entrada de blog que revisa brevemente parte de la literatura sobre transformadores de mónadas y conceptos relacionados, con un enfoque en el tratamiento categórico-teórico.