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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:14:18+00:00 2026-06-06T18:14:18+00:00

I’m trying to construct a datatype that is essentially a binary tree whose: each

  • 0

I’m trying to construct a datatype that is essentially a binary tree whose: each node’s left branch is a function that can act on the variable in each node’s right branch. I’m new to Haskell, and I’m not sure I’m going about this the right way, but my current problem is that I can’t figure out how to add my type to the Show typeclass. Here is my attempt:

{-# LANGUAGE ExistentialQuantification #-}
-- file: TS.hs                                                                                                                                                       

data TypeSentence a = forall b. Apply (TypeSentence (b->a)) (TypeSentence b)
                    | Expr a

instance (Show a) => (Show (TypeSentence a)) where
        show (Expr x) = show x
        show (Apply x y) = (show x) ++ " " ++ (show y)

instance (Show (TypeSentence b->a)) where
    show (Expr x) = show "hello"

x = Expr 1
f = Expr (+1)
s = Apply f x

However, when I load this into ghci I get the following error:

TS.hs:9:24:                                                                                                                                                          
     Could not deduce (Show (b -> a)) from the context ()                                                                                                             
     arising from a use of `show' at TS.hs:9:24-29                                                                                                                  
      Possible fix:                                                                                                                                                    
      add (Show (b -> a)) to the context of the constructor `Apply'                                                                                                  
      or add an instance declaration for (Show (b -> a))                                                                                                             
        In the first argument of `(++)', namely `(show x)'                                                                                                               
        In the expression: (show x) ++ " " ++ (show y)                                                                                                                   
        In the definition of `show':                                                                                                                                     
           show (Apply x y) = (show x) ++ " " ++ (show y)                                                                                                               
 Failed, modules loaded: none. 

Any ideas how I go about adding the Show (b->a) declaration?

Thanks.

  • 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-06T18:14:19+00:00Added an answer on June 6, 2026 at 6:14 pm

    There are a few problems with your code as written, so I’m going to go through them one by one.

    1. You can’t add a particularly informative instance for Show (a -> b). Consider how you’d have to write it:

      instance Show (a -> b) where
        show f = error "What goes here?"
      

      Since f is a function, there’s nothing you can do with it other than apply it to a value; and since a is a fully-polymorphic type, you can’t create a value of type a to apply f to. So your only option is something like

      instance Show (a -> b) where
        show _ = "<function>"
      

      As Daniel Fischer said in a comment, this is available in the Text.Show.Functions module. I wouldn’t actually bother with this, though; I’d just write something like

      instance Show a => Show (TypeSentence a) where
        show (Apply _ x) = "Apply _ " ++ show x -- This still won't work; see below
        show (Expr x)    = "Expr " ++ show x
      

      Since show can only return the one string for any function, just inline that directly.

    2. Even then, though, you still can’t write that Show instance. If you try to compile the instance above, you get the following error:

      TS.hs:8:36:
          Could not deduce (Show b) arising from a use of `show'
          from the context (Show a)
            bound by the instance declaration
            at TS.hs:7:10-40
          Possible fix:
            add (Show b) to the context of
              the data constructor `Apply'
              or the instance declaration
          In the second argument of `(++)', namely `show x'
          In the expression: "Apply _ " ++ show x
          In an equation for `show': show (Apply _ x) = "Apply _ " ++ show x
      

      The problem is that, in your definition of TypeSentence, Apply hides a variable (bound as x in the definition of show) of TypeSentence parametrized by some arbitrary existentially-hidden type b. But there’s no guarantee that b is showable, so show x won’t type check, which is the error produced above: there’s no instance for Show b, because b is arbitrary. So to get rid of that, the simplest approach would be

      instance Show a => Show (TypeSentence a) where
        show (Apply _ _) = "Apply _ _"
        show (Expr x)    = "Expr " ++ show x
      

      And that’s not particularly useful. So maybe there’s not a good Show instance for TypeSentence. (And that’s fine. Many useful types don’t have Show instances.)

    3. This one’s unrelated to everything else. The instance Show (TypeSentence b -> a) declaration tries to declare an instance of Show for functions from TypeSentence b to a; if you reparenthesize that as instance Show (TypeSentence (b -> a)), you still need both the FlexibleInstances and OverlappingInstances extension to get that to compile. So that you should probably just axe.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.