Here you go.  But I doubt somehow that Gaby had this in mind, since this is
quite usual stuff in Axiom (grep "Rep.*Union")

Martin

)abbrev domain EX Expr
Expr(): with
    eval: % -> Integer
    coerce: Integer -> %
    coerce: % -> OutputForm
    _+: (%,%) -> %
    _*: (%,%) -> %
  == add
    MkInt ==> Integer
    MkAdd ==> Record(lAdd:%,rAdd:%)
    MkMul ==> Record(lMul:%,rMul:%)
-- Rep shouldn't be a macro, I think
    Rep := Union(MkInt, MkAdd, MkMul)

    eval(x:%): Integer ==
-- in SPAD, conversion from Rep to Union(MkInt, ...) is implicit
      x case MkInt => x
-- without package calling + the compiler picks + from %...
      x case MkAdd => (eval(x.lAdd) + eval(x.rAdd))$Integer
      x case MkMul => (eval(x.lMul) * eval(x.rMul))$Integer

    coerce(n: Integer) == n::Rep

    x + y == [x,y]$MkAdd :: Rep

    x * y == [x,y]$MkMul :: Rep

    coerce(x: %): OutputForm ==
      x case MkInt => outputForm x
      x case MkAdd => hconcat [message "(",          _
                               (x.lAdd)::OutputForm, _
                               message "+",          _
                               (x.rAdd)::OutputForm, _
                               message ")"] 
      x case MkMul => hconcat [message "(",          _
                               (x.lMul)::OutputForm, _
                               message "*",          _
                               (x.rMul)::OutputForm, _
                               message ")"]





_______________________________________________
Axiom-mail mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/axiom-mail

Reply via email to