Greetings,<br><br>I'm trying to implement a function wrapper, that given any function returns a function of the same type, but that maybe applies a different argument. So far I haven't been successful, and based on previous questions to the mail list it seemed that I needed to go to an untyped module because I couldn't constraint values to a given type.<br>
<br>With this approach I encountered a problem, that originates in the contracts for polymorphic functions, I made a minimal example of my problem below:<br><br>Module1<br>#lang racket<br>; (All (A B) ((A -> (T B)) A -> (T B)))<br>
(define (untyped-wrap fun value)<br> (fun "blabla"))<br><br>(provide untyped-wrap)<br><br>Module2<br>#lang typed/racket<br>(struct: (A) T ([value : A]))<br><br>(require/typed "untyped-wrap.rkt"<br> [untyped-wrap (All (A B) ((A -> (T B)) A -> (T B)))])<br>
<br>(: wrap (All (A B) ((A -> (T B)) A -> (T B))))<br>(define (wrap fun value)<br> (untyped-wrap fun value))<br><br>(: f (Any -> (T One)))<br>(define (f x) (T 1))<br><br>When I, from the typed Module1, evaluate:<br>
<br>(wrap f 1)<br><br>I get the error:<br><br>untyped-wrap: self-contract violation, expected a(n) A16; got: "blabla"<br> contract from: (interface for untyped-wrap), blaming: (interface for untyped-wrap)<br> contract: <br>
(recursive-contract<br> (parametric->/c<br> (A16 B17) .......<br><br>My guesses:<br><br>First I thought that this should work because at the time I apply untyped-wrap it is known that A = Any and B = One. But then I realized that the typesystem can't prove that "blabla" is the right type for all A. <br>
<br> Then I thought about instantiating the polymorphic function this way:<br><br>(: wrap (All (A B) ((A -> (T B)) A -> (T B))))<br>(define (wrap fun value)<br>
( (inst untyped-wrap A B) fun value))<br><br>guessing again that A = Any and B = One should be known<br><br>but it fails with the same error....<br><br>When I write the concrete types it works.<br><br>I've been trying to write code to deal with functions in a general way, while preserving the type, but it seems very hard to do right now in typed/racket :-(<br>
<br>For instance, I had to make an ad-hoc cast going to an untyped module to implement a function with type<br>(All (C D) (EL (C -> (EL D))) -> (EL (C -> (EL D)))), and then I tried the same approach here, but this new error came up<br>
<br>I think it should be useful to provide a 'cast' operation. I think then I could say something like, relying on contracts for checking at runtime:<br><br>Module2<br>#lang typed/racket<br>(struct: (A) T ([value : A]))<br>
<br>(require/typed "untyped-wrap.rkt"<br> [untyped-wrap (Procedure Any -> (T Any))])<br>(: wrap (All (A B) ((A -> (T B)) A -> (T B))))<br>(define (wrap fun value)<br> [CAST (untyped-wrap fun value) (T B)])<br>
<br>(: f (Any -> (T One)))<br>(define (f x) (T 1))<br><br clear="all">any suggestions on how can I achieve the behavior I want??<br><br>Thanks<br><br>-- <br>Ismael<br><br>