)abbrev domain OMENC OpenMathEncoding
++ Author: Vilya Harvey
++ Basic Functions:
++ Related Constructors:
++ Also See:
++ AMS Classifications:
++ Keywords:
++ References:
++ Description:
++ \spadtype{OpenMathEncoding} is the set of valid OpenMath encodings.
OpenMathEncoding() : SetCategory with
  OMencodingUnknown : () -> %
  ++ OMencodingUnknown() is the constant for unknown encoding types. If this
  ++ is used on an input device, the encoding will be autodetected.
  ++ It is invalid to use it on an output device.
  OMencodingXML     : () -> %
  ++ OMencodingXML() is the constant for the OpenMath XML encoding.
  OMencodingSGML    : () -> %
  ++ OMencodingSGML() is the constant for the deprecated OpenMath SGML encoding.
  OMencodingBinary  : () -> %
  ++ OMencodingBinary() is the constant for the OpenMath binary encoding.
 == add
  Rep := SingleInteger

  =(u, v) == (u = v)$Rep

  import from Rep

  coerce(u) ==
    u::Rep = 0$Rep => message("Unknown")
    u::Rep = 1$Rep => message("Binary")
    u::Rep = 2::Rep => message("XML")
    u::Rep = 3::Rep => message("SGML")
    error "Bogus OpenMath Encoding Type"

  OMencodingUnknown() : % == 0::Rep
  OMencodingBinary() : % == 1::Rep
  OMencodingXML() : % == 2::Rep
  OMencodingSGML() : % == 3::Rep

)abbrev domain OMDEV OpenMathDevice
++ Author: Vilya Harvey
++ Basic Functions:
++ Related Constructors:
++ Also See:
++ AMS Classifications:
++ Keywords:
++ References:
++ Description: \spadtype{OpenMathDevice} provides support for reading
++ and writing openMath objects to files, strings etc.  It also provides
++ access to low-level operations from within the interpreter.


