20

What I mean is to define an instance of a type class that applies in a local (let or where) scope in a function. More importantly, I want to have the functions in this instance be closures, i.e. be able to close over variables in the lexical scope where the instance is defined (which means the instance may possibly work differently the next time the function it is in is called).

I can give you a simplified use case for this. Suppose I have a function that operates on a type based on a type class. In this example I use squaring, which operates on any type that instances Num (yes, squaring is very simple and can easily be re-implemented, but it is standing in for something that could be more complicated). I need to be able to use the existing function as-is (without changing it or re-implementing it).

square :: Num a => a -> a
square x = x * x

Now, suppose I wish to use this operation in modular arithmetic, i.e. addition, multiplication, etc. mod some number. This would be easy to implement for any fixed modulo base, but I want to have something generic that I can re-use for different modulo bases. I want to be able to define something like this:

newtype ModN = ModN Integer deriving (Eq, Show)

-- computes (x * x) mod n
squareModN :: 
squareModN x n =
  let instance Num ModN where
    ModN x * ModN y = ModN ((x * y) `mod` n) -- modular multiplication
    _ + _ = undefined         -- the rest are unimplemented for simplicity
    negate _ = undefined
    abs _ = undefined
    signum _ = undefined
    fromInteger _ = undefined
  in let ModN y = square (ModN x)
     in y

The point of this is that I need to use the function from above (square) that requires its argument to be a type that is an instance of a certain type class. I define a new type and make it an instance of Num; however, for it to perform modulo arithmetic correctly, it depends on the modulo base n, which, due to the generic design of this function, might change from call to call. I wish to define the instance functions as kind of one-time "callbacks" (if you will) for the square function to customize how it performs the operations this time (and only this time).

One solution might be to integrate the "closure variables" directly into the datatype itself (i.e. ModN (x, n) to represent the number and the base it belongs to), and the operations can just extract this information from the arguments. However, this has several problems: 1) For multi-argument functions (e.g. (*)) it would need to check at runtime that these information match, which is ugly; and 2) the instance might contain 0-argument "values" that I might want to depend on the closure variables, but which, since they contain no arguments, cannot extract them from arguments.

newacct
  • 119,665
  • 29
  • 163
  • 224
  • 3
    No you can't have local instances. For modular arithmetic Oleg Kiselyov and CC Shan have solved the problem in their paper "Implicit Configurations" - http://www.cs.rutgers.edu/~ccshan/prepose/prepose.pdf. Personally I tend to avoid this and simply make a newtype for the modular basis - i.e. Z7, Z12. Conal Elliott has another pattern for selecting alternative type classes, see CxMonoid in the paper "Applicative Data-Driven Computation" - http://conal.net/papers/data-driven/paper.pdf – stephen tetley Mar 03 '12 at 12:42
  • 1
    Indeed, the reflection package is essentially the packaged version of the Oleg paper. – ehird Mar 03 '12 at 12:56
  • Regarding the current state of Haskell: no. Instances will go everywhere and anywhere that they can reach. Regarding possible adjustments to Haskell in the future: maybe. Regarding the use of your own mechanism instead of typeclasses: yes. – Dan Burton Mar 03 '12 at 22:01

1 Answers1

13

The proposed extension has the same problem demonstrated in this previous answer of mine; you can use the local instances to create two Maps with the same key type but different Ord instances, causing all the invariants to come crashing down.

However, the reflection package allows you to define such a ModN type: you define a single instance with a Reifies constraint, and activate the instance for a particular n with reify. (I believe implicit parameters would also make this possible, but that extension is rarely used.)

Community
  • 1
  • 1
ehird
  • 40,602
  • 3
  • 180
  • 182