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/2710714920993231977-25px001.spad
using old system compiler.
OBJ abbreviates domain Object
------------------------------------------------------------------------
initializing NRLIB OBJ for Object
compiling into NRLIB OBJ
importing Rep
compiling exported dom : $ -> SExpression
OBJ;dom;$Se;1 is replaced by QCAR
Time: 0 SEC.
compiling exported obj : $ -> None
OBJ;obj;$N;2 is replaced by QCDR
Time: 0 SEC.
compiling exported Type : $ -> C
Time: 0 SEC.
****** Domain: C already in scope
augmenting C: (BasicType)
compiling exported = : ($,$) -> Boolean
Time: 0 SEC.
****** Domain: C already in scope
augmenting C: (Ring)
compiling exported + : ($,$) -> $
Time: 0.02 SEC.
compiling exported coerce : $ -> OutputForm
Semantic Errors:
[1] =: pretend is not a known type
[2] +: pretend is not a known type
Warnings:
[1] dom: pretendRep -- should replace by @
[2] obj: pretendRep -- should replace by @
[3] =: pretendRep -- should replace by @
[4] =: dm has no value
[5] +: pretendRep -- should replace by @
[6] +: dm has no value
[7] coerce: pretendRep -- should replace by @
[8] coerce: ob has no value
[9] coerce: dm has no value
****** comp fails at level 6 with expression: ******
error in function coerce
(SEQ (|:=| |xR| (|pretend| |x| |Rep|))
(|:=| (|:| |obj1| (|OutputForm|))
((|Sel| |Lisp| |spad2BootCoerce|) (|xR| |ob|) (|xR| |dm|)
((|Sel| (|List| (|Symbol|)) |list|) (|::| "OutputForm" (|Symbol|)))))
(|:=| |dom1|
(SEQ
(|:=| (|:| |p| (|Symbol|)) ((|Sel| |Lisp| |prefix2String|) (|xR| |dm|)))
(|:=| (|:| #1=#:G672 (|Boolean|))
(|atom?| (|pretend| |p| (|SExpression|))))
(|exit| 1
(IF #1#
((|Sel| (|List| (|Symbol|)) |list|) |p|)
| << |
((|Sel| (|Symbol|) |list|) |p|)
| >> |))))
(|exit| 1
(|hconcat|
(|cons| |obj1|
(|cons| (|::| ":" (|OutputForm|))
(COLLECT (IN |a| |dom1|) (|::| |a| (|OutputForm|))))))))
****** level 6 ******
$x:= ((Sel (Symbol) list) p)
$m:= (List (Symbol))
$f:=
((((#:G672 # #) (|p| # #) (|obj1| # #) (|xR| #) ...)))
>> Apparent user error:
NoValueMode
is an unknown mode
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
b:Object(BasicType):="abc"
Object 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?