How to write higher order functions that accept polymorphic functions as arguments in Typed Racket?

For example, how can I write a version of map that will work with polymorphic functions in Typed Racket? I am using a simple id function defined as:

 (: id : (All (A) A -> A)) (define (id x) x) 

When I try to match it above a list, I get an error message:

 > (map id '(1 2 3)) Type Checker: Polymorphic function `map' could not be applied to arguments: Types: (-> ab ... bc) (Listof a) (Listof b) ... b -> (Listof c) (-> ac) (Pairof a (Listof a)) -> (Pairof c (Listof c)) Arguments: (All (A) (-> AA)) (List One Positive-Byte Positive-Byte) Expected result: AnyValues in: (map id (quote (1 2 3))) 
+5
source share
1 answer

In this case, you need to manually create the polymorphism:

 -> (map (inst identity Integer) '(1 2 3)) - : (Listof Integer) [more precisely: (Pairof Integer (Listof Integer))] '(1 2 3) 

The reason is explained in the Typed Racket Guide here :

The typical Rackets local type inference algorithm is currently not capable of inferring types for polymorphic functions, which are used at higher orders of the arguments, which themselves are polymorphic.

(see documents for further explanation and examples)

+1
source

Source: https://habr.com/ru/post/1204083/


All Articles