/*****

Minimal OpenMath Phrasebook ( http://www.openmath.org/ ,
http://openmath.activemath.org/ ),
for Pari/GP ( http://pari.math.u-bordeaux.fr/ ),
by Marco Costantini ( http://www-math.science.unitn.it/~costanti/ ),

This file provides the conversion of Pari/GP objects to OpenMath
objects. 

The web-page for this file is http://www-math.science.unitn.it/~costanti/
for contacts write to costanti@science.unitn.it . 
This code is released under the GNU General Public License 
( http://www.gnu.org/copyleft/gpl.html ).
Copyright 2006, Marco Costantini.


Version of 
27 January 2006


USER'S MANUAL:

Either start GP normally and do
  \r parigp_openmath
or start GP as
  GP parigp_openmath

The main function is:

  om_convert( obj )

it returns a string with the OpenMath XML encoding of the Pari/GP object
<obj>. For some data types, the pseudo OpenMath Content Dictionary
"parigp" is used.


*****/



{
om_t_INT (obj) = 
local (s);
s = "<OMI>";
s = concat(s, Str(obj));
s = concat(s, "</OMI>");
return(s);
}



{
om_t_REAL (obj) =
local (s);
s = "<OMF dec=\"";
s = concat(s, Str(obj));
s = concat(s, "\" />");
return(s);
}



{
om_t_STR (obj) =
local (s);
s = "<OMSTR>";
s = concat(s, obj);
s = concat(s, "</OMSTR>");
return(s);
}



{
om_t_MAT ( obj ) =
local (s);
s = "<OMA><OMS cd=\"linalg2\" name=\"matrix\" />";
for(i=1, length(obj~),
  s = concat(s, "<OMA><OMS cd=\"linalg2\" name=\"matrixrow\" />");
  for(j=1, length(obj),
    s = concat(s, om_func(obj[i, j]))
  );
  s = concat(s, "</OMA>");
);
s = concat(s, "</OMA>");
return(s);
}



\\ t_VECSMALL doesn't allow 'component'

{
om_t_VECSMALL ( obj ) =
local (s);
s = "<OMA><OMS cd=\"parigp\" name=\"t_VECSMALL\" />";
for(i=1, length(obj),
  s = concat(s, om_func(obj[i]))
);
s = concat(s ,"</OMA>");
return(s);
}



{
om_t_generic ( obj, cd, name ) =
local (s);
s = "<OMA><OMS cd=\"";
s = concat(s, cd);
s = concat(s, "\" name=\"");
s = concat(s, name);
s = concat(s, "\" />");

if ( type(obj) == "t_POL" || type(obj) == "t_POLMOD" || type(obj) == "t_SER"
 || type(obj) == "t_RFRAC" || type(obj) == "t_RFRACN" , 
  s = concat(s, "<OMV name=\"" ); 
  s = concat(s,  variable(obj) );
  s = concat(s, "\"/>" );
);

for(i=1, length(obj),
  s = concat(s, om_func(component(obj, i)))
);
s = concat(s, "</OMA>");
return(s);
}



{
om_func ( obj ) =
local(cd, name);

\\ for types not implemented, a pseudo CD "parigp" is used
cd = "parigp";
name = type(obj);

if ( type(obj) == "t_INT", return(om_t_INT (obj) ) );
if ( type(obj) == "t_REAL", return(om_t_REAL (obj) ) );
if  (type(obj) == "t_STR", return(om_t_STR (obj) ) );

if ( type(obj) == "t_MAT", return(om_t_MAT (obj) ) );
if ( type(obj) == "t_VECSMALL", return(om_t_VECSMALL (obj) ) );

if ( type(obj) == "t_FRAC", cd ="nums1"; name="rational"; );
if ( type(obj) == "t_FRACN", cd ="nums1"; name="rational"; );
if ( type(obj) == "t_COMPLEX", cd ="complex1"; name="complex_cartesian"; );
if ( type(obj) == "t_VEC", cd ="linalg2"; name="vector"; );
if ( type(obj) == "t_COL", cd ="linalg3"; name="vector"; );
if ( type(obj) == "t_LIST", cd ="list1"; name="list"; );


\\ template for adding other types
\\if ( type(obj) == "", cd =""; name=""; );

return( om_t_generic(obj, cd, name) )

}





{
om_convert ( obj ) = 
local (s);
s = "";
s = concat(s, "<OMOBJ>");
s = concat(s, om_func(obj));
s = concat(s, "</OMOBJ>");
return(s);
}



addhelp(om_convert, \
"om_convert( obj ): it returns a string with the OpenMath XML encoding of the Pari/GP object" )



{
om_convert_print ( obj ) =
print( om_convert ( obj ) );
return()
}
