How to define Union polymorphic data structure's instance in Typed Racket? -


from typed racket guide, define union type, use (define-type some-type (u type1 type2)).

to define polymorphic data structures, use (define-type (opt a) (u ...)).

i want define polymorphic binary tree

(define-type (tree a) (u (leaf a) node)) (struct (a) leaf ([val : a])) (struct node ([left : tree] [right : tree])) (define t1 (leaf 5)) (define t2 (leaf 8)) (define t3 (node t1 t2)) 

i wondering why type of t1 leaf not tree, , how make tree ?

> t1 - : (leaf positive-byte) #<leaf> 

when this:

(define-type (tree a) (u (leaf a) node)) 

you're defining tree type constructor. shouldn't think of tree type, (tree some-concrete-type) type. rename treeof:

(define-type (treeof a) (u (leaf a) node)) (struct (a) leaf ([val : a])) (struct node ([left : treeof] [right : treeof])) 

now problem clearer. node struct expects treeof, tree of what? want this:

(define-type (treeof a) (u (leaf a) (node a))) (struct (a) leaf ([val : a])) (struct (a) node ([left : (treeof a)] [right : (treeof a)])) 

now example works:

#lang typed/racket (define-type (treeof a) (u (leaf a) (node a))) (struct (a) leaf ([val : a])) (struct (a) node ([left : (treeof a)] [right : (treeof a)])) (define t1 (leaf 5)) (define t2 (leaf 8)) (define t3 (node t1 t2)) 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -