Skip to content

Commit 48c681e

Browse files
committed
Start experimenting with indirection
1 parent 49a3a5a commit 48c681e

File tree

12 files changed

+283
-74
lines changed

12 files changed

+283
-74
lines changed

Setup.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain

TODO

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,46 @@
1-
Restriction and closure conversion
1+
Propagate sizes to all variables
2+
Make a transform that makes all arguments constant size indirecting
3+
non-constant sized types
4+
5+
map : (a -> b) -> List a -> List b
6+
map f xs = case xs of
7+
Nil -> Nil
8+
Cons x xs' -> Cons (f x) (Ref (map f (deref xs')))
9+
10+
BuiltinPtr a = Ptr a
11+
IndirectReturn a = Address -> Unit
12+
write : StackPtr a -> Address -> Unit
13+
alloc : Size -> Address
14+
15+
map : (BuiltinPtr a -> IndirectReturn b) -> BuiltinPtr (List a) -> IndirectReturn (List b)
16+
map f xsp retAdr = case deref xsp of
17+
Nil -> write Nil retAdr
18+
Cons x xs' ->
19+
write Cons retAdr;
20+
f (Ref x) (retAdr + 1);
21+
let nextAdr = alloc (sizeof (List b))
22+
map f xs' nextAdr;
23+
write nextAdr (retAdr + 1 + sizeof (List b))
24+
25+
map : (a -> b) -> Ptr (List a) -> Ptr (List b)
26+
map f xs = case deref xs of
27+
Nil -> Ref Nil
28+
Cons x xs' -> Ref (Cons (f x) (map f xs'))
29+
30+
map : (BuiltinPtr a -> IndirectReturn b) -> Ptr (List a) -> Ptr (List b)
31+
map f xsp = case deref xsp of
32+
Nil ->
33+
let res = alloc 1 in
34+
write Nil res;
35+
res
36+
Cons x xs' ->
37+
let res = alloc (1 + sizeof a + 1) in
38+
write Cons res;
39+
f (xsp + 1) (res + 1);
40+
write (map f xs') (res + sizeof a + 1);
41+
res
42+
43+
Closure conversion
244
LLVM code generation
345
Usage analysisis for better erasure
446
Add new Int type

src/Indirect.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Indirect where
2+
3+
import Syntax
4+
import qualified Syntax.Abstract as Abstract
5+
import qualified Syntax.Abstract as Constant
6+
7+
import Meta
8+
type ConstantM = ConstantSize.Expr (MetaVar ConstantSize.Expr s)
9+
10+
inferType :: AbstractM s -> TCM s (ConstantM s, ConstantM s)
11+
inferType expr = case expr of
12+
Abstract.Var v -> return (Constant.Var v, metaType v)
13+
Abstract.Global v -> undefined
14+
Con qc -> undefined
15+
Lit l -> undefined
16+
Pi h a t s -> undefined
17+
Lam h a t s -> undefined
18+
App e1 a e2 -> undefined
19+
Case e brs -> undefined

src/Infer.hs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ checkType surrR surrP expr typ = do
4242
Concrete.Lam h1 a1 s1 -> do
4343
typ' <- whnf typ
4444
case typ' of
45-
Abstract.Pi h2 a2 t2 ts2 | plicitness a1 == plicitness a2
46-
&& relevance a1 >= min (relevance a2) surrR -> do
45+
Abstract.Pi h2 a2 t2 ts2 | a1 == a2 -> do
4746
v <- forall_ (h2 <> h1) t2
4847
(body, ts2') <- checkType surrR surrP
4948
(instantiate1 (pure v) s1)
@@ -312,10 +311,10 @@ checkConstrDef (ConstrDef c (bindingsView Concrete.piView -> (args, ret))) = mdo
312311
return (ConstrDef c res, ret', size)
313312

314313
checkDataType
315-
:: MetaVar s
316-
-> DataDef Concrete.Expr (MetaVar s)
314+
:: MetaVar Abstract.Expr s
315+
-> DataDef Concrete.Expr (MetaVar Abstract.Expr s)
317316
-> AbstractM s
318-
-> TCM s ( DataDef Abstract.Expr (MetaVar s)
317+
-> TCM s ( DataDef Abstract.Expr (MetaVar Abstract.Expr s)
319318
, AbstractM s
320319
)
321320
checkDataType name (DataDef cs) typ = mdo
@@ -353,20 +352,20 @@ checkDataType name (DataDef cs) typ = mdo
353352
return (DataDef cs', typ'')
354353

355354
checkDefType
356-
:: MetaVar s
357-
-> Definition Concrete.Expr (MetaVar s)
355+
:: MetaVar Abstract.Expr s
356+
-> Definition Concrete.Expr (MetaVar Abstract.Expr s)
358357
-> AbstractM s
359-
-> TCM s ( Definition Abstract.Expr (MetaVar s)
358+
-> TCM s ( Definition Abstract.Expr (MetaVar Abstract.Expr s)
360359
, AbstractM s
361360
)
362361
checkDefType _ (Definition e) typ = first Definition <$> checkType Relevant Explicit e typ
363362
checkDefType v (DataDefinition d) typ = first DataDefinition <$> checkDataType v d typ
364363

365364
generaliseDef
366-
:: Vector (MetaVar s)
367-
-> Definition Abstract.Expr (MetaVar s)
365+
:: Vector (MetaVar Abstract.Expr s)
366+
-> Definition Abstract.Expr (MetaVar Abstract.Expr s)
368367
-> AbstractM s
369-
-> TCM s ( Definition Abstract.Expr (MetaVar s)
368+
-> TCM s ( Definition Abstract.Expr (MetaVar Abstract.Expr s)
370369
, AbstractM s
371370
)
372371
generaliseDef vs (Definition e) t = do
@@ -390,11 +389,11 @@ generaliseDef vs (DataDefinition (DataDef cs)) typ = do
390389
g = pure . B . (+ Tele (length vs))
391390

392391
generaliseDefs
393-
:: Vector ( MetaVar s
394-
, Definition Abstract.Expr (MetaVar s)
392+
:: Vector ( MetaVar Abstract.Expr s
393+
, Definition Abstract.Expr (MetaVar Abstract.Expr s)
395394
, AbstractM s
396395
)
397-
-> TCM s (Vector ( Definition Abstract.Expr (Var Int (MetaVar s))
396+
-> TCM s (Vector ( Definition Abstract.Expr (Var Int (MetaVar Abstract.Expr s))
398397
, ScopeM Int Abstract.Expr s
399398
)
400399
)
@@ -447,10 +446,10 @@ generaliseDefs xs = do
447446

448447
checkRecursiveDefs
449448
:: Vector ( NameHint
450-
, Definition Concrete.Expr (Var Int (MetaVar s))
449+
, Definition Concrete.Expr (Var Int (MetaVar Abstract.Expr s))
451450
, ScopeM Int Concrete.Expr s
452451
)
453-
-> TCM s (Vector ( Definition Abstract.Expr (Var Int (MetaVar s))
452+
-> TCM s (Vector ( Definition Abstract.Expr (Var Int (MetaVar Abstract.Expr s))
454453
, ScopeM Int Abstract.Expr s
455454
)
456455
)

src/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ test inp = do
8383
) mempty of
8484
(Left err, tr) -> do mapM_ putStrLn tr; putStrLn err
8585
(Right (res, restricted), _) -> do
86-
mapM_ print $ (show . (\(x, (d, t)) -> runPrettyM $ prettyM x <+> prettyM "=" <+> prettyTypedDef (fe d) (fe t) (fst $ bindingsView Abstract.piView $ fe t))) <$> HM.toList res
86+
mapM_ print $ ((\(x, (d, t)) -> runPrettyM $ prettyM x <+> prettyM "=" <+> prettyTypedDef (fe d) (fe t) (fst $ bindingsView Abstract.piView $ fe t))) <$> HM.toList res
8787
putStrLn "------------- erased ------------------"
8888
let erased = [(x, fe e') | (x, (e, _)) <- HM.toList res, Definition e' <- [eraseDef e]]
8989
mapM_ print $ pretty <$> erased

src/Meta.hs

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import qualified Data.Set as S
1414
import Data.STRef
1515
import qualified Data.Traversable as T
1616
import qualified Data.Vector as V
17+
import Prelude.Extras
1718

1819
import Syntax
1920
import qualified Syntax.Abstract as Abstract
@@ -22,39 +23,39 @@ import qualified Syntax.Lambda as Lambda
2223
import TCM
2324
import Util
2425

25-
type Exists s = STRef s (Either Level (AbstractM s))
26+
type Exists e s = STRef s (Either Level (e (MetaVar e s)))
2627

27-
data MetaVar s = MetaVar
28-
{ metaId :: !Int
29-
, metaType :: AbstractM s
30-
, metaHint :: !NameHint
31-
, metaRef :: !(Maybe (Exists s))
28+
data MetaVar e s = MetaVar
29+
{ metaId :: !Int
30+
, metaType :: e (MetaVar e s)
31+
, metaHint :: !NameHint
32+
, metaRef :: !(Maybe (Exists e s))
3233
}
3334

34-
type ConcreteM s = Concrete.Expr (MetaVar s)
35-
type AbstractM s = Abstract.Expr (MetaVar s)
36-
type LambdaM s = Lambda.Expr (MetaVar s)
37-
type ScopeM b f s = Scope b f (MetaVar s)
38-
type BranchesM c f s = Branches c f (MetaVar s)
35+
type ConcreteM s = Concrete.Expr (MetaVar Abstract.Expr s)
36+
type AbstractM s = Abstract.Expr (MetaVar Abstract.Expr s)
37+
type LambdaM s = Lambda.Expr (MetaVar Abstract.Expr s)
38+
type ScopeM b f s = Scope b f (MetaVar Abstract.Expr s)
39+
type BranchesM c f s = Branches c f (MetaVar Abstract.Expr s)
3940

40-
instance Eq (MetaVar s) where
41+
instance Eq (MetaVar e s) where
4142
(==) = (==) `on` metaId
4243

43-
instance Ord (MetaVar s) where
44+
instance Ord (MetaVar e s) where
4445
compare = compare `on` metaId
4546

46-
instance Hashable (MetaVar s) where
47+
instance Hashable (MetaVar e s) where
4748
hashWithSalt s = hashWithSalt s . metaId
4849

49-
instance Show (MetaVar s) where
50+
instance Show1 e => Show (MetaVar e s) where
5051
showsPrec d (MetaVar i t h _) = showParen (d > 10) $
5152
showString "Meta" . showChar ' ' . showsPrec 11 i .
52-
showChar ' ' . showsPrec 11 t . showChar ' ' . showsPrec 11 h .
53+
showChar ' ' . showsPrec1 11 t . showChar ' ' . showsPrec 11 h .
5354
showChar ' ' . showString "<Ref>"
5455

5556
showMeta
56-
:: (Functor f, Foldable f, Pretty (f String))
57-
=> f (MetaVar s)
57+
:: (Functor e, Foldable e, Functor f, Foldable f, Pretty (f String), Pretty (e String))
58+
=> f (MetaVar e s)
5859
-> TCM s Doc
5960
showMeta x = do
6061
vs <- foldMapM S.singleton x
@@ -68,9 +69,9 @@ showMeta x = do
6869
let solutions = [(sv v, pretty $ sv <$> metaType v, pretty $ fmap sv <$> msol) | (v, msol) <- zip vsl pvs]
6970
return $ pretty (sv <$> x) <> text ", vars: " <> pretty solutions
7071

71-
tr :: (Functor f, Foldable f, Pretty (f String))
72+
tr :: (Functor e, Foldable e, Functor f, Foldable f, Pretty (f String), Pretty (e String))
7273
=> String
73-
-> f (MetaVar s)
74+
-> f (MetaVar e s)
7475
-> TCM s ()
7576
tr s x = do
7677
i <- gets tcIndent
@@ -87,38 +88,38 @@ trs s x = do
8788
i <- gets tcIndent
8889
TCM.log $ mconcat (replicate i "| ") ++ "--" ++ s ++ ": " ++ show x
8990

90-
existsAtLevel :: NameHint -> AbstractM s -> Level -> TCM s (MetaVar s)
91+
existsAtLevel :: NameHint -> e (MetaVar e s) -> Level -> TCM s (MetaVar e s)
9192
existsAtLevel hint typ l = do
9293
i <- fresh
9394
ref <- liftST $ newSTRef $ Left l
9495
TCM.log $ "exists: " ++ show i
9596
return $ MetaVar i typ hint (Just ref)
9697

97-
exists :: NameHint -> AbstractM s -> TCM s (MetaVar s)
98+
exists :: NameHint -> e (MetaVar e s) -> TCM s (MetaVar e s)
9899
exists hint typ = existsAtLevel hint typ =<< level
99100

100-
existsVar :: Applicative g => NameHint -> AbstractM s -> TCM s (g (MetaVar s))
101+
existsVar :: Applicative g => NameHint -> e (MetaVar e s) -> TCM s (g (MetaVar e s))
101102
existsVar hint typ = pure <$> exists hint typ
102103

103-
existsVarAtLevel :: Applicative g => NameHint -> AbstractM s -> Level -> TCM s (g (MetaVar s))
104+
existsVarAtLevel :: Applicative g => NameHint -> e (MetaVar e s) -> Level -> TCM s (g (MetaVar e s))
104105
existsVarAtLevel hint typ l = pure <$> existsAtLevel hint typ l
105106

106-
forall_ :: NameHint -> AbstractM s -> TCM s (MetaVar s)
107+
forall_ :: NameHint -> e (MetaVar e s) -> TCM s (MetaVar e s)
107108
forall_ hint typ = do
108109
i <- fresh
109110
TCM.log $ "forall: " ++ show i
110111
return $ MetaVar i typ hint Nothing
111112

112-
forallVar :: Applicative g => NameHint -> AbstractM s -> TCM s (g (MetaVar s))
113+
forallVar :: Applicative g => NameHint -> e (MetaVar e s) -> TCM s (g (MetaVar e s))
113114
forallVar hint typ = pure <$> forall_ hint typ
114115

115-
solution :: Exists s -> TCM s (Either Level (AbstractM s))
116+
solution :: Exists e s -> TCM s (Either Level (e (MetaVar e s)))
116117
solution = liftST . readSTRef
117118

118-
solve :: Exists s -> AbstractM s -> TCM s ()
119+
solve :: Exists e s -> e (MetaVar e s) -> TCM s ()
119120
solve r x = liftST $ writeSTRef r $ Right x
120121

121-
refineIfSolved :: Exists s -> AbstractM s -> (AbstractM s -> TCM s (AbstractM s)) -> TCM s (AbstractM s)
122+
refineIfSolved :: Exists e s -> e (MetaVar e s) -> (e (MetaVar e s) -> TCM s (e (MetaVar e s))) -> TCM s (e (MetaVar e s))
122123
refineIfSolved r d f = do
123124
sol <- solution r
124125
case sol of
@@ -128,21 +129,21 @@ refineIfSolved r d f = do
128129
solve r e'
129130
return e'
130131

131-
letMeta :: NameHint -> AbstractM s -> AbstractM s -> TCM s (MetaVar s)
132+
letMeta :: NameHint -> e (MetaVar e s) -> e (MetaVar e s) -> TCM s (MetaVar e s)
132133
letMeta hint expr typ = do
133134
i <- fresh
134135
ref <- liftST $ newSTRef $ Right expr
135136
return $ MetaVar i typ hint (Just ref)
136137

137138
letVar :: Applicative g
138-
=> NameHint -> AbstractM s -> AbstractM s -> TCM s (g (MetaVar s))
139+
=> NameHint -> e (MetaVar e s) -> e (MetaVar e s) -> TCM s (g (MetaVar e s))
139140
letVar hint expr typ = pure <$> letMeta hint expr typ
140141

141-
foldMapM :: (Foldable f, Monoid m)
142-
=> (MetaVar s -> m) -> f (MetaVar s) -> TCM s m
142+
foldMapM :: (Foldable e, Foldable f, Monoid m)
143+
=> (MetaVar e s -> m) -> f (MetaVar e s) -> TCM s m
143144
foldMapM f = foldrM go mempty
144145
where
145-
go v m = (<> m) . (<> f v) <$> do
146+
go v m = (<> m) . (<> f v) <$>
146147
case metaRef v of
147148
Just r -> do
148149
sol <- solution r
@@ -151,9 +152,11 @@ foldMapM f = foldrM go mempty
151152
Right c -> foldMapM f c
152153
Nothing -> return mempty
153154

154-
abstractM :: (MetaVar s -> Maybe b)
155-
-> AbstractM s
156-
-> TCM s (ScopeM b Abstract.Expr s)
155+
abstractM
156+
:: (Monad e, Traversable e, Show1 e)
157+
=> (MetaVar e s -> Maybe b)
158+
-> e (MetaVar e s)
159+
-> TCM s (Scope b e (MetaVar e s))
157160
abstractM f e = do
158161
e' <- freeze e
159162
changed <- liftST $ newSTRef False
@@ -183,18 +186,18 @@ abstractM f e = do
183186
go _ v' = free v'
184187
free = pure . pure . pure . pure
185188

186-
abstract1M :: MetaVar s
189+
abstract1M :: MetaVar Abstract.Expr s
187190
-> AbstractM s
188191
-> TCM s (ScopeM () Abstract.Expr s)
189192
abstract1M v e = do
190193
TCM.log $ "abstracting " ++ show (metaId v)
191194
abstractM (\v' -> if v == v' then Just () else Nothing) e
192195

193196
abstractDefM
194-
:: (MetaVar s -> Maybe b)
195-
-> Definition Abstract.Expr (MetaVar s)
197+
:: (MetaVar Abstract.Expr s -> Maybe b)
198+
-> Definition Abstract.Expr (MetaVar Abstract.Expr s)
196199
-> AbstractM s
197-
-> TCM s ( Definition Abstract.Expr (Var b (MetaVar s))
200+
-> TCM s ( Definition Abstract.Expr (Var b (MetaVar Abstract.Expr s))
198201
, ScopeM b Abstract.Expr s
199202
)
200203
abstractDefM f (Definition e) t = do
@@ -207,10 +210,10 @@ abstractDefM f (DataDefinition e) t = do
207210
return (DataDefinition e', t')
208211

209212
abstractDataDefM
210-
:: (MetaVar s -> Maybe b)
211-
-> DataDef Abstract.Expr (MetaVar s)
213+
:: (MetaVar Abstract.Expr s -> Maybe b)
214+
-> DataDef Abstract.Expr (MetaVar Abstract.Expr s)
212215
-> AbstractM s
213-
-> TCM s (DataDef Abstract.Expr (Var b (MetaVar s)))
216+
-> TCM s (DataDef Abstract.Expr (Var b (MetaVar Abstract.Expr s)))
214217
abstractDataDefM f (DataDef cs) typ = mdo
215218
let inst = instantiateTele $ pure <$> vs
216219
vs = (\(_, _, _, v) -> v) <$> ps'
@@ -229,23 +232,23 @@ abstractDataDefM f (DataDef cs) typ = mdo
229232
etaLamM
230233
:: NameHint
231234
-> Annotation
232-
-> Abstract.Expr (MetaVar s)
233-
-> Scope1 Abstract.Expr (MetaVar s)
234-
-> TCM s (Abstract.Expr (MetaVar s))
235+
-> Abstract.Expr (MetaVar Abstract.Expr s)
236+
-> Scope1 Abstract.Expr (MetaVar Abstract.Expr s)
237+
-> TCM s (Abstract.Expr (MetaVar Abstract.Expr s))
235238
etaLamM n p t s = do
236239
s' <- freezeBound s
237240
return $ Abstract.etaLam n p t s'
238241

239-
freeze :: AbstractM s -> TCM s (AbstractM s)
242+
freeze :: (Monad e, Traversable e) => e (MetaVar e s) -> TCM s (e (MetaVar e s))
240243
freeze e = join <$> traverse go e
241244
where
242245
go v@(metaRef -> Just r) = either (const $ do mt <- freeze (metaType v); return $ pure v {metaType = mt})
243246
freeze =<< solution r
244247
go v = return $ pure v
245248

246-
freezeBound :: (Traversable (t Abstract.Expr), Bound t)
247-
=> t Abstract.Expr (MetaVar s)
248-
-> TCM s (t Abstract.Expr (MetaVar s))
249+
freezeBound :: (Monad e, Traversable e, Traversable (t e), Bound t)
250+
=> t e (MetaVar e s)
251+
-> TCM s (t e (MetaVar e s))
249252
freezeBound e = (>>>= id) <$> traverse go e
250253
where
251254
go v@(metaRef -> Just r) = either (const $ do mt <- freeze (metaType v); return $ pure v {metaType = mt})

0 commit comments

Comments
 (0)