-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved Lift instances for Path/OsPath types
The `Lift` instances support now parametic types and promoted data constructors for the two parameters of the Path/OsPath types.
- Loading branch information
Showing
6 changed files
with
62 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
module Utils | ||
( typeableToType | ||
) where | ||
|
||
import Data.Bifunctor (first) | ||
import Data.Kind (Type) | ||
import qualified Data.List as List | ||
import Data.Typeable (splitTyConApp) | ||
import qualified Language.Haskell.TH.Syntax as TH | ||
import Type.Reflection | ||
|
||
typeableToType :: (Typeable a) => proxy a -> TH.Type | ||
typeableToType = typeRepToType . someTypeRep | ||
|
||
typeRepToType :: SomeTypeRep -> TH.Type | ||
typeRepToType rep = | ||
uncurry (foldl' f) | ||
. first tyConToType | ||
. splitTyConApp | ||
$ rep | ||
where | ||
f :: TH.Type -> SomeTypeRep -> TH.Type | ||
f memo = TH.AppT memo . typeRepToType | ||
|
||
tyConToType :: TyCon -> TH.Type | ||
tyConToType tc = | ||
(if isType then TH.ConT else TH.PromotedT) | ||
( TH.Name | ||
(TH.OccName (List.dropWhile (== '\'') (tyConName tc))) | ||
( TH.NameG | ||
(if isType then TH.TcClsName else TH.DataName) | ||
(TH.PkgName (tyConPackage tc)) | ||
(TH.ModName (tyConModule tc)) | ||
) | ||
) | ||
|
||
isType :: Bool | ||
isType = someTypeRepKind rep == SomeTypeRep (typeRep @Type) | ||
|
||
someTypeRepKind :: SomeTypeRep -> SomeTypeRep | ||
someTypeRepKind (SomeTypeRep rep) = SomeTypeRep (typeRepKind rep) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters