2{-# LANGUAGE EmptyDataDecls #-}
4{-# LANGUAGE PatternGuards #-}
5{-# LANGUAGE ScopedTypeVariables #-}
6{-# LANGUAGE TemplateHaskell #-}
7{-# LANGUAGE TypeOperators #-}
8#ifdef USE_REFLEX_OPTIMIZER
9{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
11-- | Template Haskell helper functions for building complex 'Dynamic' values.
12module Reflex.Dynamic.TH
20import Control.Monad.State
23import Data.Monoid ((<>))
24import qualified Language.Haskell.Exts as Hs
25import qualified Language.Haskell.Meta.Syntax.Translate as Hs
26import Language.Haskell.TH
27import Language.Haskell.TH.Quote
28import qualified Language.Haskell.TH.Syntax as TH
30-- | Quote a 'Dynamic' expression. Within the quoted expression, you can use
31-- @$(unqDyn [| x |])@ to refer to any expression @x@ of type @Dynamic t a@; the
32-- unquoted result will be of type @a@
33qDynPure :: Q Exp -> Q Exp
36 let f :: forall d. Data d => d -> StateT [(Name, Exp)] Q d
38 Just (Refl :: d :~: Exp)
39 | AppE (VarE m) eInner <- d
41 -> do n <- lift $ newName "dynamicQuotedExpressionVariable"
45 (e', exprsReversed) <- runStateT (gmapM f e) []
46 let exprs = reverse exprsReversed
47 arg = foldr (\a b -> ConE 'FHCons `AppE` snd a `AppE` b) (ConE 'FHNil) exprs
48 param = foldr (\a b -> ConP 'HCons [VarP (fst a), b]) (ConP 'HNil []) exprs
49 [| $(return $ LamE [param] e') <$> distributeFHListOverDynPure $(return arg) |]
51-- | Antiquote a 'Dynamic' expression. This can /only/ be used inside of a
53unqDyn :: Q Exp -> Q Exp
54unqDyn e = [| unqMarker $e |]
56-- | This type represents an occurrence of unqDyn before it has been processed
57-- by qDyn. If you see it in a type error, it probably means that unqDyn has
58-- been used outside of a qDyn context.
61-- unqMarker must not be exported; it is used only as a way of smuggling data
64--TODO: It would be much nicer if the TH AST was extensible to support this kind of thing without trickery
65unqMarker :: a -> UnqDyn
66unqMarker = error "An unqDyn expression was used outside of a qDyn expression"
68-- | Create a 'Dynamic' value using other 'Dynamic's as inputs. The result is
69-- sometimes more concise and readable than the equivalent 'Applicative'-based
70-- expression. For example:
72-- > [mkDyn| $x + $v * $t + 1/2 * $a * $t ^ 2 |]
74-- would have a very cumbersome 'Applicative' encoding.
75mkDynPure :: QuasiQuoter
76mkDynPure = QuasiQuoter
78 , quotePat = error "mkDyn: pattern splices are not supported"
79 , quoteType = error "mkDyn: type splices are not supported"
80 , quoteDec = error "mkDyn: declaration splices are not supported"
83mkDynExp :: String -> Q Exp
84mkDynExp s = case Hs.parseExpWithMode Hs.defaultParseMode { Hs.extensions = [ Hs.EnableExtension Hs.TemplateHaskell ] } s of
85 Hs.ParseFailed (Hs.SrcLoc _ l c) err -> fail $ "mkDyn:" <> show l <> ":" <> show c <> ": " <> err
86 Hs.ParseOk e -> qDynPure $ return $ everywhere (id `extT` reinstateUnqDyn) $ Hs.toExp $ everywhere (id `extT` antiE) e
87 where TH.Name (TH.OccName occName) (TH.NameG _ _ (TH.ModName modName)) = 'unqMarker
88#if MIN_VERSION_haskell_src_exts(1,18,0)
89 antiE :: Hs.Exp Hs.SrcSpanInfo -> Hs.Exp Hs.SrcSpanInfo
92 Hs.App l (Hs.Var l $ Hs.Qual l (Hs.ModuleName l modName) (Hs.Ident l occName)) $ case se of
93 Hs.IdSplice l2 v -> Hs.Var l2 $ Hs.UnQual l2 $ Hs.Ident l2 v
94 Hs.ParenSplice _ ps -> ps
99 Hs.App (Hs.Var $ Hs.Qual (Hs.ModuleName modName) (Hs.Ident occName)) $ case se of
100 Hs.IdSplice v -> Hs.Var $ Hs.UnQual $ Hs.Ident v
101 Hs.ParenSplice ps -> ps
104 reinstateUnqDyn (TH.Name (TH.OccName occName') (TH.NameQ (TH.ModName modName')))
105 | modName == modName' && occName == occName' = 'unqMarker
106 reinstateUnqDyn x = x
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list