类型谓词胜过类型 [英] type predicates over types

查看:129
本文介绍了类型谓词胜过类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望提高收到的编译器警告的质量和数量-Common Lisp中有
可以在声明的类型以及实例中包含类型谓词
的实现特定的答案
很好,我想看看它是如何完成的,如果有人在做。

I'm looking to improve the quality and quantity of compiler warnings I receive -- is there a way in Common Lisp to include type predicates over declared types as well as over instances -- implementation-specific answers are fine, I'm interested in seeing how it's done, if anyone's doing it.

在CCL中编译以下内容:

Compiling the following in CCL:

(defun non-list (o)
  (not (listp o)))

(deftype non-list ()
  '(satisfies non-list))

(defun example (a)
  (list a))

(declaim (ftype (function (non-list) list) example))

(defun hmm ()
  (declare (optimize (debug 3) (safety 3)))
  (let ((a '(a b c))
    (b '(d e f)))
    (declare (type list a))
    (example '(g h i))
    (example a)
    (example b)))

我会在首次调用 example -提供
实例的实例,可以检查该实例获得满意。这样很好,并且通过调试设置,我会得到一个运行时错误,这很好。我想知道的是,是否可以编写如下内容:

I'll get a compiler warning on the first call to example -- the one that provides an instance which can be checked against satisfies. This is good, and with debug settings I'll get a runtime error which is good. What I'm wondering is if I can write something like the following:

(defun non-list-typep (type)
  (not (subtypep type 'list)))

并以某种方式将其集成,以便至少第二个调用-(示例a)将在编译时发出警告,因为其声明的类型为 list 会使谓词 non-list-typep

and somehow integrate it so that at least the second call -- (example a) will warn at compile time as its declared type list would fail the predicate non-list-typep

干杯!

推荐答案

背景知识

有两件事:标准语言 Common Lisp 和实现和扩展Common Lisp的多种语言。

There are two different things: a standard language Common Lisp and widely different languages implementing and extending Common Lisp.

Common Lisp作为一种语言,不需要像这样的类型警告/错误编译时间。一个实现通常可以忽略类型声明。该语言也没有真正描述当编译器不忽略类型声明时应该怎么做以及如何使用它们来静态检查类型。

Common Lisp as a language does not require type warnings/errors like that at compile time. An implementation can mostly ignore type declarations. The language also does not really describe what should be done when the compiler does not ignore type declarations and how it should use them to statically check types.

实际的普通Lisp编译器会失败在涉及静态声明类型的以下几类中:

Actual Common Lisp compilers fall in the following groups when it comes to statically declared types:


  1. 大多忽略它们。例如,Symbolics Lisp机器的编译器属于这一类。

  1. ignore them mostly. For example the compiler of the Symbolics Lisp Machine falls into this group.

大多数情况下出于优化目的使用声明的类型。我想说CCL属于这一类。我认为它不会做很多类型推断或类型信息传播。我想这也是为什么编译器比SBCL更快的原因-它做得更少。

uses declared types mostly for optimization purposes. I'd say CCL falls into this group. I don't think it does much type inference or propagation of type information. I would guess that's also a reason why the compiler is faster than SBCL - it does less.

主要将声明的类型用于优化目的,但可以进行类型推断并可以提供类型信息(用于优化)。例如,LispWorks属于这一类。

uses declared types mostly for optimization purposes, but can do type inference and can give type information (for optimization). For example LispWorks falls into this group.

使用声明的类型进行优化,进行类型推断,可以给出类型信息,并将类型声明视为编译时类型。断言。 CMUCL和SBCL编译器是该组的主要成员。

uses declared types for optimization purposes, does type inference, can give type information and regards type declarations as compile-time type assertions. The CMUCL and SBCL compilers are the main members of this group.

因此,对于类型2的实现,您最好如果要让编译器识别它,则需要声明所有内容。请注意,Common Lisp提供 THE LOCALLY 来编写声明。

So, for implementation of type 2. you best need to declare everything, if you want the compiler to recognize it. Note that Common Lisp provides THE and LOCALLY to write declarations.

如果要将类型声明作为静态类型声明并让编译器进行检查,则最好使用像 SBCL

If you want to have type declarations as static type assertions and have the compiler check them, you should better use a compiler like SBCL.

在SBCL手册中可以找到有关类型声明方法的一些背景知识:类型处理编译时出现类型错误。此处提供更多背景信息:用于CMU Common Lisp的Python编译器。请注意,CMU Common Lisp(以及后来的SBCL)的编译器称为 Python -与编程语言 Python 无关。在语言 Python (1991)出现之前,编译器就已经有其名称(1980年代早期)。

A little background on its approach to type declarations can be found in the SBCL Manual: Handling of Types and Type Errors at Compile Time. More background here: The Python Compiler for CMU Common Lisp. Note that the compiler of CMU Common Lisp (and later of SBCL) is called Python - it has nothing to do with the programming language Python. The compiler already had its name (early 1980s) before the language Python (1991) existed.

这篇关于类型谓词胜过类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