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.Eq (Eq(..))
9 import Data.Function (($), (.))
10 import Data.Hashable (Hashable, hashWithSalt)
11 import Text.Show (Show(..))
12 import qualified Control.Applicative as Functor
14 import Symantic.Parser.Grammar.Combinators
15 import Symantic.Univariant.Letable hiding (observeSharing)
16 import qualified Symantic.Univariant.Letable as Letable
17 import qualified Language.Haskell.TH.Syntax as TH
18 import qualified Symantic.Univariant.Trans as Sym
20 -- | Like 'Letable.observeSharing'
21 -- but type-binding @(letName)@ to 'TH.Name'
22 -- to avoid the trouble to always set it.
23 observeSharing :: ObserveSharing TH.Name repr a -> repr a
24 observeSharing = Letable.observeSharing
26 -- | Needed by 'observeSharing'.
27 instance Hashable TH.Name where
28 hashWithSalt s = hashWithSalt s . show
30 -- Combinators semantics for the 'ObserveSharing' interpreter.
32 ( Letable letName repr
36 , Satisfiable tok repr
37 ) => Satisfiable tok (ObserveSharing letName repr)
39 ( Letable letName repr
44 ) => Alternable (ObserveSharing letName repr)
46 ( Letable letName repr
51 ) => Applicable (ObserveSharing letName repr)
53 ( Letable letName repr
58 ) => Selectable (ObserveSharing letName repr)
60 ( Letable letName repr
65 ) => Matchable (ObserveSharing letName repr) where
66 -- Here the default definition does not fit
67 -- since there is no lift* for the type of 'conditional'
68 -- and its default definition does not handles 'bs'
69 -- as needed by the 'ObserveSharing' transformation.
70 conditional a cs bs b = observeSharingNode $ ObserveSharing $
72 Functor.<$> unObserveSharing a
73 Functor.<*> Functor.pure cs
74 Functor.<*> mapM unObserveSharing bs
75 Functor.<*> unObserveSharing b
77 ( Letable letName repr
82 {- TODO: the following constraints are for the current Foldable,
83 - they will have to be removed when Foldable will have Sym.lift2 as defaults
87 ) => Foldable (ObserveSharing letName repr)
89 ( Letable letName repr
94 ) => Lookable (ObserveSharing letName repr)
96 -- Combinators semantics for the 'CleanDefs' interpreter.
97 instance Applicable repr => Applicable (CleanDefs letName repr)
98 instance Alternable repr => Alternable (CleanDefs letName repr)
99 instance Satisfiable tok repr => Satisfiable tok (CleanDefs letName repr)
100 instance Selectable repr => Selectable (CleanDefs letName repr)
101 instance Matchable repr => Matchable (CleanDefs letName repr) where
102 conditional a cs bs b = CleanDefs $
104 Functor.<$> unCleanDefs a
105 Functor.<*> Functor.pure cs
106 Functor.<*> mapM unCleanDefs bs
107 Functor.<*> unCleanDefs b
108 instance Lookable repr => Lookable (CleanDefs letName repr)
109 instance Foldable repr => Foldable (CleanDefs letName repr) where
110 chainPre = Sym.lift2 chainPre
111 chainPost = Sym.lift2 chainPost