Import a data constructor without importing the type - 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. :/

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.

My current solution, discovered moments before posting:
Give up on trying to import (:%).
Keep the qualified import.
Change :% to Ratio.:% everywhere (patterns and expressions).
Result:
{-# LANGUAGE NoImplicitPrelude #-}
module Number.SqrtRatio (T(..), ratioPart) where
import qualified Number.Ratio as 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 Ratio.:% d
fromRatio :: (Ring.C x) => Ratio.T x -> T x
fromRatio (n Ratio.:% d) = T n d Ring.one
Ugly.

Related

Type family constraints at runtime // Couldn't match type `1 <=? n0' with 'True

StackOverflow!
For reasons that would like to remain between me and God, I'm currently playing around with promoting runtime naturals to the type level. I've been following this approach with GHC.TypeLits, which has worked out fine so far.
However, in one instance, I have an additional constraint of 1 <= n, i.e. my promoted natural not to be just any natural, but at least 1. This is also from GHC.TypeLits And I am unsure if/how it is possible to extract and make that information known.
Here's a minimal non-working example:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
import Data.Maybe
import Data.Proxy
import GHC.TypeLits
import Numeric.Natural
data AnyNat (n :: Nat) where
AN :: AnyNat n
data AtLeast1Nat (n :: Nat) where
AL1N :: AtLeast1Nat n
promote0 :: Natural -> AnyNat n
promote0 k = case sn of
SomeNat (_ :: Proxy p) -> AN
where
sn = (fromJust . someNatVal . toInteger) k
promote1 :: (KnownNat n, 1 <= n) => Natural -> AtLeast1Nat n
promote1 k = case sn of
SomeNat (_ :: Proxy p) -> AL1N
where
sn = (fromJust . someNatVal . toInteger) k
main :: IO ()
main = do nat_in <- getLine
let nat = read nat_in :: Natural
let p0 = promote0 nat
let p1 = promote1 nat
putStrLn "Last statement must be an expression"
This produces this error (full error here, but this is the relevant part):
* Couldn't match type `1 <=? n0' with 'True
arising from a use of `promote1'
The type variable `n0' is ambiguous
Honestly, this isn't too surprising and I (think I) do understand why this happens. The Natural that we give in could be any of them, so why would we be able to derive that 1 <= n? That's why it works fine for promote0 and not promote1.
My question is hence, is there any way to also check (and propagate to type-level) this information so I can use it as intended, or am I using the wrong approach here?
You're using the wrong approach.
As discussed in the comments, promote0 (and similarly promote1) isn't doing what you're hoping. The problem is that the AN on the right-hand-side of the case has type AnyNat n for some n entirely unrelated to the term sn. You could have written:
promote0 k = case 2+2 of 4 -> AN
and gotten much the same effect. Note the critical difference between your code and the other Stack Overflow answer you link: in that answer, the type variable n in the case scrutinee is used to type something (via ScopedTypeVariables) in the case branch. You bind a type variable p in your scrutinee but then don't use it for anything.
If we consider your actual problem, suppose we want to write something like this:
import qualified Data.Vector.Sized as V
main = do
n <- readLn :: IO Int
let Just v = V.fromList (replicate n 1)
v2 = v V.++ v
print $ v2
This won't type check. It gives an error on V.fromList about the lack of a KnownNat constraint. The issue is that v has been assigned a type S.Vector k Int for some k :: Nat. But V.fromList performs a runtime check that the length of the input list (the run time value n) is equal to the type-level k. To do this, k must be converted to a runtime integer which requires KnownNat k.
The general solution, as you've guessed, is to construct a SomeNat that basically contains a KnownNat n => n that's unknown at compile time. However, you don't want to try to promote it to a known type-level Nat (i.e., you don't want promote0). You want to leave it as-is and case match on it at the point you need its type-level value. That type-level value will be available within the case but unavailable outside the case, so no types that depend on n can "escape" the case statement.
So, for example, you can write:
import qualified Data.Vector.Sized as V
import Data.Proxy
import GHC.TypeNats
main = do
n <- readLn :: IO Int
-- keep `sn` as the type-level representation of the runtime `n`
let sn = someNatVal (fromIntegral n)
-- scrutinize it when you need its value at type level
case sn of
-- bind the contained Nat to the type variable `n`
SomeNat (Proxy :: Proxy n) -> do
-- now it's available at the type level
let v = V.replicate #n 1 -- using type-level "n"
v2 = v V.++ v
print v2
but you can't write:
main :: IO ()
main = do
n <- readLn :: IO Int
let sn = someNatVal (fromIntegral n)
let v2 = case sn of
SomeNat (Proxy :: Proxy n) ->
let v = V.replicate #n 1
in v V.++ v
print v2
You'll get an error that a type variable is escaping its scope. (If you want to let sized vectors leak outside the case, you need to make use of V.SomeSized or something similar.)
As for the main part of your question about handling a 1 <= n constraint, dealing with inequalities for type-level naturals is a major headache. I think you'll need to post a minimal example of exactly how you want to use this constraint in the context of a sized vector imlementation, in order to get a decent answer.

