Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

<<<<< EVACUATION ROUTE <<<<<


devel / comp.lang.lisp / how to declare the type of a variable

SubjectAuthor
* how to declare the type of a variableJinsong Zhao
+* Re: how to declare the type of a variableAxel Reichert
|`- Re: how to declare the type of a variableJinsong Zhao
`* Re: how to declare the type of a variableSpiros Bousbouras
 +- Re: how to declare the type of a variableTom Russ
 `- Re: how to declare the type of a variableJinsong Zhao

1
how to declare the type of a variable

<sul5dl$ruc$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16877&group=comp.lang.lisp#16877

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!feeder8.news.weretis.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jszhao@yeah.net (Jinsong Zhao)
Newsgroups: comp.lang.lisp
Subject: how to declare the type of a variable
Date: Thu, 17 Feb 2022 17:44:21 +0800
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <sul5dl$ruc$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 17 Feb 2022 09:44:22 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="60dad5eb806e50084c90d48839f569b0";
logging-data="28620"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19vlSthRyCK6umMFyUNDd5e"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.6.0
Cancel-Lock: sha1:8+Ke/rY3AkWMSp3raSjbDN5y854=
Content-Language: en-US
 by: Jinsong Zhao - Thu, 17 Feb 2022 09:44 UTC

Hi there,

I am not very familiar with the type system of Common Lisp. Here is a
snippet of Pascal code to declare the type of a variable. How to do the
same thing in Common Lisp?

Code in Pascal:
type ASCIIcode=0..255;
xord:array[char]of ASCIIcode;
xchr:array[ASCIIcode]of char;

for me, the type ASCIIcode is a subrange of the integers. And xord and
xchr are two array with the type of ASCIIcode and char.

I don't know how to translate them into Common Lisp in a elegant style.

Thanks a lot in advance.

Best,
Jinsong

Re: how to declare the type of a variable

<87k0dtyca2.fsf@axel-reichert.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16879&group=comp.lang.lisp#16879

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: mail@axel-reichert.de (Axel Reichert)
Newsgroups: comp.lang.lisp
Subject: Re: how to declare the type of a variable
Date: Thu, 17 Feb 2022 12:15:33 +0100
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <87k0dtyca2.fsf@axel-reichert.de>
References: <sul5dl$ruc$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="89f5b416d46a43727d7cfc508f08328a";
logging-data="5925"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Ay1Ow6CnCSJYWYvsrAtxO5c1SrL3RWH8="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:5ydO0riU1Lyq5xwym3IsG7qS4tw=
sha1:ypUejEP5v/WT2Brk2qIK/J6CdaA=
 by: Axel Reichert - Thu, 17 Feb 2022 11:15 UTC

Jinsong Zhao <jszhao@yeah.net> writes:

> I am not very familiar with the type system of Common Lisp.

Searching the web for

"common lisp" cookbook types

should list a page in top position that will get you started.

Axel

Re: how to declare the type of a variable

<9og=695OOes3naf9+@bongo-ra.co>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16883&group=comp.lang.lisp#16883

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: spibou@gmail.com (Spiros Bousbouras)
Newsgroups: comp.lang.lisp
Subject: Re: how to declare the type of a variable
Date: Thu, 17 Feb 2022 17:49:45 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 94
Message-ID: <9og=695OOes3naf9+@bongo-ra.co>
References: <sul5dl$ruc$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 17 Feb 2022 17:49:45 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="d15bc0604173a459718f2328ad5e1426";
logging-data="27679"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19D3a5i0nvqsWURXYdqk/YW"
Cancel-Lock: sha1:raYDIE9wODlUjX5pfjepgNQz0bg=
In-Reply-To: <sul5dl$ruc$1@dont-email.me>
X-Organisation: Weyland-Yutani
X-Server-Commands: nowebcancel
 by: Spiros Bousbouras - Thu, 17 Feb 2022 17:49 UTC

On Thu, 17 Feb 2022 17:44:21 +0800
Jinsong Zhao <jszhao@yeah.net> wrote:
> Hi there,
>
> I am not very familiar with the type system of Common Lisp. Here is a
> snippet of Pascal code to declare the type of a variable. How to do the
> same thing in Common Lisp?

Pascal is statically typed whereas CL is dynamically typed. Furthermore
in CL if you declare an object to be of some type and then you assign
to it a value not of that type , it's undefined behaviour. So it is
uncertain what your expectations are when you say "same thing". But to
get you started...

> Code in Pascal:
> type ASCIIcode=0..255;

(deftype ASCIIcode () '(integer 0 255))
(deftype ASCIIcode2 () '(unsigned-byte 8))

Either one of these describe the same thing.

(let ((a 12))
(declare (type ASCIIcode a))
a)

Returns 12.

(let ((a 256))
(declare (type ASCIIcode a))
a)

Undefined behaviour. SBCL signals a condition , clisp returns 256. Both
behaviours are conforming as indeed any behaviour would be.

(let ((a 12))
(declare (type ASCIIcode2 a))
a)

Returns 12.

(let ((a 256))
(declare (type ASCIIcode2 a))
a)

Undefined behaviour. SBCL signals a condition , clisp returns 256.

> xord:array[char]of ASCIIcode;
> xchr:array[ASCIIcode]of char;

I am guessing that the second declaration says that xchr is a vector
[arrays of dimension 1 are called vectors in CL] where the index is
of type ASCIIcode [so takes values from 0 to 255] and each element
is of type char .The best approximation I could come up with which
works on SBCL is

(let ((vec (make-sequence 'vector 256 :initial-element #\a)))
(declare (type (vector t 256) vec)) ; This is the declaration you want
vec)

or

(let ((vec (coerce (make-sequence 'vector 256 :initial-element #\a)
'(vector character))))
(declare (type (vector character 256) vec)) ; This is the
; declaration you want
vec)

..But

(let ((vec (make-sequence 'vector 256 :initial-element #\a)))
(declare (type (vector character 256) vec))
vec)

causes SBCL to complain

The value #(#\a #\a #\a #\a #\a #\a #\a #\a #\a #\a #\a #\a ...)
is not of type
(VECTOR CHARACTER 256).

which puzzles me. But hopefully I have given you enough to get you started.

xord is a vector where each element is of type ASCIIcode but what
are the possible indexes ?

> for me, the type ASCIIcode is a subrange of the integers. And xord and
> xchr are two array with the type of ASCIIcode and char.

Not clear enough.

> I don't know how to translate them into Common Lisp in a elegant style.

--
vlaho.ninja/prog

Re: how to declare the type of a variable

<aada22fa-8754-4f53-8138-6a70df6187f6n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16887&group=comp.lang.lisp#16887

  copy link   Newsgroups: comp.lang.lisp
X-Received: by 2002:a05:600c:35d1:b0:37c:d45c:af57 with SMTP id r17-20020a05600c35d100b0037cd45caf57mr7483091wmq.149.1645125941241;
Thu, 17 Feb 2022 11:25:41 -0800 (PST)
X-Received: by 2002:a9d:da4:0:b0:5a1:8740:f395 with SMTP id
33-20020a9d0da4000000b005a18740f395mr1419134ots.46.1645125940136; Thu, 17 Feb
2022 11:25:40 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.128.88.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.lisp
Date: Thu, 17 Feb 2022 11:25:39 -0800 (PST)
In-Reply-To: <9og=695OOes3naf9+@bongo-ra.co>
Injection-Info: google-groups.googlegroups.com; posting-host=2603:8000:7603:1703:c8fe:7a0:e6bc:de59;
posting-account=05zmAwoAAAAJZM-3jv1hCWLHGZQceqwA
NNTP-Posting-Host: 2603:8000:7603:1703:c8fe:7a0:e6bc:de59
References: <sul5dl$ruc$1@dont-email.me> <9og=695OOes3naf9+@bongo-ra.co>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <aada22fa-8754-4f53-8138-6a70df6187f6n@googlegroups.com>
Subject: Re: how to declare the type of a variable
From: taruss@google.com (Tom Russ)
Injection-Date: Thu, 17 Feb 2022 19:25:41 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Tom Russ - Thu, 17 Feb 2022 19:25 UTC

On Thursday, February 17, 2022 at 9:49:52 AM UTC-8, Spiros Bousbouras wrote:
> On Thu, 17 Feb 2022 17:44:21 +0800
> Jinsong Zhao <jsz...@yeah.net> wrote:
> > Hi there,
> >
> > I am not very familiar with the type system of Common Lisp. Here is a
> > snippet of Pascal code to declare the type of a variable. How to do the
> > same thing in Common Lisp?
> Pascal is statically typed whereas CL is dynamically typed. Furthermore
> in CL if you declare an object to be of some type and then you assign
> to it a value not of that type , it's undefined behaviour. So it is
> uncertain what your expectations are when you say "same thing". But to
> get you started...
> [Snip]

One of the big differences between CL and most other typed languages is
that the declarations of types
(declare (type x (integer 0 255)))
is a *promise* to the compiler. You as the programmer are promising that
the variable X will have the indicated type. This is something that the compiler
is *not* required to check and is also *not* required to generate a run-time
error.

If you want to ensure that a variable has a particular type, you need to use
the CHECK-TYPE macro:
(check-type x (integer 0 255))
which will throw an error if the type check fails.

Re: how to declare the type of a variable

<sun2dg$9cb$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16889&group=comp.lang.lisp#16889

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jszhao@yeah.net (Jinsong Zhao)
Newsgroups: comp.lang.lisp
Subject: Re: how to declare the type of a variable
Date: Fri, 18 Feb 2022 11:05:16 +0800
Organization: A noiseless patient Spider
Lines: 16
Message-ID: <sun2dg$9cb$1@dont-email.me>
References: <sul5dl$ruc$1@dont-email.me> <87k0dtyca2.fsf@axel-reichert.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 18 Feb 2022 03:05:20 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="e0793d3f6760be7dfc4a5c2cfb36d4e1";
logging-data="9611"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18PqoTMSr56giRRmxQKpsMS"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.6.0
Cancel-Lock: sha1:mtUyaZqA92rQ+Z5mh/KJItWX2Cs=
In-Reply-To: <87k0dtyca2.fsf@axel-reichert.de>
Content-Language: en-US
 by: Jinsong Zhao - Fri, 18 Feb 2022 03:05 UTC

On 2022/2/17 19:15, Axel Reichert wrote:
> Jinsong Zhao <jszhao@yeah.net> writes:
>
>> I am not very familiar with the type system of Common Lisp.
>
> Searching the web for
>
> "common lisp" cookbook types
>
> should list a page in top position that will get you started.
>
> Axel

Thanks for the hint. I got the start point at:
https://lispcookbook.github.io/cl-cookbook/type.html

Re: how to declare the type of a variable

<sun2rg$fme$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16890&group=comp.lang.lisp#16890

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jszhao@yeah.net (Jinsong Zhao)
Newsgroups: comp.lang.lisp
Subject: Re: how to declare the type of a variable
Date: Fri, 18 Feb 2022 11:12:44 +0800
Organization: A noiseless patient Spider
Lines: 113
Message-ID: <sun2rg$fme$1@dont-email.me>
References: <sul5dl$ruc$1@dont-email.me> <9og=695OOes3naf9+@bongo-ra.co>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 18 Feb 2022 03:12:48 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="e0793d3f6760be7dfc4a5c2cfb36d4e1";
logging-data="16078"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Sjrwc8z2u6I0g2X8t15eD"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.6.0
Cancel-Lock: sha1:GXj680TBidoTlriZ62Dlv3RIS1c=
In-Reply-To: <9og=695OOes3naf9+@bongo-ra.co>
Content-Language: en-US
 by: Jinsong Zhao - Fri, 18 Feb 2022 03:12 UTC

On 2022/2/18 1:49, Spiros Bousbouras wrote:
> On Thu, 17 Feb 2022 17:44:21 +0800
> Jinsong Zhao <jszhao@yeah.net> wrote:
>> Hi there,
>>
>> I am not very familiar with the type system of Common Lisp. Here is a
>> snippet of Pascal code to declare the type of a variable. How to do the
>> same thing in Common Lisp?
>
> Pascal is statically typed whereas CL is dynamically typed. Furthermore
> in CL if you declare an object to be of some type and then you assign
> to it a value not of that type , it's undefined behaviour. So it is
> uncertain what your expectations are when you say "same thing". But to
> get you started...
>
>> Code in Pascal:
>> type ASCIIcode=0..255;
>
> (deftype ASCIIcode () '(integer 0 255))
> (deftype ASCIIcode2 () '(unsigned-byte 8))
>
> Either one of these describe the same thing.
>
> (let ((a 12))
> (declare (type ASCIIcode a))
> a)
>
> Returns 12.
>
> (let ((a 256))
> (declare (type ASCIIcode a))
> a)
>
> Undefined behaviour. SBCL signals a condition , clisp returns 256. Both
> behaviours are conforming as indeed any behaviour would be.
>
> (let ((a 12))
> (declare (type ASCIIcode2 a))
> a)
>
> Returns 12.
>
> (let ((a 256))
> (declare (type ASCIIcode2 a))
> a)
>
> Undefined behaviour. SBCL signals a condition , clisp returns 256.
>
>> xord:array[char]of ASCIIcode;
>> xchr:array[ASCIIcode]of char;
>
> I am guessing that the second declaration says that xchr is a vector
> [arrays of dimension 1 are called vectors in CL] where the index is
> of type ASCIIcode [so takes values from 0 to 255] and each element
> is of type char .The best approximation I could come up with which
> works on SBCL is
>
> (let ((vec (make-sequence 'vector 256 :initial-element #\a)))
> (declare (type (vector t 256) vec)) ; This is the declaration you want
> vec)
>
> or
>
> (let ((vec (coerce (make-sequence 'vector 256 :initial-element #\a)
> '(vector character))))
> (declare (type (vector character 256) vec)) ; This is the
> ; declaration you want
> vec)
>

Thanks a lot for the above demo codes and explanation. It helps.

code:
(coerce (make-sequence 'vector 256 :initial-element #\a)
'(vector character))
could be simplified to:
(make-sequence '(vector character) 256 :initial-element #\a)

> .But
>
> (let ((vec (make-sequence 'vector 256 :initial-element #\a)))
> (declare (type (vector character 256) vec))
> vec)
>
> causes SBCL to complain
>
> The value #(#\a #\a #\a #\a #\a #\a #\a #\a #\a #\a #\a #\a ...)
> is not of type
> (VECTOR CHARACTER 256).
>
> which puzzles me. But hopefully I have given you enough to get you started.

I asked on sbcl-help mailing list, the explanation is:

On 2022/2/18 10:41, Douglas Katzman wrote:
Asking whether an object is (vector character) is asking whether that
vector can hold characters and NOTHING BUT characters.
The syntax #(obj1 obj2 obj3) creates a general vector, technically a
(vector t), which is not a vector that can hold /only/ characters.

> xord is a vector where each element is of type ASCIIcode but what
> are the possible indexes ?
>
That also confused me. I do not know how Pascal deal with it.

>> for me, the type ASCIIcode is a subrange of the integers. And xord and
>> xchr are two array with the type of ASCIIcode and char.
>
> Not clear enough.
>
>> I don't know how to translate them into Common Lisp in a elegant style.
>


devel / comp.lang.lisp / how to declare the type of a variable

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor