oldk1331 wrote:
>
> In Stack if bags.spad:
>     Rep := Reference List S
>     parts s == deref s
> 
> That's shared structure, not copy, thus
> 
> (11) -> x := stack [1,2,3,4]
> 
>    (11)  [1,2,3,4]
>                                                  Type: 
> Stack(PositiveInteger)
> (12) -> y := parts x
> 
>    (12)  [1,2,3,4]
>                                                   Type: 
> List(PositiveInteger)
> (13) -> y.1 := 5
> 
>    (13)  5
>                                                         Type: 
> PositiveInteger
> (14) -> x
> 
>    (14)  [5,2,3,4]
>                                                  Type: 
> Stack(PositiveInteger)
> 
> 
> I think this is wrong.  We should mention that parts do not share
> structure with its argument in the doc (in aggcat.spad).

Well, there are to concepts: ownership and destructive operations.
Basic rule is that you should not pass object that you do not
own to a destructive operation.  You apparently want to own
results of functions, but this is not the case.  There is
a handful of functions which always transfer ownership to
you, notably 'copy' and 'new'.  In FriCAS default is that
data may be shared, so you can not safely pass it to
destructive operations.

Some functions store references to data that you pass to
them.  Once a function stored reference to an object
it is shared -- you no longer own it.

Coming back to 'parts': you do not own the result so
you can not modify it safely.  Consider the following:

- one can relatively easy add extra copy, but it is
  impossible to eliminate copy done by library function
  (except by not using function which make the copy)
- it is very unusual to apply destructive operations
  to the result of 'parts'
- destructive operations are done to improve efficiency,
  excessive copying in many case would negate speed
  gain so FriCAS puts here burden on the user: normal
  routines are nondestructive and hence safe but
  destructive operations require that users obey
  appropriate protocol.

Note that 'construct' is allowed to store references to
is argument as long as normal operations do not modify
refernced object.  So, for example 'construct' in List
simply returns back its argument.  That is OK because
you do not own result of 'construct' so legally you
can only pass it to normal (non-destructive) operations.
Note: if you owned argument to 'construct' and after
storing result of 'construct' you discarded all
references to the argument, then you performed transfer
of ownership and now you own result of 'construct'.

-- 
                              Waldek Hebisch

-- 
You received this message because you are subscribed to the Google Groups 
"FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/fricas-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to