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

  • SEARCH
  • Home
  • 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 8616475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:37:44+00:00 2026-06-12T05:37:44+00:00

I have this code: data SafeValue a = SafeValue a a a deriving Eq

  • 0

I have this code:

data SafeValue a = SafeValue a a a deriving Eq

class Safe a where
  check::a->Bool
  (+++)::a->a->a

instance (Num a, Eq a) => Safe (SafeValue a) where
  check (SafeValue x y z) | x == y = True
                          | x == z = True
                          | y == z = True
                          | otherwise = False
  (SafeValue a b c)+++(SafeValue x y z) = let new_val = SafeValue (a+x) (b+y) (c+z)
                                          in if check new_val then new_val
                                                              else error "Error"

I would like to add to class Safe a function such as:

make_new 3 --> SafeValue 3 3 3

I don’t know how to add it, since the tipy should be something like:

make_new::b->a

but in the istance declaration ghci claims that it is not sure about what b is.

Can someone help, please?

  • 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-06-12T05:37:45+00:00Added an answer on June 12, 2026 at 5:37 am

    The core problem is that you’re promising that make_new works for all types b to produce a value of a. However, this doesn’t make sense given how SafeValue a works: given some type a, you get a SafeValue a out. So what you really want is for make_new to take a value of some type a but give you a value of SafeValue a. More generally, you want the result to be of some type s a where s is the actual type you’re writing an instance for and a can be any type.

    What you need to do is make the class accept values of a “higher-kinded” type. (What this means is that the class should expect a type like SafeValue that takes a further parameter. You can do that like this:

    class Safe s where
      check :: s a -> Bool
      (+++) :: s a -> s a -> s a
      make_new :: a ->  s a
    

    Then your instance will look like this:

    instance Safe SafeValue where ...
    

    Note the important difference: instead of making an instance for SafeValue a, you’re making on for SafeValue without the type parameter.

    However, this has another problem: now you cannot constrain a to be part of Num and Eq!

    You can solve this with an extension called multi-parameter type classes. So your final version would be:

    class Safe s a where
      check :: s a -> Bool
      (+++) :: s a -> s a -> s a
      make_new :: a -> s a
    

    and your instance would be:

    instance (Num a, Eq a) => Safe SafeValue a where ...
    

    To make all this work, you need to enable two extensions:

    {-# LANGUAGE FlexibleInstances     #-}
    {-# LANGUAGE MultiParamTypeClasses #-}
    

    The first one lets you write instances in more ways. Normally, you can only write instances for a type that looks like T a b c where T is a type and a b c are type variables; with this extension, the restriction is lifted and you can write instances like the one I showed.

    The multiparameter typeclass extension allows typeclasses that act on more than one type. This allows you to make a class that depends on both s and a.

    A final note: using typeclasses at all may not be the right choice for your example. Are you planning to write any more types for the Safe class? If you aren’t, then you shouldn’t use a typeclass at all. However, learning a bit about multiparameter typeclasses is still useful, so you should consider playing around with them at some point.

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

Sidebar

Related Questions

I have this code for update: public Boolean update() { try { data.put(ContactsContract.Groups.SHOULD_SYNC, true);
I have this code for BinaryTree creation and traversal class Node { Integer data;
I have this code for my Data Access class where I want to load
I have this code to update users data, but I can't write the for
I have this code to display the data retrieved from DB in accounts_view.php :
I have this code to update my SQL database from data in a textbox,
i have this simple code to load data from other php page using get
I have this code: ... request data = new request(); data.username = formNick; xml
I have this code to sort an array of objects. The data in the
I have this code inside a iframe: window.addEventListener('message', function(e){ if(e.data == 'test') console.log(e); },

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.