On Mon, Mar 12, 2012 at 08:47:04PM +0100, Waldek Hebisch wrote:
> Serge D. Mechveliani wrote:
> > 
> > People,
> > I need to build  (in Spad)  an element  x : (D : EuclideanDomain)
> > into  SExpression.
> > I do not mind to set it there, for example, via  OutputForm.
> > So, I try
> > 
> > -----------------------------------------
> > )abbrev package FOO Foo
> > Foo(D : EuclideanDomain) : with
> >                            f : D -> SExpression
> >  ==
> >    add
> >      f x ==
> >           xOF  := coerce(x)       :: OutputForm
> >           xI   := convert(xOF)    $InputForm
> >           xStr := unparse(xI)     :: String
> >           convert(xStr :: Symbol) $SExpression
> > -----------------------------------------
[..]

> > 1. Is there possible a simpler code?
> 
> Why:
> 
>       f x == convert(empty()$List(SExpression))@SExpression
> 
> does not do?  In other words, what properties the mapping you
> want should have.
> 
> BTW: I am pretty sure that the mapping I gave (which always
> gives empty list) is useless for you.  But the mapping you
> build looks almost equally useless, so I have no clue what
> you really want.  Note that your function produces a single
> string, so it is not clear why you want SExpression and
> not Sting as a result type.  Also, the String you get
> in general is not enough to reconstruct the 'x' you
> started with, so I do not know what uses beyond debugging
> you have in mind.
> 
> BTW2:
> 
>      f x == (coerce(x)::OutputForm) pretend SExpression
> 
> may be better than what you have (but still is lossy).


All right. I need to specify the question. I have

-----------------------------------------------------------------
OF ==> OutputForm
)abbrev domain CONSDOM ConstructedDomain

++ It defines a  domain  given by its type and also by its construction,
++ with each level presented by a  type  and by its  constructor.
++ Example:
++   UP(x, Fraction INT)  is expressed as  [UP(x, F), ("UP" extD2)],
++         where
++         extD1 : ConstructedDomain := [Integer, ZSymbol]
++         F : Type                  := Fraction Integer
++         extD2 : ConstructedDomain := [F, ("Fr" extD1)]

ConstructedDomain() : Export == Implementation  where

  UPCons     ==> Record(upCons_var : Symbol,    upCons_coef    : %)
  ResEucCons ==> Record(resEucCons_argDom : %,  resEucCons_gen : Any)

  Export == Type  with
            coerce          : String         -> SExpression
            coerce          : %              -> OF
            coerce          : %              -> SExpression
            baseDomCons     : (Type, Symbol) -> %
            frDomCons       : %              -> %
            upDomCons       : (Symbol, %)    -> %
            resEucDomCons   : (%, Any)       -> %
            ...
  Implementation == add

    Rep := Record(consD_dom : Type, consD_cons : ProperCons)

    ProperCons := Union(cons_basic : Symbol,   cons_fr     : %,
                         cons_up   : UPCons,   cons_resEuc : ResEucCons)

    coerce(s: String) : SExpression == convert(s ::Symbol) $SExpression

    baseDomCons(T : Type, s : Symbol) : % ==  [T, [s]]
    frDomCons(cd : %) : % ==
                           T := cd.consD_dom pretend IntegralDomain
                           [Fraction T, [cd]]
...
----------------------------------------------------------
  
I am going to do almost all the needed in  parsing a call
via this ConstructedDomain.

But for auxiliary          testing and printing,  
it is desirable to convert  
             ConstructedDomain <--> SymbDomCons
                                    ==> SymbolicConstructionOfDomain.

And performance is not important for this conversion. 
I do not want to repeat this messy stuff with defining a new domain
for the symbolic construction. Instead, I try  SExpression. 
For example,
    UP(x, Fraction INT)  presented as  ConstructedDomain 
    <--> 
    (upC x (Fr Z))  :: SExpression. 

Here  upC, x, Fr, Z  are symbols converted to SExpression. 
For this, I need an injective map
                    coerce : ConstructedDomain -> SExpression. 

By this map, the constructed domains may even occur comparable by `='. 
And the inverse (partial) map, I think, is easy to program
(the performance is not essential in this part).
And the result of this `coerce' must be printable to  OutputForm (OF)
easily, and this OF must be naturally readable by a human.
This `coerce' tends to be something close to printing to OF.

Now, for a  Residue Ring of an EuclideanDomain,  

the construction includes an ideal generator   g: (D: EuclideanDomain)
(I use  coerce-retract-pretend for  Any <-> D  
and Type <--> EuclideanDomain).

And this  g  needs to build into the whole  SExpression.
For this, we need an injective map  D -> SExpression, 
and I am lazy to write much code.
I thought:  at run time,  D  will have   :: OF  for its elements,  
all such data are printable in my program.
So,  g :: OF :: SExpression   will do, only it needs a correct 
expression.
May be,       (coerce(g)::OutputForm) pretend SExpression

suggested by Waldek will do (my variant was in 2 lines). I shall try.

And the conversion from  ConstructedDomain  is now as follows:

  coerce(cd : %) : SExpression ==         
                             -- it ignores the type part at each level
      c := cd.consD_cons
      c case cons_basic => convert(c.cons_basic) $SExpression   -- INT

      c case cons_up =>                                    -- UP(x, D)
                          upc  := c.cons_up
                          v    := convert(upc.upCons_var) $SExpression
                          cfDC := upc.upCons_coef
                          up   := "UP" :: SExpression
                          convert([up, coerce cfDC]) $SExpression

      c case cons_resEuc =>              -- Residue ring for Euclidean
                rsc   := c.cons_resEuc
                argDC := rsc.resEucCons_argDom
                argD  := argDC.consD_dom :: EuclideanDomain
                g     := retract(rsc.resEucCons_gen) $ANY1(argD)
                gS    := (coerce(g)::OutputForm) pretend SExpression
                Rs    := "ResEuc" :: SExpression
                convert([Rs, coerce argDC, gS]) $SExpression

So far, everything is compiled. But this needs testing by running 
exmples.

Thanks,

------
Sergei
[email protected]
 

-- 
You received this message because you are subscribed to the Google Groups 
"FriCAS - computer algebra system" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/fricas-devel?hl=en.

Reply via email to