The elusive DEFTYPE

Discussion of Common Lisp

The elusive DEFTYPE

Postby garethw » Sat Aug 18, 2012 1:42 pm

Hi all,

I'm trying to broaden my understanding of the CL type system, and I was very surprised how little there is on (user-defined) types in Seibel's "Practical Common Lisp", Lamkin's "Successful Lisp" and Touretzky's "CL: A Gentle Introduction to Symbolic Computation". "DEFTYPE", for instance, doesn't even appear in the index of any one of them. CLtL2 and the Hyper Spec have plenty of info, but they're more reference works, obviously, and difficult to learn the basics from.

From what I've gathered so far, it seems like a really great compromise between static and dynamic typing - so I'm surprised it gets so little press.

I'm curious why this is - is it considered an advanced topic? Technically deficient? Not useful in practice? Or maybe not useful in many domains? Heck, it's not even in PG's "On Lisp".

My particular interest is in modelling 4-state logic (I'm a hardware guy), and I'm trying to figure out where to even begin - cons cells? class? struct? Seibel doesn't talk about DEFSTRUCT either.

Not asking for a tutorial, just would like to be pointed in roughly the right direction.

cheers,
~gareth

PS. Absolutely loving this journey so far, but man do I get lost sometimes. 27 years of programming imperatively, mostly in ALGOL-derived languages, sure narrows one's mind.
PPS. Which is why I've ended up here. I've never resorted to asking for help in any other programming language. I've really appreciated the kind and thoughtful responses I've had here.
~garethw
User avatar
garethw
 
Posts: 38
Joined: Fri Jul 13, 2012 12:56 pm
Location: Ottawa, ON

Re: The elusive DEFTYPE

Postby Ramarren » Sun Aug 19, 2012 11:30 am

garethw wrote:I'm curious why this is - is it considered an advanced topic? Technically deficient? Not useful in practice?


In my experience at least, DEFTYPE is not really that useful in practice, other than defining short names for complex array types and the like. The standard does not specify any type inference mechanism, which means that most static type checking is an implementation extensions (I think SBCL does this most extensively), so it can't be relied on in books.

DEFTYPE is fairly limited, it can mostly create synonyms which are non-recursive specializations or unions/intersections of preexisting types. While creating a type defined by arbitrary predicates using SATISFIES the compiler cannot do anything with that, so the only thing such types are useful for are run-time type checks.

Also types in CL are purely representational, and are mostly useful for allowing the compiler to create more specialized code for given data types. There is no possibility of creating a distinction like with Haskell's newtype where two objects are of the same data type but different semantic types.

garethw wrote:and I'm trying to figure out where to even begin - cons cells? class? struct? Seibel doesn't talk about DEFSTRUCT either.


Structs in Common Lisp are partially a historical artefact, which remains because they are slightly faster in some cases. For most applications classes are better, if only because the DEFCLASS syntax is clearer (in my opinion, at least). Especially during prototyping, since structs do not have a proper redefinition protocol.
Ramarren
 
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland


Return to Common Lisp

Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests