Comments (5)
how much do we want this feature? This will be much harder to achieve mainly because of how the
algorithm is structured. The problem lies in the fact that even a type synonym with no args
might require some beta-reduction to be correctly reified, ie:
type MyType = MyOtherType Int (Bool, [Float])
If you think this is an important feature to support, I'll think harder about it.
from generics-mrsop.
I also ran into this in the wild;
I'd like to tackle this for this 2.2 version unifying with sop-core
from generics-mrsop.
Note that the th-abstraction
package (which I mentioned in #56) has a function which does exactly what you need: resolveTypeSynonyms
. You can even use it without buying the whole library, since the function works on Template Haskell Type
s.
from generics-mrsop.
@serras That seems like a lovely library; I have just tried implementing our Generics.MRSOP.TH
module with it this morning but dind't really work for us for two main reasons, explained below.
tl;dr
A naive impl based on th-abstraction
won't work. I will think a bit harder about it
Reason 1
All the generation is based on our own datatype:
data DTI var = ADT Name ... [CI var] | New Name ... (CI var)
data CI var = NormalConstr ... | RecordConstr ...
And the beauty of it is that the type pipeline is as follows:
- reify, then simplify a type to our internal language:
sty <- simpl <$> reify name
, heresty :: STy
. - Register
sty
in a datatbase, assigning it an index. - Flatten the topmost type applications, yielding
head , args :: (STy , [STy])
. For example,
ifsty == Maybe Int
in (1), we will havehead == Maybe
andargs == [Int]
. - Now we reify the definition of
head
in (3) and beta-reduce with the arguments inargs
.
This yields aDTI STy
, in the maybe case would be something in the lines of:Datatype "Maybe" [Int] [Constr "Just" [simpl Int] , Constr "Nothing"] :: DTI STy
- We now mapM over this
DTI STy
with a functionkeepGoing :: STy -> Writer [STy] IK
, where
wetell
the types we have not yet registered, and need to recurse on them, but return
aDTI IK
, which is already exactly the code we need. (data IK = I Int | K Name
)
Once we reach a fixpoint of the process above, we end up with a [DTI IK]
, with self-contained indexes. This makes the generation of the codes very easy. Using th-abstraction
we have this
monstrous DatatypeInfo
type with all sorts of info we don't need.
Reason 2
The resolveTypeSynonyms
is very aggresive. For example, It resolves all occurencs of String
to [Char]
, which makes the engine detecting that String
is supposed to be an opaque type
fail. I'm yet unsure on how to get around that!
from generics-mrsop.
I believe I found a decent hack for it in 7c8cdab
Type synonyms seem to be working when generating codes.
Before type synonyms, we kept the following state through the generation:
data Idxs
= Idxs { idxsNext :: Int
, idxsMap :: M.Map STy (Int , Maybe (DTI IK))
}
deriving (Show)
I have added an explicit "this type is equivalent to this" in there:
data Idxs
= Idxs { idxsNext :: Int
, idxsMap :: M.Map STy (Int , Maybe (DTI IK))
, idxsSyns :: M.Map STy STy
}
deriving (Show)
Now, every time we see a type synonym we exand, reduce and register as equivalent,
as it can be seen here
from generics-mrsop.
Related Issues (20)
- Create pattern synonyms for `Tag` automatically
- Implement Attribute Grammars HOT 8
- TH Generates dupicate pattern synonyms if type is in AST multiple times HOT 1
- GHC 8.4.3 triggers GHC Bug during Haddock generations :( HOT 3
- Use Generic Generic Programming to avoid Template Haskell? HOT 2
- Fix is AnnFix HOT 1
- Use efficient indexed Sum from Oleg's Freer Monads paper HOT 3
- Depend on sop-core HOT 14
- Change Eq1 and Show1 names HOT 1
- Drop Monad constraint on mapNPM and friends HOT 1
- Slightly tweak Rep and Fix for pedagogical reasons HOT 5
- Can only derive one datatype per module HOT 4
- Hide examples from documentation
- Make Attribute Grammar combinators monadic HOT 1
- Bring Treefixes into mrsop. HOT 1
- GHC 8.4.3 crashes with hs-elisp parser. HOT 2
- Better Naming Framework for TH code
- Use `th-abstraction`
- Bring back `ShowHO` and `EqHO` as we had before HOT 3
- Rename Holes constructors; make it standard HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from generics-mrsop.