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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:05:47+00:00 2026-06-02T21:05:47+00:00

Given the following definitions: import Control.Monad.ST import Data.STRef fourty_two = do x <- newSTRef

  • 0

Given the following definitions:

import Control.Monad.ST
import Data.STRef

fourty_two = do
  x <- newSTRef (42::Int)
  readSTRef x

The following compiles under GHC:

main = (print . runST) fourty_two -- (1)

But this does not:

main = (print . runST) $ fourty_two -- (2)

But then as bdonlan points out in a comment, this does compile:

main = ((print . runST) $) fourty_two -- (3)

But, this does not compile

main = (($) (print . runST)) fourty_two -- (4)

Which seems to indicate that (3) only compiles due to special treatment of infix $, however, it still doesn’t explain why (1) does compile.

Questions:

1) I’ve read the following two questions (first, second), and I’ve been led to believe $ can only be instantiated with monomorphic types. But I would similarly assume . can only be instantiated with monomorphic types, and as a result would similarly fail.
Why does the first code succeed but the second code does not? (e.g. is there a special rule GHC has for the first case that it can’t apply in the second?)

2) Is there a current GHC extension that compiles the second code? (perhaps ImpredicativePolymorphism did this at some point, but it seems deprecated, has anything replaced it?)

3) Is there any way to define say `my_dollar` using GHC extensions to do what $ does, but is also able to handle polymorphic types, so (print . runST) `my_dollar` fourty_two compiles?

Edit: Proposed Answer:

Also, the following fails to compile:

main = ((.) print runST) fourty_two -- (5)

This is the same as (1), except not using the infix version of ..

As a result, it seems GHC has special rules for both $ and ., but only their infix versions.

  • 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-02T21:05:48+00:00Added an answer on June 2, 2026 at 9:05 pm
    1. I’m not sure I understand why the second doesn’t work. We can look at the type of print . runST and observe that it is sufficiently polymorphic, so the blame doesn’t lie with (.). I suspect that the special rule that GHC has for infix ($) just isn’t quite sufficient. SPJ and friends might be open to re-examining it if you propose this fragment as a bug on their tracker.

      As for why the third example works, well, that’s just because again the type of ((print . runST) $) is sufficiently polymorphic; in fact, it’s equal to the type of print . runST.

    2. Nothing has replaced ImpredicativePolymorphism, because the GHC folks haven’t seen any use cases where the extra programmer convenience outweighed the extra potential for compiler bugs. (I don’t think they’d see this as compelling, either, though of course I’m not the authority.)
    3. We can define a slightly less polymorphic ($$):

      {-# LANGUAGE RankNTypes #-}
      infixl 0 $$
      ($$) :: ((forall s. f s a) -> b) -> ((forall s. f s a) -> b)
      f $$ x = f x
      

      Then your example typechecks okay with this new operator:

      *Main> (print . runST) $$ fourty_two
      42
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following data table definitions in SQLite Version 3.7.5: CREATE TABLE [Books] (
Given the following simple BST definition: data Tree x = Empty | Leaf x
// given following array: $data = array( 0=>array( data=>object1, col=>array( 0=>array( data=>object2, col=>array( 0=>array(
Given the following entity definitions: @Entity class abstract A { Collection<A> parents; } @Entity
I'm getting the following warning with this code: WARNING: Using 'Given/When/Then' in step definitions
Given the following definitions Foo has_many :bars, :dependent => :nullify, :autosave => false Bar
Given the following definitions: public enum SampleEnum1 { Uno, Dos, Tres } public enum
Given the following C definitions: #define SYNC_BYTE_1 0x5A #define SYNC_BYTE_2 0xA5 and pointer declaration:
Given the following class definitions: public class BaseClass { public string SomeProp1 { get;
I was given the following struct definitions for an assignment revolving around queues and

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.