User loginNavigation |
archivesreturn-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))) |
Browse archivesActive forum topics |
Recent comments
4 days 4 hours ago
4 days 8 hours ago
4 days 8 hours ago
22 weeks 5 days ago
27 weeks 11 hours ago
28 weeks 4 days ago
28 weeks 4 days ago
31 weeks 2 days ago
35 weeks 6 days ago
35 weeks 6 days ago