Polymorphic lens without template haskell

I am trying to create a polymorphic lens decleration (without template haskell) for multiple types.
module Sample where
import Control.Lens
data A = A {_value:: Int}
data B = B {_value:: Int}
data C = C {_value:: String}
value = lens _value (\i x -> i{_value=x}) -- <<< ERROR
But I get following error:
Ambiguous occurrence ‘_value’
It could refer to either the field ‘_value’,
defined at library/Sample.hs:5:13
or the field ‘_value’, defined at
library/Sample.hs:4:13
or the field ‘_value’, defined at
library/Sample.hs:3:13
|
6 | value = lens _value (\i x -> i{_value=x}) -- <<< ERROR
| ^^^^^^
So, goal is to have value lens to work on all three types A, B and C. Is there a way to achieve that?
Thanks.
Lenses can be derived without TH by generic-lens. You can specialize the generic field lens to a specific field and give it a name as follows.
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeApplications #-}
import GHC.Generics (Generic)
import Control.Lens (Lens, (^.))
import Data.Generics.Product (HasField(field))
data A = A { _value :: Int } deriving Generic
data B = B { _value :: Int } deriving Generic
data C = C { _value :: String } deriving Generic
value :: HasField "_value" s t a b => Lens s t a b
value = field #"_value"
main :: IO ()
main = print (A 0 ^. value, B 0 ^. value, C "0" ^. value)
Haskell does not support overloading functions the way a language like Java or C++ does. To do what you want you need to use a typeclass like so.
class HasValue a b where
value :: Lens' a b
data A = A {_valueA:: Int}
data B = B {_valueB:: Int}
data C = C {_valueC:: String}
instance HasValue A Int where
value = lens _valueA (\i x -> i{_valueA=x})
instance HasValue B Int where
value = lens _valueB (\i x -> i{_valueB=x})
instance HasValue C String where
value = lens _valueC (\i x -> i{_valueC=x}
You'll need to enable multiparameter typeclasses to do this.
If you want to avoid DuplicateRecordFields, another option is to define everything in its own module, though this will require qualified imports for them to be imported into the same module.
module Sample.A where
import Control.Lens
data A = A {_value:: Int}
value = lens _value (\i x -> i{_value=x})
module Sample.B where
import Control.Lens
data B = B {_value:: Int}
value = lens _value (\i x -> i{_value=x})
module Sample.C where
import Control.Lens
data C = C {_value:: String}
value = lens _value (\i x -> i{_value=x})
module Main where
import qualified Sample.A as A
import qualified Sample.B as B
import qualified Sample.C as C
main :: IO ()
main = print (A.A 0 ^. A.value, B.B 0 ^. B.value, C.C "0" ^. C.value)

How can I deal with “typedef”-style data types with a minimum of boilerplate?

I defined a custom data type that contains a single field:
import Data.Set (Set)
data GraphEdge a = GraphEdge (Set a)
Defining my own type feels more semantically correct but it leads to a lot of boilerplate in my functions. Any time I want to use the built-in Set functions I have to unwrap the inner set and later rewrap it:
import Data.Set (map)
modifyItemsSomehow :: Ord a => GraphEdge a -> GraphEdge a
modifyItemsSomehow (GraphEdge items) = GraphEdge $ Set.map someFunction items
This could be improved slightly by making it a record, like
import Data.Set (Set, map)
data GraphEdge a = GraphEdge { unGraphEdge :: Set a }
modifyItemsSomehow = GraphEdge . map someFunction . unGraphEdge
but this still feels far from ideal. What is the most idiomatic way to handle this kind of boilerplate when dealing with a user-defined data type that consists of a single field?
Before anything else, you should make sure to use newtype for single-field single-constructor types. data introduces runtime overhead and extra laziness, and prevents us from using the first two of the following techniques.
First, you can use GeneralizedNewtypeDeriving when possible:
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
newtype Foo a = Foo a deriving (Eq, Show, Ord, Num)
foo :: Foo Int
foo = 0
bar :: Foo Int
bar = foo * 120
Second, you can use coerce to generally convert between newtype wrappings:
import Data.Coerce
newtype Foo a = Foo a
newtype Bar a = Bar a
a :: [(Foo (Bar Int), Foo ())]
a = [(Foo (Bar 0), Foo ())]
b :: [(Int, ())]
b = coerce a
Third, you can use iso-s from lens to concisely move operations over/under newtype constructors.
{-# LANGUAGE TemplateHaskell #-}
import Data.Set (Set)
import qualified Data.Set as Set
import Control.Lens
newtype GraphEdge a = GraphEdge (Set a)
makePrisms ''GraphEdge
modifyItemsSomehow :: Ord a => GraphEdge a -> GraphEdge a
modifyItemsSomehow = _GraphEdge %~ Set.map someFunction

How to create a value in compdata

I've got the following compdata example code
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TemplateHaskell #-}
module Web.Rusalka.Hello (
iB, iV, Z) where
import Data.Comp
import Data.Comp.Ops
import Data.Comp.Show ()
import Data.Comp.Derive
data B e = B Bool
data V a e = V a
type Z a = Term (B :+: V a)
$(derive [makeFunctor, makeTraversable, makeFoldable,
makeEqF, makeShowF, smartConstructors, smartAConstructors,
makeArbitrary, makeArbitraryF]
[''B, ''V])
(You'll note that in fact, everything in Z is a leaf node.)
Now, as I understand it, this has created two functions, iB and iV, that can be used to create (Z a) s. However, I can't for the life of me figure
out how to create, for instance a (Z Int). What do I need to put in? (Or what am I misunderstanding?)
iB True :: Z Int or iV (1 :: Int) :: Z Int produce valid, printable expressions within this module.

Derivative Towers and how to use the vector-space package (haskell)

I am working with Haskell for quite a while now, but I am far from being an expert. But I see that the functional approach to programming suits me the best.
So far I am working on a project to calculate some serious stuff, like currents and potentials radiated from a given structure.
I followed the blog written by Conal Elliott (here is some more Linear Maps) which is very nice and fundamental.
Unfortunately, I am lacking a simple example :)
To be more precise, I have a curve
f:[0,1] in R -> R³
t -> a*e_y + 2*t*e_z
which is a simple straight line at (0,a,2*t).
When I want to calculate the derivative of f, e.g. for the length of the curve, I know the mathematical result, which is quite simple (0,0,2), but how do I accomplish this in Haskell, especially with the vector-space package?
I really want to use this library because of its functionality, it is exactly the approach I would have take too (but I am not that far ahead on the Haskell road)
What I have so far is this:
{-# LANGUAGE Rank2Types, TypeOperators, FlexibleContexts, TypeFamilies #-}
{-# OPTIONS_GHC -Wall #-}
import Numeric.GSL.Integration
import Data.VectorSpace
import Data.Basis
import Data.Cross
import Data.Derivative
import Data.LinearMap
type Vec3 s = Three s
prec :: Double
prec = 1E-9
f1 :: (Floating s, VectorSpace s, Scalar s ~ s) => s -> s
f1 = id
c1 :: Double -> Vec3 Double
c1 = \t -> linearCombo [((v 0 0 1),f1 t),(( v 0 1 0),2)]
derivC :: Double -> Vec3 (Double :> Double)
derivC t = c1 (pureD t)
It is the the actual implementation of the pureD function, so far nothing that I have tried works to get this snippet to compile. I get the following error:
tests.hs:26:12:
Couldn't match expected type `Double :> Double'
with actual type `Double'
Expected type: Vec3 (Double :> Double)
Actual type: Vec3 Double
In the return type of a call of `c1'
In the expression: c1 (pureD t)
Failed, modules loaded: none.
There is also a graphics library which uses vector-space and there is even an example on a torus, where pureD is used. I tried to deduce the example but I don't see how I can map it to my problem.
Any help would be greatly appreciated.
Thanks in advance
PS: I cannot post all the links I'd like to, but am willing to provide
That's an interesting library.. Thanks for sharing.
Although I don't understand the concept of the library yet,
how about this code:
{-# LANGUAGE Rank2Types, TypeOperators, FlexibleContexts, TypeFamilies #-}
module Main where
import Data.LinearMap
import Data.Maclaurin
diff :: (Double :~> (Double,Double,Double) ) -> (Double :~> (Double,Double,Double))
diff f = \x -> (atBasis (derivative (f x)) ())
eval :: (Double :~> (Double,Double,Double)) -> Double -> (Double,Double,Double)
eval f x = powVal (f x)
f :: Double :~> (Double,Double,Double)
f x = tripleD (pureD 0,pureD 1,(2*idD) x)
*Main> map (eval f) [0,0.2 .. 1]
[(0.0,1.0,0.0),(0.0,1.0,0.4),(0.0,1.0,0.8),(0.0,1.0,1.2000000000000002),
(0.0,1.0,1.6000000000000003),(0.0,1.0,2.0000000000000004)]
*Main> map (eval (diff f)) [0,0.2 .. 1]
[(0.0,0.0,2.0),(0.0,0.0,2.0),(0.0,0.0,2.0),(0.0,0.0,2.0),(0.0,0.0,2.0),
(0.0,0.0,2.0)]
*Main> map (eval (diff $ diff f)) [0,0.2 .. 1]
[(0.0,0.0,0.0),(0.0,0.0,0.0),(0.0,0.0,0.0),(0.0,0.0,0.0),(0.0,0.0,0.0),(0.0,0.0,0.0)]
Try also g x = tripleD (pureD 0,idD x,(idD*idD) x) (which seem to represent the curve (0,x,x^2)).
You might want to try the ad package, which does its best to make it easy to do automatic differentiation of functions written in transparent idiomatic Haskell.
$ cabal install ad
$ ghci
Prelude> :m + Numeric.AD
Prelude Numeric.AD> diffF (\t->let a=3 in [0,a,2*t]) 7
[0,0,2]
Prelude Numeric.AD> let f t = let a=3 in [0,a,2*t]
Prelude Numeric.AD> diffF f 17
[0,0,2]

Resources