axiom-mail
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Axiom-mail] Spad and inductive types


From: Martin Rubey
Subject: Re: [Axiom-mail] Spad and inductive types
Date: 07 May 2007 10:14:19 +0200
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.4

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 ")"]







reply via email to

[Prev in Thread] Current Thread [Next in Thread]