[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Cannot understand coercion

chong.franklin

2/2/2016 2:13:00 PM

This is a quote from SICP about coercion. This section talks about the arithmetic package with ordinary numbers, rational numbers and complex numbers and doing cross type operations on them (E.G. adding a complex number to an ordinary number.)

"This coercion scheme has many advantages over the method of defining explicit cross-type operations, as outlined above. Although we still need to write coercion procedures to relate the types (possibly N^2 procedures for a system with N types), we need to write only one procedure for each pair of types rather than a different procedure for each collection of types and each generic operation."

I'm confused on this line:

"possibly N^2 procedures for a system with N types"

Let's take the arithmetic package example. The operations that deel with two ordinary numbers (scheme-number scheme-number), two rational numbers (rational rational) and two complex numbers (complex complex) are the same types, so they are not included in the coercion procedures.

We have three types, these are the coercion procedures I can think of with just two arguments.

(scheme-number rational)
(scheme-number complex)
(rational scheme-number)
(rational complex)
(complex scheme-number)
(complex rational)

These are not n^2 coercion procedures. There are only six coercion procedures here, not 9. I think I'm not really understanding this part of the text at all. Can someone explain what I missed?

Finally, here's a footnote regarding this part of the text.

"If we are clever, we can usually get by with fewer than N^2 coercion procedures. For instance, if we know how to convert from type 1 to type 2 and from type 2 to type 3, then we can use this knowledge to convert from type 1 to type 3. This can greatly decrease the number of coercion procedures we need to supply explicitly when we add a new type to the system."

From what I understand if we can convert an ordinary number to a rational number, then convert that rational number into a complex number, we wouldn't need a procedure that converts an ordinary into a complex. Now I'm confused because the original stepts doesn't really looks like N^2 at all.

Can anyone explain this more clearly?
3 Answers

Paul Wallich

2/2/2016 2:38:00 PM

0

On 2/2/16 9:12 AM, franklin wrote:
> This is a quote from SICP about coercion. This section talks about the arithmetic package with ordinary numbers, rational numbers and complex numbers and doing cross type operations on them (E.G. adding a complex number to an ordinary number.)
>
> "This coercion scheme has many advantages over the method of defining explicit cross-type operations, as outlined above. Although we still need to write coercion procedures to relate the types (possibly N^2 procedures for a system with N types), we need to write only one procedure for each pair of types rather than a different procedure for each collection of types and each generic operation."
>
> I'm confused on this line:
>
> "possibly N^2 procedures for a system with N types"
>
> Let's take the arithmetic package example. The operations that deel with two ordinary numbers (scheme-number scheme-number), two rational numbers (rational rational) and two complex numbers (complex complex) are the same types, so they are not included in the coercion procedures.
>
> We have three types, these are the coercion procedures I can think of with just two arguments.
>
> (scheme-number rational)
> (scheme-number complex)
> (rational scheme-number)
> (rational complex)
> (complex scheme-number)
> (complex rational)
>
> These are not n^2 coercion procedures. There are only six coercion procedures here, not 9. I think I'm not really understanding this part of the text at all. Can someone explain what I missed?

I think you may be missing the coercions of a type to itself. Yeah, not
a very interesting operation, but from the point of view of a complete
system, it still has to be able to recognize "Oh, this number is already
of the type desired, so I don't do anything."

If you leave out the identity coercion, as you keep increasing the
number of types, you see the number of coercions needed is n*(n-1), and
a you head for infinity, that's going to get closer and closer to n^2.
But I doubt SICP would pull that.


Pascal J. Bourguignon

2/2/2016 6:40:00 PM

0

franklin <chong.franklin@gmail.com> writes:

> This is a quote from SICP about coercion. This section talks about the
> arithmetic package with ordinary numbers, rational numbers and complex
> numbers and doing cross type operations on them (E.G. adding a complex
> number to an ordinary number.)
>
> "This coercion scheme has many advantages over the method of defining
> explicit cross-type operations, as outlined above. Although we still
> need to write coercion procedures to relate the types (possibly N^2
> procedures for a system with N types), we need to write only one
> procedure for each pair of types rather than a different procedure for
> each collection of types and each generic operation."
>
> I'm confused on this line:
>
> "possibly N^2 procedures for a system with N types"
>
> Let's take the arithmetic package example. The operations that deel
> with two ordinary numbers (scheme-number scheme-number), two rational
> numbers (rational rational) and two complex numbers (complex complex)
> are the same types, so they are not included in the coercion
> procedures.
>
> We have three types, these are the coercion procedures I can think of with just two arguments.
>
> (scheme-number rational)
> (scheme-number complex)
> (rational scheme-number)
> (rational complex)
> (complex scheme-number)
> (complex rational)
>
> These are not n^2 coercion procedures. There are only six coercion
> procedures here, not 9. I think I'm not really understanding this part
> of the text at all. Can someone explain what I missed?

You missed the 3 identities.


> Finally, here's a footnote regarding this part of the text.
>
> "If we are clever, we can usually get by with fewer than N^2 coercion
> procedures. For instance, if we know how to convert from type 1 to
> type 2 and from type 2 to type 3, then we can use this knowledge to
> convert from type 1 to type 3. This can greatly decrease the number of
> coercion procedures we need to supply explicitly when we add a new
> type to the system."
>
> From what I understand if we can convert an ordinary number to a
> rational number, then convert that rational number into a complex
> number, we wouldn't need a procedure that converts an ordinary into a
> complex. Now I'm confused because the original stepts doesn't really
> looks like N^2 at all.

But now, you may have introduced more numerical errors (or some other
errors for non-numerical data types), when one or both coersions are
lossy. So even if it's possible to chain conversions, you may still
want to implement the N^2-N =O(N^2) functions, so that each of them can
be optimized in speed and in precision.

You can see the effect of coersions (translations) in serie illustrated
by: https://youtu.be/33UFRcLd...

> Can anyone explain this more clearly?

I don't know.


--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

Barry Margolin

2/2/2016 6:47:00 PM

0

In article <874mdrhs6x.fsf@kuiper.lan.informatimago.com>,
"Pascal J. Bourguignon" <pjb@informatimago.com> wrote:

> franklin <chong.franklin@gmail.com> writes:
> > Finally, here's a footnote regarding this part of the text.
> >
> > "If we are clever, we can usually get by with fewer than N^2 coercion
> > procedures. For instance, if we know how to convert from type 1 to
> > type 2 and from type 2 to type 3, then we can use this knowledge to
> > convert from type 1 to type 3. This can greatly decrease the number of
> > coercion procedures we need to supply explicitly when we add a new
> > type to the system."
> >
> > From what I understand if we can convert an ordinary number to a
> > rational number, then convert that rational number into a complex
> > number, we wouldn't need a procedure that converts an ordinary into a
> > complex. Now I'm confused because the original stepts doesn't really
> > looks like N^2 at all.
>
> But now, you may have introduced more numerical errors (or some other
> errors for non-numerical data types), when one or both coersions are
> lossy. So even if it's possible to chain conversions, you may still
> want to implement the N^2-N =O(N^2) functions, so that each of them can
> be optimized in speed and in precision.

But some coercions aren't lossy, so it would be safe to chain them. For
instance, converting a real number to complex just requires adding a
zero imaginary part, with no change in precision to the real part.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***