Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value type in haskell

Tags:

types

haskell

I'm trying to debug and would like to be able to get a function which returns a type of a value. Is such a thing possible?

I dont wan't :t, I wan't something which can be used in code

such as:

[showType i | i <- [0..5] ]

returns

[Int,Int,Int,Int,Int]
like image 702
ditoslav Avatar asked Nov 26 '25 21:11

ditoslav


2 Answers

The Typeable class encodes each type uniquely at compile time and can be used for this purpose:

import Data.Typeable

showType :: Typeable a => a -> String
showType = show . typeOf

with a result of:

*Main> showType (+)
"Integer -> Integer -> Integer"
*Main> showType [1..5]
"[Integer]"
*Main> map showType [1..5]
["Integer","Integer","Integer","Integer","Integer"]

All this said, Bheklilr is right. What you actually want might be something different so more details would help us help you.

like image 104
Thomas M. DuBuisson Avatar answered Nov 29 '25 19:11

Thomas M. DuBuisson


Maybe you rather want something like TypedHoles. It allows you to write _ instead of some expression, and the compiler will tell you what type this “hole” has, or should have.

like image 45
Joachim Breitner Avatar answered Nov 29 '25 19:11

Joachim Breitner