[GHC] #8297: Allow implicit parameters to take a default value

#8297: Allow implicit parameters to take a default value ----------------------------------------------+---------------------------- Reporter: schyler | Owner: Type: feature request | Status: new Priority: normal | Milestone: _|_ Component: Compiler | Version: Keywords: | Operating System: Architecture: Unknown/Multiple | Unknown/Multiple Difficulty: Moderate (less than a day) | Type of failure: Blocked By: | None/Unknown Related Tickets: | Test Case: | Blocking: ----------------------------------------------+---------------------------- -XImplicitParams is not that useful except as a type hole. I had an idea where they could be adjusted to be really useful. Consider a function `primes` which uses a wheel sieve. {{{ -- primes: takes a size of the wheel to use, returns a lazy list of primes primes :: Int -> [Int] }}} Your average Joe may not be concerned about the technical details of this function. Why should he have to bother giving a wheel size? The function documentation might say `6` is a good value, but what if some research is done in the future and the library maintainer changes it, later deciding `7` is better? Joe wants to trust the library maintainer, but doesn't want to be burdened by maintaining this arbitrary constant in his code, nor does he like having to pass another parameter to primes. The type of `primes` could be implemented using an implicit parameter like this: {{{ primes -- returns an infinite list of primes primes :: (?wheelSize :: Int) -> [Int] }}} Joe still has to provide a wheelSize however, and now it's even more verbose than before. Wouldn't it be nice if the person who wrote the function could define a default value for `wheelSize`? Joe could use `primes` by itself in his code, whereas Bob, who runs his code on a very powerful server, could tune the performance of his sieve manually by explicitly using `primes where wheelSize = 100`. My suggestion is to develop a syntax which allows functions to define ''default'' values for their implicit parameters. I'm sure the community would have some fun coming up with creative uses for this in their libraries! As an extreme situation, consider the below example; {{{ -- takes a foo, bar, alpha, beta, gamma and epsilon for performance tuning. ouch. aVeryComplexSieveEx :: Int -> Int -> Int -> Int -> Int ... -> r -- f $ aVeryComplexSieveEx 1 2 3 4 ... aVeryComplexSieveEx :: SomeCompulsaryRecordConstructor -> Int -> r -- f $ aVeryComplexSieveEx (SomeCompulsaryRecordConstructor { ... }) 3 }}} vs {{{ aVeryComplexSieveEx :: (?foo :: Int, ?bar :: Int, ?alpha :: Int, ?beta :: Int, ?gamma :: Int) -> Int -> r -- f $ let ?alpha = 1337 in aVeryComplexSieveEx 3 }}} As you can see it would be beneficial for libraries to expose *optional* ways for users to tune internal values in this manner, without having to pass record constructors with many options or make new functions with tiresome numbers of parameters. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8297 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8297: Allow implicit parameters to take a default value ----------------------------+---------------------------------------------- Reporter: schyler | Owner: Type: feature | Status: new request | Milestone: _|_ Priority: normal | Version: Component: | Keywords: Compiler | Architecture: Unknown/Multiple Resolution: | Difficulty: Moderate (less than a day) Operating System: | Blocked By: Unknown/Multiple | Related Tickets: Type of failure: | None/Unknown | Test Case: | Blocking: | ----------------------------+---------------------------------------------- Comment (by schyler): I have not included a suggested syntax in the ticket. If there is interest in this extension of the existing extension, that would be a good discussion to have with masters of the esoteric. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8297#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8297: Allow implicit parameters to take a default value ----------------------------+---------------------------------------------- Reporter: schyler | Owner: Type: feature | Status: new request | Milestone: _|_ Priority: low | Version: Component: | Keywords: Compiler | Architecture: Unknown/Multiple Resolution: | Difficulty: Moderate (less than a day) Operating System: | Blocked By: Unknown/Multiple | Related Tickets: Type of failure: | None/Unknown | Test Case: | Blocking: | ----------------------------+---------------------------------------------- Changes (by schyler): * priority: normal => low -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8297#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8297: Allow implicit parameters to take a default value ----------------------------+---------------------------------------------- Reporter: schyler | Owner: Type: feature | Status: closed request | Milestone: _|_ Priority: low | Version: Component: | Keywords: Compiler | Architecture: Unknown/Multiple Resolution: invalid | Difficulty: Moderate (less than a day) Operating System: | Blocked By: Unknown/Multiple | Related Tickets: Type of failure: | None/Unknown | Test Case: | Blocking: | ----------------------------+---------------------------------------------- Changes (by carter): * status: new => closed * resolution: => invalid Comment: this seems more a case for using the convention of having a function with a default config, and a more general version that takes a config record, rather than requiring implicit parameters, which are in some sense a way to do dynamic scoping (which is unrelated to type holes) eg {{{ sieve :: In -> Out sieveWithConfig :: In -> Config -> Out }}} i'm closing this for now, because there doesn't seem to be a strong use case that isn't covered by the *WithConfig idiom. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8297#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8297: Allow implicit parameters to take a default value ----------------------------+---------------------------------------------- Reporter: schyler | Owner: Type: feature | Status: closed request | Milestone: _|_ Priority: low | Version: Component: | Keywords: Compiler | Architecture: Unknown/Multiple Resolution: invalid | Difficulty: Moderate (less than a day) Operating System: | Blocked By: Unknown/Multiple | Related Tickets: Type of failure: | None/Unknown | Test Case: | Blocking: | ----------------------------+---------------------------------------------- Comment (by schyler): Proved "impossible" on IRC. {{{ 12:40 PM <dolio> Ghoul_: By the way, I thought of an even more devious problem with your implicit proposal. :) 12:41 PM <dolio> Suppose I write 'sort' such that the comparison is implicit (instead writing both sort and sortBy). 12:42 PM <dolio> But, I say the default comparison is the one given by Ord. Now sort imposes a type class obligation iff you don't have an implicit argument binding in scope. }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8297#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC