User loginNavigation |
return-type polymorphism of monads done right in a dynamic languageWhile monads can be encoded in dynamically-typed language, the usual encodings are usually uglier than monads in type-class-based language because type inference is not there to help determine which monad instance we're talking about. Some use-cases of type inference can be replaced by dynamic type dispatch, but not the so-called "return type polymorphism" where it is the type expected by the context that matters, not the type carried by values. Monads are just one instance where return-type polymorphism matters (in the form of "return : forall a, a -> m a"), but this is a general problem with other structures (typically structures with a default element such as monoids/groups). Intuitively it seems that this problem would be solveable by keeping, in the dynamic type representation, a "not known yet" marker, and using the free monad operations in this case. But there is a gap from intuition from usable code, and Tony Garnock-Jones just wrote a very nice blog post exposing this technique in Racket: Monads in Dynamically-Typed Languages (run-io (do (mdisplay "Enter a number: ") n <- mread all-n <- (return (for/list [(i n)] i)) evens <- (return (do i <- all-n #:guard (even? i) (return i))) (return evens))) By gasche at 2015-01-26 10:14 | LtU Forum | previous forum topic | next forum topic | other blogs | 4903 reads
|
Browse archives
Active forum topics |
Recent comments
23 weeks 2 days ago
23 weeks 2 days ago
23 weeks 2 days ago
45 weeks 3 days ago
49 weeks 5 days ago
51 weeks 2 days ago
51 weeks 2 days ago
1 year 1 week ago
1 year 6 weeks ago
1 year 6 weeks ago