# How hard would it be to get signatures for 2htdp/image?

**URL:** https://racket.discourse.group/t/how-hard-would-it-be-to-get-signatures-for-2htdp-image/3309
**Category:** Questions & Answers
**Created:** [November 11, 2024, 3:35pm UTC](https://racket.discourse.group/t/how-hard-would-it-be-to-get-signatures-for-2htdp-image/3309 "2024-11-11T15:35:40Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![ToddOBryan](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/toddobryan/32/1962_2.png) [@ToddOBryan](https://racket.discourse.group/u/ToddOBryan)
#### Post date: [November 11, 2024, 3:35pm UTC](https://racket.discourse.group/t/how-hard-would-it-be-to-get-signatures-for-2htdp-image/3309/1 "2024-11-11T15:35:40Z")

</div>

Given that Beginning Student has support for signatures, how hard would it be to allow Image, Color, etc as types so that signatures could be used with image functions?

---

<div class="post-metadata">

### Author: ![EmEf](https://avatars.discourse-cdn.com/v4/letter/e/53a042/32.png) [@EmEf](https://racket.discourse.group/u/EmEf)
#### Post date: [November 11, 2024, 3:52pm UTC](https://racket.discourse.group/t/how-hard-would-it-be-to-get-signatures-for-2htdp-image/3309/2 "2024-11-11T15:52:12Z")

</div>

On Nov 11, 2024, at 10:45 AM, Todd O'Bryan via Racket Discourse asked “[g]iven that Beginning Student has support for signatures, 'whether it would hard] to allow Image, Color, etc as types so that signatures could be used with image functions.”

The answer is sadly simple:

```
— labor, lots of 
— organizing an appropriate teachpack for inclusion with HtDP. 

```

The current language is somewhat impoverished for the “image aspect” of our teaching world, but it comes with all the ingredients for getting this right.

(Having said this, I doubt I would use checked signatures in a first course of my design. This course is overloaded and having checked signatures distracts from the major teaching goal of “good program design demands mental discipline.” My guess is that this will become even more important when AI takes over the production of 20-50% of our code, because reading code and spotting problems requires even more of the mental discipline than programming in a type-driven, but unchecked context.)

---

<div class="post-metadata">

### Author: ![ToddOBryan](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/toddobryan/32/1962_2.png) [@ToddOBryan](https://racket.discourse.group/u/ToddOBryan)
#### Post date: [November 11, 2024, 6:20pm UTC](https://racket.discourse.group/t/how-hard-would-it-be-to-get-signatures-for-2htdp-image/3309/3 "2024-11-11T18:20:20Z")

</div>

I'm willing to provide a lot of labor if someone can point me in the correct direction.

I appreciate the idea that we encourage mental discipline, but Javascript is being slowly supplanted by Typescript and Python3's optional typing allows type-checking. I'm dealing with a lot of muddied thinking that doesn't get caught until I can grade student code, at which point it may be entrenched.

I have students typing contracts like:

`;; my-function: size text color -> image`

because they don't really understand what a "type" is. Being able to require

`(: Number String Color -> Image)`

and have an error appear if they try

`(: Size Text Color -> Image)`

would be a godsend for me. I mean, students only learn from feedback, and if the contracts are just comments, then the only feedback they're going to get is from me. I don't want them to have to wait that long.

Honestly, the type checking prior to running is icing on the cake. I'd he happy with contracts in code that weren't checked until runtime, but I need some way to allow students to self-check their understanding of types.

---

<div class="post-metadata">

### Author: ![ToddOBryan](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/toddobryan/32/1962_2.png) [@ToddOBryan](https://racket.discourse.group/u/ToddOBryan)
#### Post date: [November 11, 2024, 6:38pm UTC](https://racket.discourse.group/t/how-hard-would-it-be-to-get-signatures-for-2htdp-image/3309/4 "2024-11-11T18:38:58Z")

</div>

I think I could add types to `2htdp/image` as easily as

```scheme
(define-type StringMode (U "solid" "outline"))
(define-type SymbolMode (U 'solid 'outline))
(define-type AlphaMode Byte)

(define-type Mode (U StringMode SymbolMode AlphaMode))

```

And then do something similar for `YPlace`, `XPlace`, etc., and add signatures for all the functions provided.

`(define-type Color ...)` would be a bit of a pain, but as long as the type system doesn't mind unions with a lot of literal values, it shouldn't be a problem.

I'm not sure how you'd `(define-type Image ...)` except that images are instances of `bitmap%` or `image-snip%`, so hopefully there's a way to declare a type as a union of classes or something.

