The following code compiles (requires bootStrapMode) and seems to work:

--
)abbrev category APPL Applicative
Applicative(S : Type, A: (Type->Type)) : Category == Type with
    pure: S -> A(S)
    ap: (A(S->S),A(S)) -> A(S)

)boot $bootStrapMode := true
)abbrev domain AMAYBE AMaybe
AMaybe(S : Type) : Public == Private where
  Public == Join(RetractableTo S, Functor S, Applicative(S,AMaybe)) with
    just : S -> %
      ++ \spad{just x} injects the value `x' into %.
    fromJust : % -> S
    nothing? : % -> Boolean
    nothing : () -> %
      ++ \spad{nothing} represents failure or absence of value.
    if S has CoercibleTo OutputForm then CoercibleTo OutputForm
  Private == add
    Rep := Union(S, "failed")
    just x == x
    nothing() == "failed"
    nothing? x == x case "failed"
    fromJust x ==
       nothing? x => error "nothing in fromJust"
       x@S
    map(f, x) ==
        nothing? x => nothing()
        just f fromJust x
    -- Applicative operators
    --pure(x : S) : AMaybe(S) == just x
    pure(x) == just x
    --ap(f : AMaybe(S->S), x : AMaybe(S)) : AMaybe(S) ==
    ap(f, x) ==
        nothing? x => nothing()
        nothing? f => nothing()
        just((fromJust f)(fromJust x))
    if S has CoercibleTo OutputForm then
      coerce(x: %): OutputForm ==
        nothing? x => paren(empty()$OutputForm)$OutputForm
        (x@T)::OutputForm

)boot $bootStrapMode := false
)abbrev domain AMAYBE AMaybe
... repeat above ...
--

In the interpreter:

(1) -> f(x:INT):INT == x + 1
   Function declaration f : Integer -> Integer has been added to
      workspace.
   Compiled code for f has been cleared.
   1 old definition(s) deleted for function or rule f
                                                                   Type: Void
(2) -> ap(pure(f)$AMaybe(INT->INT),pure(-1)$AMaybe(INT))
   Compiling function f with type Integer -> Integer

   (2)  0
                                                        Type: AMaybe(Integer)

But it seems that interpreter has rather poor support for this usage. E.g.

(3) -> AMaybe(INT) has Applicative(INT,AMaybe)

   Although AMaybe is the name of a constructor, a full type must be
      specified in the context you have used it. Issue )show AMaybe for
      more information.


On 27 October 2016 at 06:40, oldk1331 <[email protected]> wrote:
> Does everyone all agree that "MapCategory" is a proper name?
>
> There's a problem when implementing Haskell's Applicative:
>
> Applicative A exports signature:
>
>    ap : (A(S->S), A(S)) -> A(S)  -- % is A(S)
>
> First, you can't express A(S->S) in category definition.
> Second, for a specific domain, compiler doesn't accept
> type A(S->S). (For example, in domain List, you can't use List(S->S) .)
>
> In interpreter, for List, ap can be defined as this, with or without type:
> ap(f:List(INT->INT),x:List(INT)):List(INT) == concat map(y+->map(y,x),f)
> ap(f,x) == concat map(y+->map(y,x),f)
>
> Note that in general it should use function 'join : M(M a) -> M a'
> instead of concat, and this signature also can not be expressed
> in category.
>
> I think these 2 limitations are not fundamental and can be worked out.
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/fricas-devel.
> For more options, visit https://groups.google.com/d/optout.

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/fricas-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to