Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a "local" typeclass instance?

Tags:

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.

like image 608
newacct Avatar asked Mar 03 '12 11:03

newacct


1 Answers

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.)

like image 54
ehird Avatar answered Sep 18 '22 17:09

ehird