Skip to content

Is it possible to make Mutable Types support subtype? #1128

@NoahStoryM

Description

@NoahStoryM

I tried to write mlength for mlist, but it can't work for (MPairof Natural Null) type:

(: mlength (-> (MListof Natural) Natural))
(define mlength
  (λ (m)
    (if (null? m)
        0
        (add1 (mlength (mcdr m))))))

(: mls (MPairof Natural Null))
(define mls (mcons 9 '()))


(mlength mls)

I can understand it: (MPairof Natural Null) is not subtype of (MListof Natural)
because we can't run (set-mcdr! mls (ann (mcons 8 '()) (MListof Natural))).
But it's legal for mlength to run (set-mcdr! m (ann (mcons 8 '()) (MListof Natural)))
in its body when mls bound to m, which cause conflict.

I wonder if it's possible to make Mutable Types support subtype, and when TR
found conflict, just throw an exception?
(I haven't studied type theory, I just wonder if it's feasible?)

for example:

(: mls (MListof Natural))
(define mls (mcons 9 '()))

(: change-1st! (-> (MListof Any) #:immediately Void))
(define change-1st!
  (λ (mls)
    (unless (null? mls)
      (set-mcar! mls 'sym))))

(displayln mls)
(change-1st! mls)
(displayln mls)

when apply mls to change-1st!, it will record mls's type and value.
If mls's type is changed, then TR throws an exception.
(typecheck happens when the Mutable Type's assign operator was applied if the keyword is #:immediately,
or it happens when the calculation is finished if the keyword is #:end,
or it doesn't happen if the keyword is #:never)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions