-
Notifications
You must be signed in to change notification settings - Fork 1
/
Eval.hs
579 lines (497 loc) · 21.6 KB
/
Eval.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE StandaloneDeriving #-}
module Eval where
import Prelude hiding (Num(..), pi)
import Pretty
import TT
import Data.Monoid hiding (Sum)
import Data.Dynamic
import Algebra.Classes hiding (Sum)
import Data.List (sort, isSubsequenceOf, intersect)
import Control.Applicative
import Control.Monad.Except
import Control.Monad.Reader
data Proot a = NoErr a | Err [D] deriving Functor
type Assumption = ((Loc,Loc),(Env,Env))
newtype ConvM a = ConvM {fromConvM :: ReaderT [Assumption] Proot a}
deriving (Functor,Monad,Applicative,Alternative,MonadError D,MonadReader [Assumption])
type Err = ConvM ()
testErr :: Err -> Maybe D
testErr (ConvM x) = case runReaderT x [] of
NoErr () -> Nothing
Err es -> Just (prettyErrConj es)
noErr :: Err
noErr = pure ()
instance MonadError D Proot where
throwError x = Err [x]
catchError (Err (x:_xs)) f = f x
catchError (Err []) _ = error "MonadError D: panic: empty error"
catchError (NoErr x) _ = NoErr x
instance Applicative Proot where
pure = NoErr
(<*>) = ap
instance Alternative Proot where
empty = throwError "nondescript error"
(<|>) (NoErr x) _ = (NoErr x)
(<|>) _ (NoErr x) = NoErr x
(<|>) (Err x) (Err y) = Err (x++y)
prettyErrConj :: [D] -> D
prettyErrConj (errs) = encloseSep "" "" " and " errs
instance Monad Proot where
(Err x) >>= _ = Err x
NoErr x >>= f = f x
-- | Lookup a value in the environment
look :: Ident -> Env -> (Binder, Val)
look x (Pair rho (n@(y,_l),u))
| x == y = (n, u)
| otherwise = look x rho
look x r@(PDef es r1) = case lookupIdent x es of
Just (y,t) -> (y,eval r t)
Nothing -> look x r1
look x Empty = error ("panic: variable not found in env:" ++ show x)
etaExpandRecord :: VTele -> Val -> [Val]
etaExpandRecord VEmpty _ = []
etaExpandRecord VBot _ = error "VBot escaped"
etaExpandRecord (VBind (x,_) _rig _ xs) r = let v = projVal x r in v:etaExpandRecord (xs v) r
evalDecls :: TDecls Val -> Env -> (Env,[(String,Val)])
evalDecls (Mutual decls) ρ = (ρ',[(x,eval ρ' y) | Decl (x,_) _ y <- decls])
where ρ' = PDef [ (x,y) | Decl x _ y <- decls ] ρ
evalDecls (Open (VRecordT tele) t) ρ = (foldl Pair ρ (zip (teleBinders tele) vs),[])
where vs = etaExpandRecord tele (eval ρ t)
evalDecls (Open _ _) _ = error "panic: open of non-record"
evalDeclss :: [TDecls Val] -> Env -> (Env,[(String,Val)])
evalDeclss dss ρ = foldl (\(e,vs) ds -> let (e',vs') = evalDecls ds e in (e',vs++vs')) (ρ,[]) dss
eval :: Env -> CTer -> Val
eval _ U = VU
eval e (App r s) = app (eval e r) (eval e s)
eval e (Var i) = snd (look i e)
eval e (Pi nm r a b) = VPi nm r (eval e a) (eval e b)
-- eval e (Lam x t) = Ter (Lam x t) e -- stop at lambdas
eval e (Lam x _ t) = VLam (fst x) $ \x' -> eval (Pair e (x,x')) t
eval e (RecordT bs) = VRecordT $ evalTele e bs
eval e (Record fs) = VRecord [(l,eval e x) | (l,x) <- fs]
eval e (Proj l a) = projVal l (eval e a)
eval e (Where t decls) = eval (fst (evalDeclss decls e)) t
eval e (Module decls) = VRecord (snd (evalDeclss decls e))
eval _ (Con name) = VCon name
eval e (Split pr alts) = Ter (Split pr alts) e
eval _ (Sum pr) = VSum pr
eval _ (Undef x) = VVar $ "<undefined: " ++ show x ++ " >"
eval _ (Real r) = VPrim (toDyn r) (show r)
eval _ (Prim ('#':nm)) = VAbstract nm
eval _ (Prim nm) = lkPrim nm
eval _ (Import _ v) = v
eval e (Meet t u) = vMeet (eval e t) (eval e u)
eval e (Join t u) = vJoin (eval e t) (eval e u)
eval e (Singleton t u) = VSingleton (eval e t) (eval e u)
abstract :: String -> [Val] -> Val
abstract x = foldl app (VAbstract x)
binOp :: (Typeable a, Typeable b, Typeable c, Show c) =>
(a -> b -> c) -> String -> Val
binOp op opName = VLam "x" $ \vx -> VLam "y" $ \vy -> case (vx,vy) of
(VPrim (fromDynamic -> Just x) _, VPrim (fromDynamic -> Just y) _) ->
let z = op x y
in VPrim (toDyn z) (show z)
_ -> abstract opName [vx,vy]
lkPrim :: String -> Val
lkPrim "-" = binOp ((-) :: Double -> Double -> Double) "-"
lkPrim "+" = binOp ((+) :: Double -> Double -> Double) "+"
lkPrim "*" = binOp ((*) :: Double -> Double -> Double) "*"
lkPrim "positive?" = VLam "x" $ \xi ->
VLam "type" $ \ty ->
VLam "true" $ \true ->
VLam "false" $ \false -> case xi of
VPrim (fromDynamic -> Just (x::Double)) _ -> if x >= 0
then true `app` (abstract "positive!" [xi])
else false `app` VLam "neg" (\q -> -- the type system prevents getting here.
abstract "impossible" [q,(abstract "negative!" [xi])])
_ -> abstract "positive?" [xi,ty,true,false]
lkPrim p = abstract p []
real :: Val
real = VAbstract "R"
positive :: Val -> Val
positive v = abstract ">=0" [v]
pattern VBottom :: Val
pattern VBottom = VSum []
pattern VTop :: Val
pattern VTop = VRecordT VEmpty
infixr -->
(-->) :: Val -> Val -> Val
a --> b = pi "_" a $ \_ -> b
pi :: String -> Val -> (Val -> Val) -> Val
pi nm a f = VPi nm Free a $ VLam nm f
lkPrimTy :: String -> Val
lkPrimTy "-" = real --> real --> real
lkPrimTy "+" = real --> real --> real
lkPrimTy "*" = real --> real --> real
lkPrimTy "positive?" = pi "x" real $ \x ->
pi "type" VU $ \ty ->
(positive x --> ty) --> ((positive x --> VBottom) --> ty) --> ty
lkPrimTy "#R" = VU
lkPrimTy "#>=0" = real --> VU
lkPrimTy "#Ind" = VU
lkPrimTy p = error ("No type for primitive: " ++ show p)
evalTele :: Env -> Tele Val -> VTele
evalTele _ [] = VEmpty
evalTele e (((x,l),r,t):ts) = VBind (x,l) r t' (\x' -> evalTele (Pair e ((x,l),x')) ts)
where t' = eval e t
vJoin :: Val -> Val -> Val
vJoin VTop _ = VTop
vJoin _ VTop = VTop
vJoin VBottom i = i
vJoin i VBottom = i
-- vJoin (VPi nm a b) (VPi _ a' b') = VPi nm (vMeet a a') (vJoin b b') -- EQUALITY of codomain is needed
vJoin (VRecordT fs) (VRecordT fs') | botTele x = VJoin (VRecordT fs) (VRecordT fs')
| otherwise = VRecordT x
where x = joinFields fs fs'
vJoin x y = VJoin x y
vMeet :: Val -> Val -> Val
vMeet VBottom _ = VBottom
vMeet _ VBottom = VBottom
vMeet VTop i = i
vMeet i VTop = i
vMeet (VSingleton t v) t' = VSingleton (vMeet t t') v
vMeet t' (VSingleton t v) = VSingleton (vMeet t' t) v
vMeet (VSum xs) (VSum ys) = VSum (xs `intersect` ys)
vMeet (VRecordT fs) (VRecordT fs') | botTele x = VMeet (VRecordT fs) (VRecordT fs')
| otherwise = VRecordT x
where x = meetFields fs fs'
-- vMeet (VPi nm a b) (VPi _ a' b') = VPi nm (vJoin a a') (vMeet b b') -- EQUALITY of codomain is needed
vMeet x y = case testErr (conv 0 x y) of
Nothing -> x
Just _ -> VMeet x y
hasField :: String -> VTele -> Bool
hasField l fs = l `elem` (map fst (teleBinders fs))
lacksField :: String -> VTele -> Bool
lacksField l fs = not (hasField l fs)
-- | Is this a bottom telescope?
botTele :: VTele -> Bool
botTele VEmpty = False
botTele VBot = True
botTele (VBind _ _ _ t) = botTele (t (error "botTele: cannot look at values!"))
-- | the meet of two telescopes
meetFields :: VTele -> VTele -> VTele
meetFields VEmpty fs = fs
meetFields fs VEmpty = fs
meetFields fs@(VBind (l,ll) r a t) fs'@(VBind (l',ll') r' a' t')
| l == l' = VBind (l,ll) (r /\ r') (vMeet a a') (\x -> meetFields (t x) (t' x))
| lacksField l' fs = VBind (l',ll') r' a' (\x -> meetFields fs (t' x))
| lacksField l fs' = VBind (l,ll) r a (\x -> meetFields fs' (t x))
| otherwise = VBot
meetFields VBot _ = VBot
meetFields _ VBot = VBot
-- | the join of two telescopes
joinFields :: VTele -> VTele -> VTele
joinFields VEmpty _ = VEmpty
joinFields _ VEmpty = VEmpty
joinFields fs@(VBind (l,ll) r a t) fs'@(VBind (l',_ll') r' a' t')
| "__REMOVE__" `occursIn` a = joinFields (t remove) fs'
| "__REMOVE__" `occursIn` a' = joinFields fs (t' remove)
| l == l' = VBind (l,ll) (r \/ r') (vJoin a a') (\x -> joinFields (t x) (t' x))
| lacksField l' fs = joinFields fs (t' remove)
| lacksField l fs' = joinFields fs' (t remove)
| otherwise = VBot
where remove = VVar "__REMOVE__"
joinFields VBot _ = VBot
joinFields _ VBot = VBot
app :: Val -> Val -> Val
app (VLam _ f) u = f u
-- app (Ter (Lam cs x t) e) u = eval (Pair e (x,u)) t
app (Ter (Split _ nvs) e) (VCon name) = case lookup name nvs of
Just t -> eval e t
Nothing -> error $ "app: Split with insufficient branches; " ++
"missing case for " ++ name
app u@(Ter (Split _ _) _) v | isNeutral v = VSplit u v -- v should be neutral
| otherwise = error $ "app: VSplit " ++ show v
++ " is not neutral"
app r s | isNeutral r = VApp r s -- r should be neutral
| otherwise = error $ "app: VApp " ++ show r ++ " is not neutral"
evals :: Env -> [(Binder,CTer)] -> [(Binder,Val)]
evals env bts = [ (b,eval env t) | (b,t) <- bts ]
projVal :: String -> Val -> Val
projVal l (VRecord fs) = case lookup l fs of
Just x -> x
Nothing -> error $ "projVal: could not find field " ++ show l
projVal l u | isNeutral u = VProj l u
| otherwise = error $ show u ++ " should be neutral"
convs :: Int -> [Val] -> [Val] -> Err
convs k a b = sequence_ $ zipWith (conv k) a b
satisfy :: Bool -> D -> Err
satisfy p err = if p then noErr else throwError err
equal :: (Pretty a, Eq a) => a -> a -> Err
equal a b = satisfy (a == b) (different a b)
included :: (Pretty a, Ord a) => a -> a -> Err
included a b = satisfy (a <= b) (sep [pretty a,"⊈",pretty b])
different :: (Pretty a) => a -> a -> D
different a b = (sep [pretty a,"≠",pretty b])
noSub :: (Pretty a2, Pretty a, Pretty a1) =>
a2 -> a1 -> a -> Err
noSub z a b = throwError (sep [pretty a,"not a subtype of",pretty b,"when inhabitant is",pretty z])
-- | @conv k a b@ Checks that @a@ can be converted to @b@.
conv :: Int -> Val -> Val -> Err
conv _ VU VU = noErr
conv k (VSingleton t v) (VSingleton t' v') = conv k t t' >> conv k v v'
conv k (VLam _ f) (VLam _ g) = do
let v = mkVar k
conv (k+1) (f v) (g v)
conv k (VLam _ f) g = do
let v = mkVar k
conv (k+1) (f v) (app g v)
conv k f (VLam _ g) = do
let v = mkVar k
conv (k+1) (app f v) (g v)
conv k (Ter (Lam x _ u) e) (Ter (Lam x' _ u') e') = do
let v = mkVar k
conv (k+1) (eval (Pair e (x,v)) u) (eval (Pair e' (x',v)) u')
conv k (Ter (Lam x _ u) e) u' = do
let v = mkVar k
conv (k+1) (eval (Pair e (x,v)) u) (app u' v)
conv k u' (Ter (Lam x _ u) e) = do
let v = mkVar k
conv (k+1) (app u' v) (eval (Pair e (x,v)) u)
conv k (Ter (Split p t) e) (Ter (Split p' _t') e') =
(p `equal` p') >> convEnv (uniqSplitFVs t) k e e' -- note : p == p' ---> t == t'
conv _ (VSum p) (VSum p') = equal p p'
conv _k (Ter (Undef p) _e) (Ter (Undef p') _e') =
(p `equal` p')
conv k (VPi _ r u v) (VPi _ r' u' v') = do
let w = mkVar k
equal r r' >> conv k u' u >> conv (k+1) (app v w) (app v' w)
conv k (VRecordT fs) (VRecordT fs') =
convTele k fs fs'
conv k (VProj l u) (VProj l' u') = equal l l' >> conv k u u'
conv _ (VCon c) (VCon c') =
(c `equal` c')
conv k (VRecord fs) (VRecord fs') = convFields k fs fs'
conv k (VApp u v) (VApp u' v') = conv k u u' >> conv k v v'
conv k (VSplit u v) (VSplit u' v') = conv k u u' >> conv k v v'
conv _ (VVar x) (VVar x') = x `equal` x'
conv _ (VAbstract n) (VAbstract n') = n `equal` n'
conv k (VJoin a b) (VJoin a' b') = (conv k a a' >> conv k b b') <|> (conv k a b' >> conv k b a')
conv k (VMeet a b) (VMeet a' b') = (conv k a a' >> conv k b b') <|> (conv k a b' >> conv k b a')
conv _ (VPrim _ _) (VPrim _ _) = noErr
conv _ x x' = throwError (different x x')
-- -- @sub _ x:a b@: check that x:a also has type b.
sub :: Int -> [Val] -> Val -> Val -> Err
sub _ _ VU VU = noErr
sub _ _ VBottom _ = noErr
sub _ _ (VSum xs) (VSum ys) = if sort xs `isSubsequenceOf` sort ys then noErr else throwError (pretty xs <+> "has more cases than" <+> pretty ys)
sub _ _ _ (VRecordT VEmpty) = noErr
sub k fs (VPi _ r u v) (VPi _ r' u' v') = do
let w = mkVar k
included r' r >> sub k [w] u' u >> sub (k+1) [app f w | f <- fs] (app v w) (app v' w)
sub k z (VRecordT fs) (VRecordT fs') = subTele k z fs fs'
sub k x (VJoin a b) c = sub k x a c >> sub k x b c
sub k x c (VJoin a b) = sub k x c a <|> sub k x c b
sub k x c (VMeet a b) = sub k x c a >> sub k x c b
sub k x (VMeet a b) c = sub k x a c <|> sub k x b c
sub k x (VSingleton t v) t' = sub k (v:x) t t'
sub k x t (VSingleton t' v') = sub k x t t' >> anyOf (map (conv k v') x)
sub k x (VSplit u v) (VSplit u' v') = conv k v v' >> sub k x u u'
sub k x a@(Ter (Split p t) e) a'@(Ter (Split p' t') e') = conv k a a' <|> do
subs <- ask
-- Did we already consider this subtyping problem (p ⊑ p')?
case lookup (p,p') subs of
Just (rho,rho') -> do -- Yes...
-- Was it in the same environment?
convEnv (uniqSplitFVs t) k e rho
convEnv (uniqSplitFVs t') k e' rho'
-- If no, then we must bail out (or we could recurse forever.)
Nothing ->
local (((p,p'),(e,e')):) $ -- no; let's remember that we considered it, in the envs (e,e')
forM_ t $ \(lbl,b) -> do -- check each branch for subtyping.
case lookup lbl t' of
Just b' -> sub k x (eval e b) (eval e' b')
Nothing -> error "sub: Split: panic"
sub k _ x x' = conv k x x'
anyOf :: [Err] -> Err
anyOf [] = error "anyOf: at least one choice is necessary!"
anyOf x = foldr1 (<|>) x
convEnv :: [Ident] -> Int -> Env -> Env -> Err
convEnv xs k e e' = sequence_ $ zipWith convMaybe (valOfFreeEnv e) (valOfFreeEnv e') where
valOfFreeEnv env = [lookup x ee | x <- xs]
where ee = valOfEnv env
convMaybe Nothing Nothing = noErr
convMaybe (Just x) (Just y) = conv k x y
convMaybe _ _ = throwError ("Variable found in one env but not the other")
convTele :: Int -> VTele -> VTele -> Err
convTele _ VEmpty VEmpty = noErr
convTele k (VBind (l,_) r a t) (VBind (l',_) r' a' t') = do
let v = mkVar k
equal r r' >> equal l l' >> conv k a a' >> convTele (k+1) (t v) (t' v)
convTele _ x x' = throwError (different x x')
subTele :: Int -> [Val] -> VTele -> VTele -> Err
subTele _ _ _ VEmpty = noErr -- all records are a subrecord of the empty record
subTele k zs (VBind (l,_ll) r a t) (VBind (l',ll') r' a' t') = do
let zl = [projVal l z | z <- zs]
vs = case a of
VSingleton _ v' -> v':zl
_ -> zl
if l == l'
then included r r' >> sub k vs a a' >> anyOf [subTele (k+1) zs (t v) (t' v) | v <- vs]
else anyOf [subTele (k+1) zs (t v) (VBind (l',ll') r' a' t') | v <- vs]
subTele _ z x x' = noSub z x x'
-- FIXME: Subtyping of records isn't complete. To be complete, one
-- would have to create a graph representation of the dependencies in
-- a record, and then check the covering of the graphs.
convFields :: Int -> [(String,Val)] -> [(String,Val)] -> Err
convFields _ [] [] = noErr
convFields k ((l,u):fs) ((l',u'):fs') = equal l l' >> conv k u u' >> convFields k fs fs'
convFields _ x x' = throwError (different x x')
--------------------
instance Pretty Val where pretty = showVal 0
arrow :: Rig -> D
arrow (PolarPair (Fin 1 :.. Fin 1) (Fin 0 :.. Fin 0)) = "-o"
arrow Free = "->"
arrow r = pretty r <> "->"
showVal :: Int -> Val -> D
showVal ctx t0 = case t0 of
(VSum branches) -> encloseSep "{" "}" "|" (map pretty branches)
VU -> "Type"
(VJoin u v) -> pp 2 (\p -> p u <+> "\\/" <+> p v)
(VMeet u v) -> pp 3 (\p -> p u <+> "/\\" <+> p v)
(Ter t env) -> showTer ctx env t
(VCon c) -> ("`" <> pretty c)
(VPi nm r a f) -> pp 1 $ \p ->
if dependent f then withVar nm $ \v -> (parens (pretty v <+> prettyBind r <+> pretty a) <+> arrow r) </> p (f `app` (VVar v))
else (showVal 2 a <+> arrow r) </> p (f `app` (VVar "_"))
(VApp _ _) -> pp 4 (\p -> hang 2 (p u) (showArgs vs))
where (u:vs) = fnArgs t0
(VSplit (Ter (Split _ branches) env) v) -> sep [
hang 2 ("case" <+> pretty v <+> "of") (showSplitBranches env branches)
-- ,"with env: " <> (encloseSep "(" ")" "," (showEnv env)) -- too noisy
]
(VVar x) -> pretty x
(VRecordT tele) -> pretty tele
(VRecord fs) -> tupled [hang 2 (pretty l <> " =") (pretty e) | (l,e) <- fs]
(VProj f u) -> pp 5 (\p -> p u <> "." <> pretty f)
(VLam nm f) -> pp 1 $ \p -> withVar nm $ \v ->
hang 0 ("\\" <> pretty v <+> "->") (p (f $ VVar v))
(VPrim _ nm) -> pretty nm
(VAbstract nm) -> pretty ('#':nm)
(VSingleton t v) -> pretty t <> "(= " <> pretty v <> ")"
where pp :: Int -> ((Val -> D) -> D) -> D
pp opPrec k = prn opPrec (k (showVal opPrec))
prn opPrec = (if opPrec < ctx then parens else id)
fnArgs :: Val -> [Val]
fnArgs (VApp u v) = fnArgs u ++ [v]
fnArgs x = [x]
occursIn :: forall v. Value v => String -> v -> Bool
x `occursIn` a = x `elem` unknowns a
dependent :: Val -> Bool
dependent f = "__DEPENDS?__" `occursIn` (f `app` VVar "__DEPENDS?__")
showArgs :: [Val] -> D
showArgs = sep . map (showVal 5)
instance Show Val where
show = render . pretty
prettyLook :: Ident -> Env -> D
prettyLook x (Pair rho ((y,_l),u))
| x == y = pretty u
| otherwise = prettyLook x rho
prettyLook x (PDef es r1) = case lookupIdent x es of
Just ((y,_loc),_t) -> pretty y -- <> "[DEF]"
Nothing -> prettyLook x r1
prettyLook x Empty = pretty x {- typically bound in a Split -}
prettyTele :: VTele -> [D]
prettyTele VEmpty = []
prettyTele (VBind (nm,_l) r ty rest) = (pretty nm <+> prettyBind r <+> pretty ty) : prettyTele (rest $ VVar nm)
prettyBind :: Rig -> D
prettyBind Free = ":"
prettyBind r = "::" <> pretty r
instance Pretty VTele where
pretty = encloseSep "[" "]" ";" . prettyTele
instance Pretty Env where
pretty e = encloseSep "(" ")" ";" (showEnv e)
showEnv :: Env -> [D]
showEnv e0 = case e0 of
Empty -> []
(PDef _xas env) -> showEnv env
(Pair env ((x,_),u)) -> (hang 2 (pretty x <+> "=") (pretty u)) : showEnv env
instance Pretty a => Pretty (Ter' a) where
pretty = showTer 0 Empty
showTele :: Pretty a => Env -> Tele a -> [D]
showTele _ [] = mempty
showTele ρ (((x,_loc),r,t):tele) = (pretty x <+> prettyBind r <+> showTer 0 ρ t) : showTele ρ tele
showTer :: forall a. Pretty a => Int -> Env -> Ter' a -> D
showTer ctx ρ t0 = case t0 of
Import i _ -> sep ["import",pretty i]
Module ds -> hang 2 "module" (vcat (map (showDecls ρ) ds))
U -> "U"
(Meet e0 e1) -> pp 2 $ \p -> p e0 <+> "/\\" <+> p e1
(Join e0 e1) -> pp 2 $ \p -> p e0 <+> "\\/" <+> p e1
(App _ _) -> pp 4 $ \p -> p e0 <+> showTersArgs ρ es
where (e0:es) = fnArgsTer t0
(Pi _ _r a (Lam ("_",_) _ t)) -> pp 1 $ \p -> (showTer 2 ρ a <+> "->") </> p t
(Pi _ r a (Lam x _ t)) -> pp 1 $ \p -> (parens (pretty x <+> prettyBind r <+> showTer 0 ρ a) <+> "->") </> p t
(Pi _ _r e0 e1) -> "Pi" <+> showTersArgs ρ [e0,e1]
(Lam (x,_) _ e) -> pp 2 (\p -> hang 0 ("\\" <> pretty x <+> "->") (p e))
(Proj l e) -> pp 5 (\p -> p e <> "." <> pretty l)
(RecordT ts) -> encloseSep "[" "]" ";" (showTele ρ ts)
(Record fs) -> encloseSep "(" ")" "," [hang 2 (pretty l <> " =") (showTer 0 ρ e) | (l,e) <- fs]
(Where e d) -> pp 0 (\p -> hang 2 (p e) (hang 2 "where" (vcat $ map (showDecls ρ) d)))
(Var x) -> prettyLook x ρ
(Con c) -> "`" <> pretty c
(Split _l branches) -> hang 2 "split" (showSplitBranches ρ branches)
(Sum branches) -> encloseSep "{" "}" "|" (map pretty branches)
(Undef _) -> "undefined (1)"
(Real r) -> showy r
(Prim n) -> showy n
(Singleton t v) -> showTer ctx ρ t <> "(= " <> showTer 0 ρ v <> ")"
where pp :: Int -> ((Ter' a -> D) -> D) -> D
pp opPrec k = prn opPrec (k (showTer opPrec ρ))
prn opPrec = (if opPrec < ctx then parens else id)
fnArgsTer :: Ter' t -> [Ter' t]
fnArgsTer (App u v) = fnArgsTer u ++ [v]
fnArgsTer x = [x]
showSplitBranches :: Pretty a => Env -> [Brc a] -> D
showSplitBranches ρ branches = encloseSep "{" "}" ";"
[hang 2 (pretty l <+> "↦") (showTer 0 ρ t) | (l,t) <- branches]
instance Pretty Ctxt where
pretty ctxt = vcat [pretty nm <+> ":" <+> pretty typ | ((nm,_),typ) <- ctxt]
showTersArgs :: Pretty a => Env -> [Ter' a] -> D
showTersArgs ρ = sep . map (showTer 5 ρ)
showDecl :: Pretty a => Env -> Decl a -> D
showDecl ρ (Decl b typ ter) = vcat [pretty b <+> ":" <+> showTer 0 ρ typ,
pretty b <+> "=" <+> showTer 0 ρ ter]
showDecls :: Pretty a => Env -> TDecls a -> D
showDecls ρ (Open _ x) = "open " <> showTer 0 ρ x
showDecls ρ (Mutual defs) = vcat (map (showDecl ρ) defs)
class Value v where
unknowns :: v -> [String] -- aka "free variables"
instance Value Val where
unknowns v0 = case v0 of
VSum _ -> []
VU -> []
VPi _binder _rig x y -> unknowns x ++ unknowns y
VRecordT x -> unknowns x
VRecord x -> concatMap (unknowns . snd) x
VCon _ -> []
VApp x y -> unknowns x ++ unknowns y
VSplit x y -> unknowns x ++ unknowns y
VProj _ x -> unknowns x
VLam _ f -> unknowns (f (VVar "___UNK___"))
VPrim{} -> []
VAbstract{} -> []
VMeet x y -> unknowns x ++ unknowns y
VJoin x y -> unknowns x ++ unknowns y
VVar x -> [x]
Ter _ env -> unknowns env
VSingleton t u -> unknowns t ++ unknowns u
instance Value Env where
unknowns Empty = []
unknowns (Pair env (_,u)) = unknowns env ++ unknowns u
unknowns (PDef _ env) = unknowns env
instance Value VTele where
unknowns VEmpty = []
unknowns (VBind _binder _rig x y) = unknowns x ++ unknowns (y (VVar "___UNK___"))
unknowns VBot = []
deriving instance Show ModuleState