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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:40:04+00:00 2026-06-12T09:40:04+00:00

Today I played with using type classes to inductively construct functions of a predicate

  • 0

Today I played with using type classes to inductively construct functions of a predicate of any arity taking as inputs any combination of any types, that returned other predicates of the same type but with some basic operation applied. For example

conjunction (>2) even

would return a predicate that evaluates to true for even numbers larger than two and

conjunction (>=) (<=)

would return =

All well and good, got that part working, but it raised the question, what if I wanted to define conjunction of two predicates as being a predicate that takes one input for each input of each conjoined predicate? For example:

:t conjunction (>) not

would return Ord a => a -> a -> Bool -> Bool. Can this be done? If so, how?

  • 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-12T09:40:05+00:00Added an answer on June 12, 2026 at 9:40 am

    We will need TypeFamilies for this solution.

    {-# LANGUAGE TypeFamilies #-}
    

    The idea is to define a class Pred for n-ary predicates:

    class Pred a where
      type Arg a k :: *
      split :: a -> (Bool -> r) -> Arg a r
    

    The problem is all about re-shuffling arguments to the predicates, so this is what the class aims to do. The associated type Arg is supposed to give access to the arguments of an n-ary predicate by replacing the final Bool with k, so if we have a type

    X = arg1 -> arg2 -> ... -> argn -> Bool
    

    then

    Arg X k = arg1 -> arg2 -> ... -> argn -> k
    

    This will allow us to build the right result type of conjunction where all arguments of the two predicates are to be collected.

    The function split takes a predicate of type a and a continuation of type Bool -> r and will produce something of type Arg a r. The idea of split is that if we know what to do with the Bool we obtain from the predicate in the end, then we can do other things (r) in between.

    Not surprisingly, we’ll need two instances, one for Bool and one for functions for which the target is already a predicate:

    instance Pred Bool where
      type Arg Bool k = k
      split b k = k b
    

    A Bool has no arguments, so Arg Bool k simply returns k. Also, for split, we have the Bool already, so we can immediately apply the continuation.

    instance Pred r => Pred (a -> r) where
      type Arg (a -> r) k = a -> Arg r k
      split f k x = split (f x) k
    

    If we have a predicate of type a -> r, then Arg (a -> r) k must start with a ->, and we continue by calling Arg recursively on r. For split, we can now take three arguments, the x being of type a. We can feed x to f and then call split on the result.

    Once we have defined the Pred class, it is easy to define conjunction:

    conjunction :: (Pred a, Pred b) => a -> b -> Arg a (Arg b Bool)
    conjunction x y = split x (\ xb -> split y (\ yb -> xb && yb))
    

    The function takes two predicates and returns something of type Arg a (Arg b Bool). Let’s look at the example:

    > :t conjunction (>) not
    conjunction (>) not
      :: Ord a => Arg (a -> a -> Bool) (Arg (Bool -> Bool) Bool)
    

    GHCi doesn’t expand this type, but we can. The type is equivalent to

    Ord a => a -> a -> Bool -> Bool
    

    which is exactly what we want. We can test a number of examples, too:

    > conjunction (>) not 4 2 False
    True
    > conjunction (>) not 4 2 True
    False
    > conjunction (>) not 2 2 False
    False
    

    Note that using the Pred class, it is trivial to write other functions (like disjunction), too.

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

Sidebar

Related Questions

Today I generated a webservice client with CXF 2.6.1 (latest version), using the tool
I love to eat chicken. Today I went running, swimming and played basketball. My
today i found it these chart service by google and i played around with
My brain feels slow today. I'm writing pre/post/invariants in Python using decorators. Currently, I
I'm new to using vectors...I just started studying them today. I'm trying to create
Today I overloaded the << operator in one of my classes: #ifndef TERMINALLOG_HH #define
I've been coding a game in C++ using the SDL library. Today while changing
today i hit a really annoing problem with wpf. i just want to align
Today at work, I was given a task to fix a bug within our
Today I have had a problem with hiding text with text-indent: -9999px rule. I

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.