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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:27:35+00:00 2026-05-17T21:27:35+00:00

In Haskell, is it possible to write a function with a signature that can

  • 0

In Haskell, is it possible to write a function with a signature that can accept two different (although similar) data types, and operate differently depending on what type is passed in?

An example might make my question clearer. If I have a function named myFunction, and two types named MyTypeA and MyTypeB, can I define myFunction so that it can only accept data of type MyTypeA or MyTypeB as its first parameter?

type MyTypeA = (Int, Int, Char, Char)
type MyTypeB = ([Int], [Char])

myFunction :: MyTypeA_or_MyTypeB -> Char
myFunction constrainedToTypeA = something
myFunction constrainedToTypeB = somethingElse

In an OOP language, you could write what I’m trying to achieve like so:

public abstract class ConstrainedType {
}

public class MyTypeA extends ConstrainedType {
    ...various members...
}

public class MyTypeB extends ConstrainedType {
    ...various members...
}

...

public Char myFunction(ConstrainedType a) {
    if (a TypeOf MyTypeA) {
        return doStuffA();
    }
    else if (a TypeOf MyTypeB) {
        return doStuffB();
    }
}

I’ve been reading about algebraic data types and I think I need to define a Haskell type, but I’m not sure how to go about defining it so that it can store one type or another, and also how I use it in my own functions.

  • 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-17T21:27:35+00:00Added an answer on May 17, 2026 at 9:27 pm

    Yes, you are correct, you are looking for algebraic data types. There is a great tutorial on them at Learn You a Haskell.

    For the record, the concept of an abstract class from OOP actually has three different translations into Haskell, and ADTs are just one. Here is a quick overview of the techniques.

    Algebraic Data Types

    Algebraic data types encode the pattern of an abstract class whose subclasses are known, and where functions check which particular instance the object is a member of by down-casting.

    abstract class IntBox { }
    
    class Empty : IntBox { }
    
    class Full : IntBox {
        int inside;
        Full(int inside) { this.inside = inside; }
    }
    
    int Get(IntBox a) {
        if (a is Empty) { return 0; }
        if (a is Full)  { return ((Full)a).inside; }
        error("IntBox not of expected type");
    }
    

    Translates into:

    data IntBox = Empty | Full Int
    
    get :: IntBox -> Int
    get Empty = 0
    get (Full x) = x
    

    Record of functions

    This style does not allow down-casting, so the Get function above would not be expressible in this style. So here is something completely different.

    abstract class Animal { 
        abstract string CatchPhrase();
        virtual void Speak() { print(CatchPhrase()); }
    }
    
    class Cat : Animal {
        override string CatchPhrase() { return "Meow"; }
    }
    
    class Dog : Animal {
        override string CatchPhrase() { return "Woof"; }
        override void Speak() { print("Rowwrlrw"); }
    }
    

    Its translation in Haskell doesn’t map types into types. Animal is the only type, and Dog and Cat are squashed away into their constructor functions:

    data Animal = Animal {
        catchPhrase :: String,
        speak       :: IO ()
    }
    
    protoAnimal :: Animal
    protoAnimal = Animal {
        speak = putStrLn (catchPhrase protoAnimal)
    }
    
    cat :: Animal
    cat = protoAnimal { catchPhrase = "Meow" }
    
    dog :: Animal
    dog = protoAnimal { catchPhrase = "Woof", speak = putStrLn "Rowwrlrw" }
    

    There are a few different permutations of this basic concept. The invariant is that the abstract type is a record type where the methods are the fields of the record.

    EDIT: There is a good discussion in the comments on some of the subtleties of this approach, including a bug in the above code.

    Typeclasses

    This is my least favorite encoding of OO ideas. It is comfortable to OO programmers because it uses familiar words and maps types to types. But the record of functions approach above tends to be easier to work with when things get complicated.

    I’ll encode the Animal example again:

    class Animal a where
        catchPhrase :: a -> String
        speak       :: a -> IO ()
    
        speak a = putStrLn (catchPhrase a)
    
    data Cat = Cat 
    instance Animal Cat where
        catchPhrase Cat = "Meow"
    
    data Dog = Dog
    instance Animal Dog where
        catchPhrase Dog = "Woof"
        speak Dog = putStrLn "Rowwrlrw"
    

    This looks nice, doesn’t it? The difficulty comes when you realize that even though it looks like OO, it doesn’t really work like OO. You might want to have a list of Animals, but the best you can do right now is Animal a => [a], a list of homogeneous animals, eg. a list of only Cats or only Dogs. Then you need to make this wrapper type:

    {-# LANGUAGE ExistentialQuantification #-}
    
    data AnyAnimal = forall a. Animal a => AnyAnimal a
    instance Animal AnyAnimal where
        catchPhrase (AnyAnimal a) = catchPhrase a
        speak (AnyAnimal a) = speak a
    

    And then [AnyAnimal] is what you want for your list of animals. However, it turns out that AnyAnimal exposes exactly the same information about itself as the Animal record in the second example, we’ve just gone about it in a roundabout way. Thus why I don’t consider typeclasses to be a very good encoding of OO.

    And thus concludes this week’s edition of Way Too Much Information!

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

Sidebar

Related Questions

Can you use Atom for programming C in Haskell? Would that be possible and
Is it possible to write the Y Combinator in Haskell? It seems like it
Possible Duplicate: Split a number into its digits with Haskell how can i convert
In Haskell, is there a way to restrict a monad M a so that
I know a Haskell module name, but I can't figure out in what package
Possible Duplicate: Haskell Error: Couldn't match expected type Integer' against inferred type Int' How
Haskell is givinig me a headache today. I want to handle an exception. When
I'm currently learning Haskell, Which language (F# or Haskell) do you prefer for programming
From the haskell report: The quot, rem, div, and mod class methods satisfy these
In reading Haskell-related stuff I sometimes come across the expression tying the knot, 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.