module Data.HList.RecordPuns (
pun
) where
import Language.Haskell.TH
import Language.Haskell.TH.Quote
import Data.HList.Record
import Data.HList.FakePrelude
import Data.List
import Data.HList.HList
pun :: QuasiQuoter
pun = QuasiQuoter {
quotePat = suppressWarning mp . parseRec,
quoteExp = suppressWarning me . parseRec,
quoteDec = error "Data.HList.RecordPuns.quoteDec",
quoteType = error "Data.HList.RecordPuns.quoteType"
}
suppressWarning f (V a) = f (C [V a])
suppressWarning f x = f x
extracts xs = do
record <- newName "record"
let val = tupE
[ [| $(varE record) .!. $label |]
| x <- xs,
let label = [| Label :: Label $(litT (strTyLit x)) |],
x /= "_"
]
ensureLength = [| $(varE record) `asTypeOf` $(minLen xs) |]
lamE [varP record] [| $val `const` $ensureLength |]
minLen :: [t] -> ExpQ
minLen [] = [| error "Data.HList.RecordPuns.minLen" :: r (es :: [*]) |]
minLen (_ : xs) = [| (error "Data.HList.RecordPuns.minLen"
:: r es -> r (e ': es)) $(minLen xs) |]
mkPair :: String -> ExpQ -> ExpQ
mkPair x xe = [| (Label :: Label $(litT (strTyLit x))) .=. $xe |]
me :: Tree -> ExpQ
me (C as) = foldr (\(l,e) acc -> [| $(mkPair l e) .*. $acc |]) [| emptyRecord |] (mes as)
me (D _as) = error "Data.HList.RecordPuns.mp impossible"
me a = do
reportWarning $ "Data.HList.RecordPuns.mp implicit {} added around:" ++ show a
me (C [a])
mes :: [Tree] -> [(String, ExpQ)]
mes (V a : V "@": b : c) = (a, [| $(me b) `hLeftUnion` $(dyn a) |]) : mes c
mes (V a : C b : c) = (a, me (C b)) : mes c
mes (V a : D b : c) = (a, me (C b)) : mes c
mes (V a : b) = (a, varE (mkName a)) : mes b
mes [] = []
mes inp = error $ "Data.HList.RecordPuns.mes: cannot translate remaining:" ++
show (map ppTree inp)
mp :: Tree -> PatQ
mp (C as) =
let extractPats = mps as
tupleP = tupP [ p | (binding, p) <- extractPats, binding /= "_" ]
in viewP (extracts (map fst extractPats)) tupleP
mp (D as) = conP 'Record
[(foldr ( \ (n,p) xs -> conP 'HCons
[ let ty
| n == "_" = [| undefined :: Tagged anyLabel t |]
| otherwise = [| undefined :: Tagged $(litT (strTyLit n)) t |]
in viewP [| \x -> x `asTypeOf` $ty |]
(conP 'Tagged [p]),
xs])
(conP 'HNil [])
(mps as))]
mp a = do
reportWarning $ "Data.HList.RecordPuns.mp implicit {} added around:" ++ show a
mp (C [a])
mps :: [Tree] -> [(String, PatQ)]
mps (V a : V "@" : b : c) = (a, asP (mkName a) (mp b)) : mps c
mps (V a : C b : c) = (a, mp (C b)) : mps c
mps (V a : D b : c) = (a, mp (D b)) : mps c
mps (V "_" : b) = ("_", wildP) : mps b
mps (V a : b) = (a, varP (mkName a)) : mps b
mps [] = []
mps inp = error $ "Data.HList.RecordPuns.mps: cannot translate remaining pattern:" ++
show (map ppTree inp)
data Tree = C [Tree]
| D [Tree]
| V String
deriving Show
parseRec :: String -> Tree
parseRec str = case parseRec' 0 0 [[]] $ lexing str of
[x] -> x
x -> C (reverse x)
parseRec' :: Int -> Int -> [[Tree]] -> [String] -> [Tree]
parseRec' n m accum ("{" : rest) = parseRec' (n+1) m ([] : accum) rest
parseRec' n m accum ("(" : rest) = parseRec' n (m+1) ([] : accum) rest
parseRec' n m (a:b:c) ("}" : rest) = parseRec' (n1) m ((C (reverse a) : b) : c) rest
parseRec' n m (a:b:c) (")" : rest) = parseRec' n (m1) ((D (reverse a) : b) : c) rest
parseRec' n m (b:c) (a : rest)
| a `notElem` ["{","}","(",")"] = parseRec' n m ((V a : b) : c) rest
parseRec' 0 0 (a:_) [] = a
parseRec' _ _ accum e = error ("Data.HList.RecordPuns.parseRec' unexpected: " ++ show e
++ "\n parsed:" ++ show (reverse accum))
ppTree :: Tree -> String
ppTree (C ts) = "{" ++ unwords (map ppTree ts) ++ "}"
ppTree (D ts) = "(" ++ unwords (map ppTree ts) ++ ")"
ppTree (V x) = x
lexing = unfoldr (\v -> case lex v of
("", "") : _ -> Nothing
e : _ -> Just e
_ -> Nothing)