chicken-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-hackers] strange csc-warning (Version 4.13.0)


From: John Cowan
Subject: Re: [Chicken-hackers] strange csc-warning (Version 4.13.0)
Date: Sat, 30 Jun 2018 13:48:24 -0400

I've reformatted your code to Lisp standard style (2 spaces instead of tabs, no parens on their own lines) for better readability.

(define strange-warning (lambda (sym options)
  (let ((el (assq sym options)))
    (if (pair? el)
      (if (list? el)
        "list"
        (cdr el)) ; <== strange warning here
      "found nothing"))))

While this code is hard to understand, the warning is incorrect.  In particular, if el => (a . b), then it is a pair but not a list, and the (cdr el) branch will be taken correctly, returning b.  This is a minor compiler bug on at least two levels: no warning should be produced, and the reference to "false" is incorrect.  You should file a bug at bugs.callcc.org.

I would say that discriminating between pairs and lists this way is a bad idea: you should always use lists with a car that distinguishes the two types of values you want to treat differently.  In particular, pair? is O(1) whereas list? is O(n) in the length of the list.  There's such a thing as carrying dynamic typing too far.

(define no-warning (lambda (sym options)
  (let ((el (assq sym options)))
    (if (list? el)
      "list"
      (if (pair? el)
        (cdr el) ; <== no warning here
        "found nothing")))))

No warning is to be expected.  Again, if el => (a . b), it will fail the list? test but pass the pair? test, and taking its cdr will produce b.  However, this code is *not* semantically identical with strange-warning.  If el is (), then it is a list but not a pair, and this procedure will return "list", whereas strange-warning will return "found nothing".

(define also-no-warning (lambda (sym options)
  (define el (assq sym options))
  (if (pair? el)
    (if (list? el)
      "list"
      (cdr el)) ; <== also no warning here
    "found nothing")))

Again, no warning is to be expected.  The fact that strange-warning triggers a warning but also-no-warning does not is very mysterious.



On Sat, Jun 30, 2018 at 5:18 AM, Martin Schneeweis <address@hidden> wrote:
Hi,

the attached file contains 3 procedures

  a) strange-warning
  b) no-warning
  c) also-no-warning

(a) and (b) should be semantically identical - just the if's are
rearranged.

"csc strange-warning.scm" produces a type-warning for (a) but not for
(b):

  ...call to `cdr', expected argument #1 of type `pair' but was given
  an argument of type `false'...

(c) has the same if-arrangement as (a) - but the let-form is replaced
with a define-form.

If you are wondering: i reduced my original procedure to the minimum -
the original version is a procedure that returns the value for a key in
an association list using either "cdr" or "cadr" depending on the type
of the assq-return-value.

lg
Martin

----
chicken -version
(c) 2008-2017, The CHICKEN Team
(c) 2000-2007, Felix L. Winkelmann
Version 4.13.0 (rev 68eeaaef)
linux-unix-gnu-x86-64 [ 64bit manyargs dload ptables ]
compiled 2017-12-11 on yves.more-magic.net (Linux)

_______________________________________________
Chicken-hackers mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


reply via email to

[Prev in Thread] Current Thread [Next in Thread]