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 7940435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:23:16+00:00 2026-06-03T23:23:16+00:00

Lets say I’ve got the following code type IsTall = Bool type IsAlive =

  • 0

Lets say I’ve got the following code

type IsTall = Bool
type IsAlive = Bool

is_short_alive_person is_tall is_alive = (not is_tall) && is_alive

Say, later on, I’ve got the following

a :: IsAlive
a = False

b :: IsTall
b = True

And call the following, getting the two arguments around the wrong way:

is_short_alive_person a b

This successfully compiles unfortunately, and at runtime tall dead people are instead found instead of short alive people.

I would like the above example not to compile.

My first attempt was:

newtype IsAlive = IsAlive Bool
newtype IsTall = IsTall Bool

But then I can’t do something like.

switch_height :: IsTall -> IsTall
switch_height h = not h

As not is not defined on IsTalls, only Bools.

I could explicitly extract the Bools all the time, but that largely defeats the purpose.

Basically, I want IsTalls to interact with other IsTalls, just like they’re Bools, except they won’t interact with Bools and IsAlives without an explicit cast.

What’s the best way to achieve this.


p.s. I think I’ve achieved this with numbers by doing in GHC:

{-# LANGUAGE GeneralizedNewtypeDeriving #-}

newtype UserID = UserID Int deriving (Eq, Ord, Num)
newtype GroupID = GroupID Int deriving (Eq, Ord, Num)

(i.e. UserID’s and GroupID’s shouldn’t interact)

but I can’t seem to do this with Bools (deriving Bool doesn’t work). I’m not even sure the above is the best approach anyway.

  • 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-03T23:23:17+00:00Added an answer on June 3, 2026 at 11:23 pm

    You can get some way towards this, using newtypes and a class if you import the Prelude hiding the boolean functions you want to use with your IsTall and IsAlive values. You redefine the boolean functions as methods in the class, for which you then make instances for all 3 of the Bool, IsTall, and IsAlive types. If you use GeneralizedNewtypeDeriving you can even get the IsTall and IsAlive instances without having to write the wrapping/unwrapping boilerplate by hand.

    Here’s an example script I actually tried out in ghci:

    {-# LANGUAGE GeneralizedNewtypeDeriving #-}
    
    import Prelude hiding ((&&), (||), not)
    import qualified Prelude
    
    class Boolish a where
        (&&) :: a -> a -> a
        (||) :: a -> a -> a
        not :: a -> a
    
    instance Boolish Bool where
        (&&) = (Prelude.&&)
        (||) = (Prelude.||)
        not = Prelude.not
    
    newtype IsTall = IsTall Bool
        deriving (Eq, Ord, Show, Boolish)
    
    newtype IsAlive = IsAlive Bool
        deriving (Eq, Ord, Show, Boolish)
    

    You can now &&, ||, and not values of any of the three types, but not together. And they’re separate types, so your function signatures can now restrict which of the 3 they want to accept.

    Higher order functions defined in other modules will work fine with this, as in:

    *Main> map not [IsTall True, IsTall False]
    [IsTall False,IsTall True]
    

    But you won’t be able to pass an IsTall to any other function defined elsewhere that expects a Bool, because the other module will still be using the Prelude version of the boolean functions. Language constructs like if ... then ... else ... will still be a problem too (although a comment by hammar on Norman Ramsey’s answer says you can fix this with another GHC extension). I’d probably add a toBool method to that class to help uniformly convert back to regular Bools to help mitigate such problems.

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

Sidebar

Related Questions

Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Lets say I've got two interchangeable pieces of code and I want to figure
Lets say we have the following code: class C t where g :: t
Lets say I have this code: <?php class hello { var $greeting = hello;
Lets say that I have this code $(document).ready(function() { $(#changeText).click(function() { $(#textBox).html(My<br><br><br>text<br><br><br>is<br><br><br>changed!); }); });
Lets say we have this code: <div id='m1'> <div class=c></div> <div class=c></div> <div class=c></div>
Lets say in a file I have some code in two files part of
Lets say I have a single object of type Car which I want to
Lets say i have the following controller: // // GET: /Courses/Edit/5 public ActionResult Edit(int
Lets say I have two tables in Postgres: Name: table_rad Column Type id integer

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.