1 {-# LANGUAGE DefaultSignatures #-}
2 {-# LANGUAGE TemplateHaskell #-}
3 {-# LANGUAGE UndecidableInstances #-}
4 module Symantic.Parser.Grammar.Production where
6 import Data.Bool (Bool(..))
7 import Data.Char (Char)
9 import Data.Functor.Identity (Identity(..))
10 import Prelude (undefined)
11 import Text.Show (Show(..), showString)
12 import qualified Data.Either as Either
13 import qualified Data.Eq as Eq
14 import qualified Data.Function as Fun
15 import qualified Data.Maybe as Maybe
16 import qualified Language.Haskell.TH as TH
17 import qualified Language.Haskell.TH.Syntax as TH
18 import Type.Reflection (Typeable)
20 import Symantic.Univariant.Data
21 import Symantic.Univariant.Lang
22 import Symantic.Univariant.Optim
23 import Symantic.Univariant.Trans
24 import Symantic.Univariant.View
28 -- * Type 'Production'
31 { prodValue :: SomeData Identity a
32 , prodCode :: SomeData TH.CodeQ a
33 --, prodView :: SomeData View a
36 production :: a -> TH.CodeQ a -> Production a
37 production v c = Production
38 { prodValue = SomeData (Var (Identity v))
39 , prodCode = SomeData (Var c)
42 prod :: TH.Lift a => a -> Production a
43 prod x = production x [||x||]
45 runValue :: Production a -> a
46 runValue x = runIdentity (trans x)
47 runCode :: Production a -> TH.CodeQ a
50 instance Trans Production Identity where
51 trans Production{prodValue = SomeData x} = trans x
52 instance Trans Production TH.CodeQ where
53 trans Production{prodCode = SomeData x} = trans x
55 instance Abstractable Production where
58 { prodValue = prodValue f .@ prodValue x
59 , prodCode = prodCode f .@ prodCode x
62 { prodValue = lam (\x -> prodValue (f Production{prodValue = x}))
63 , prodCode = lam (\x -> prodCode (f Production{prodCode = x}))
66 { prodValue = lam1 (\x -> prodValue (f Production{prodValue = x}))
67 , prodCode = lam1 (\x -> prodCode (f Production{prodCode = x}))
69 const = Production const const
70 ($) = Production ($) ($)
71 (.) = Production (.) (.)
72 flip = Production flip flip
74 instance Eitherable Production where
75 left = Production left left
76 right = Production right right
77 instance (TH.Lift c, Typeable c) => Constantable c Production where
78 constant c = Production (constant c) (constant c)
79 instance Maybeable Production where
80 nothing = Production nothing nothing
81 just = Production just just
82 instance Listable Production where
83 nil = Production nil nil
84 cons = Production cons cons
85 instance Equalable Production where
86 equal = Production equal equal
88 optimizeProduction :: Production a -> Production a
89 optimizeProduction p = Production
90 { prodValue = normalOrderReduction (prodValue p)
91 , prodCode = normalOrderReduction (prodCode p)
95 class Tokenable repr where
96 token :: tok -> repr tok
98 Liftable repr => Tokenable (Output repr) =>
100 token = lift Fun.. token
102 instance Show (SomeData ValueCode a) where
103 showsPrec p (SomeData x) = showsPrec p (trans @_ @View x)
107 -- * Type 'ValueCode'
108 data ValueCode a = ValueCode
112 instance Trans ValueCode ValueCode where
114 instance Abstractable ValueCode where
116 { value = runIdentity (Identity (value f) .@ (Identity (value x)))
117 , code = code f .@ code x
120 { value = runIdentity (lam (Identity Fun.. value Fun.. f Fun.. (`ValueCode` undefined) Fun.. runIdentity))
121 , code = lam (code Fun.. f Fun.. ValueCode undefined)
124 const = ValueCode (runIdentity const) const
125 flip = ValueCode (runIdentity flip) flip
126 id = ValueCode (runIdentity id) id
127 ($) = ValueCode (runIdentity ($)) ($)
128 (.) = ValueCode (runIdentity (.)) (.)
129 instance Anythingable ValueCode
130 instance TH.Lift c => Constantable c ValueCode where
131 constant c = ValueCode (runIdentity (constant c)) (constant c)
132 instance Listable ValueCode where
133 cons = ValueCode (runIdentity cons) cons
134 nil = ValueCode (runIdentity nil) nil
135 instance Equalable ValueCode where
136 equal = ValueCode (runIdentity equal) equal
137 instance Eitherable ValueCode where
138 left = ValueCode (runIdentity left) left
139 right = ValueCode (runIdentity right) right
140 instance Maybeable ValueCode where
141 nothing = ValueCode (runIdentity nothing) nothing
142 just = ValueCode (runIdentity just) just
146 instance Anythingable Identity
147 instance Abstractable Identity where
148 f .@ x = Identity (runIdentity f (runIdentity x))
149 lam f = Identity (runIdentity Fun.. f Fun.. Identity)
152 const = Identity Fun.const
153 flip = Identity Fun.flip
155 ($) = Identity (Fun.$)
156 (.) = Identity (Fun..)
157 instance Constantable c Identity where
159 instance Eitherable Identity where
160 left = Identity Either.Left
161 right = Identity Either.Right
162 instance Equalable Identity where
163 equal = Identity (Eq.==)
164 instance Listable Identity where
167 instance Maybeable Identity where
168 nothing = Identity Maybe.Nothing
169 just = Identity Maybe.Just
172 instance Anythingable TH.CodeQ
173 instance Abstractable TH.CodeQ where
174 (.@) f x = [|| $$f $$x ||]
175 lam f = [|| \x -> $$(f [||x||]) ||]
179 const = [|| Fun.const ||]
180 flip = [|| \f x y -> f y x ||]
181 ($) = [|| (Fun.$) ||]
182 (.) = [|| (Fun..) ||]
183 instance TH.Lift c => Constantable c TH.CodeQ where
184 constant c = [|| c ||]
185 instance Eitherable TH.CodeQ where
186 left = [|| Either.Left ||]
187 right = [|| Either.Right ||]
188 instance Equalable TH.CodeQ where
189 equal = [|| (Eq.==) ||]
190 instance Listable TH.CodeQ where
193 instance Maybeable TH.CodeQ where
194 nothing = [|| Maybe.Nothing ||]
195 just = [|| Maybe.Just ||]