Ducktator - A Duck Type Validator

Leslie V. [email protected] wrote:

Ah thank-you, that’s great. But it would not work unless every
execution path within the method receiving the object was executed:

Well, yes. I didn’t say it’s perfect. :slight_smile:

…so is there some magic that could overcome that limitation?

You could make Probe significantly more complex so that it records
invoked
methods and alters the return value between true and false or something
else
etc. and goes on probing until no new execution sequences are found. I
guess this can soon get messy…

Regards

robert

On 9/20/06, Austin Z. [email protected] wrote:

You are in a maze of twisty little passages, all alike. // halo • statue

Hmm, and just a few hours back I was saying in my journal how friendly
the Ruby community is (http://lesliev.livejournal.com). But then I
didn’t think everyone was going to let Ola post-and-run like that, no
siree!

Ola B. wrote:

I beg to differ, and according to the pick-axe, this interpretation is
correct. Duck-typing isn’t to trust your callers. Duck typing is to
check if something quacks like a duck and walks like a duck. Well, me
calling respond_to? checks if the object quacks and walks. What you are
describing isn’t a technique, it is the absence of a technique.

Austin is right. If you’re checking types, that isn’t duck typing.

Hal

On 9/20/06, Ola B. [email protected] wrote:

I don’t want to continue this discussion in this venue, since the
discussion isn’t really about what I meant with the project and such.
Duck typing also seems to be a very loaded (might I say religious) word
for people in the Ruby-community.

What a shameful blog post, Ola. Since you’re not interested in
dialogue, I’ll join your monologue with a blog post, myself. You’re
welcome not to read it or to read it, but you should be ashamed of
your blog post here.

http://www.halostatue.ca/2006/09/20/duck-typing-getting-it-name-calling-and-ola-bini/

-austin

Hi,

In message “Re: [ANN] Ducktator - A Duck Type Validator”
on Thu, 21 Sep 2006 08:59:45 +0900, Hal F.
[email protected] writes:

|Austin is right. If you’re checking types, that isn’t duck typing.

Although I am not Dave T., I consider it is a variation of duck
typing, if the type describes behavior, not structure nor type name.
Signature validation is approximation of type-by-behavior. As Austin
stated, some doesn’t consider it duck typing, I agree. But Keeping
strict definition of duck typing is not that important to justify
strong words in the list (and blogs).

For signature validation, we should note that respond_to? does not
work well for objects with method_missing.

						matz.

Devin M. wrote:

Do we need separate terms
to describe the different meanings Eric enumerated (and potentially
others)? Well, no, but it’d be helpful. I’ll start.
Oh, and lastly:

Can we expect everybody in the world to conform to a standard set of
vocabulary, should we come to agree on one? HELL NO!

That is all.

Devin

Devin M. wrote:

Devin M. wrote:

Do we need separate terms
to describe the different meanings Eric enumerated (and potentially
others)? Well, no, but it’d be helpful. I’ll start.
Oh, and lastly:

Can we expect everybody in the world to conform to a standard set of
vocabulary, should we come to agree on one?

You know, it would really help :slight_smile:

Austin is right. If you’re checking types, that isn’t duck typing.
Boy, we really love to argue about semantics, don’t we?

What does it matter whether Ducktator “implements duck typing” or
“validates duck types” or “enforces duckly contracts” or “ducktates
method signatures” or “imposes a reducktionist philosophy” or “is just
ducky” or is none of those things?

Is the term “duck typing” overloaded? Yes (see Eric’s ruby-talk:215359).
Is it getting more overloaded? Yes. Do newbs come in here with opinions
of the meaning of “duck typing” that differ from yours? Yes. Is that
going to happen more frequently? Probably. Is that going to prevent you
from explaining what you mean by example? No. Do we need separate terms
to describe the different meanings Eric enumerated (and potentially
others)? Well, no, but it’d be helpful. I’ll start.

For backwards compatibility with the great majority, “duck typing” will
mean “just calling methods on an object, without doing any validation on
the behavior of that object.”* Using respond_to? as a guard – let’s
call that “duck but verify.” Using is_a? (and friends) as a guard –
well, “crap” comes to mind, but let’s just go with “Class testing.”

(On a related note… we didn’t ever decide on a replacement for the
term “singleton class,” did we? I liked “shadow class,” myself. :P)

And “static” means “not at run-time,” no?

Devin
(In short: “What matz said.”)

  • Though, for me, “duck typing” meant just that: If it looks like a duck
    and walks like a duck… Which allows for pretty much anything except
    Class testing (sticking a q-tip in the duck’s mouth and waiting 2 weeks
    for the lab tests to come back). Really, “just calling methods” is more
    equivalent to catching something, stripping it, cooking it, taking a
    bite, and going “yup, tastes like duck.” But hey, peer pressure.

Devin M. wrote:

For backwards compatibility with the great majority, “duck typing” will
mean “just calling methods on an object, without doing any validation on
the behavior of that object.”*

The great majority of whom? If you mean rubyists, perhaps you’re right
(though I don’t know). If you mean programmers, language designers and
computer scientists in general, I think you’re wrong.

“Duck typing” is not something that you do, and is not even how you
do something
; it is a method of relating objects – a sort of “type
system” (“type” in the generic sense not the C-type sense; i.e., “A
type indicates a set of values that have the same sort of generic
meaning or intended purpose (although some types, such as abstract
types and function types, might not get represented as values in the
running computer program).” [1]).

In the terms of category theory: "Instead of focusing merely on the
individual objects (groups) possessing a given structure […] category
theory emphasizes the morphisms - the structure-preserving processes

  • between these objects. […] A category is itself a type of
    mathematical structure, so we can look for ‘processes’ which preserve
    this structure in some sense. Such a process is called a functor. It
    associates to every object of one category an object of another
    category; and to every morphism in the first category a morphism in the
    second." [2]

So “In computer science, duck typing is a term for dynamic typing
typical of some programming languages, such as Smalltalk or Visual
FoxPro, where a variable’s value itself determines what the variable
can do. Thus an object having all the methods described in an interface
can be made to implement that interface dynamically at runtime, even if
the object’s class does not include the interface in its implements
clause. It also implies that an object is interchangeable with any
other object that implements the same interface, regardless of whether
the objects have a related inheritance hierarchy.” [3]

The python glossary defines the expression similarly: “Pythonic
programming style that determines an object’s type by inspection of its
method or attribute signature rather than by explicit relationship to
some type object (“If it looks like a duck and quacks like a duck, it
must be a duck.”) By emphasizing interfaces rather than specific types,
well-designed code improves its flexibility by allowing polymorphic
substitution. Duck-typing avoids tests using type() or isinstance().
Instead, it typically employs hasattr() tests or [Easier to ask for
forgiveness than permission] programming” [4] Note also that python
calls the method of programming which you call “duck typing” – “just
calling methods on an object, without doing any validation on the
behavior of that object” – “Easier to ask for forgiveness than
permission”

Structural type systems, as found in dialects of ML and other
languages, are very similar to duck typing. “In structural typing, two
objects or terms are considered to have compatible types if the types
have identical structure.” [5] This “structure” is often referred to as
the “shape” or “signature” of the object / type.

Now I don’t know if Dave T. used the expression “duck typing”
differently than others generally do, leading to it having a different
nuance in the ruby community; or if perhaps the community didn’t
understand Mr. Thomas; or some combination of the two (or, as a third
alternative, the community doesn’t generally have that notion of “duck
typing”). But generally speaking “duck typing” does not require or even
imply any specific programming approach (including the approach where
one simply expects / trusts that one object will act like / has the
same (or pragmatically similar) signature as another). If it did, then
(in many cases) when you write begin…rescue, you’d be violating “duck
typing”, because you’re no longer trusting the object, but are imposing
a condition on it – you’re just doing it “after the fact”, so to
speak: try it → failed → do something else. This is no different in
concept from ask about it → failed → do something else, it’s just a
different implementation.

So, in colloquial terms, I see no reason why “ask if it walks like a
duck before you believe its a duck” should be a violation of “duck
typing” any more than “expect it to walk like a duck but don’t believe
its a duck if it doesn’t”.

Ps. I also agree with Matz. I see no reason for zealotry. If I’m wrong,
that’s OK; I’ve been wrong plenty of times before and will be wrong
again. No need to get upset over it.

[1] Type system - Wikipedia
[2] Category theory - Wikipedia
[3] Duck typing - Wikipedia
[4] http://docs.python.org/tut/node18.html#l2h-46
[5] Structural type system - Wikipedia

Regards,
Jordan

MonkeeSage wrote:

[snippage]

Overall I don’t disagree with you. :wink:

Now I don’t know if Dave T. used the expression “duck typing”
differently than others generally do, leading to it having a different
nuance in the ruby community; or if perhaps the community didn’t
understand Mr. Thomas; or some combination of the two (or, as a third
alternative, the community doesn’t generally have that notion of “duck
typing”).

One thing I’m wondering is: Is there any evidence that this term
originated other than with Dave T.?

Certainly it’s known outside the Ruby community now, but didn’t the
term originate within the community?

Not that it matters really. AFAIK Dave himself doesn’t claim to have
coined the term. And I take it for granted that the concept is older
than the term.

So, in colloquial terms, I see no reason why “ask if it walks like a
duck before you believe its a duck” should be a violation of “duck
typing” any more than “expect it to walk like a duck but don’t believe
its a duck if it doesn’t”.

I think that most people (me, anyway) perceive that checking type
is antithetical to the spirit of duck typing.

I’m not adamant about it. And I do perform this kind of checking
sometimes (gasp!).

Cheers,
Hal

MonkeeSage wrote:

I trust everything that Wikipedia says.
(Well, I paraphrased that a bit…)

You, there’s a way of thinking about the phrase “duck typing” that’ll
help explain that it’s really a description of a programming strategy.*
Intepret “duck” as a verb. When you use “duck typing,” as a programmer,
you “duck” the programming language’s definition of “typing.” (Class, in
Ruby’s case) Wait, that sounds familiar… did I read that somewhere?

Just a thought.

Devin
(I was kidding, Jordan. Just messing wit j00. I lack knowledge of
category theory.)

  • Scanning over chapter 23 of the PickAxe seems to confirm that that was
    the intention – pages 370-1 use the phrases “duck typing philosophy”
    and “this style of laissez-faire programming,” though, granted, the book
    isn’t consistent. Hey, the phrase is out there. It doesn’t seem to have
    a canonical definition. It’s an ad hoc term. Treat it as such, I say.

On 21.09.2006 05:20, Devin M. wrote:

Devin M. wrote:

Do we need separate terms to describe the different meanings Eric
enumerated (and potentially others)? Well, no, but it’d be helpful.
I’ll start.
Oh, and lastly:

Can we expect everybody in the world to conform to a standard set of
vocabulary, should we come to agree on one? HELL NO!

Devin, I don’t want to stress this but you may be underestimating the
value of clear terms a bit. IMHO we should at least /try/ to get a
common understanding of some core concepts so we talk past each other as
little as possible. So, yes I think these discussions are necessary
from time to time. Sometimes the line between persistence and zealotry
is blurred. And unfortunately sometimes these discussions then take
such an unfriendly turn as this thread did.

Kind regards

robert

Hal F. wrote:

One thing I’m wondering is: Is there any evidence that this term
originated other than with Dave T.?

Certainly it’s known outside the Ruby community now, but didn’t the
term originate within the community?

Not that it matters really. AFAIK Dave himself doesn’t claim to have
coined the term. And I take it for granted that the concept is older
than the term.

From everything I can gather Mr. Thomas at least popularized the term
if not coined it. I just meant that I didn’t know if he used the term
differently from the way it has now become generalized and applied to
other languages or theories of language design. I just found this,
however, which is rather interesting [1].

I’m not adamant about it. And I do perform this kind of checking
sometimes (gasp!).

What!? You actually do that? Are you some kind of communist or
something? Stop oppressing your objects and just leave them be free to
be who they want to be!! Heh. :stuck_out_tongue_winking_eye:

[1] http://wiki.rubygarden.org/Ruby/page/show/DuckTyping

========

Devin M. wrote:

MonkeeSage wrote:

I trust everything that Wikipedia says.
(Well, I paraphrased that a bit…)

LOL! Well I did quote WP alot, didn’t I. It was just easier that trying
to 'splain all that stuff myself (and I have a tendency to be
ambiguous, if you haven’t noticed already, heh).

