Skip to content

Commit 267d74b

Browse files
committed
properly compile type elims
1 parent f9ba97e commit 267d74b

File tree

3 files changed

+47
-42
lines changed

3 files changed

+47
-42
lines changed

src/Agda2Lambox/Compile/Type.hs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,23 @@ import Control.Monad ( mapM )
1313
import Data.List ( foldl' )
1414
import Data.Function ( (&) )
1515
import Data.Bifunctor ( second )
16-
import Data.Maybe ( isJust )
16+
import Data.Maybe ( isJust, mapMaybe )
1717
import Data.Function ( applyWhen )
1818

1919
import Agda.Syntax.Common ( unArg, Arg (Arg) )
2020
import Agda.Syntax.Internal
21+
import Agda.TypeChecking.Substitute ( absBody, TelV (theCore), absApp )
22+
import Agda.TypeChecking.Telescope (telView)
2123
import Agda.TypeChecking.Monad.Base ( TCM, MonadTCM (liftTCM), Definition(..))
22-
import Agda.Utils.Monad (ifM)
24+
import Agda.TypeChecking.Monad.Signature ( canonicalName )
25+
import Agda.TypeChecking.Telescope ( mustBePi )
26+
import Agda.Utils.Monad ( ifM )
2327

2428
import qualified LambdaBox as LBox
2529
import Agda2Lambox.Compile.Utils
2630
import Agda2Lambox.Compile.Monad
2731
import Agda.Compiler.Backend (HasConstInfo(getConstInfo), Definition(Defn), AddContext (addContext))
2832
import Agda.Utils (isDataOrRecDef, getInductiveParams, isArity, maybeUnfoldCopy)
29-
import Agda.TypeChecking.Substitute (absBody, TelV (theCore))
30-
import Agda.TypeChecking.Telescope (telView)
31-
import Agda.TypeChecking.Monad.Signature ( canonicalName )
3233

3334

3435
-- | The kind of variables that are locally bound
@@ -113,11 +114,15 @@ compileType tvars = runC tvars . compileTypeC
113114
compileTypeC :: Type -> C LBox.Type
114115
compileTypeC = local (\e -> e { insertVars = False }) . fmap snd . compileTopLevelTypeC
115116

116-
compileElims :: Elims -> C [LBox.Type]
117-
compileElims = mapM \case
118-
Apply a -> fmap snd $ compileTypeTerm $ unArg a
119-
Proj{} -> genericError "type-level projection elim not supported."
120-
IApply{} -> genericError "type-level cubical path application not supported."
117+
compileElims :: Type -> Args -> C [LBox.Type]
118+
compileElims _ [] = pure []
119+
compileElims ty (x:xs) = do
120+
(a, b) <- mustBePi ty
121+
rest <- compileElims (absApp b $ unArg x) xs
122+
first <- ifM (liftTCM $ isLogical a)
123+
(pure LBox.TBox)
124+
(fmap snd $ compileTypeTerm $ unArg x)
125+
pure (first : rest)
121126

122127
getTypeVarInfo :: Dom Type -> TCM LBox.TypeVarInfo
123128
getTypeVarInfo typ = do
@@ -164,8 +169,9 @@ compileTypeTerm = \case
164169
else if isDataOrRecDef def then do
165170
lift $ requireDef q
166171
ind <- liftTCM $ toInductive q
172+
let args = mapMaybe isApplyElim es
167173
([],) . foldl' LBox.TApp (LBox.TInd ind)
168-
<$> compileElims (take (getInductiveParams def) es)
174+
<$> compileElims defType (take (getInductiveParams def) args)
169175

170176
-- otherwise, it must have been compiled as a type scheme,
171177
-- and therefore is kept with all arguments.
@@ -175,9 +181,11 @@ compileTypeTerm = \case
175181
-- TODO(flupe): possibly merge the logic with the above for datatypes
176182
else do
177183
q <- liftTCM $ canonicalName q
184+
lift $ requireDef q
178185
let ts = qnameToKName q
186+
let args = mapMaybe isApplyElim es
179187
([],) . foldl' LBox.TApp (LBox.TConst ts)
180-
<$> compileElims es
188+
<$> compileElims defType args
181189

182190
Pi dom codom -> do
183191
let domType = unDom dom

src/Agda2Lambox/Compile/TypeScheme.hs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,24 @@ compileTypeScheme Defn{..} = do
5151
TelV tyargs _ <- telView defType
5252
let Function{..} = theDef
5353
case funClauses of
54-
[cl] | onlyVarsPat (namedClausePats cl)
55-
, isJust (clauseBody cl) -> do
56-
cl <- etaExpandClause cl
57-
addContext (KeepNames $ clauseTel cl) do
58-
-- retrieve the telescope of arguments that have not been introduced yet
59-
let
60-
-- number of parameters introduced in the clause
61-
nvars = length $ namedClausePats cl
62-
args = drop nvars $ telToList tyargs
54+
[cl]
55+
| onlyVarsPat (namedClausePats cl)
56+
, isJust (clauseBody cl) -> do
6357

64-
-- we traverse explicit lambdas
65-
underLams :: Term -> [Dom (ArgName, Type)] -> CompileM LBox.Type
66-
underLams (Lam ai t) (dom:rest) =
67-
underAbstraction (snd <$> dom) t \body ->
68-
underLams body rest
69-
underLams t args =
70-
fmap snd $ runCNoVars nvars $ compileTypeTerm t
58+
-- We eta-expand the clause to maximal arity by inserting variable patterns
59+
-- and applying the body to variables.
60+
-- This also lifts all lambdas from the body to variable patterns.
61+
cl <- etaExpandClause cl
7162

72-
Just body = clauseBody cl
63+
let
64+
-- number of parameters introduced in the clause
65+
nvars = length $ namedClausePats cl
66+
Just body = clauseBody cl
7367

74-
res <- underLams body args
75-
tvarInfo <- compileTele tyargs
68+
(_, res) <- addContext (KeepNames $ clauseTel cl) $ runCNoVars nvars $ compileTypeTerm body
69+
tvarInfo <- compileTele tyargs
7670

77-
pure $ TypeAliasDecl $ Just (tvarInfo, res)
71+
pure $ TypeAliasDecl $ Just (tvarInfo, res)
7872

7973
-- if there isn't exactly one clause, we give up
8074
-- compiling the type alias and return TAny

test/Scheme.agda

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# OPTIONS --erasure #-}
1+
{-# OPTIONS --erasure --prop #-}
22
-- compilation of λ□ type schemes
33
module Scheme where
44

@@ -11,23 +11,26 @@ length : {A : Set} → List A → Nat
1111
length [] = 0
1212
length (_ ∷ xs) = suc (length xs)
1313

14-
record Σ (A : Set) (B : A Set) : Set where
14+
data Eq {A : Set} (x : A) : A -> Prop where
15+
rfl : Eq x x
16+
17+
record Σ (A : Set) (B : A Prop) : Set where
1518
constructor _,_
1619
field
17-
fst : A
20+
fst : A
1821
snd : B fst
19-
Σ-syntax : (A : Set) (B : A Set) Set
22+
Σ-syntax : (A : Set) (B : A Prop) Set
2023
Σ-syntax = Σ
2124

2225
syntax Σ-syntax A (λ x B) = [ x ∈ A ∣ B ]
2326

2427
-- NOTE: this doesn't compile properly?
2528
-- the snd projection is compiled while it shouldn't be?
26-
record Σ0 (A : Set) (@0 B : A Set) : Set where
27-
constructor _,_
28-
field
29-
fst : A
30-
@0 snd : B fst
29+
-- record Σ0 (A : Set) (@0 B : A → Set) : Set where
30+
-- constructor _,_
31+
-- field
32+
-- fst : A
33+
-- @0 snd : B fst
3134

3235
-- expected: type scheme
3336
-- ­ vars: [a, b]
@@ -50,7 +53,7 @@ ListAlias' A = List A
5053
-- - vars: [a, n]
5154
-- - type: Σ (List a) □
5255
Vec : (A : Set) Nat Set
53-
Vec A n = [ xs ∈ List A ∣ length xs n ]
56+
Vec A n = [ xs ∈ List A ∣ Eq (length xs) n ]
5457

5558
Bad : Bool Set
5659
Bad false = Nat

0 commit comments

Comments
 (0)