Common-Lisp Recursive Type Specifier [duplicate]

2019-02-20 04:24发布

This question already has an answer here:

So I would like to write a type specifier for list that allows one to specify the types of the list elements. Something like :

(deftype list-of (v) (cons ,v (or null (list-of ,v))))

but of course, that's not going to work because as we all know, recursive types are not supported. One could map something like "satisfies" of course (as has been mentioned here: Is it possible to define a recursive type in Common Lisp?), but that kind of negates the advantage of a clean recursive definition suitable for language analysis and offline code analysis. (E.g., proving code is correct, and or translating between languages).

Any suggestions? Seems like this is a missing bit of standard, unless I'm missing something obvious, given we typically use lists to represent sets - why not have a simple way to declare the types of set elements?

标签: common-lisp
1条回答
放荡不羁爱自由
2楼-- · 2019-02-20 05:04

Sorry, this isn't allowed. The DEFTYPE specification says:

Recursive expansion of the type specifier returned as the expansion must terminate, including the expansion of type specifiers which are nested within the expansion.

This language was the result of a the RECURSIVE-DEFTYPE cleanup issue during the standardization process. An implementation could allow recursive type definitions in some cases as an extension, but you can't depend on it portably.

查看更多
登录 后发表回答