login  home  contents  what's new  discussion  bug reports     help  links  subscribe  changes  refresh  edit

Edit detail for SandBox Object revision 1 of 2

1 2
Editor:
Time: 2007/11/18 18:30:34 GMT-8
Note: Second attempt

changed:
-
Type-safe Objects in SPAD

\begin{spad}
)abbrev domain OBJ Object

Object(C:Type): CoercibleTo(OutputForm) with
        Type            : % -> C
        new             : (SExpression, None) -> %
        dom             : % -> SExpression
        obj             : % -> None

        if C has BasicType then
          BasicType
        if C has Ring then
          _+            : (%,%) -> %

 == add

     Rep == Record(dm: SExpression, ob: None)
     import Rep

     dom(x:%):SExpression     == (x pretend Rep).dm

     obj(x:%):None            == (x pretend Rep).ob

     Type(x:%):C              == dom(x) pretend C

     if C has BasicType then
       _=(x:%,y:%):Boolean      == 
         xR := x pretend Rep
         yR := y pretend Rep
         if xR.dm = yR.dm then
             D:BasicType := xR.dm pretend BasicType
             X:D := (obj x) pretend D
             Y:D := (obj y) pretend D
             (X=Y)$D
         else false

     if C has Ring then
       _+(x:%,y:%):% ==
         xR := x pretend Rep
         yR := y pretend Rep
         if xR.dm = yR.dm then
             D:= xR.dm pretend Ring
             X:D := (obj x) pretend D
             Y:D := (obj y) pretend D
             Z:= (X+Y)$D
             new(xR.dm,Z pretend None)
         else error "not possible"

     coerce(x):OutputForm ==
       xR := x pretend Rep
       obj1 : OutputForm :=
         spad2BootCoerce(xR.ob, xR.dm,
            list("OutputForm"::Symbol)$List(Symbol))$Lisp
       dom1 :=
         p:Symbol := prefix2String(xR.dm)$Lisp
         atom?(p pretend SExpression) => list(p)$List(Symbol)
         list(p)$Symbol
       hconcat cons(obj1, cons(":"::OutputForm, [a::OutputForm for a in dom1]))

     new(domain:SExpression, object:None):% ==
       (isValidType(domain)$Lisp)@Boolean => construct(domain, object)$Rep pretend %
       domains := devaluate(domain)$Lisp
       (isValidType(domains)$Lisp)@Boolean => construct(domains, object)$Rep pretend %
       error "must have a valid domain as first argument"
\end{spad}

\begin{spad}
)abbrev package HAS Has

Has(C:Type,S:C): with
        coerce      : S         -> Object(C)
        avail       : Object(C) -> Union(S, "failed")
        is?         : Object(C) -> Boolean
        get         : Object(C) -> S

    == add

        Sexpr:SExpression := devaluate(S)$Lisp

        is?(a:Object(C)):Boolean  == dom(a) = Sexpr

        coerce(s:S):Object(C) == new(Sexpr, s pretend None)

        avail(a:Object(C)):Union(S,"failed") ==
            is? a => obj(a) pretend S
            "failed"

        get(a:Object(C)):S ==
            is? a => obj(a) pretend S
            error "Cannot retract value."
\end{spad}

\begin{axiom}
)sh Object(SetCategory)
)sh Object(Ring)
)sh Has
)sh Has(SetCategory,Integer)
\end{axiom}

\begin{axiom}
b:Object(BasicType):="abc"
a:=[1,"xxx",1.2]::List Object SetCategory
a.1
a.2
Type(a.2)
(a.1 = a.2)$Object(SetCategory)
is?(a.2)$Has(SetCategory,String)
get(a.2)@String
avail(a.3)$Has(SetCategory,Float)
avail(a.2)$Has(SetCategory,Float)
avail(a.1)$Has(SetCategory,Float) case Float
\end{axiom}

\begin{axiom}
n:Object(Ring)
n:=coerce(1)$Has(Ring,Integer)
m:Object(Ring):=coerce(2)$Has(Ring,Integer)
n+m
\end{axiom}

Type-safe Objects in SPAD

spad
)abbrev domain OBJ Object
Object(C:Type): CoercibleTo(OutputForm) with Type : % -> C new : (SExpression, None) -> % dom : % -> SExpression obj : % -> None
if C has BasicType then BasicType if C has Ring then _+ : (%,%) -> %
== add
Rep == Record(dm: SExpression, ob: None) import Rep
dom(x:%):SExpression == (x pretend Rep).dm
obj(x:%):None == (x pretend Rep).ob
Type(x:%):C == dom(x) pretend C
if C has BasicType then _=(x:%,y:%):Boolean == xR := x pretend Rep yR := y pretend Rep if xR.dm = yR.dm then D:BasicType := xR.dm pretend BasicType X:D := (obj x) pretend D Y:D := (obj y) pretend D (X=Y)$D else false
if C has Ring then _+(x:%,y:%):% == xR := x pretend Rep yR := y pretend Rep if xR.dm = yR.dm then D:= xR.dm pretend Ring X:D := (obj x) pretend D Y:D := (obj y) pretend D Z:= (X+Y)$D new(xR.dm,Z pretend None) else error "not possible"
coerce(x):OutputForm == xR := x pretend Rep obj1 : OutputForm := spad2BootCoerce(xR.ob, xR.dm, list("OutputForm"::Symbol)$List(Symbol))$Lisp dom1 := p:Symbol := prefix2String(xR.dm)$Lisp atom?(p pretend SExpression) => list(p)$List(Symbol) list(p)$Symbol hconcat cons(obj1, cons(":"::OutputForm, [a::OutputForm for a in dom1]))
new(domain:SExpression, object:None):% == (isValidType(domain)$Lisp)@Boolean => construct(domain, object)$Rep pretend % domains := devaluate(domain)$Lisp (isValidType(domains)$Lisp)@Boolean => construct(domains, object)$Rep pretend % error "must have a valid domain as first argument"
spad
   Compiling FriCAS source code from file 
      /var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/2739318749442070762-25px001.spad
      using old system compiler.
   OBJ abbreviates domain Object 
------------------------------------------------------------------------
   initializing NRLIB OBJ for Object 
   compiling into NRLIB OBJ 
************* USER ERROR **********
available signatures for Rep: 
    NONE
NEED Rep: () -> ?
****** comp fails at level 1 with expression: ******
((DEF (|Rep|) (NIL) (NIL)
  (|Record| (|:| |dm| (|SExpression|)) (|:| |ob| (|None|)))))
****** level 1  ******
$x:= (DEF (Rep) (NIL) (NIL) (Record (: dm (SExpression)) (: ob (None))))
$m:= $EmptyMode
$f:=
((((|$Information| #) (|$DomainsInScope| # # #) (|coerce| #) (~= #) ...)))
>> Apparent user error: unspecified error

spad
)abbrev package HAS Has
Has(C:Type,S:C): with coerce : S -> Object(C) avail : Object(C) -> Union(S, "failed") is? : Object(C) -> Boolean get : Object(C) -> S
== add
Sexpr:SExpression := devaluate(S)$Lisp
is?(a:Object(C)):Boolean == dom(a) = Sexpr
coerce(s:S):Object(C) == new(Sexpr, s pretend None)
avail(a:Object(C)):Union(S,"failed") == is? a => obj(a) pretend S "failed"
get(a:Object(C)):S == is? a => obj(a) pretend S error "Cannot retract value."
spad
   Compiling FriCAS source code from file 
      /var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/6566032851093813704-25px002.spad
      using old system compiler.
   HAS abbreviates package Has 
------------------------------------------------------------------------
   initializing NRLIB HAS for Has 
   compiling into NRLIB HAS 
****** Domain: C already in scope
   compiling exported is? : Object C -> Boolean
   Semantic Errors: 
      [1]  Object is not a known type
Warnings: [1] is?: dom has no value
****** comp fails at level 2 with expression: ****** error in function is?
(= | << | (|dom| |a|) | >> | |Sexpr|) ****** level 2 ****** $x:= (dom a) $m:= $EmptyMode $f:= ((((|a| # #) (|Sexpr| # #) (|#| #) (= #) ...)))
>> Apparent user error: cannot compile (dom a)

fricas
)sh Object(SetCategory)
Object is an unknown constructor and so is unavailable. Did you mean to use -> but type something different instead?
fricas
)sh Object(Ring)
Object is an unknown constructor and so is unavailable. Did you mean to use -> but type something different instead?
fricas
)sh Has
Has is a package constructor Abbreviation for Has is HAS This constructor is not exposed in this frame. ------------------------------- Operations --------------------------------
>> System error: The function BOOT::|Has| is undefined.

fricas
b:Object(BasicType):="abc"
Object is an unknown constructor and so is unavailable. Did you mean to use -> but type something different instead? a:=[1,"xxx",1.2]::List Object SetCategory
Object is an unknown constructor and so is unavailable. Did you mean to use -> but type something different instead? a.1
There are no library operations named a Use HyperDoc Browse or issue )what op a to learn if there is any operation containing " a " in its name.
Cannot find a definition or applicable library operation named a with argument type(s) PositiveInteger
Perhaps you should use "@" to indicate the required return type, or "$" to specify which version of the function you need. a.2
There are no library operations named a Use HyperDoc Browse or issue )what op a to learn if there is any operation containing " a " in its name.
Cannot find a definition or applicable library operation named a with argument type(s) PositiveInteger
Perhaps you should use "@" to indicate the required return type, or "$" to specify which version of the function you need. Type(a.2)
There are no library operations named a Use HyperDoc Browse or issue )what op a to learn if there is any operation containing " a " in its name.
Cannot find a definition or applicable library operation named a with argument type(s) PositiveInteger
Perhaps you should use "@" to indicate the required return type, or "$" to specify which version of the function you need. (a.1 = a.2)$Object(SetCategory)
Object is an unknown constructor and so is unavailable. Did you mean to use -> but type something different instead? is?(a.2)$Has(SetCategory,String)
Has is an unknown constructor and so is unavailable. Did you mean to use -> but type something different instead? get(a.2)@String
There are no library operations named a Use HyperDoc Browse or issue )what op a to learn if there is any operation containing " a " in its name.
Cannot find a definition or applicable library operation named a with argument type(s) PositiveInteger
Perhaps you should use "@" to indicate the required return type, or "$" to specify which version of the function you need. avail(a.3)$Has(SetCategory,Float)
Has is an unknown constructor and so is unavailable. Did you mean to use -> but type something different instead? avail(a.2)$Has(SetCategory,Float)
Has is an unknown constructor and so is unavailable. Did you mean to use -> but type something different instead? avail(a.1)$Has(SetCategory,Float) case Float
Has is an unknown constructor and so is unavailable. Did you mean to use -> but type something different instead?

fricas
n:Object(Ring)
Object is an unknown constructor and so is unavailable. Did you mean to use -> but type something different instead? n:=coerce(1)$Has(Ring,Integer)
Has is an unknown constructor and so is unavailable. Did you mean to use -> but type something different instead? m:Object(Ring):=coerce(2)$Has(Ring,Integer)
Object is an unknown constructor and so is unavailable. Did you mean to use -> but type something different instead? n+m

\label{eq1}n + m(1)
Type: Polynomial(Integer)