Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redo struct signatures #83

Merged
merged 2 commits into from
Dec 28, 2020
Merged

Conversation

mikesperber
Copy link
Member

This is from a discussion Matthias Felleisen and I had at ICFP 2019:

The previous version of the signature support had as the name of the
signature the name of the struct, which made for confusing error
messages.

Now, camelcase the struct name: dillo -> Dillo, foo-bar -> FooBar.

This is in line with HtDP's conventions, I think. Also, primitive
signatures start with an upper-case letter.

The signature constructors append an "Of": DilloOf, FooBarOf etc.

@mikesperber
Copy link
Member Author

Here's some code that uses it:

; Ordinary structs
(define-struct dillo (alive? weight))

(: make-dillo (Boolean Integer -> Dillo))
(: dillo? (Any -> Boolean))
(: dillo-alive? (Dillo -> Boolean))
(: dillo-weight (Dillo -> Integer))

(: run-over-dillo (Dillo -> Dillo))

; almost works, but doesn't because:
; - no parametric signatures for mutable structs
; - all structs are mutable in Advanced student
#;(check-property
 (for-all ((dillo (DilloOf Boolean Integer)))
   (not (dillo-alive? (run-over-dillo dillo)))))

; Note we can't just write Dillo instead of (DilloOf ...) as the
; signatures of the fields aren't specified.

(define (run-over-dillo dillo)
  (make-dillo #false
              (dillo-weight dillo)))


; Lists

(define-struct empty-list ())

(define nil (make-empty-list))

(define-struct pair (car cdr))

(: make-pair (%a (ListOf %a) -> (PairOf %a (ListOf %a))))
(: pair-car ((PairOf %a (ListOf %a)) -> %a))
(: pair-cdr ((PairOf %a (ListOf %a)) -> (ListOf %a)))

(define (ListOf a)
  (signature
   (mixed EmptyList
          (PairOf a (ListOf a)))))

(define lis1
  (make-pair 5 (make-pair 7 (make-pair 3 nil))))

(: list-sum ((ListOf Number) -> Number))

(check-expect (list-sum lis1) 15)

(define (list-sum lis)
  (cond
    ((empty-list? lis) 0)
    ((pair? lis)
     (+ (pair-car lis)
        (list-sum (pair-cdr lis))))))

#|
I would really like:

- signatures in *BSL, not just ASL

- inline struct signatures:

(define-struct dillo
   ([alive? Boolean]
    [weight Integer]))

   Then we could also do:

(check-property
 (for-all ((dillo Dillo))
   (not (dillo-alive? (run-over-dillo dillo)))))
 
- combine that with parametric signatures

(define-struct (pair a)
  ([car a]
   [cdr (ListOf a)]))

  We could then replace (PairOf x (ListOf x)) by (PairOf x)

|#

@mikesperber
Copy link
Member Author

Tests and documentation forthcoming, once there is agreement on the design.

@mfelleisen
Copy link
Contributor

I don’t quite understand this part:

"I would really like:

  • signatures in *BSL, not just ASL

  • inline struct signatures:”

The first one is something you can re-enable. It used to work, right?

The second one would mean a major change to the syntax of structs, even if we make these field specs optional. Students could stumble into this notation and get rather confusing error messages.

@mikesperber
Copy link
Member Author

@mfelleisen I believe it's always been the case that they were only available in ASL, at your request. But indeed, they would be easy to turn on in all the *SL.

As to inline struct signatures: I agree with your assessment. We introduced this in SdP, but that has new language levels and a new keyword. Maybe that's the way to do it?

@mfelleisen
Copy link
Contributor

mfelleisen commented Jan 13, 2020 via email

@samth
Copy link
Member

samth commented Nov 2, 2020

What's the status here?

@mfelleisen
Copy link
Contributor

mfelleisen commented Nov 2, 2020 via email

@mikesperber
Copy link
Member Author

@samth Adding tests for that is next on my list, then it should be ready to merge.

@mikesperber mikesperber force-pushed the struct-signatures branch 3 times, most recently from 9351a59 to 1cff04b Compare November 9, 2020 11:28
@mikesperber
Copy link
Member Author

Tests are there now.

@mikesperber
Copy link
Member Author

Documentation is there.

@samth @mfelleisen Could you take a look and let me know what's missing?

This is from a discussion Matthias Felleisen and I had at ICFP 2019:

The previous version of the signature support had as the name of the
signature the name of the struct, which made for confusing error
messages.

Now, camelcase the struct name: dillo -> Dillo, foo-bar -> FooBar.

This is in line with HtDP's conventions, I think.  Also, primitive
signatures start with an upper-case letter.

The signature constructors append an "Of": DilloOf, FooBarOf etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants