On this page:
7.1 Introduction
«structure»
«expand-to-tagged»
«structure?»
7.2 Defining untagged structures with define-structure
«define-structure»
7.3 Implementation of Structure  Top and Structure  Top?
«StructureTop?»
«StructureTop»
7.4 Supertypes for structures
«structure-supertype»
«expand-to-tagged-supertype»
7.5 Putting it all together
«*»
7.1

7 User API for untagged structures

    7.1 Introduction

    7.2 Defining untagged structures with define-structure

    7.3 Implementation of StructureTop and StructureTop?

    7.4 Supertypes for structures

    7.5 Putting it all together

7.1 Introduction

Untagged structures are implemented exactly like tagged structures, except that they always use the untagged tag name.

(define-multi-id structure
  #:type-expander  «expand-to-tagged»
  #:match-expander «expand-to-tagged»
  #:call           «expand-to-tagged»)

All three cases simply expand to (tagged. (untagged . original-arguments)).

(λ/syntax-case (_ . original-arguments) ()
  (syntax/top-loc stx
    (tagged untagged . original-arguments)))

The structure? predicate is implemented in the same way:

(define-syntax structure?
  (λ/syntax-case (_ . original-arguments) ()
    (syntax/top-loc stx
      (tagged? untagged . original-arguments))))

7.2 Defining untagged structures with define-structure

The define-structure expands to the (define-tagged. (#:tag. (untagged . original-arguments))), which uses define-tagged but forces the tag name to be untagged.

(define-syntax/case (define-structure . original-arguments) ()
  (syntax/top-loc stx
    (define-tagged #:tag untagged . original-arguments)))

7.3 Implementation of StructureTop and StructureTop?

The StructureTop? predicate is defined in terms of tagged-any-fields-predicate:

(define-syntax StructureTop?
  (make-id+call-transformer-delayed
   (λ () (tagged-any-fields-predicate #'untagged))))

Similarly, the StructureTop type is defined using tagged-any-fields-type:

(define-type-expander (StructureTop stx)
  (syntax-case stx ()
    [id
     (identifier? #'id)
     (tagged-any-fields-type #'untagged)]))

7.4 Supertypes for structures

Like the structure and structure? identifiers, structure-supertype is defined in terms of its tagged structure counterpart, tagged-supertype:

(λ/syntax-case (_ . original-arguments) ()
    (syntax/top-loc stx
      (tagged-supertype untagged . original-arguments)))

7.5 Putting it all together

«*» ::=
(require phc-toolkit
         "tagged.hl.rkt"
         "tagged-structure-low-level.hl.rkt"
         "tagged-supertype.hl.rkt"
         multi-id
         type-expander
         (for-syntax racket/base
                     phc-toolkit/untyped))
 
(provide structure
         structure?
         define-structure
         StructureTop
         StructureTop?
         structure-supertype)
 
«structure»
«structure?»
«define-structure»
«StructureTop»
«StructureTop?»
«structure-supertype»