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 G
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
34 , G.Satisfiable tok repr
35 ) => G.Satisfiable tok (ObserveSharing letName repr)
37 ( Letable letName repr
42 ) => G.Alternable (ObserveSharing letName repr)
44 ( Letable letName repr
49 ) => G.Applicable (ObserveSharing letName repr)
51 ( Letable letName repr
56 ) => G.Selectable (ObserveSharing letName repr)
58 ( Letable letName repr
63 ) => G.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 does not handles 'bs'
67 -- as needed by the 'ObserveSharing' transformation.
68 conditional a cs bs b = observeSharingNode $ ObserveSharing $
70 <$> unObserveSharing a
72 <*> mapM unObserveSharing bs
73 <*> unObserveSharing b
75 ( Letable letName repr
80 {- TODO: the following constraints are for the current Foldable,
81 - they will have to be removed when Foldable will have Sym.lift2 as defaults
85 ) => G.Foldable (ObserveSharing letName repr)
87 ( Letable letName repr
92 ) => G.Lookable (ObserveSharing letName repr)
94 -- Combinators semantics for the 'CleanDefs' interpreter
95 instance G.Applicable repr => G.Applicable (CleanDefs letName repr)
96 instance G.Alternable repr => G.Alternable (CleanDefs letName repr)
97 instance G.Satisfiable tok repr => G.Satisfiable tok (CleanDefs letName repr)
98 instance G.Selectable repr => G.Selectable (CleanDefs letName repr)
99 instance G.Matchable repr => G.Matchable (CleanDefs letName repr) where
100 conditional a cs bs b = CleanDefs $
104 <*> mapM unCleanDefs bs
106 instance G.Lookable repr => G.Lookable (CleanDefs letName repr)
107 instance G.Foldable repr => G.Foldable (CleanDefs letName repr) where
108 chainPre = Sym.lift2 G.chainPre
109 chainPost = Sym.lift2 G.chainPost