The Artima Developer Community
Sponsored Link

Weblogs Forum
A Type System is a Set of Tests

73 replies on 5 pages. Most recent reply: Apr 26, 2006 4:08 AM by Achilleas Margaritis

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 73 replies on 5 pages [ 1 2 3 4 5 | » ]
Michael Feathers

Posts: 448
Nickname: mfeathers
Registered: Jul, 2003

A Type System is a Set of Tests (View in Weblogs)
Posted: Apr 13, 2006 8:19 AM
Reply to this message Reply
Summary
Why don't we treat it like a set of tests?
Advertisement

I've had a lot of feedback since my last blog. A few people pointed out that Common Lisp and Erlang do something similar. Several people pointed toward Guido's desire to add static typing to Python (I had read about that.. it would be cool to see if this separate compiled type annotation thing could work in Python), and several pointed toward Gilad Bracha's paper on pluggable type systems [PDF] and it looks like the same idea. I knew someone had to have thought of this before. I wish I'd seen those references earlier :-(

One thing I'm wondering, though. In that paper, Gilad talks about type systems being pluggable. What would it be like if they were end user extendable? I know AspectJ allows you to produce errors and warnings based upon static aspects that you specify. For instance, you can have AspectJ produce an error if a someone creates an object of a particular class using new in another particular class. Essentially, that extends the type system.. but from another point of view, couldn't it be seen as a test for a design constraint?

Let's try this point of view on for a second.. The type system of a language is really a set of tests, run by the compiler or the generated runtime. Perhaps we should treat the static end of a type system as a set of tests. Make it them programmable. A project could start out with a set of constraints, as tests, and elaborate them as the project progresses.


James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: A Type System is a Set of Tests Posted: Apr 13, 2006 10:08 AM
Reply to this message Reply
I don't know that I see it as a set of test, or at least, I don't find that to be the greatest value of static typing. Here's an off-the-cuff analogy: I see it as more of a registration system. When I declare an variable to be of a type, I'm saying this this the API I am using. If you are going to change it, you need to either make sure you don't break me or you're going to hae to fix me.

Whether that fits with the rest of your post or not, I'm not sure.

Michael Feathers

Posts: 448
Nickname: mfeathers
Registered: Jul, 2003

Re: A Type System is a Set of Tests Posted: Apr 13, 2006 11:15 AM
Reply to this message Reply
> I don't know that I see it as a set of test, or at least,
> I don't find that to be the greatest value of static
> typing. Here's an off-the-cuff analogy: I see it as more
> of a registration system. When I declare an variable to
> be of a type, I'm saying this this the API I am using. If
> you are going to change it, you need to either make sure
> you don't break me or you're going to hae to fix me.
>
> Whether that fits with the rest of your post or not, I'm
> not sure.

I think it does. It's just a different perspective on the same thing. For what you were describing I'd say that the test is something like:

Verify: "all things declared as X have these operations and no part of the program attempts to invoke any other operations on Xs."

I agree that there are many other ways to see a type system and people like them for many reasons, but I think it's interesting that we could articulate a type system as a set of tests. Right now, these tests are usually hidden in a compiler or a runtime system.

Jay Sachs

Posts: 30
Nickname: jaysachs
Registered: Jul, 2005

Re: A Type System is a Set of Tests Posted: Apr 13, 2006 1:10 PM
Reply to this message Reply
> Let's try this point of view on for a second.. The type system of a language is really a set of tests, run by the compiler or the generated runtime.

Interesting. For a language that is not fully statically typed, would you consider a runtime exception a "test"? A thrown CastClassExceptions is certainly the type system in action (or is that "type system inaction"?) but I wouldn't classify it is a "test". My distinction here is that things like that are not something executed, either by compiler or runtime, before deployment.

By that razor, runtime checks are not tests, since they're not part of a repeatable process. Triggering certain runtime behaviors, including verifying type checks at runtime, can be encoded in artifacts (e.g. unit tests) I'd consider tests.

Static typing could be classified as a test, as the validation is checked during compilation, a repeatable process.

Hui Deng

Posts: 5
Nickname: dhui
Registered: Mar, 2004

Re: A Type System is a Set of Tests Posted: Apr 13, 2006 7:01 PM
Reply to this message Reply
what a cool thing if we could have a first-calss type-checking mechanism!

Michael Feathers

Posts: 448
Nickname: mfeathers
Registered: Jul, 2003

Re: A Type System is a Set of Tests Posted: Apr 13, 2006 7:01 PM
Reply to this message Reply
> > Let's try this point of view on for a second.. The type
> system of a language is really a set of tests, run by the
> compiler or the generated runtime.
>
> Interesting. For a language that is not fully statically
> typed, would you consider a runtime exception a "test"? A
> thrown CastClassExceptions is certainly the type system in
> action (or is that "type system inaction"?) but I wouldn't
> classify it is a "test". My distinction here is that
> things like that are not something executed, either by
> compiler or runtime, before deployment.

I could argue that either way. The neat thing is that some IDEs drop their UT errors in the same pane as the compiler errors. One could consider UT execution part of a compilation's semantic check or the semantic checks of compilation as part of test execution. They are more alike than different to me.

> By that razor, runtime checks are not tests, since they're
> not part of a repeatable process. Triggering certain
> runtime behaviors, including verifying type checks at
> runtime, can be encoded in artifacts (e.g. unit tests) I'd
> consider tests.

I agree.

> Static typing could be classified as a test, as the
> validation is checked during compilation, a repeatable
> process.

David Kemp

Posts: 1
Nickname: dkemp
Registered: Apr, 2006

Re: A Type System is a Set of Tests Posted: Apr 13, 2006 10:38 PM
Reply to this message Reply
For me, types are more a form of reliable documentation than a set of tests. When using a 3rd party library, it is nice to know the expected parameter and return types without having to rely on out-of-date or missing documentation, or having to inspect the code.

Also, the extremely useful auto-completion that IDE's can do with a static language is in a sense due to the formal/reliable documentation that the static typing provides.

- David

Vesa Karvonen

Posts: 116
Nickname: vkarvone
Registered: Jun, 2004

Types are O(1) tests are O(n) where n is the number of clients Posted: Apr 14, 2006 12:32 AM
Reply to this message Reply
The type system of a language is really a set of tests, run by the compiler or the generated runtime.

As I've argued elsewhere

http://lambda-the-ultimate.org/node/1311#comment-15148

a type safe interface is a solution once and for all.

Michael Feathers

Posts: 448
Nickname: mfeathers
Registered: Jul, 2003

Re: A Type System is a Set of Tests Posted: Apr 14, 2006 5:22 AM
Reply to this message Reply
> For me, types are more a form of reliable documentation
> than a set of tests. When using a 3rd party library, it
> is nice to know the expected parameter and return types
> without having to rely on out-of-date or missing
> documentation, or having to inspect the code.
>
> Also, the extremely useful auto-completion that IDE's can
> do with a static language is in a sense due to the
> formal/reliable documentation that the static typing
> provides.

That's fine. Notice that it's not incompatible with anything I'm saying. I'm not saying get rid of static types; I'm saying put the type system under the control of the programmer.

We're in an odd state right now. The static vs. dynamic debate is so far in the forefront that we can't seem to talk about typing at all without the conversation moving in that direction. It's a shame. There are so many more things to talk about.

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: A Type System is a Set of Tests Posted: Apr 14, 2006 7:05 AM
Reply to this message Reply
> A few people pointed out that Common
> Lisp and Erlang do something similar. Several people
> pointed toward Guido's desire to add static typing to
> Python (I had read about that.. it would be cool to see if
> this separate compiled type annotation thing could work in
> Python), and several pointed toward Gilad Bracha's paper
> on <a
> href=http://pico.vub.ac.be/~wdmeuter/RDL04/papers/Bracha.pd
> f>pluggable type systems [PDF]</a> and it looks like the
> same idea. I knew someone had to have thought of this
> before. I wish I'd seen those references earlier :-(</p>

I'm glad you didn't, because as usual you bring your own unique and intelligent approach to the table, enriching the discussion.

> <p>Let's try this point of view on for a second.. The
> type system of a language is really a set of tests, run by
> the compiler or the generated runtime. Perhaps we we
> should treat the static end of a type system as a set of
> tests. Make it them programmable. A project could start
> out with a set of constraints, as tests, and elaborate
> them as the project progresses.</p>

I like this idea. So how about this: place these tests in a java-style interface, to distinguish them from implementation code? Then allow them to be conditionally compiled.

What do you think?

Jay Sachs

Posts: 30
Nickname: jaysachs
Registered: Jul, 2005

Re: A Type System is a Set of Tests Posted: Apr 14, 2006 8:13 AM
Reply to this message Reply
To be precise, the statically verifiable parts of a type system could usefully be construed as tests, but the dynamic part (runtime checks, etc) are not. This pedant would be much happier if this thread were titled "The statically veriable parts of a type system comprise a set of tests".

Erik Engbrecht

Posts: 210
Nickname: eengbrec
Registered: Apr, 2006

Re: A Type System is a Set of Tests Posted: Apr 15, 2006 7:07 PM
Reply to this message Reply
>We're in an odd state right now. The static vs. dynamic
>debate is so far in the forefront that we can't seem to talk
>about typing at all without the conversation moving in that
>direction. It's a shame. There are so many more things to
>talk about.

Isn't that the truth.

I definately think static typing (and other forms of static code checking) and automated testing are very similar in purpose and should be grouped together. Both serve to give the programmer rapid feedback as to the correctness of his program.

The purpose of a programming language is for the programmer to communicate with the computer. Specifying type information (and potentially additional DBC information) allows the computer to better validate that that what the programmer is saying makes sense.

The problem is, often times at the early stages of writing a program, the programmer doesn't understand the program adequately for type information to be overly meaningful. Think of specifying type information as trying to explain the details of a concept that you don't understand to another person. All it produces is confusion.

Conversely, failing to specify type information for an understood program opens the door to unnecessary confusion. It's like being vague when there's not reason to be vague. It's like hiding something from the computer.

So specifying static type information in early code is similar to premature optimization, but failing to do so in more mature code invites errors.

The program should be no more and no less precisely defined than it is precisely understood.

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: A Type System is a Set of Tests Posted: Apr 15, 2006 9:57 PM
Reply to this message Reply
> The purpose of a programming language is for the
> programmer to communicate with the computer.

More importantly is to communicate the purpose to other programmers. A computer doesn't care how convoluted or unreadable a program turns out to be.

Achilleas Margaritis

Posts: 674
Nickname: achilleas
Registered: Feb, 2005

Re: A Type System is a Set of Tests Posted: Apr 17, 2006 5:05 AM
Reply to this message Reply
What we need is not only pluggable type systems, but a programming language that is programmable at compile-time. In other words, we need a compiler that has no default parts other than the one specifying the mechanism that allows it to perform compile-time processing. All these concepts (pluggable type systems, AspectJ etc) fall under the same question: "how can I program the compiler to interpret my code the way I like?"...

Most programming languages offer a fixed way of interpreting code. Some offer clever macro systems (Lisp/Scheme, Dylan etc) or archaic ones (C/C++), but no language so far has been invented that the compiler can be fully defined in itself. There are extensions of mainstream programming languages like MetaC++ or MetaJava, but they are not mainstream yet. I think that meta-programming is the key to pluggable type systems, aspects etc.

Michael Feathers

Posts: 448
Nickname: mfeathers
Registered: Jul, 2003

Re: A Type System is a Set of Tests Posted: Apr 17, 2006 7:25 AM
Reply to this message Reply
> > I knew someone had to have thought of this
> > before. I wish I'd seen those references earlier
> :-(</p>
>
> I'm glad you didn't, because as usual you bring your own
> unique and intelligent approach to the table, enriching
> the discussion.
>
> > <p>Let's try this point of view on for a second.. The
> > type system of a language is really a set of tests, run
> by
> > the compiler or the generated runtime. Perhaps we we
> > should treat the static end of a type system as a set
> of
> > tests. Make it them programmable. A project could
> start
> > out with a set of constraints, as tests, and elaborate
> > them as the project progresses.</p>
>
> I like this idea. So how about this: place these tests in
> a java-style interface, to distinguish them from
> implementation code? Then allow them to be conditionally
> compiled.
>
> What do you think?

It seems okay except that I can imagine these tests (which stand in for type rules) spanning across several types. I'm rather excited by Stroustrup's idea of use-patterns for expressing 'concepts' in C++. One of the neat things is that they, like tests, are example-based. Have you looked into that work at all?

Flat View: This topic has 73 replies on 5 pages [ 1  2  3  4  5 | » ]
Topic: A Type System is a Set of Tests Previous Topic   Next Topic Topic: Thinking in C, Beta 3

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use