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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:39:22+00:00 2026-06-18T19:39:22+00:00

This is a type question regarding Data.Reflection in Haskell. Reflection lets me take an

  • 0

This is a type question regarding Data.Reflection in Haskell. Reflection lets me take an Int and convert it to a type.

The functions f and g below are our best attempts at something reasonable, if you have a better way, let’s have it!

For example, I could add numbers mod 41 by doing something like:

import Data.Reflection
import Data.Proxy

newtype Zq q i = Zq i deriving (Eq)
instance (Reifies q i, Integral i) => Num (Zq q i) where
   (...)
zqToIntegral :: (Reifies q i, Integral i) => Zq q i -> i
   (...)

f :: forall i . (Integral i) => i -> (forall q . Reifies q i => Zq q i) -> i
f modulus k = 
    reify modulus (\ (_::Proxy t) -> zqToIntegral (k :: Zq t i)

Then

>>:t (f 41 (31+15))
(f 41 (31+15)) :: Integral i => i

However, we would like to write a function like:

g :: forall i . (Integral i) => i -> (forall q . Reifies q i => Zq q i) -> Zq q i
g modulus k = 
    reifyIntegral modulus (\ (_::Proxy t) -> (k :: Zq t i)

and would like to get:

>>:t (g 41 (31+15))
(g 41 (31+15)) :: <some type info> => Zq q i

The difference is that we would like to be able to return a type that uses a reified int. At least one problem with the definition above is that the rank-2 type q is not visible to the return type.

The signature for reify in Data.Reflection is

reify :: a -> (forall s. Reifies s a => Proxy s -> r) -> r

which as far we can tell requires the rank-2 type, and we don’t know (if it is indeed possible) how to expose this type to the return type of the function.

  • 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-18T19:39:23+00:00Added an answer on June 18, 2026 at 7:39 pm

    You can lift a value to a type. But the type can’t escape the function that does the lifting. That’s just how rank 2 types work. Imagine if we could write g as you describe. Then, given some user-input value, we could get a Zq of varying qs at runtime.

    But then what could we do with them?

    If we have two Zq with the same q, we can add them. But we wouldn’t know that they were the same q or not until runtime! And that’s too late to check types, since we need to decide if we can add them or not at compile time. If you ignore the fact that q is in a phantom position, this is the same reason you can’t have a function that returns an Int or a Bool at compile time, based on input (you can use an Either, of course).

    So, as noted in the comment, you can do a few different things.

    One thing you could do is just return the modulus (i.e. the value-level version of the q) at runtime, along with your result. Then you can always use it later.

    That looks like this:

    g :: forall i . (Integral i) => i -> (forall q . Reifies q i => Zq q i) -> (i,i)
    g modulus k = reify modulus (\ m@(_::Proxy t) -> (zqToIntegral (k :: Zq t i), reflect m))
    

    Another thing you can do is use existentials like so:

    data ZqE i = forall q. ZqE (Zq q i)
    
    h :: forall i . (Integral i) => i -> (forall q . Reifies q i => Zq q i) -> ZqE i
    h modulus k = reify modulus (\ (_::Proxy t) -> ZqE (k :: Zq t i))
    

    Now the only thing we can do with our ZqE is unpack it and return something else which also doesn’t expose q directly in the type.

    Note that we have no way to know that the q in any two ZqE are equal, so we can’t do operations combining them directly, but rather should be creating them all under the same reify call. And this is not a bug, but a feature!

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

Sidebar

Related Questions

Updated Question: $(this).attr(EmployeeId, 'A42345'); $.ajax({ type: POST, url: url, data: {EmployeeId: ' + id
This question is regarding the mad functions available in OpenCL which promise significant improvements
I know this type of question has been asked and answered before but I
It seems this type of question has been asked and answered a number of
I have seen many answers for this type of question but its not related
Firstly, I know this [type of] question is frequently asked, so let me preface
I am referring to this question type Churchlist t u = (t->u->u)->u->u In lambda
I have little question. I found this on internet: Type t = typeof(MyClass); MethodInfo[]
This is a simple question really. I've been using the new type of constructors
The opposite of this question: How do I add a type to GWT's Serialization

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.