OpenMathDevice() : with
  OMopenFile    : (String, String, OpenMathEncoding) -> %
  ++ OMopenFile(f, mode, enc) opens file \spad{f} for reading or writing
  ++ OpenMath objects (depending on \spad{mode} which can be "r", "w"
  ++ or "a" for read, write and append respectively), in the encoding
  ++ \spad{enc}.
  OMopenString  : (String, OpenMathEncoding) -> %
  ++ OMopenString(s, mode) opens the string \spad{s} for reading or writing
  ++ OpenMath objects in encoding \spad{enc}.
  OMclose       : % -> Void
  ++ OMclose(dev) closes \spad{dev}, flushing output if necessary.
  OMsetEncoding : (%, OpenMathEncoding) -> Void
  ++ OMsetEncoding(dev, enc) sets the encoding used for reading or writing
  ++ OpenMath objects to or from \spad{dev} to \spad{enc}.
  OMputApp      : % -> Void
  ++ OMputApp(dev) writes a begin application token to \spad{dev}.
  OMputAtp      : % -> Void
  ++ OMputAtp(dev) writes a begin attribute pair token to \spad{dev}.
  OMputAttr     : % -> Void
  ++ OMputAttr(dev) writes a begin attribute token to \spad{dev}.
  OMputBind     : % -> Void
  ++ OMputBind(dev) writes a begin binder token to \spad{dev}.
  OMputBVar     : % -> Void
  ++ OMputBVar(dev) writes a begin bound variable list token to \spad{dev}.
  OMputError    : % -> Void
  ++ OMputError(dev) writes a begin error token to \spad{dev}.
  OMputObject   : % -> Void
  ++ OMputObject(dev) writes a begin object token to \spad{dev}.
  OMputEndApp   : % -> Void
  ++ OMputEndApp(dev) writes an end application token to \spad{dev}.
  OMputEndAtp   : % -> Void
  ++ OMputEndAtp(dev) writes an end attribute pair token to \spad{dev}.
  OMputEndAttr  : % -> Void
  ++ OMputEndAttr(dev) writes an end attribute token to \spad{dev}.
  OMputEndBind  : % -> Void
  ++ OMputEndBind(dev) writes an end binder token to \spad{dev}.
  OMputEndBVar  : % -> Void
  ++ OMputEndBVar(dev) writes an end bound variable list token to \spad{dev}.
  OMputEndError : % -> Void
  ++ OMputEndError(dev) writes an end error token to \spad{dev}.
  OMputEndObject : % -> Void
  ++ OMputEndObject(dev) writes an end object token to \spad{dev}.
  OMputInteger  : (%, Integer) -> Void
  ++ OMputInteger(dev, i) writes the integer \spad{i} to \spad{dev}.
  OMputFloat    : (%, DoubleFloat) -> Void
  ++ OMputFloat(dev, i) writes the float \spad{i} to \spad{dev}.
  OMputVariable : (%, Symbol) -> Void
  ++ OMputVariable(dev, i) writes the variable \spad{i} to \spad{dev}.
  OMputString   : (%, String) -> Void
  ++ OMputString(dev, i) writes the string \spad{i} to \spad{dev}.
  OMputSymbol   : (%, String, String) -> Void
  ++ OMputSymbol(dev, cd, s) writes the symbol \spad{s} from CD \spad{cd}
  ++ to \spad{dev}.

  OMgetApp      : % -> Void
  ++ OMgetApp(dev) reads a begin application token from \spad{dev}.
  OMgetAtp      : % -> Void
  ++ OMgetAtp(dev) reads a begin attribute pair token from \spad{dev}.
  OMgetAttr     : % -> Void
  ++ OMgetAttr(dev) reads a begin attribute token from \spad{dev}.
  OMgetBind     : % -> Void
  ++ OMgetBind(dev) reads a begin binder token from \spad{dev}.
  OMgetBVar     : % -> Void
  ++ OMgetBVar(dev) reads a begin bound variable list token from \spad{dev}.
  OMgetError    : % -> Void
  ++ OMgetError(dev) reads a begin error token from \spad{dev}.
  OMgetObject   : % -> Void
  ++ OMgetObject(dev) reads a begin object token from \spad{dev}.
  OMgetEndApp   : % -> Void
  ++ OMgetEndApp(dev) reads an end application token from \spad{dev}.
  OMgetEndAtp   : % -> Void
  ++ OMgetEndAtp(dev) reads an end attribute pair token from \spad{dev}.
  OMgetEndAttr  : % -> Void
  ++ OMgetEndAttr(dev) reads an end attribute token from \spad{dev}.
  OMgetEndBind  : % -> Void
  ++ OMgetEndBind(dev) reads an end binder token from \spad{dev}.
  OMgetEndBVar  : % -> Void
  ++ OMgetEndBVar(dev) reads an end bound variable list token from \spad{dev}.
  OMgetEndError : % -> Void
  ++ OMgetEndError(dev) reads an end error token from \spad{dev}.
  OMgetEndObject : % -> Void
  ++ OMgetEndObject(dev) reads an end object token from \spad{dev}.
  OMgetInteger  : % -> Integer
  ++ OMgetInteger(dev) reads an integer from \spad{dev}.
  OMgetFloat    : % -> DoubleFloat
  ++ OMgetFloat(dev) reads a float from \spad{dev}.
  OMgetVariable : % -> Symbol
  ++ OMgetVariable(dev) reads a variable from \spad{dev}.
  OMgetString   : % -> String
  ++ OMgetString(dev) reads a string from \spad{dev}.
  OMgetSymbol   : % -> Record(cd : String, name : String)
  ++ OMgetSymbol(dev) reads a symbol from \spad{dev}.

  OMgetType     : % -> Symbol
  ++ OMgetType(dev) returns the type of the next object on \spad{dev}.
 == add
  OMopenFile(fname : String, fmode : String, enc : OpenMathEncoding) : % ==
    OM_-OPENFILEDEV(fname, fmode, enc)$Lisp
  OMopenString(str : String, enc : OpenMathEncoding) : % ==
    OM_-OPENSTRINGDEV(str, enc)$Lisp
  OMclose(dev : %) : Void ==
    OM_-CLOSEDEV(dev)$Lisp
  OMsetEncoding(dev : %, enc : OpenMathEncoding) : Void ==
    OM_-SETDEVENCODING(dev, enc)$Lisp

  OMputApp(dev : %) : Void == OM_-PUTAPP(dev)$Lisp
  OMputAtp(dev : %) : Void == OM_-PUTATP(dev)$Lisp
  OMputAttr(dev : %) : Void == OM_-PUTATTR(dev)$Lisp
  OMputBind(dev : %) : Void == OM_-PUTBIND(dev)$Lisp
  OMputBVar(dev : %) : Void == OM_-PUTBVAR(dev)$Lisp
  OMputError(dev : %) : Void == OM_-PUTERROR(dev)$Lisp
  OMputObject(dev : %) : Void == OM_-PUTOBJECT(dev)$Lisp
  OMputEndApp(dev : %) : Void == OM_-PUTENDAPP(dev)$Lisp
  OMputEndAtp(dev : %) : Void == OM_-PUTENDATP(dev)$Lisp
  OMputEndAttr(dev : %) : Void == OM_-PUTENDATTR(dev)$Lisp
  OMputEndBind(dev : %) : Void == OM_-PUTENDBIND(dev)$Lisp
  OMputEndBVar(dev : %) : Void == OM_-PUTENDBVAR(dev)$Lisp
  OMputEndError(dev : %) : Void == OM_-PUTENDERROR(dev)$Lisp
  OMputEndObject(dev : %) : Void == OM_-PUTENDOBJECT(dev)$Lisp
  OMputInteger(dev : %, i : Integer) : Void == OM_-PUTINT(dev, i)$Lisp
  OMputFloat(dev : %, f : DoubleFloat) : Void == OM_-PUTFLOAT(dev, f)$Lisp
  --OMputByteArray(dev: %, b: Array Byte): Void == OM_-PUTBYTEARRAY(dev, b)$Lisp
  OMputVariable(dev : %, v : Symbol) : Void == OM_-PUTVAR(dev, v)$Lisp
  OMputString(dev : %, s : String) : Void == OM_-PUTSTRING(dev, s)$Lisp
  OMputSymbol(dev : %, cd : String, nm : String) : Void == OM_-PUTSYMBOL(dev, cd, nm)$Lisp

  OMgetApp(dev : %) : Void == OM_-GETAPP(dev)$Lisp
  OMgetAtp(dev : %) : Void == OM_-GETATP(dev)$Lisp
  OMgetAttr(dev : %) : Void == OM_-GETATTR(dev)$Lisp
  OMgetBind(dev : %) : Void == OM_-GETBIND(dev)$Lisp
  OMgetBVar(dev : %) : Void == OM_-GETBVAR(dev)$Lisp
  OMgetError(dev : %) : Void == OM_-GETERROR(dev)$Lisp
  OMgetObject(dev : %) : Void == OM_-GETOBJECT(dev)$Lisp
  OMgetEndApp(dev : %) : Void == OM_-GETENDAPP(dev)$Lisp
  OMgetEndAtp(dev : %) : Void == OM_-GETENDATP(dev)$Lisp
  OMgetEndAttr(dev : %) : Void == OM_-GETENDATTR(dev)$Lisp
  OMgetEndBind(dev : %) : Void == OM_-GETENDBIND(dev)$Lisp
  OMgetEndBVar(dev : %) : Void == OM_-GETENDBVAR(dev)$Lisp
  OMgetEndError(dev : %) : Void == OM_-GETENDERROR(dev)$Lisp
  OMgetEndObject(dev : %) : Void == OM_-GETENDOBJECT(dev)$Lisp
  OMgetInteger(dev : %) : Integer == OM_-GETINT(dev)$Lisp
  OMgetFloat(dev : %) : DoubleFloat == OM_-GETFLOAT(dev)$Lisp
  --OMgetByteArray(dev: %): Array Byte == OM_-GETBYTEARRAY(dev)$Lisp
  OMgetVariable(dev : %) : Symbol == OM_-GETVAR(dev)$Lisp
  OMgetString(dev : %) : String == OM_-GETSTRING(dev)$Lisp
  OMgetSymbol(dev : %) : Record(cd : String, name : String) == OM_-GETSYMBOL(dev)$Lisp

  OMgetType(dev : %) : Symbol == OM_-GETTYPE(dev)$Lisp

)abbrev domain OMCONN OpenMathConnection
++ Author: Vilya Harvey
++ Basic Functions:
++ Related Constructors:
++ Also See:
++ AMS Classifications:
++ Keywords:
++ References:
++ Description: \spadtype{OpenMathConnection} provides low-level functions
++ for handling connections to and from \spadtype{OpenMathDevice}s.


