oldk1331 wrote: > > There's a long thread in 2011 talking about monad, > but that discussion is not very clear and not what > I am going to talk about. > https://groups.google.com/d/msg/fricas-devel/UCFkQGgOOf0/oj9-FygocVgJ > > The motivation get me into this topic is simple: > In OpenAxiom, I see there's a Maybe domain instead > of Union(T, "failed"). I want to have Maybe in FriCAS. > > However, unlike Haskell, Maybe in OpenAxiom is not > a Monad. > > So I did some research about how to implement Monad > in FriCAS: > > ## Functor > First, a simpler concept, Functor. OpenAxiom has this > Category defined, basicly, exports 'map(S->S, %)->%'. > > I think FriCAS should also use this Categoty, ")dis op map" gives > 81 exposed and 15 unexposed signature, that's too messy. > > This Functor category is easy to understand, but it can't > handle more than 2 types: > map : (A->B, Functor A) : Functor B > It is impossible to export this signature in a category, and I use > a "hacky" method to implement it, see FunctorPackage bellow. > > ## Maybe > Maybe is an abstraction over Union(T, "failed"), but it also has > some property: Maybe is a Functor and a Monad. > > See the implementation of "map" in Maybe and FunctorPackage. > > ## Monad > Functor is a category exports map : (S -> S, %) -> %, > Monad is a category exports bind : (S -> %, %) -> %, > or its infix form (% >>= (S -> %)) : % > > For more than 2 types, > bind : (A -> Monad B, Monad A) -> Monad B > >>= : (Monad A, A -> Monad B) -> Monad B > > It seems that FriCAS doesn't support infix operator with > 3 letters, I use "_>_>" in the code bellow.

## Advertising

Well, FriCAS lexer has fixed set non-alphabetic tokens and '>>=' is not one but two tokens ('>>' followed by '='). > ## Usage > This is just a simple example: > > myinv(x:FRAC INT):Maybe FRAC INT == if x = 0 then nothing()$Maybe(FRAC > INT) else just(1/x) > > (3) -> myinv 4 > > 1 > (3) â > 4 > Type: Maybe(Fraction(Integer)) > (4) -> myinv 0 > > (4) () > Type: Maybe(Fraction(Integer)) > (5) -> myinv 4 >> myinv > > (5) 4 > Type: Maybe(Fraction(Integer)) > (6) -> myinv 0 >> myinv > > (6) () > Type: Maybe(Fraction(Integer)) > > Compared with: > (7) -> inv 4 > > 1 > (7) â > 4 > Type: Fraction(Integer) > (8) -> inv inv 4 > > (8) 4 > Type: Fraction(Integer) > (9) -> inv inv 0 > > >> Error detected within library code: > not invertible > > ## > > This is just a "proof of concept" version, you can see the > Haskell version of Maybe from here: > https://hackage.haskell.org/package/base-4.9.0.0/docs/Data-Maybe.html > > So, do you support implement such Functor/Monad/Maybe > in FriCAS? I do not understand why you want this. One problem is length of 'Union(Something, "failed")', but we can use macros like: Maybe(T) ==> Union(T, "failed") UF(T) ==> Union(T, "failed") or UF1 ==> Union(Something, "failed") You seem to want to use '>>' to convert functions from T to Union(T, "failed") into functions from Union(T, "failed") to Union(T, "failed"). I am not sure if this will work well: currently both Spad compiler and interpteter have very limited ability to "invent" new types. Actually Spad compiler need some explicit or implicit import to use a type. Interpeter has a bunch of hardcoded rules, go beyond them and it can not find type. So there is good chance that you will have to explicitely import your packages/domains for all involved T-s. Also, Spad compiler knows about unions, so we can use code like: ru := retractIfCan(x)@Union(T, "failed") ru case "failed" => "failed" r := ru::T and Spad compiler knows that coercion in third line is legit, because we excluded the other possibility in line 2. With 'Maybe' compiler will no longer accept code like above. Of course you may add explicit functions to test for failure and explicit function for coercion which calls error if value is not T, but then you loose static checking: once you add such coercion you can call it without performing check before. -- Waldek Hebisch -- You received this message because you are subscribed to the Google Groups "FriCAS - computer algebra system" group. To unsubscribe from this group and stop receiving emails from it, send an email to fricas-devel+unsubscr...@googlegroups.com. To post to this group, send email to fricas-devel@googlegroups.com. Visit this group at https://groups.google.com/group/fricas-devel. For more options, visit https://groups.google.com/d/optout.