Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7580525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:56:22+00:00 2026-05-30T17:56:22+00:00

What I mean is to define an instance of a type class that applies

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:56:23+00:00Added an answer on May 30, 2026 at 5:56 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've defined a custom error type that I want to use with the Error
Hi I have a bean and I want to define it as a CustomScoped
I have a LRESULT CALLBACK function in a header file(hook.h) that I both forward
Say I have models: class Animal(models.Model): type = models.CharField(max_length=255) class Dog(Animal): def make_sound(self): print
I have several doctrine models that have relationships to other models. Let's call those
Is there a Pythonic way (I mean, no pure SQL query) to define an
I mean things like: FK1 -> 1FK2 -> 2PK Please, note that 1FK2 is
I mean, if I have an object and I apply 3 transforms to it,
I mean when the user starts my application(exe). I want it to start directly
I mean, I want the logger name to reflect the source.jsp file, no matter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.