Re: Spad and inductive types

Martin Rubey <[email protected]>
Newsgroups gmane.comp.mathematics.axiom.user
Message-ID <[email protected]>
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 ")"]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.