简体   繁体   中英

How can I find out which (concrete) types satisfy a set of typeclass constraints?

Given a number of typeclass constraints:

{-# LANGUAGE ConstraintKinds, MultiParamTypeClasses #-}
import Data.Array.Unboxed(Ix,IArray,UArray)

type IntLike a = (Ord a, Num a, Enum a, Show a, Ix a, IArray UArray a)

How can I find out which types satisfy IntLike , ie all the mentioned constraints jointly ?

I can puzzle together the information needed from the output of ghci's :info command, and then doublecheck my work by calling (or having ghci typecheck)

isIntLike :: IntLike -> Bool
isIntLike = const True

at various types, eg isIntLike (3::Int) .

Is there a way to get ghci to do this for me?

I'm currently interested in concrete types, but wouldn't mind having a more general solution which also does clever stuff with unifying contexts!

Community Wiki answer based on the comments:

You can do this using template haskell.

main = print $(reify ''Show >>= stringE . show).

This won't work for type synonyms - rather, reify returns the AST representing the type synonym itself, without expanding it. You can check for type synonyms which are constraints, extract the constraints of which that type synonym consists, and continue reifying those.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM