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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:48:18+00:00 2026-06-11T02:48:18+00:00

I want pass a sub data type into function. An example: data Car a

  • 0

I want pass a sub data type into function.

An example:

data Car a = Ford | Opel | BMW | Mercedes a deriving (Show)

data Mercedes a = SClass a| CClass a| MClass a


myfunc::Car->String
myfunc a = show a ++ " works correctly"

but I do:

myfunc CClass “x”

it says

not in scope: constructor CClass

  • 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-11T02:48:20+00:00Added an answer on June 11, 2026 at 2:48 am

    To achieve “sub”-types, one has a few options. The first is just to use more data types, as you are attempting:

    data Car a = Ford (Ford a) | Mercedes (Mercedes a) deriving (Show)
    
    data Ford a = ModelT a | Falcon a deriving (Show) -- different Fords models
    data Mercedes a = CClass a | SClass a deriving (Show) -- different Mercedes models
    

    Note that being a type constructor is different to being a data constructor, so one can’t just use data Car = .. | Mercedes a: this is saying that one of the ways to make a Car String is just by saying Mercedes "a", it has no correlation with the type called Mercedes. Now one can just use a function like:

    horsepower :: Car a -> Float
    horsepower (Ford (ModelT _)) = 1
    horsepower (Ford (Falcon _)) = 500
    horsepower (Mercedes (CClass _)) = 300
    horsepower (Mercedes (SClass _)) = 400
    

    And your example would be:

    myfunc :: Show a => Car a -> String
    myfunc a = show a ++ " works correctly"
    
    -- myfunc (Ford (ModelT "A")) => Ford (ModelT "A") works correctly
    -- myfunc (Mercedes (CClass "b")) => Mercedes (CClass "b") works correctly
    

    Another way to allow “subtyping” like this is to use a type class (depending on the specifics, this might be a better option). E.g.

    class (Show c) => Car c where
        make :: String
        model :: c -> String
        horsepower :: c -> Float
        number_of_seats :: c -> Int
        -- etc.
    
    data Ford a = ModelT a | Falcon a deriving (Show)
    instance (Show a) => Car (Ford a) where
        make = "Ford"
        horsepower (ModelT _) = 1
        horsepower (Falcon _) = 500
        -- etc.
    
    
    data Mercedes a = CClass a | SClass a deriving (Show)
    instance (Show a) => Car (Mercedes a) where
        make = "Mercedes"
        horsepower (CClass _) = 300
        horsepower (SClass _) = 400
        -- etc.
    

    Your example of myfunc might be:

    myfunc :: (Car c) => c -> String
    myfunc c = (make `asTypeOf` c) ++ " " ++ show c ++ " works correctly"
    
    -- myfunc (ModelT "A") => Ford ModelT "A" works correctly
    -- myfunc (CClass "b") => Mercedes CClass "b" works correctly
    

    Also note that one must call functions like function (Ford (Ford "a")) (or function (CClass "a")) with parenthesis.

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

Sidebar

Related Questions

When we want to pass data from an activity to a sub activity we
My Ajax code $(document).ready(function() { $(#sub).click(function() { $.ajax({ type: POST, url: jqueryphp.php, data: txt1=
My data is just number, I want pass it from VC1 to VC2 for
I want to pass data from second tab to first tab using delegates.But delegate
If I pass a hash to a sub: parse(\%data); Should I use a variable
I want to pass the address of a sub as a callback to another
I want to pass my set of objects and variables to VBA function, make
I want to pass data from form1 to form2 and i used The Properties
I want to use LINQ to pass data from one custom collection to another.
I have a situation where i want to pass a object Type as a

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.