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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:58:25+00:00 2026-06-01T13:58:25+00:00

I’m trying to write a fairly polymorphic library. I’ve run into a situation that’s

  • 0

I’m trying to write a fairly polymorphic library. I’ve run into a situation that’s easier to show than tell. It looks a bit like this:

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
import Data.Map (Map)
import qualified Data.Map as Map

class Format f where type Target f
class Format f => Formatter x f where
  target :: forall y. Formatable y => Target f -> x -> y
class Formatable y where name :: y -> String

instance Formatable Integer where name = show
instance Formatable Int where name = show

split :: forall x f. (Format f, Formatter x f) => x -> f -> String -> [Either String (Target f)]
split = undefined

display :: forall x f. (Format f, Formatter x f) => f -> String -> x -> String
display f str x = let
  chunks = split x f str
  built = foldr apply "" chunks
  apply (Left s) accum = accum ++ s
  apply (Right t) accum = accum ++ name (target t x)
  in foldr apply "" chunks

Essentially, we have polymorphic Formats, which define a number of Targets. There are also a number of Formattable objects, which know how to respond to a bunch of different format options (reduced here to simply name).

These Formattables are composed in a variety of ways, and can respond to a number of different targets. Formatters are essentially the router between Format and Formattable — given a target (from a specific format) they respond with a suitable Formattable object.

This is all pretty abstract. Here’s an example:

  • DateFormat specifies targets like Year, Month, and Day.
  • MonthType is a Formattable newtype of Int that has names such as “February”
  • There is also the simple instance Formattable Int where name = show
  • DateTime might be a type synonym for (Int, MonthType, Int).

(Obviously, I’ve cut out a lot of machinery here, such as piping the correct values around, but you get the idea.)

The display function is fairly simple. It takes a formatter, a string specifying the format, an object to display, and renders it all into a string.

First it breaks the string up into targets and strings. For example, a date formatter might break the string "%Y-%m-%d" into [Right Year, Left "-", Right Month, Left "-", Right Day]. The split function does that, and has been redacted here.

The display function simply tracks down the Formattables for each target and accumulates the string.

Or, at least, it`s supposed to.

But it fails typechecking with the following error:

Reduced.hs:20:16:
    Could not deduce (Target f ~ Target f0)
    from the context (Format f, Formatter x f)
      bound by the type signature for
                 display :: (Format f, Formatter x f) => f -> String -> x -> String
      at Reduced.hs:(19,5)-(24,30)
    NB: `Target' is a type function, and may not be injective
    Expected type: [Either [Char] (Target f0)]
      Actual type: [Either String (Target f)]
    In the return type of a call of `split'
    In the expression: split x f str
    In an equation for `chunks': chunks = split x f str
Failed, modules loaded: none.

and I can’t for the life of me figure out why. What am I doing wrong?

  • 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-01T13:58:26+00:00Added an answer on June 1, 2026 at 1:58 pm

    The problem is that Target f does not determine f, which means that the function

    target :: (Formatter f x, Formatable y) => Target f -> x -> y
    

    can never be called. No matter what type annotation you give to target, you can’t nail down what f is, and so the compiler can never figure out which Formatter instance to use. I’m not 100% sure, but probably the solution is not to use multi-parameter type classes and to let one of x or f be a function of the other. Also, you should probably just delete the Format class entirely (did you know you don’t need a class to use a type family?). Perhaps something like this:

    class Formatter x where
        type Format x
        target :: Formatable y => Format x -> x -> y
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
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
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,

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.