(I was kidding, Jordan. Just messing wit j00. I lack knowledge of
category theory.)

No worries, I caught that you were just kidding. We’s c00, mang. :wink:

  • Scanning over chapter 23 of the PickAxe seems to confirm that that was
    the intention – pages 370-1 use the phrases “duck typing philosophy”
    and “this style of laissez-faire programming,” though, granted, the book
    isn’t consistent. Hey, the phrase is out there. It doesn’t seem to have
    a canonical definition. It’s an ad hoc term. Treat it as such, I say.

Interesting. So, judging by the link I posted above in reply to Hal,
and this reference, it seems that sometimes Mr. Thomas uses the word in
different ways (or as a qualifier that nuances it differently).

Tim Bates touched on the topic in a mailing list post from 2004: “How
to duck type? - the psychology of static typing in Ruby” [2] He
considers the object signature validation approach (which he labels as
(3)) to be compatible with duck typing, but not the “Zen” of it (which
he labels as (4)), which is just to use the object.

David Black also has some interesting comments in this RCR [3].

[2] http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/100511
[3] RCR 318: easy way(s) to make a duck

I personally don’t really care how one uses the term, but I just think
that one valid use is the way myself and Ola were using it. Of course,
I’m not biased toward the definition I use – this is a purely
objective observation! :wink:

