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' but type-binding @(letName)@ to 'TH.Name' to help type inference.
21 observeSharing :: ObserveSharing TH.Name repr a -> repr a
22 observeSharing = Letable.observeSharing
24 instance Hashable TH.Name where
25 hashWithSalt s = hashWithSalt s . show
27 -- Combinators semantics for the 'ObserveSharing' interpreter
29 ( Letable letName repr
34 ) => Comb.Charable (ObserveSharing letName repr)
36 ( Letable letName repr
40 , Comb.Alternable repr
41 ) => Comb.Alternable (ObserveSharing letName repr)
43 ( Letable letName repr
47 , Comb.Applicable repr
48 ) => Comb.Applicable (ObserveSharing letName repr)
50 ( Letable letName repr
54 , Comb.Selectable repr
55 ) => Comb.Selectable (ObserveSharing letName repr)
57 ( Letable letName repr
62 ) => Comb.Matchable (ObserveSharing letName repr) where
63 -- Here the default definition does not fit since there is no lift* for its type and thus handles 'bs' itself,
64 -- which is not the transformation wanted.
65 conditional cs bs a b = observeSharingNode $ ObserveSharing $
67 <$> mapM unObserveSharing bs
68 <*> unObserveSharing a
69 <*> unObserveSharing b
71 ( Letable letName repr
76 {- TODO: the following constraints are for the current Foldable,
77 - they will have to be removed when Foldable will have Sym.lift2 as defaults
79 , Comb.Applicable repr
80 , Comb.Alternable repr
81 ) => Comb.Foldable (ObserveSharing letName repr)
83 ( Letable letName repr
88 ) => Comb.Lookable (ObserveSharing letName repr)
90 -- Combinators semantics for the 'CleanDefs' interpreter
91 instance Comb.Applicable repr => Comb.Applicable (CleanDefs letName repr)
92 instance Comb.Alternable repr => Comb.Alternable (CleanDefs letName repr)
93 instance Comb.Charable repr => Comb.Charable (CleanDefs letName repr)
94 instance Comb.Selectable repr => Comb.Selectable (CleanDefs letName repr)
95 instance Comb.Matchable repr => Comb.Matchable (CleanDefs letName repr) where
96 conditional cs bs a b = CleanDefs $
98 <$> mapM unCleanDefs bs
101 instance Comb.Lookable repr => Comb.Lookable (CleanDefs letName repr)
102 instance Comb.Foldable repr => Comb.Foldable (CleanDefs letName repr) where
103 chainPre = Sym.lift2 Comb.chainPre
104 chainPost = Sym.lift2 Comb.chainPost