At any rate, I'm happy to take a shot at this.

---

<div class="post-metadata">

### Author: ![EmEf](https://avatars.discourse-cdn.com/v4/letter/e/53a042/32.png) [@EmEf](https://racket.discourse.group/u/EmEf)
#### Post date: [November 11, 2024, 6:54pm UTC](https://racket.discourse.group/t/how-hard-would-it-be-to-get-signatures-for-2htdp-image/3309/5 "2024-11-11T18:54:28Z")

</div>

Checked signatures are _not_ types.

You will need to read up on Mike S.’s signature docs and build base signatures from there. Then combine them and equip some of the functions in 2/image and 2/universe with such signatures.

I recommend putting it all in a separate file for now and telling students to require this file, after you have written solutions with these things to the problems you assign, We can work on turning this file into a teachpack later.

---

<div class="post-metadata">

### Author: ![mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@mike](https://racket.discourse.group/u/mike)
#### Post date: [November 13, 2024, 12:29pm UTC](https://racket.discourse.group/t/how-hard-would-it-be-to-get-signatures-for-2htdp-image/3309/6 "2024-11-13T12:29:12Z")

</div>

It's probably mostly a matter of copying & uppercasing the names here:

> <https://github.com/racket/deinprogramm/blob/master/deinprogramm-lib/deinprogramm/sdp/image.rkt>

---

<div class="post-metadata">

### Author: ![ToddOBryan](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/toddobryan/32/1962_2.png) [@ToddOBryan](https://racket.discourse.group/u/ToddOBryan)
#### Post date: [November 19, 2024, 6:21pm UTC](https://racket.discourse.group/t/how-hard-would-it-be-to-get-signatures-for-2htdp-image/3309/7 "2024-11-19T18:21:20Z")

</div>

Thank you, Mike! I think I almost have it working the way I'd like. The issue is `color` vs `image-color`, where I'd prefer `'blue`, `"blue"`, and `(make-color 0 0 255)` to all satisfy the signature `Color`, if possible.

Matthias mentioned that I should read up. Is there an article or something I could read?

In particular, can you explain what `signature/arbitrary` does? I've changed

```scheme
(define Mode (signature/arbitrary
              (quickcheck:arbitrary-one-of string=? "solid" "outline")
              mode (predicate mode?)))

```

the first `Mode` to upper-case, but not the one in the last line. It seems to work, but I can't figure out what the `mode` before `(predicate mode?)` is doing.

It looks like if I have students use uppercase names for structs that I'll get the signatures for free. As in

```scheme
(define-struct Student (name grade))

```

gives me `Student` for signatures, `Student?` as a predicate, and `Student-name` and `Student-grade` as selectors.

Is adding `(: make-Student (String Number -> Student))` the right way to specify what types the fields are expected to be?

And two more questions:

1. Is there an easy way to do signatures for unions? Those cases that HtDP used to define like:

```scheme
;; A posn-to-draw is:
;; number (x-value, with y-value assumed to be 0), or
;; posn

```

1. I added a signature for the `2htdp/image` `circle` function, but it looks like the contract gets checked before the signature, so even if we added signatures for all the functions, it wouldn't make a difference in terms of what students would get as feedback. Even if I do this,

```scheme
(: circle2 (Number Mode Color -> Image))
(define (circle2 r m c) (circle r m c))

```

and call `(circle2 "solid" 20 "blue")` the error I get is from the contract `"circle: expects a non negative real number as first argument, given "solid" "`, not the signature violation.

Any way to add signatures that will actually do anything?

---

<div class="post-metadata">

### Author: ![ToddOBryan](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/toddobryan/32/1962_2.png) [@ToddOBryan](https://racket.discourse.group/u/ToddOBryan)
#### Post date: [November 28, 2024, 12:16am UTC](https://racket.discourse.group/t/how-hard-would-it-be-to-get-signatures-for-2htdp-image/3309/8 "2024-11-28T00:16:18Z")

</div>

@mike Not sure if you've seen this, but just wondering if you have any ideas.

---

<div class="post-metadata">

### Author: ![maueroats](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@maueroats](https://racket.discourse.group/u/maueroats)
#### Post date: [November 30, 2024, 3:08pm UTC](https://racket.discourse.group/t/how-hard-would-it-be-to-get-signatures-for-2htdp-image/3309/9 "2024-11-30T15:08:09Z")

</div>

