1 {-# LANGUAGE MagicHash #-}
2 {-# LANGUAGE TemplateHaskell #-}
3 module Symantic.Parser.Staging where
5 import Data.Bool (Bool)
6 import Data.Char (Char)
7 import qualified Data.Function as Fun
8 import Data.Either (Either(..))
10 import Data.Ord (Ord(..))
11 import Data.Maybe (Maybe(..))
12 import Language.Haskell.TH (TExpQ)
13 import Text.Show (Show(..), showParen, showString)
14 import qualified Data.Eq as Eq
15 import qualified Data.Function as Function
17 import Symantic.Base.Univariant
20 -- | Compile-time 'value' and corresponding 'code' (that can produce that value at runtime).
21 data ValueCode a = ValueCode
25 getValue :: ValueCode a -> a
26 getValue = unValue Function.. value
27 getCode :: ValueCode a -> TExpQ a
28 getCode = unCode Function.. code
29 type instance Unlift ValueCode = ValueCode
30 instance Liftable ValueCode where
33 instance Unliftable ValueCode where
38 newtype Value a = Value { unValue :: a }
39 type instance Unlift Value = Value
40 instance Liftable Value where
43 instance Unliftable Value where
48 newtype Code a = Code { unCode :: TExpQ a }
49 type instance Unlift Code = Code
50 instance Liftable Code where
53 instance Unliftable Code where
57 -- * Class 'Haskellable'
58 -- | Final encoding of some Haskellable functions
59 -- useful for some optimizations in 'optGram'.
60 class Haskellable (repr :: * -> *) where
61 haskell :: Unlift repr a -> repr a
62 (.) :: repr ((b->c) -> (a->b) -> a -> c)
63 ($) :: repr ((a->b) -> a -> b)
64 (.@) :: repr (a->b) -> repr a -> repr b
65 bool :: Bool -> repr Bool
66 char :: Char -> repr Char
67 cons :: repr (a -> [a] -> [a])
68 const :: repr (a -> b -> a)
69 eq :: Eq a => repr a -> repr (a -> Bool)
70 flip :: repr ((a -> b -> c) -> b -> a -> c)
74 left :: repr (l -> Either l r)
75 right :: repr (r -> Either l r)
76 nothing :: repr (Maybe a)
77 just :: repr (a -> Maybe a)
78 -- instance Haskellable Identity
80 -- ** Type 'Haskellable'
81 -- | Initial encoding of 'Haskellable'
83 Haskell :: ValueCode a -> Haskell a
84 (:.) :: Haskell ((b->c) -> (a->b) -> a -> c)
85 (:$) :: Haskell ((a->b) -> a -> b)
86 (:@) :: Haskell (a->b) -> Haskell a -> Haskell b
87 Const :: Haskell (a -> b -> a)
88 Flip :: Haskell ((a -> b -> c) -> b -> a -> c)
90 instance Show (Haskell a) where
92 Haskell{} -> showString "Haskell"
93 (:.) -> showString "(.)"
94 (:$) -> showString "($)"
97 Fun.$ showString "(@) "
101 Const -> showString "const"
102 Flip -> showString "flip"
103 Id -> showString "id"
104 type instance Unlift Haskell = ValueCode
105 instance Liftable Haskell where
107 instance Unliftable Haskell where
109 Haskell x -> haskell x
112 (:@) f x -> (.@) (unlift f) (unlift x)
120 instance Haskellable Haskell where
128 bool b = Haskell (bool b)
129 char c = Haskell (char c)
130 eq x = Haskell (eq (unlift x))
135 right = Haskell right
136 nothing = Haskell nothing
138 instance Haskellable ValueCode where
139 haskell = Function.id
140 (.) = ValueCode (.) (.)
141 ($) = ValueCode ($) ($)
142 (.@) f x = ValueCode ((.@) (value f) (value x)) ((.@) (code f) (code x))
143 bool b = ValueCode (bool b) (bool b)
144 char c = ValueCode (char c) (char c)
145 cons = ValueCode cons cons
146 const = ValueCode const const
147 eq x = ValueCode (eq (value x)) (eq (code x))
148 flip = ValueCode flip flip
150 nil = ValueCode nil nil
151 unit = ValueCode unit unit
152 left = ValueCode left left
153 right = ValueCode right right
154 nothing = ValueCode nothing nothing
155 just = ValueCode just just
156 instance Haskellable Value where
158 (.) = Value (Function..)
159 ($) = Value (Function.$)
160 (.@) f x = Value (unValue f (unValue x))
164 const = Value Function.const
165 eq x = Value (unValue x Eq.==)
166 flip = Value Function.flip
167 id = Value Function.id
172 nothing = Value Nothing
174 instance Haskellable Code where
176 (.) = Code [|| \f g x -> f (g x) ||]
177 ($) = Code [|| \f x -> f x ||]
178 (.@) f x = Code [|| $$(unCode f) $$(unCode x) ||]
179 bool b = Code [|| b ||]
180 char c = Code [|| c ||]
181 cons = Code [|| \x xs -> x : xs ||]
182 const = Code [|| \x _ -> x ||]
183 eq x = Code [|| \y -> $$(unCode x) Eq.== y ||]
184 flip = Code [|| \f x y -> f y x ||]
185 id = Code [|| \x -> x ||]
186 nil = Code [|| [] ||]
187 unit = Code [|| () ||]
188 left = Code [|| Left ||]
189 right = Code [|| Right ||]
190 nothing = Code [|| Nothing ||]
191 just = Code [|| Just ||]