|
|
last edited 13 years ago by Bill Page |
2 | ||
Editor: Bill Page
Time: 2011/11/14 18:22:18 GMT-8 |
||
Note: testing |
changed: - This may be a better definition of Monad in Axiom. Ref: http://en.wikibooks.org/wiki/Haskell/Category_theory A monad is an endofunctor In Axiom a functor is a domain constructor. An endofunctor takes a domain in some category and constructs a domain in that same category. \begin{aldor}[monadcat] --)abbrev category MONADC MonadCat #pile #include "axiom" MonadCat(A : Type, M: Type -> Type): Category == with if A has SetCategory then SetCategory fmap: (A->A, %) -> % unit: A -> % join: M % -> % mult: (%, %) -> % \end{aldor} \begin{axiom} )show MonadCat \end{axiom} \begin{aldor} --)abbrev domain MONADL MonadList #pile #include "axiom" #library lmonadcat "monadcat.ao" import from lmonadcat MonadList(A:Type): MonadCat(A,List) == add Rep == List A import from Rep rrep(x:%):Rep == x pretend Rep --perr(x:Rep):% == x pretend % fmap(f:A->A,x:%):% == per(map(f,rep x)$ListFunctions2(A,A)) unit(x:A):% == per list(x) join(x:List %):% == per(concat(map(rrep,x)$ListFunctions2(%,Rep))) mult(x:%,y:%):% == per concat(rep x,rep y) if A has SetCategory then coerce(x:%):OutputForm == rep(x)::OutputForm ((x:%) = (y:%)):Boolean == L:List Boolean := map(_=$A,rep x,rep y)$ListFunctions3(A,A,Boolean) reduce(_and,L,false)$ListFunctions2(Boolean,Boolean) \end{aldor} \begin{axiom} )show MonadList(Integer) \end{axiom} \begin{axiom} L1 := unit(1)$MonadList(Integer) L2 := unit(2)$MonadList(Integer) L3 := unit(3)$MonadList(Integer) J1:=mult(L1,L2)$MonadList(Integer) J2:=join([L1,L2])$MonadList(Integer) fmap(x+->x+1,J1)$MonadList(Integer) \end{axiom} \begin{spad} )abbrev domain MAYBE2 Maybe2 Maybe2(R:SetCategory):SetCategory == add Rep := Union(R,"failed") rep(x:%):Rep == x pretend Rep per(x:Rep):% == x pretend % \end{spad} \begin{spad} )abbrev domain MAYBE2 Maybe2 Maybe2(R:SetCategory):MonadCat(R,Maybe2) == add Rep ==> Union(R,"failed") rep(x:%):Rep == x pretend Rep per(x:Rep):% == x pretend % fmap(f:R->R,x:%):% == rep(x) case R => per f(rep x) per "failed" unit(x:R):% = per x join(x:Maybe %):% == rep(x) case "failed" => per "failed" rep(rep x) case "failed" => per "failed" x \end{spad} \begin{axiom} )show Maybe \end{axiom} \begin{axiom} x:Maybe(Integer):= unit(-1) \end{axiom}
This may be a better definition of Monad in Axiom.
Ref: http://en.wikibooks.org/wiki/Haskell/Category_theory
In Axiom a functor is a domain constructor. An endofunctor takes a domain in some category and constructs a domain in that same category.
--)abbrev category MONADC MonadCat #pile #include "axiom" MonadCat(A : Type,M: Type -> Type): Category == with if A has SetCategory then SetCategory fmap: (A->A, %) -> % unit: A -> % join: M % -> % mult: (%, %) -> %
Compiling FriCAS source code from file /var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/monadcat.as using Aldor compiler and options -O -Fasy -Fao -Flsp -lfricas -Mno-ALDOR_W_WillObsolete -DFriCAS -Y $FRICAS/algebra -I $FRICAS/algebra Use the system command )set compiler args to change these options. The )library system command was not called after compilation.
)show MonadCat
The )show system command is used to display information about types or partial types. For example,)show Integer will show information about Integer .
MonadCat is not the name of a known type constructor. If you want to see information about any operations named MonadCat ,issue )display operations MonadCat
--)abbrev domain MONADL MonadList #pile #include "axiom" #library lmonadcat "monadcat.ao" import from lmonadcat MonadList(A:Type): MonadCat(A,List) == add Rep == List A import from Rep rrep(x:%):Rep == x pretend Rep --perr(x:Rep):% == x pretend %
fmap(f:A->A,x:%):% == per(map(f, rep x)$ListFunctions2(A, A)) unit(x:A):% == per list(x) join(x:List %):% == per(concat(map(rrep, x)$ListFunctions2(%, Rep))) mult(x:%, y:%):% == per concat(rep x, rep y) if A has SetCategory then coerce(x:%):OutputForm == rep(x)::OutputForm ((x:%) = (y:%)):Boolean == L:List Boolean := map(_=$A, rep x, rep y)$ListFunctions3(A, A, Boolean) reduce(_and, L, false)$ListFunctions2(Boolean, Boolean)
Compiling FriCAS source code from file /var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/2630045957127378990-25px003.as using Aldor compiler and options -O -Fasy -Fao -Flsp -lfricas -Mno-ALDOR_W_WillObsolete -DFriCAS -Y $FRICAS/algebra -I $FRICAS/algebra Use the system command )set compiler args to change these options. The )library system command was not called after compilation.
)show MonadList(Integer)
The )show system command is used to display information about types or partial types. For example,)show Integer will show information about Integer .
MonadList is not the name of a known type constructor. If you want to see information about any operations named MonadList ,issue )display operations MonadList
Integer is not the name of a known type constructor. If you want to see information about any operations named Integer ,issue )display operations Integer
L1 := unit(1)$MonadList(Integer)
There are no library operations named MonadList Use HyperDoc Browse or issue )what op MonadList to learn if there is any operation containing " MonadList " in its name.
Cannot find a definition or applicable library operation named MonadList with argument type(s) Type
Perhaps you should use "@" to indicate the required return type,or "$" to specify which version of the function you need.
)abbrev domain MAYBE2 Maybe2 Maybe2(R:SetCategory):SetCategory == add Rep := Union(R,"failed") rep(x:%):Rep == x pretend Rep per(x:Rep):% == x pretend %
Compiling FriCAS source code from file /var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/4607644844684909994-25px006.spad using old system compiler. MAYBE2 abbreviates domain Maybe2 ------------------------------------------------------------------------ initializing NRLIB MAYBE2 for Maybe2 compiling into NRLIB MAYBE2 compiling local rep : $ -> Rep MAYBE2;rep is replaced by x Time: 0 SEC.
compiling local per : Rep -> $ MAYBE2;per is replaced by x Time: 0 SEC.
(time taken in buildFunctor: 0)
;;; *** |Maybe2| REDEFINED
;;; *** |Maybe2| REDEFINED Time: 0 SEC.
Warnings: [1] rep: pretendRep -- should replace by @ [2] per: pretend$ -- should replace by @
Cumulative Statistics for Constructor Maybe2 Time: 0 seconds
finalizing NRLIB MAYBE2 Processing Maybe2 for Browser database: --->-->Maybe2(): Missing Description ; compiling file "/var/aw/var/LatexWiki/MAYBE2.NRLIB/MAYBE2.lsp" (written 06 APR 2022 03:04:34 AM):
; /var/aw/var/LatexWiki/MAYBE2.NRLIB/MAYBE2.fasl written ; compilation finished in 0:00:00.013 ------------------------------------------------------------------------ Maybe2 is now explicitly exposed in frame initial Maybe2 will be automatically loaded when needed from /var/aw/var/LatexWiki/MAYBE2.NRLIB/MAYBE2
)abbrev domain MAYBE2 Maybe2 Maybe2(R:SetCategory):MonadCat(R,Maybe2) == add Rep ==> Union(R, "failed") rep(x:%):Rep == x pretend Rep per(x:Rep):% == x pretend %
fmap(f:R->R,x:%):% == rep(x) case R => per f(rep x) per "failed" unit(x:R):% = per x join(x:Maybe %):% == rep(x) case "failed" => per "failed" rep(rep x) case "failed" => per "failed" x
Compiling FriCAS source code from file /var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/2461590120764584468-25px007.spad using old system compiler. MAYBE2 abbreviates domain Maybe2 ------------------------------------------------------------------------ initializing NRLIB MAYBE2 for Maybe2 compiling into NRLIB MAYBE2 cannot produce category object: (|MonadCat| R |Maybe2|)
>> Apparent user error: cannot produce category object
)show Maybe
The )show system command is used to display information about types or partial types. For example,)show Integer will show information about Integer .
Maybe is not the name of a known type constructor. If you want to see information about any operations named Maybe ,issue )display operations Maybe
x:Maybe(Integer):= unit(-1)
There are no library operations named Maybe Use HyperDoc Browse or issue )what op Maybe to learn if there is any operation containing " Maybe " in its name.
Cannot find a definition or applicable library operation named Maybe with argument type(s) Type
Perhaps you should use "@" to indicate the required return type,or "$" to specify which version of the function you need.