Skip to content

Commit 28e9e6a

Browse files
committed
actually do generate projections
1 parent 048e4a3 commit 28e9e6a

File tree

7 files changed

+81
-51
lines changed

7 files changed

+81
-51
lines changed

Playground.v

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
1-
From MetaCoq.Template Require Import Loader.
1+
From MetaCoq.Template Require Import Loader Ast.
22
From MetaCoq.ErasurePlugin Require Import Erasure Loader.
3-
From MetaCoq.Utils Require Import utils.
4-
From MetaCoq.Template Require Import Ast.
5-
From MetaCoq.Erasure.Typed Require Import ResultMonad.
6-
From MetaCoq.Erasure.Typed Require Import Optimize.
7-
From MetaCoq.Erasure.Typed Require Import Extraction.
8-
From Coq Require Import ZArith.
9-
From Coq Require Import List.
10-
From Coq Require Import String.
11-
From Coq Require Import Nat.
12-
13-
Import MCMonadNotation.
3+
From MetaCoq.Utils Require Import utils.
4+
From Coq Require Import ZArith List String Nat.
5+
146
Import ListNotations.
157

16-
Local Open Scope string.
17-
Local Notation "'bs_to_s' s" := (bytestring.String.to_string s) (at level 200).
18-
Local Notation "'s_to_bs' s" := (bytestring.String.of_string s) (at level 200).
8+
Check run_erase_program.
199

10+
Locate run_erase_program.
2011

21-
Program Definition cic_to_box p :=
22-
run_erase_program default_erasure_config ([], p) _.
12+
Program Definition cic_to_box (p : program) :=
13+
run_erase_program default_erasure_config p _.
2314
Next Obligation.
24-
split. easy.
2515
split.
2616
now eapply assume_that_we_only_erase_on_welltyped_programs.
2717
cbv [PCUICWeakeningEnvSN.normalizationInAdjustUniversesIn].
@@ -52,7 +42,23 @@ with odd (n : nat) :=
5242
| S n => even n
5343
end.
5444

55-
Definition prog := double.
45+
Set Primitive Projections.
46+
Set Printing Projections.
47+
48+
Record Prod (A B : Type) : Type := Pair
49+
{ fst : A
50+
; snd : B
51+
}.
52+
53+
Arguments Pair {_ _} _ _.
54+
Arguments fst {_ _} _.
55+
Arguments snd {_ _} _.
56+
57+
Definition pair : Prod bool bool := Pair true false.
58+
59+
Definition prog := pair.(fst).
5660

57-
MetaCoq Quote Recursively Definition ex1 := prog.
61+
MetaCoq Quote Recursively Definition ex1 := fst.
62+
Check ex1.
63+
Print ex1.
5864
Eval vm_compute in cic_to_box ex1.

src/Agda/Utils.hs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ etaExpandAlt = \case
113113
TALit l b -> TALit l <$> etaExpandCtor b
114114

115115

116+
-- * projections
117+
118+
isRecordProjection :: Defn -> Maybe (QName, QName)
119+
isRecordProjection d
120+
| Function{..} <- d
121+
, Right Projection{..} <- funProjection
122+
, Just recName <- projProper
123+
= Just (recName, projOrig)
124+
| otherwise
125+
= Nothing
126+
116127

117128
{-
118129
lookupCtx :: MonadTCEnv m => Int -> m (String, Type)
@@ -419,15 +430,6 @@ isFunDef = \case
419430
Function{} -> True
420431
_ -> False
421432
422-
isRecordProjection :: Defn -> Maybe (QName, QName)
423-
isRecordProjection d
424-
| Function{..} <- d
425-
, Right Projection{..} <- funProjection
426-
, Just recName <- projProper
427-
= Just (recName, projOrig)
428-
| otherwise
429-
= Nothing
430-
431433
funCC :: (MonadTCM m, HasConstInfo m) => QName -> m CompiledClauses
432434
funCC q = do
433435
def <- theDef <$> getConstInfo q

src/Agda2Lambox/Convert/Function.hs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,12 @@ convertFunctionBody Defn{defName} =
4242
Just t <- liftTCM $ toTreeless EagerEvaluation defName
4343
convert =<< liftTCM (etaExpandCtor t)
4444

45-
-- | Whether a function is a (true) record projection.
46-
isProjection :: Either ProjectionLikenessMissing Projection -> Bool
47-
isProjection (Left _) = False
48-
isProjection (Right Projection{projProper}) = isJust projProper
49-
5045
-- | Whether to compile a function definition to λ□.
5146
shouldCompileFunction :: Definition -> Bool
5247
shouldCompileFunction def@Defn{theDef} | Function{..} <- theDef
5348
= not (theDef ^. funInline) -- not inlined (from module application)
5449
&& isNothing funExtLam -- not a pattern-lambda-generated function NOTE(flupe): ?
5550
&& isNothing funWith -- not a with-generated function NOTE(flupe): ?
56-
&& not (isProjection funProjection) -- not a record projection
5751
&& hasQuantityω def -- non-erased
5852

5953
-- | Convert a function definition to a λ□ declaration.
@@ -71,9 +65,6 @@ convertFunction defn@Defn{defName, theDef} =
7165
if any (not . isFunction) mdefs then
7266
fail "Induction-recursion and other weird mutual defs not supported."
7367
else inMutuals ms do
74-
-- NOTE(flupe):
75-
-- maybe reverse ms here?
76-
-- it's unclear in which order mutual fixpoints are added to the local context
7768
let Just k = elemIndex defName ms
7869

7970
Just . ConstantDecl . Just . flip LFix k <$>

src/Agda2Lambox/Convert/Term.hs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
1-
{-# LANGUAGE LambdaCase, FlexibleInstances #-}
1+
{-# LANGUAGE LambdaCase, FlexibleInstances, NamedFieldPuns #-}
22
module Agda2Lambox.Convert.Term () where
33

44
import Control.Monad.Reader ( ask, liftIO )
5+
import Data.Foldable ( foldlM )
56
import Data.List ( elemIndex )
7+
import Data.Bifunctor ( second )
8+
import Data.Functor ( (<&>) )
69

710
import Utils
811

912
import Agda ( liftTCM, getConstructorData, getConstructors )
1013
import qualified Agda as A
1114
import Agda.Lib ()
12-
import Agda.Utils
15+
import Agda.Utils ( isRecordProjection )
1316
import Agda.Syntax.Literal
17+
import Agda.Syntax.Internal ( unDom )
1418
import Agda.Syntax.Abstract.Name ( QName(..), ModuleName(..) )
1519
import Agda.Syntax.Common.Pretty ( prettyShow )
1620
import Agda.TypeChecking.Primitive.Base ( getBuiltinName )
17-
import Agda.Compiler.Backend ( builtinNat, builtinZero, builtinSuc )
21+
import Agda.Compiler.Backend hiding (Name)
1822

1923
import LambdaBox
2024
import qualified LambdaBox as L
2125

2226
import Agda2Lambox.Monad
2327
import Agda2Lambox.Convert.Class
2428

25-
2629
-- | Compiling (treeless) Agda terms into Lambox expressions.
2730
instance A.TTerm ~> L.Term where
2831
go = \case
2932
A.TVar n -> return $ LRel n
3033
A.TPrim pr -> go pr
3134
A.TDef qn -> do
32-
Env{..} <- ask
35+
Env{mutuals, boundVars} <- ask
3336
return case qn `elemIndex` mutuals of
3437
Nothing -> LConst $ qnameToKerName qn
3538
Just i -> LRel $ i + boundVars
@@ -57,8 +60,8 @@ instance A.TTerm ~> L.Term where
5760
calts <- traverse go talts
5861
cind <- go caseType
5962
return $ LCase cind 0 (LRel n) calts
60-
A.TUnit -> return LBox
61-
A.TSort -> return LBox
63+
A.TUnit -> return LBox
64+
A.TSort -> return LBox
6265
A.TErased -> return LBox
6366
A.TCoerce tt -> fail "Coerces are not supported."
6467
A.TError terr -> fail "Errors are not supported."

src/LambdaBox.hs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import Agda.Syntax.Common.Pretty
88
import Agda.Syntax.Abstract.Name ( QName(..), ModuleName(..) )
99
import Agda.Syntax.Common.Pretty ( prettyShow )
1010

11-
-- TODO(flupe): Record projections?
12-
1311
-- | Identifiers.
1412
-- Used only for pretty-printing by MetaCoq?
1513
-- MetaCoq uses bytestrings, it doesn't matter here.
@@ -124,18 +122,17 @@ data Term
124122
| LVar Ident -- ^ Free variable with identifier
125123
-- NOTE(flupe): not needed?
126124
| LLam Term -- ^ Lambda abstraction
127-
| LLet Term Term
125+
| LLet Term Term
128126
-- ^ Let bindings.
129127
-- Unused in the backend, since Agda itself no longer has let bindings
130128
-- in the concrete syntac.
131129
| LApp Term Term -- ^ Term application
132130
| LConst KerName -- ^ Named constant.
133-
-- TODO(flupe): I *think* constructors have to be fully-applied.
134131
| LCtor Inductive Int [Term] -- ^ Inductive constructor.
135132
| LCase Inductive Int Term [([Name], Term)]
136133
-- ^ Pattern-matching case construct.
137134
| LFix MFixpoint Int
138-
-- ^ Fixpoint combinator.
135+
-- ^ Fixpoint combinator.
139136
-- The index refers to which fixpoint we select out of
140137
-- the generated (mutual) fixpoints.
141138
deriving (Eq, Show)

test/Proj.agda

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
data Bool : Set where true false : Bool
2+
3+
record Pair (A B : Set) : Set where
4+
constructor _,_
5+
field
6+
fst : A
7+
snd : B
8+
open Pair
9+
10+
pair : Pair Bool Bool
11+
pair = true , false
12+
13+
second : Bool
14+
second = snd pair
15+
{-# COMPILE AGDA2LAMBOX second #-}

theories/CheckWF.v

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,23 @@ Notation "x 'eqn:' p" := (exist _ x p) (only parsing, at level 20).
2525

2626
Definition eflags : EEnvFlags :=
2727
{| has_axioms := true;
28-
term_switches := all_term_flags;
28+
term_switches :=
29+
{| has_tBox := true
30+
; has_tRel := true
31+
; has_tVar := true
32+
; has_tEvar := true
33+
; has_tLambda := true
34+
; has_tLetIn := true
35+
; has_tApp := true
36+
; has_tConst := true
37+
; has_tConstruct := true
38+
; has_tCase := true
39+
; has_tProj := false (* Our backends shouldn't produce projections *)
40+
; has_tFix := true
41+
; has_tCoFix := true
42+
; has_tPrim := all_primitive_flags
43+
; has_tLazy_Force := true
44+
|};
2945
has_cstr_params := false; (* Agda already drops constructor params *)
3046
cstr_as_blocks := true; (* The backend fully applies ctors *)
3147
|}.

0 commit comments

Comments
 (0)