mokus0 / dependent-sum-template Goto Github PK
View Code? Open in Web Editor NEWTemplate Haskell code to generate instances of classes from dependent-sum
Template Haskell code to generate instances of classes from dependent-sum
It seems to work with dependent-sum 0.4.
It looks like the canonical version of this package lives at https://github.com/mokus0/dependent-sum/tree/master/dependent-sum-template
If so, this repo should be changed to say so.
# cabal install
Resolving dependencies...
Configuring dependent-sum-template-0.0.0.2...
Building dependent-sum-template-0.0.0.2...
Preprocessing library dependent-sum-template-0.0.0.2...
[1 of 2] Compiling Data.GADT.Show.TH ( src/Data/GADT/Show/TH.hs, dist/build/Data/GADT/Show/TH.o )
[2 of 2] Compiling Data.GADT.Compare.TH ( src/Data/GADT/Compare/TH.hs, dist/build/Data/GADT/Compare/TH.o )
src/Data/GADT/Compare/TH.hs:92:10:
No instance for (Applicative (GComparing a b))
arising from the superclasses of an instance declaration
In the instance declaration for ‘Monad (GComparing a b)’
Failed to install dependent-sum-template-0.0.0.2
cabal: Error: some packages failed to install:
dependent-sum-template-0.0.0.2 failed during the building phase. The exception
was:
ExitFailure 1
dependent-sum-template
must be uploaded to Hackage, since lambdabot now depends on it.
Generated GCompare instances sometimes (always?) have overlapping patterns. I think it just needs to conditionally omit the catch-all lines if there are multiple constructors, but this needs to be checked out.
Here I've asked a question initially about polymorphism with dependent-map
s.
And we've found that the following definitions can work.
data Foo1 a where
AnInt :: Foo1 Int
AString :: Foo1 String
ANum :: (Num n, Typeable n, Show n) => Foo1 n
data Foo2 n a where
AnInt :: Foo2 n Int
AString :: Foo2 n String
ANum :: (Num n, Typeable n, Show n) => Foo2 n
If one defines instances by hand, e.g.:
instance GEq (Foo2 n) where
geq AnInt AnInt = return Refl
geq AString AString = return Refl
geq ANum ANum = return Refl
geq _ _ = Nothing
instance GCompare (Foo2 n) where
gcompare AnInt AnInt = GEQ
gcompare AnInt _ = GLT
gcompare _ AnInt = GGT
gcompare AString AString = GEQ
gcompare AString _ = GLT
gcompare _ AString = GGT
gcompare (ANum :: Foo2 n a) (ANum :: Foo2 n b) = (eqT @a @b) & \case
Just (Refl :: a :~: b) -> GEQ
Nothing -> error "This shouldn't happen"
gcompare ANum _ = GLT
gcompare _ ANum = GGT
But I wonder, doesn't/couldn't deriveGEq
, deriveGCompare
support deriving these kinds of instances somehow? Because hand-defining these doesn't scale much. E.g.
deriveGEq ''Foo2
deriveGCompare ''Foo2
deriveGEq ''(Foo2 n)
deriveGCompare ''(Foo2 n)
These give me the following kinds of errors, respectively:
exp-dep-map.hs:39:1: error:
• Expecting one more argument to ‘Foo2’
Expected kind ‘* -> *’, but ‘Foo2’ has kind ‘* -> * -> *’
• In the first argument of ‘GEq’, namely ‘Foo2’
In the instance declaration for ‘GEq Foo2’
|
39 | deriveGEq ''Foo
| ^^^^^^^^^^^^^^^^^^^^
exp-dep-map.hs:40:19: error: parse error on input ‘Foo2’
|
40 | deriveGEq ''(Foo2 n)
| ^^^
Also, is #6 relevant? PolyKinds makes me think that it may be, as one of the type errors is a kind-error.
cc obsidiansystems/dependent-map#16, because it would be nice to see a small self-contained example in the readme/docs if/when pol'ism is supported.
p.s. If it turns out that this is hard to solve with TH, maybe instances can be derived via generics too?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.