OpenMathConnection() : with
  OMmakeConn    : SingleInteger -> % ++ \spad{OMmakeConn}
  OMcloseConn   : % -> Void ++ \spad{OMcloseConn}
  OMconnInDevice : %-> OpenMathDevice ++ \spad{OMconnInDevice : }
  OMconnOutDevice : %-> OpenMathDevice ++ \spad{OMconnOutDevice : }
  OMconnectTCP  : (%, String, SingleInteger) -> Boolean ++ \spad{OMconnectTCP}
  OMbindTCP     : (%, SingleInteger) -> Boolean ++ \spad{OMbindTCP}
 == add
  OMmakeConn(timeout : SingleInteger) : % == OM_-MAKECONN(timeout)$Lisp
  OMcloseConn(conn : %) : Void == OM_-CLOSECONN(conn)$Lisp

  OMconnInDevice(conn : %) : OpenMathDevice ==
    OM_-GETCONNINDEV(conn)$Lisp
  OMconnOutDevice(conn : %) : OpenMathDevice ==
    OM_-GETCONNOUTDEV(conn)$Lisp

  OMconnectTCP(conn : %, host : String, port : SingleInteger) : Boolean ==
    OM_-CONNECTTCP(conn, host, port)$Lisp
  OMbindTCP(conn : %, port : SingleInteger) : Boolean ==
    OM_-BINDTCP(conn, port)$Lisp

)abbrev package OMPKG OpenMathPackage
++ Author: Vilya Harvey
++ Basic Functions:
++ Related Constructors:
++ Also See:
++ AMS Classifications:
++ Keywords:
++ References:
++ Description: \spadtype{OpenMathPackage} provides some simple utilities
++ to make reading OpenMath objects easier.

OpenMathPackage() : with
  OMread            : OpenMathDevice -> Any
  ++ OMread(dev) reads an OpenMath object from \spad{dev} and passes it
  ++ to FriCAS.
  OMreadFile        : String -> Any
  ++ OMreadFile(f) reads an OpenMath object from \spad{f} and passes it
  ++ to FriCAS.
  OMreadStr         : String -> Any
  ++ OMreadStr(f) reads an OpenMath object from \spad{f} and passes it
  ++ to FriCAS.
  OMlistCDs         : () -> List(String)
  ++ OMlistCDs() lists all the CDs supported by FriCAS.
  OMlistSymbols     : String -> List(String)
  ++ OMlistSymbols(cd) lists all the symbols in \spad{cd}.
  OMsupportsCD?      : String -> Boolean
  ++ OMsupportsCD?(cd) returns true if FriCAS supports \spad{cd}, false
  ++ otherwise.
  OMsupportsSymbol? : (String, String) -> Boolean
  ++ OMsupportsSymbol?(s, cd) returns true if FriCAS supports symbol \spad{s}
  ++ from CD \spad{cd}, false otherwise.
  OMunhandledSymbol : (String, String) -> Exit
  ++ OMunhandledSymbol(s, cd) raises an error if FriCAS reads a symbol which it
  ++ is unable to handle.  Note that this is different from an unexpected
  ++ symbol.
 == add
  import from OpenMathEncoding
  import from OpenMathDevice
  import from String

  OMunhandledSymbol(u, v) ==
    error concat ["FriCAS is unable to process the symbol ",u," from CD ",v,"."]

  OMread(dev : OpenMathDevice) : Any ==
    interpret(OM_-READ(dev)$Lisp :: InputForm)

  OMreadFile(filename : String) : Any ==
    dev := OMopenFile(filename, "r", OMencodingUnknown())
    res : Any := interpret(OM_-READ(dev)$Lisp :: InputForm)
    OMclose(dev)
    res

  OMreadStr(str : String) : Any ==
    strp : None := OM_-STRINGTOSTRINGPTR(str)$Lisp
    dev := OMopenString(strp pretend String, OMencodingUnknown())
    res : Any := interpret(OM_-READ(dev)$Lisp :: InputForm)
    OMclose(dev)
    res

  OMlistCDs() : List(String) ==
    OM_-LISTCDS()$Lisp pretend List(String)

  OMlistSymbols(cd : String) : List(String) ==
    OM_-LISTSYMBOLS(cd)$Lisp pretend List(String)

  import from SExpression

  OMsupportsCD?(cd : String) : Boolean ==
    not null? OM_-SUPPORTSCD(cd)$Lisp

  OMsupportsSymbol?(cd : String, name : String) : Boolean ==
    not null? OM_-SUPPORTSSYMBOL(cd, name)$Lisp

--Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
--All rights reserved.
--
--Redistribution and use in source and binary forms, with or without
--modification, are permitted provided that the following conditions are
--met:
--
--    - Redistributions of source code must retain the above copyright
--      notice, this list of conditions and the following disclaimer.
--
--    - Redistributions in binary form must reproduce the above copyright
--      notice, this list of conditions and the following disclaimer in
--      the documentation and/or other materials provided with the
--      distribution.
--
--    - Neither the name of The Numerical ALgorithms Group Ltd. nor the
--      names of its contributors may be used to endorse or promote products
--      derived from this software without specific prior written permission.
--
--THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
--IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
--TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
--PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
--OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
--EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
--PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
--PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
--LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
--NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
--SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