> [@ToddOBryan](#):
>
> I added a signature for the `2htdp/image` `circle` function, but it looks like the contract gets checked before the signature, so even if we added signatures for all the functions, it wouldn't make a difference in terms of what students would get as feedback.  
> [...]  
> Any way to add signatures that will actually do anything?

When I tried to reproduce your results using BSL with simple numbers (Racket 8.15), I got:

1. error from bad signature; and ALSO
2. a testing error window explaining that there were a signature violations, listing line and value

That is, the signature violations are noted but execution is attempted anyway. That is definitely what you want for beginners.

The contract error probably stops the rest of the code from running, but the signature error won't.

Is your setup different from this?

Sample code:

```scheme
(define (xerror a b)
  -1)
(: f (Number -> Number))
(define (f x)
  (if (number? x)
      (* x x)
      (error 'f "input should be a number"))) ;; change to xerror to observe signature violations do not stop later code from running
(f 5)
(f "Bad")
(f "Does not run")

(check-expect (f 5) 25)
(check-expect (f 10) 100)

```

---

<div class="post-metadata">

### Author: ![mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@mike](https://racket.discourse.group/u/mike)
#### Post date: [March 27, 2026, 12:38pm UTC](https://racket.discourse.group/t/how-hard-would-it-be-to-get-signatures-for-2htdp-image/3309/10 "2026-03-27T12:38:36Z")

</div>

I am very sorry for not seeing your post until now. In case it's still relevant:

- The second operand `signature/arbitrary` is simply a name for printing the resulting signature.
- Yes, `(: make-Student (String Number -> Student))` is good.

1. You can do unions via `mixed`, i.e. `(define posn-to-drawn (signature (mixed number Posn)))`
2. Signature violations get reported along with test failures. So you need to get to that reporting to see them. I'm unsure of the context to your call to `circle2` - in a regular BSL program, say, you should see the signature violation.

---

<div class="post-metadata">

### Author: ![LiberalArtist](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/liberalartist/32/151_2.png) [@LiberalArtist](https://racket.discourse.group/u/LiberalArtist)
#### Post date: [March 27, 2026, 1:48pm UTC](https://racket.discourse.group/t/how-hard-would-it-be-to-get-signatures-for-2htdp-image/3309/11 "2026-03-27T13:48:12Z")

</div>

> [@ToddOBryan](#):
>
> I'm dealing with a lot of muddied thinking that doesn't get caught until I can grade student code, at which point it may be entrenched.
> 
> I have students typing contracts like:
> 
> `;; my-function: size text color -> image`
> 
> because they don't really understand what a "type" is. Being able to require
> 
> `(: Number String Color -> Image)`
> 
> …
> 
> would be a godsend for me.
> 
> …
> 
> I need some way to allow students to self-check their understanding of types.

(Caveat: I’ve never used the checked signature support in the teaching languages, and I have no experience in computer science pedagogy, so disregard this if I’m missing the point.)

To me, the comment contract actually seems to reflect a more precise understanding of types than the signature code expresses.

A `size` will certainly be a `Number`, but in \*SL (and Racket and Scheme), numbers include `-1/3+5i`, which `size` communicates is not appropriate.

Similarly, `text` communicates a more subtle distinction between the two `String`s in a function call like `(my-function 5 "Hello, world!" "blue")`: some strings are meant for humans, others for machines. In this sense, every time some UI displays `cats &amp; dogs` with a double-encoded ampersand, there has been a type error.

Obviously, the extent to which a given type system can represent and/or enforce these distinctions—statically, dynamically, or not at all—will vary widely. Sometimes, one might want to use features like [`define-new-subtype`](https://docs.racket-lang.org/ts-reference/Experimental_Features.html#%28form._%28%28lib._typed-racket%2Fbase-env%2Fprims..rkt%29._define-new-subtype%29%29) or Haskell’s `newtype`, if available, or to introduce an extra layer of wrapping. But the discipline of thinking through, and writing down in a comment, when you expect a `size` rather than just any `Number` is _especially_ valuable when you must work without support from the language’s type system for making that distinction.

Nonetheless, this aspect is definitely a challenge:

> [@ToddOBryan](#):
>
> … if the contracts are just comments, then the only feedback they're going to get is from me.

---

<div class="post-metadata">

### Author: ![mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@mike](https://racket.discourse.group/u/mike)
#### Post date: [March 29, 2026, 10:28am UTC](https://racket.discourse.group/t/how-hard-would-it-be-to-get-signatures-for-2htdp-image/3309/12 "2026-03-29T10:28:54Z")

</div>

I agree with you. Signatures are regular values, so you can define aliases like:

```scheme
(define Size (signature Number))

```

... to express intent more clearly. Does that help?
