I finally understand what Waldek called as "compiler support"
after reading some of the discussion a few years back.

On 10/16/16 6:40 AM, Waldek Hebisch wrote:
> 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.


BUT! Current Spad compiler does not have such special checks,
as this example shows, is this a bug?

--------------
)abbrev package TEST Test

Test() : Exp == Imp where
  Exp == with
    exquo2 : (Integer, Integer) -> Union(Integer, "failed")
  Imp == add
    exquo2(x, y) ==
        a := x exquo y
        t : Integer := a::Integer -- NO COMPILER ERROR HERE!
        t
---------------

I understand that Waldek want to avoid "coercion which calls error if
value is not T", aka "fromJust : Maybe a -> a" which is a not total
function, it passes compiler check but may give runtime error.

Let me present how Haskell solves this problem:

From my limited Haskell experience, there are 3 ways:

1. Pattern matching.  Effectively equals to
    map : (Maybe A, A -> A) -> Maybe A
That is, deals with both the branch of "failed" and branch with type A.

2. Monad style >>=.
That is, use '>>=' on a series of functions with signature 'A -> Maybe A',
use '>>= : (Maybe A, A -> Maybe A) : Maybe A' to compose a series
of computation.

3. 'do' notation, which syntactic sugar over Monad.


I believe this "Maybe" style error handling is the right way to go.
Better abstraction, better model.

- Qian

--
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/fricas-devel/7c90d89f-3322-82b5-a47a-9afc5475da5c%40gmail.com.

Reply via email to