Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import a data constructor without importing the type

Tags:

import

haskell

numeric-prelude does this thing where every data type is named T and every type-class is named C. For the sake of... consistency, I suppose I'll play along:

{-# LANGUAGE NoImplicitPrelude #-}
module Number.SqrtRatio (T(..), ratioPart) where

import qualified Number.Ratio as Ratio
import Number.Ratio ((:%))

import qualified Algebra.Ring as Ring
import NumericPrelude.Base

-- | A number whose square is rational, canonicalized as a rational
--   times the square root of a squarefree integer.
data T x = T {
    numerator :: !x,
    denominator :: !x,
    rootNum :: !x
    } deriving (Eq, Show)

ratioPart :: T x -> Ratio.T x
ratioPart (T n d _) = n :% d

fromRatio :: (Ring.C x) => Ratio.T x -> T x
fromRatio (n :% d) = T n d Ring.one

ghc is not impressed:

Number/SqrtRatio.hs:5:22:
    In module ‘Number.Ratio’:
      ‘(:%)’ is a data constructor of ‘T’
    To import it use
      ‘import’ Number.Ratio( T( (:%) ) )
    or
      ‘import’ Number.Ratio( T(..) )

Sure thing buddy, I can comply:

{-# LANGUAGE NoImplicitPrelude #-}
module Number.SqrtRatio (T, ratioPart) where

import qualified Number.Ratio as Ratio
import Number.Ratio (T((:%)))
--       newly added ^

...but this also ends up importing Ratio.T, which conflicts with my T!

ratioPart :: T x -> Ratio.T x
{-           ^-- Ambiguous occurrence ‘T’
    It could refer to either ‘Number.SqrtRatio.T’,
                             defined at Number/SqrtRatio.hs:11:1
                          or ‘Number.Ratio.T’,
                             imported from ‘Number.Ratio’ at Number/SqrtRatio.hs:5:22-28
-}

Alright, so how about import Number.Ratio (T((:%))) hiding T?

Number/SqrtRatio.hs:5:31: parse error on input ‘hiding’

I'm at a bit of a loss, gaise. :/

like image 628
Exp HP Avatar asked Sep 10 '16 23:09

Exp HP


1 Answers

Turns out there is a proper way to do this:

{-# LANGUAGE NoImplicitPrelude, PatternSynonyms #-}
module Number.SqrtRatio (T(..), ratioPart) where

import qualified Number.Ratio as Ratio
import Number.Ratio (pattern (:%))

Note that I've used the -XPatternSynonyms extension not to actually define any pattern synonym, just to enable the pattern keyword so it's clear I want to import the value constructor :% alone.

like image 115
leftaroundabout Avatar answered Sep 23 '22 18:09

leftaroundabout