Cheers to you and Hal,
Jordan

On 9/21/06, Devin M. [email protected] wrote:

And “static” means “not at run-time,” no?

Haskell is a statically typed programming language. Hugs is a Haskell
interpreter. Is the language Hugs interprets not statically typed,
because the type checking is performed at runtime?

Static typing, I think, is about having type checking as a separate
(conceptually, not necessarily implementation-wise) phase from the
phase that actually performs the program’s function, but lets not
start a fight over that, too.

For posterity, I just want to say that I love all you guys (i.e., all
my fellow rubyists). I think of us like a big family. Even though we
may have some scuffles from time to time, at the end of the day, blood
is thicker than water. The ruby community is one of the best I’ve ever
come across, and I don’t want newbies to get the impression that we’re
all fractured and at each other’s throats all day long. I don’t think
that is true at all. So I just wanted to say explicitly that all of you
are awsome (whether we agree or not)! In the words of Bob Marley, “one
love”!

Regards,
Jordan

Eero S. wrote:

You know, it would really help :slight_smile:

remember the eigenclass / metaclass / singleton / virtual / adhoc /
whatever class discussion?

Inspired by Jordan’s sappiness, I would like to offer a public apology
for those who felt themselves slighted in anyway by what I have said in
this discussion, or in my blog post.

The blog entry was not an attack at the people who disagreed with me,
and if someone interpreted it in that way, I am sorry.

The point I was aiming at, is that this discussion about duck typing
seem to exhibit various interpretations of the concept, and everyone was
ready to defend their own versions of it, but there isn’t an easy way to
say who was wrong or who right, since the concept is a philosophy more
than a practice or a technique.

I am truly sorry I even used the word duck typing for my project, no
matter if it has anything to do with duck typing or not.

The Ruby community is wonderful, and I didn’t write with the intent of
trolling or driving a wedge in it.


Ola B. (http://ola-bini.blogspot.com)
JvYAML, RbYAML, JRuby and Jatha contributor
System Developer, Karolinska Institutet (http://www.ki.se)
OLogix Consulting (http://www.ologix.com)

“Yields falsehood when quined” yields falsehood when quined.

Austin Z. wrote:

On 9/21/06, Ola B. [email protected] wrote:

Inspired by Jordan’s sappiness, I would like to offer a public apology
for those who felt themselves slighted in anyway by what I have said
in this discussion, or in my blog post.

Apology accepted. I have been a little harsh in my response to your
post, so I apologize for the harshness involved. I’d actually like to
chat with you off-list, if you’re amenable to that.

-austin

Good, shake hands

Sure thing, as long as it isn’t about duck typing. =)


Ola B. (http://ola-bini.blogspot.com)
JvYAML, RbYAML, JRuby and Jatha contributor
System Developer, Karolinska Institutet (http://www.ki.se)
OLogix Consulting (http://www.ologix.com)

“Yields falsehood when quined” yields falsehood when quined.

On 9/21/06, Ola B. [email protected] wrote:

Inspired by Jordan’s sappiness, I would like to offer a public apology
for those who felt themselves slighted in anyway by what I have said
in this discussion, or in my blog post.

Apology accepted. I have been a little harsh in my response to your
post, so I apologize for the harshness involved. I’d actually like to
chat with you off-list, if you’re amenable to that.

-austin

On 9/21/06, Robert K. [email protected] wrote:

Devin, I don’t want to stress this but you may be underestimating the
value of clear terms a bit. IMHO we should at least /try/ to get a
common understanding of some core concepts so we talk past each other as
little as possible. So, yes I think these discussions are necessary
from time to time. Sometimes the line between persistence and zealotry
is blurred. And unfortunately sometimes these discussions then take
such an unfriendly turn as this thread did.

Here is a shot at giving some terms without trying to judge what is
best:

pure duck typing : just use the object as-is. Don’t do anything that
will
check it to see if it has specific capabilities, ancestors, class, etc.

virtual duck typing : the functionality is equivalent to pure duck
typing,
but for whatever reason #respond_to?, #class, etc has been used. This
could
be to check capabilities sooner rather than waiting for a method
missing, or
it could be done for performance reasons by special casing certain
classes (
i.e. builtins).

duck type : a description of what capabilities an object needs in the
context of pure or virtual duck typing. This description could be quite
recursive going into the duck type of the arguments passed to the
methods
and the return values of the methods.

complete type : a description of all of the capabilities of an object.

multi-method : a method that checks the capabilities or class of an
argument
to choose between different functionality. Other terms to go along with
this: multi-typing?, multi-type?.

class conversion : using a method (i.e. #to_*) to convert an object to a
specific class. If the result of this conversion must be a specific
class
(or descendent of) and not just have a certain “duck type”, it probably
should not be considered duck-typed.