1 {-# OPTIONS_GHC -fno-warn-orphans #-}
2 module Symantic.Parser.Grammar.ObserveSharing
3 ( module Symantic.Univariant.Letable
4 , module Symantic.Parser.Grammar.ObserveSharing
7 import Control.Monad (mapM)
8 import Data.Function (($), (.))
9 import Data.Hashable (Hashable, hashWithSalt)
10 import Text.Show (Show(..))
11 import qualified Control.Applicative as Functor
13 import Symantic.Parser.Grammar.Combinators
14 import Symantic.Univariant.Letable hiding (observeSharing)
15 import qualified Symantic.Univariant.Letable as Letable
16 import qualified Language.Haskell.TH.Syntax as TH
17 import qualified Symantic.Univariant.Trans as Sym
19 -- | Like 'Letable.observeSharing'
20 -- but type-binding @(letName)@ to 'TH.Name'
21 -- to avoid the trouble to always set it.
23 Letsable TH.Name repr =>
24 ObserveSharing TH.Name repr a ->
26 observeSharing os = lets defs body
27 where (body, defs) = Letable.observeSharing os
29 -- | Needed by 'observeSharing'.
30 instance Hashable TH.Name where
31 hashWithSalt s = hashWithSalt s . show
32 instance MakeLetName TH.Name where
33 makeLetName _ = TH.qNewName "name"
35 -- Combinators semantics for the 'ObserveSharing' interpreter.
37 ( Letable TH.Name repr
38 , Satisfiable tok repr
39 ) => Satisfiable tok (ObserveSharing TH.Name repr)
41 ( Letable TH.Name repr
43 ) => Alternable (ObserveSharing TH.Name repr)
45 ( Letable TH.Name repr
47 ) => Applicable (ObserveSharing TH.Name repr)
49 ( Letable TH.Name repr
51 ) => Selectable (ObserveSharing TH.Name repr)
53 ( Letable TH.Name repr
55 ) => Matchable (ObserveSharing TH.Name repr) where
56 -- Here the default definition does not fit
57 -- since there is no lift* for the type of 'conditional'
58 -- and its default definition does not handles 'bs'
59 -- as needed by the 'ObserveSharing' transformation.
60 conditional a cs bs b = observeSharingNode $ ObserveSharing $
62 Functor.<$> unObserveSharing a
63 Functor.<*> Functor.pure cs
64 Functor.<*> mapM unObserveSharing bs
65 Functor.<*> unObserveSharing b
67 ( Letable TH.Name repr
69 {- TODO: the following constraints are for the current Foldable,
70 - they will have to be removed when Foldable will have Sym.lift2 as defaults
74 ) => Foldable (ObserveSharing TH.Name repr)
76 ( Letable TH.Name repr
78 ) => Lookable (ObserveSharing TH.Name repr)
80 -- Combinators semantics for the 'FinalizeSharing' interpreter.
81 instance Applicable repr => Applicable (FinalizeSharing TH.Name repr)
82 instance Alternable repr => Alternable (FinalizeSharing TH.Name repr)
83 instance Satisfiable tok repr => Satisfiable tok (FinalizeSharing TH.Name repr)
84 instance Selectable repr => Selectable (FinalizeSharing TH.Name repr)
85 instance Matchable repr => Matchable (FinalizeSharing TH.Name repr) where
86 conditional a cs bs b = FinalizeSharing $
88 Functor.<$> unFinalizeSharing a
89 Functor.<*> Functor.pure cs
90 Functor.<*> mapM unFinalizeSharing bs
91 Functor.<*> unFinalizeSharing b
92 instance Lookable repr => Lookable (FinalizeSharing TH.Name repr)
93 instance Foldable repr => Foldable (FinalizeSharing TH.Name repr) where
94 chainPre = Sym.lift2 chainPre
95 chainPost = Sym.lift2 chainPost