1 {-# OPTIONS_GHC -fno-warn-orphans #-}
2 module Symantic.Parser.Grammar.ObserveSharing
3 ( module Symantic.Parser.Grammar.ObserveSharing
7 import Control.Monad (mapM)
8 import Control.Applicative (Applicative(..))
9 import Data.Eq (Eq(..))
10 import Data.Function (($), (.))
11 import Data.Functor ((<$>))
12 import Data.Hashable (Hashable, hashWithSalt)
13 import Text.Show (Show(..))
15 import Symantic.Univariant.Letable as Letable
16 import qualified Symantic.Univariant.Trans as Sym
17 import qualified Symantic.Parser.Grammar.Combinators as Comb
18 import qualified Language.Haskell.TH.Syntax as TH
20 -- | Like 'Letable.observeSharing'
21 -- but type-binding @(letName)@ to 'TH.Name' to help type inference.
22 observeSharing :: ObserveSharing TH.Name repr a -> repr a
23 observeSharing = Letable.observeSharing
25 instance Hashable TH.Name where
26 hashWithSalt s = hashWithSalt s . show
28 -- Combinators semantics for the 'ObserveSharing' interpreter
30 ( Letable letName repr
35 ) => Comb.Charable (ObserveSharing letName repr)
37 ( Letable letName repr
41 , Comb.Alternable repr
42 ) => Comb.Alternable (ObserveSharing letName repr)
44 ( Letable letName repr
48 , Comb.Applicable repr
49 ) => Comb.Applicable (ObserveSharing letName repr)
51 ( Letable letName repr
55 , Comb.Selectable repr
56 ) => Comb.Selectable (ObserveSharing letName repr)
58 ( Letable letName repr
63 ) => Comb.Matchable (ObserveSharing letName repr) where
64 -- Here the default definition does not fit
65 -- since there is no lift* for the type of 'conditional'
66 -- and its default definition handles does not handles 'bs'
67 -- as needed by the 'ObserveSharing' transformation.
68 conditional cs bs a b = observeSharingNode $ ObserveSharing $
70 <$> mapM unObserveSharing bs
71 <*> unObserveSharing a
72 <*> unObserveSharing b
74 ( Letable letName repr
79 {- TODO: the following constraints are for the current Foldable,
80 - they will have to be removed when Foldable will have Sym.lift2 as defaults
82 , Comb.Applicable repr
83 , Comb.Alternable repr
84 ) => Comb.Foldable (ObserveSharing letName repr)
86 ( Letable letName repr
91 ) => Comb.Lookable (ObserveSharing letName repr)
93 -- Combinators semantics for the 'CleanDefs' interpreter
94 instance Comb.Applicable repr => Comb.Applicable (CleanDefs letName repr)
95 instance Comb.Alternable repr => Comb.Alternable (CleanDefs letName repr)
96 instance Comb.Charable repr => Comb.Charable (CleanDefs letName repr)
97 instance Comb.Selectable repr => Comb.Selectable (CleanDefs letName repr)
98 instance Comb.Matchable repr => Comb.Matchable (CleanDefs letName repr) where
99 conditional cs bs a b = CleanDefs $
101 <$> mapM unCleanDefs bs
104 instance Comb.Lookable repr => Comb.Lookable (CleanDefs letName repr)
105 instance Comb.Foldable repr => Comb.Foldable (CleanDefs letName repr) where
106 chainPre = Sym.lift2 Comb.chainPre
107 chainPost = Sym.lift2 Comb.chainPost