Twitter: @fumieval
fumieval / barbies-th Goto Github PK
View Code? Open in Web Editor NEWEasy declaration of strippable HKD
Home Page: http://hackage.haskell.org/package/barbies-th
License: BSD 3-Clause "New" or "Revised" License
Easy declaration of strippable HKD
Home Page: http://hackage.haskell.org/package/barbies-th
License: BSD 3-Clause "New" or "Revised" License
Twitter: @fumieval
barbies-th works fine with DuplicateRecordFields enabled (thanks to the name unmangling patches); but it can't handle record fields that are actually duplicated. Possibly this is an accepted limitation, but it would be nice to document it I think. The error message is not very friendly:
... Assembler messages: ... Error: symbol `testlibzm0zi1zi0zi0zminplace_Lib_qux_info' is already defined ... testlibzm0zi1zi0zi0zminplace_Lib_qux_info
@Elvecent reported this as well, at the bottom of the thread in #3.
GHC 8.10.7
barbies == 2.0.3.1
barbies-th == 0.1.10
Example:
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DuplicateRecordFields #-}
module Lib where
import Barbies.TH (declareBareB)
declareBareB
[d|
data Foo = Foo
{ qux :: Integer
}
|]
declareBareB
[d|
data Bar = Bar
{ qux :: Integer
}
|]
cabal-version: 3.4
name: testlib
version: 0.1.0.0
build-type: Simple
common warnings
ghc-options: -Wall
library
import: warnings
exposed-modules: Lib
build-depends:
base ^>=4.14.3.0,
barbies == 2.0.3.1,
barbies-th == 0.1.10
hs-source-dirs: src
default-language: Haskell2010
peter@gtower:~/2022october8_reproduce_barbies_error$ cabal build
Build profile: -w ghc-8.10.7 -O1
In order, the following will be built (use -v for more details):
- testlib-0.1.0.0 (lib) (first run)
Preprocessing library for testlib-0.1.0.0..
Building library for testlib-0.1.0.0..
[1 of 1] Compiling Lib ( src/Lib.hs, /home/peter/2022october8_reproduce_barbies_error/dist-newstyle/build/x86_64-linux/ghc-8.10.7/testlib-0.1.0.0/build/Lib.o, /home/peter/2022october8_reproduce_barbies_error/dist-newstyle/build/x86_64-linux/ghc-8.10.7/testlib-0.1.0.0/build/Lib.dyn_o )
/tmp/ghc80775_0/ghc_2.s: Assembler messages:
/tmp/ghc80775_0/ghc_2.s:1298:0: error:
Error: symbol `testlibzm0zi1zi0zi0zminplace_Lib_qux_info' is already defined
|
1298 | testlibzm0zi1zi0zi0zminplace_Lib_qux_info:
| ^
/tmp/ghc80775_0/ghc_2.s:1329:0: error:
Error: symbol `testlibzm0zi1zi0zi0zminplace_Lib_qux_closure' is already defined
|
1329 | testlibzm0zi1zi0zi0zminplace_Lib_qux_closure:
| ^
`gcc' failed in phase `Assembler'. (Exit code: 1)
I tried to minimize the testcase further by expanding the splices and minimizing the generated code, but I was unable to achieve further minimization. I expanded one splice using the code-action offered by HLS. I found that the generated code is referencing a symbol called (Unit :: (Type -> Constraint) -> Type -> Constraint). I stubbed out such a type like this:
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE UndecidableSuperClasses #-}
import Data.Kind (Constraint, Type)
class c t => Unit (c :: Type -> Constraint) (t :: Type) where
{- empty body -}
When I added that bit, the generated code started compiling. That is, without the "symbol already defined" error.
Thanks for the library, seems cool.
Currently, if I want to use Barbies-TH to create a HKD that has per-field Semigroup
and Monoid
instances, I can do it like so:
declareBareB [d|
data CPUOut = CPUOut
{ progAddr :: PC
, memAddr :: Ptr
, memWrite :: Maybe Cell
, output :: Maybe Cell
, inputNeeded :: Bool
}|]
deriving via Barbie (CPUOut Covered) f instance (forall a. Semigroup (f a)) => Semigroup (CPUOut Covered f)
deriving via Barbie (CPUOut Covered) f instance (forall a. Monoid (f a)) => Monoid (CPUOut Covered f)
Would it be possible to automate this / hide the deriving via
magic, and allow the user to instead write
declareBareB [d|
data CPUOut = CPUOut
{ progAddr :: PC
, memAddr :: Ptr
, memWrite :: Maybe Cell
, output :: Maybe Cell
, inputNeeded :: Bool
}
deriving (Semigroup, Monoid)|]
which would generate the same code as above?
I have problems deriving Show and Eq instances for a data class when following the instructions here: https://github.com/fumieval/barbies-th?tab=readme-ov-file#deriving-pass-through
I added these two test cases to the test suite of barbies-th and they also fail to come up with the according instances.
test_show :: String
test_show = show test_con
test_eql :: Bool
test_eql = test_con == test_con
Do I have a problem understanding how to derive the instances with barbies-th or is the functionality broken at the moment?
It would be nice to be able to do something like this:
declareBareB [d|
data Foo = Foo { foo :: Int }
data Bar = Bar { bar :: Foo }
|]
And have both Foo
and Bar
become barbies, with Foo
inheriting the clothing style form Bar
Example source illustrating the problem:
{-# LANGUAGE TemplateHaskell, FlexibleInstances, DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
import Barbies
import Data.Barbie.TH
declareBareB [d|
data R = MkR
{ foo :: Int
} |]
This fails with:
src/Foo.hs:6:1: error:
Illegal variable name: ‘$sel:foo:MkR’
When splicing a TH declaration:
data R_0 sw_1 h_2
= MkR_3 {$sel:foo:MkR_4 :: (Barbies.Internal.Wear.Wear sw_1
h_2
GHC.Types.Int)}
deriving GHC.Generics.Generic
|
6 | declareBareB [d|
| ^^^^^^^^^^^^^^^^...
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.