-
Notifications
You must be signed in to change notification settings - Fork 26
/
Core.hs
257 lines (221 loc) · 9.44 KB
/
Core.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
{-# LANGUAGE DeriveFoldable, DeriveFunctor, DeriveTraversable, FlexibleContexts, FlexibleInstances, MonadComprehensions, OverloadedStrings, PatternSynonyms, RankNTypes, TemplateHaskell, TypeFamilies, ViewPatterns #-}
module Syntax.Core where
import Control.Monad
import Data.Bifoldable
import Data.Bifunctor
import Data.Bitraversable
import Data.Deriving
import Data.Foldable as Foldable
import Data.Hashable
import Data.Vector(Vector)
import Syntax
import TypedFreeVar
import TypeRep(TypeRep)
import Util
import Util.Tsil
-- | Expressions with meta-variables of type @m@ and variables of type @v@.
data Expr m v
= Var v
| Meta m (Vector (Plicitness, Expr m v))
| Global QName
| Con QConstr
| Lit Literal
| Pi !NameHint !Plicitness (Type m v) (Scope1 (Expr m) v)
| Lam !NameHint !Plicitness (Type m v) (Scope1 (Expr m) v)
| App (Expr m v) !Plicitness (Expr m v)
| Let (LetRec (Expr m) v) (Scope LetVar (Expr m) v)
| Case (Expr m v) (Branches Plicitness (Expr m) v) (Type m v)
| ExternCode (Extern (Expr m v)) (Type m v)
| SourceLoc !SourceLoc (Expr m v)
deriving (Foldable, Functor, Traversable)
-- | Synonym for documentation purposes
type Type = Expr
-------------------------------------------------------------------------------
-- Helpers
unSourceLoc :: Expr m v -> Expr m v
unSourceLoc (SourceLoc _ e) = unSourceLoc e
unSourceLoc e = e
sourceLocView :: Expr m v -> (SourceLoc, Expr m v)
sourceLocView (SourceLoc loc (unSourceLoc -> e)) = (loc, e)
sourceLocView e = (noSourceLoc "sourceLocView", e)
type FreeExprVar m = FreeVar Plicitness (Expr m)
lam :: FreeExprVar m -> Expr m (FreeExprVar m) -> Expr m (FreeExprVar m)
lam v e = Lam (varHint v) (varData v) (varType v) $ abstract1 v e
pi_ :: FreeExprVar m -> Expr m (FreeExprVar m) -> Expr m (FreeExprVar m)
pi_ v e = Pi (varHint v) (varData v) (varType v) $ abstract1 v e
lams :: Foldable t => t (FreeExprVar m) -> Expr m (FreeExprVar m) -> Expr m (FreeExprVar m)
lams xs e = foldr lam e xs
pis :: Foldable t => t (FreeExprVar m) -> Expr m (FreeExprVar m) -> Expr m (FreeExprVar m)
pis xs e = foldr pi_ e xs
let_
:: Vector (FreeExprVar m, SourceLoc, Expr m (FreeExprVar m))
-> Expr m (FreeExprVar m)
-> Expr m (FreeExprVar m)
let_ ds body = do
let abstr = letAbstraction $ fst3 <$> ds
ds' = LetRec
[ LetBinding (varHint v) loc (abstract abstr e) (varType v)
| (v, loc, e) <- ds
]
Let ds' $ abstract abstr body
pattern MkType :: TypeRep -> Expr m v
pattern MkType rep = Lit (TypeRep rep)
apps :: Foldable t => Expr m v -> t (Plicitness, Expr m v) -> Expr m v
apps = Foldable.foldl' (uncurry . App)
appsView :: Expr m v -> (Expr m v, [(Plicitness, Expr m v)])
appsView = second toList . go
where
go (unSourceLoc -> App e1 p e2) = second (`Snoc` (p, e2)) $ go e1
go e = (e, Nil)
varView :: Expr m a -> Maybe a
varView (unSourceLoc -> Var v) = Just v
varView _ = Nothing
distinctVarView :: (Eq v, Hashable v, Traversable t) => t (p, Expr m v) -> Maybe (t (p, v))
distinctVarView es = case traverse (traverse varView) es of
Just es' | distinct (snd <$> es') -> Just es'
_ -> Nothing
piView :: Expr m v -> Maybe (NameHint, Plicitness, Type m v, Scope1 (Expr m) v)
piView (unSourceLoc -> Pi h a t s) = Just (h, a, t, s)
piView _ = Nothing
lamView :: Expr m v -> Maybe (NameHint, Plicitness, Type m v, Scope1 (Expr m) v)
lamView (unSourceLoc -> Lam h a t s) = Just (h, a, t, s)
lamView _ = Nothing
typeApp :: Expr m v -> Plicitness -> Expr m v -> Maybe (Expr m v)
typeApp (piView -> Just (_, p, _, s)) p' e | p == p' = Just $ Util.instantiate1 e s
typeApp _ _ _ = Nothing
typeApps :: Foldable t => Expr m v -> t (Plicitness, Expr m v) -> Maybe (Expr m v)
typeApps = foldlM (\e (p, e') -> typeApp e p e')
usedPiView
:: Expr m v
-> Maybe (NameHint, Plicitness, Expr m v, Scope1 (Expr m) v)
usedPiView (piView -> Just (n, p, e, s@(unusedScope -> Nothing))) = Just (n, p, e, s)
usedPiView _ = Nothing
usedPisViewM :: Expr m v -> Maybe (Telescope Plicitness (Expr m) v, Scope TeleVar (Expr m) v)
usedPisViewM = bindingsViewM usedPiView
piTelescope :: Expr m v -> Telescope Plicitness (Expr m) v
piTelescope (pisView -> (tele, _)) = tele
pisView :: Expr m v -> (Telescope Plicitness (Expr m) v, Scope TeleVar (Expr m) v)
pisView = bindingsView piView
lamsViewM :: Expr m v -> Maybe (Telescope Plicitness (Expr m) v, Scope TeleVar (Expr m) v)
lamsViewM = bindingsViewM lamView
arrow :: Plicitness -> Expr m v -> Expr m v -> Expr m v
arrow p a b = Pi mempty p a $ abstractNone b
quantifiedConstrTypes
:: DataDef (Type m) v
-> (Plicitness -> Plicitness)
-> [ConstrDef (Type m v)]
quantifiedConstrTypes (DataDef ps cs) anno = map (fmap $ quantify Pi ps') cs
where
ps' = mapAnnotations anno ps
-------------------------------------------------------------------------------
-- Instances
deriveEq1 ''Expr
deriveEq ''Expr
deriveOrd1 ''Expr
deriveOrd ''Expr
deriveShow1 ''Expr
deriveShow ''Expr
instance Applicative (Expr m) where
pure = return
(<*>) = ap
instance Monad (Expr m) where
return = Var
expr >>= f = case expr of
Var v -> f v
Meta m vs -> Meta m $ second (>>= f) <$> vs
Global v -> Global v
Con c -> Con c
Lit l -> Lit l
Pi h a t s -> Pi h a (t >>= f) (s >>>= f)
Lam h a t s -> Lam h a (t >>= f) (s >>>= f)
App e1 a e2 -> App (e1 >>= f) a (e2 >>= f)
Let ds scope -> Let (ds >>>= f) (scope >>>= f)
Case e brs retType -> Case (e >>= f) (brs >>>= f) (retType >>= f)
ExternCode c t -> ExternCode ((>>= f) <$> c) (t >>= f)
SourceLoc loc e -> SourceLoc loc (e >>= f)
instance GBind (Expr m) where
global = Global
gbind f expr = case expr of
Var _ -> expr
Meta m es -> Meta m (second (gbind f) <$> es)
Global v -> f v
Con _ -> expr
Lit _ -> expr
Pi h a t s -> Pi h a (gbind f t) (gbound f s)
Lam h a t s -> Lam h a (gbind f t) (gbound f s)
App e1 a e2 -> App (gbind f e1) a (gbind f e2)
Let ds scope -> Let (gbound f ds) (gbound f scope)
Case e brs retType -> Case (gbind f e) (gbound f brs) (gbind f retType)
ExternCode c t -> ExternCode (gbind f <$> c) (gbind f t)
SourceLoc loc e -> SourceLoc loc (gbind f e)
instance Bifunctor Expr where bimap = bimapDefault
instance Bifoldable Expr where bifoldMap = bifoldMapDefault
instance Bitraversable Expr where
bitraverse f g expr = case expr of
Var v -> Var <$> g v
Meta m es -> Meta <$> f m <*> traverse (traverse $ bitraverse f g) es
Global v -> pure $ Global v
Con c -> pure $ Con c
Lit l -> pure $ Lit l
Pi h a t s -> Pi h a <$> bitraverse f g t <*> bitraverseScope f g s
Lam h a t s -> Lam h a <$> bitraverse f g t <*> bitraverseScope f g s
App e1 a e2 -> App <$> bitraverse f g e1 <*> pure a <*> bitraverse f g e2
Let ds scope -> Let <$> bitraverseLet f g ds <*> bitraverseScope f g scope
Case e brs retType -> Case <$> bitraverse f g e <*> bitraverseBranches f g brs <*> bitraverse f g retType
ExternCode c t -> ExternCode <$> traverse (bitraverse f g) c <*> bitraverse f g t
SourceLoc loc e -> SourceLoc loc <$> bitraverse f g e
hoistMetas
:: Monad f
=> (forall a. meta -> Vector (Plicitness, Expr meta' a) -> f (Expr meta' a))
-> Expr meta v
-> f (Expr meta' v)
hoistMetas f expr = case expr of
Var v -> pure $ Var v
Meta m es -> f m =<< traverse (traverse $ hoistMetas f) es
Global v -> pure $ Global v
Con c -> pure $ Con c
Lit l -> pure $ Lit l
Pi h a t s -> Pi h a <$> hoistMetas f t <*> transverseScope (hoistMetas f) s
Lam h a t s -> Lam h a <$> hoistMetas f t <*> transverseScope (hoistMetas f) s
App e1 a e2 -> App <$> hoistMetas f e1 <*> pure a <*> hoistMetas f e2
Let ds scope -> Let <$> transverseLet (hoistMetas f) ds <*> transverseScope (hoistMetas f) scope
Case e brs retType -> Case <$> hoistMetas f e <*> transverseBranches (hoistMetas f) brs <*> hoistMetas f retType
ExternCode c t -> ExternCode <$> traverse (hoistMetas f) c <*> hoistMetas f t
SourceLoc loc e -> SourceLoc loc <$> hoistMetas f e
hoistMetas_
:: Monad f
=> (meta -> f ())
-> Expr meta v
-> f ()
hoistMetas_ f = void . hoistMetas (\m es -> const (Meta m es) <$> f m)
instance (v ~ Doc, Pretty m, Eq m) => Pretty (Expr m v) where
prettyM expr = case expr of
Var v -> prettyM v
Meta m es -> prettyApps (prettyM m) ((\(p, e) -> prettyAnnotation p $ prettyM e) <$> es)
Global g -> prettyM g
Con c -> prettyM c
Lit l -> prettyM l
Pi _ a t (unusedScope -> Just e) -> parens `above` arrPrec $
prettyAnnotation a (prettyM t)
<+> "->" <+>
associate arrPrec (prettyM e)
(usedPisViewM -> Just (tele, s)) -> withTeleHints tele $ \ns ->
parens `above` arrPrec $
prettyTeleVarTypes ns tele <+> "->" <+>
associate arrPrec (prettyM $ instantiateTele (pure . fromName) ns s)
Pi {} -> error "impossible prettyPrec pi"
(lamsViewM -> Just (tele, s)) -> withTeleHints tele $ \ns ->
parens `above` absPrec $
"\\" <> prettyTeleVarTypes ns tele <> "." <+>
prettyM (instantiateTele (pure . fromName) ns s)
Lam {} -> error "impossible prettyPrec lam"
App e1 a e2 -> prettyApp (prettyM e1) (prettyAnnotation a $ prettyM e2)
Let ds s -> parens `above` letPrec $ withLetHints ds $ \ns ->
"let" <+> align (prettyLet ns ds)
<+> "in" <+> prettyM (instantiateLet (pure . fromName) ns s)
Case e brs retType -> parens `above` casePrec $
"case" <+> inviolable (prettyM e) <+> "of" <+> parens (prettyM retType)
<$$> indent 2 (prettyM brs)
ExternCode c t -> parens `above` annoPrec $
prettyM c <+> ":" <+> prettyM t
SourceLoc _ e -> prettyM e