Skip to content

Commit 5027847

Browse files
committed
add flag for typed compil, weird gadts
1 parent 542f848 commit 5027847

15 files changed

+503
-363
lines changed

Playground.v

+28-21
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,49 @@
11
From MetaCoq.Template Require Import Loader Ast.
22
From MetaCoq.ErasurePlugin Require Import Erasure Loader.
33
From MetaCoq.Utils Require Import utils.
4+
From MetaCoq.Erasure.Typed Require Import ResultMonad.
5+
From MetaCoq.Erasure.Typed Require Import Optimize.
6+
From MetaCoq.Erasure.Typed Require Import Extraction.
47
From Coq Require Import ZArith List String Nat.
58

9+
Import MCMonadNotation.
610
Import ListNotations.
711

812
Check run_erase_program.
913

1014
Locate run_erase_program.
1115

1216
Program Definition cic_to_box (p : program) :=
13-
run_erase_program default_erasure_config p _.
17+
run_erase_program default_erasure_config ([], p) _.
1418
Next Obligation.
19+
split. easy.
1520
split.
1621
now eapply assume_that_we_only_erase_on_welltyped_programs.
1722
cbv [PCUICWeakeningEnvSN.normalizationInAdjustUniversesIn].
1823
pose proof @PCUICSN.normalization.
1924
split; typeclasses eauto.
2025
Qed.
2126

27+
Definition no_check_args :=
28+
{| check_wf_env_func Σ := Ok (assume_env_wellformed Σ);
29+
template_transforms := [];
30+
pcuic_args :=
31+
{| optimize_prop_discr := true;
32+
extract_transforms := [dearg_transform (fun _ => None) true true false false false] |} |}.
33+
34+
Definition cic_to_box_typed p :=
35+
entry <- match p.2 with
36+
| tConst kn _ => Ok kn
37+
| tInd ind _ => Ok (inductive_mind ind)
38+
| _ => Err ("Expected program to be a tConst or tInd")
39+
end;;
40+
Σ <- extract_template_env
41+
no_check_args
42+
p.1
43+
(KernameSet.singleton entry)
44+
(fun k => false);;
45+
Ok Σ.
46+
2247
Definition not (x : bool) :=
2348
match x with
2449
| true => true
@@ -42,23 +67,5 @@ with odd (n : nat) :=
4267
| S n => even n
4368
end.
4469

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).
60-
61-
MetaCoq Quote Recursively Definition ex1 := fst.
62-
Check ex1.
63-
Print ex1.
64-
Eval vm_compute in cic_to_box ex1.
70+
MetaCoq Quote Recursively Definition ex1 := odd.
71+
Eval vm_compute in cic_to_box_typed ex1.

agda2lambox.cabal

+5
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ executable agda2lambox
1616
hs-source-dirs: src
1717
main-is: Main.hs
1818
other-modules: Agda.Utils,
19+
Agda2Lambox.Compile.Target,
1920
Agda2Lambox.Compile.Utils,
2021
Agda2Lambox.Compile.Monad,
2122
Agda2Lambox.Compile.Term,
2223
Agda2Lambox.Compile.Function,
2324
Agda2Lambox.Compile.Inductive,
2425
Agda2Lambox.Compile.Type,
2526
Agda2Lambox.Compile,
27+
LambdaBox.Names,
28+
LambdaBox.Term,
29+
LambdaBox.Type,
30+
LambdaBox.Env,
2631
LambdaBox,
2732
CoqGen,
2833
Paths_agda2lambox

src/Agda2Lambox/Compile.hs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# LANGUAGE NamedFieldPuns #-}
1+
{-# LANGUAGE NamedFieldPuns, DataKinds #-}
22
module Agda2Lambox.Compile
33
( compileDefinition
44
) where
@@ -13,14 +13,18 @@ import Agda.TypeChecking.Monad ( liftTCM, getConstInfo )
1313
import Agda.Utils.Monad ( whenM )
1414

1515
import Agda.Utils ( hasPragma, isDataOrRecDef )
16+
1617
import Agda2Lambox.Compile.Monad
18+
import Agda2Lambox.Compile.Target
1719
import Agda2Lambox.Compile.Utils
1820
import Agda2Lambox.Compile.Function ( compileFunction )
1921
import Agda2Lambox.Compile.Inductive ( compileInductive )
20-
import LambdaBox
22+
23+
import LambdaBox.Names
24+
import LambdaBox.Env ( GlobalDecl )
2125

2226

23-
compileDefinition :: Definition -> CompileM (Maybe (KerName, GlobalDecl))
27+
compileDefinition :: Definition -> CompileM (Maybe (KerName, GlobalDecl Untyped))
2428
compileDefinition defn@Defn{..} = do
2529
fmap (qnameToKerName defName,) <$> -- prepend kername
2630
case theDef of

src/Agda2Lambox/Compile/Function.hs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# LANGUAGE NamedFieldPuns #-}
1+
{-# LANGUAGE NamedFieldPuns, DataKinds #-}
22
-- | Convert Agda functions to λ□ constant declarations
33
module Agda2Lambox.Compile.Function
44
( compileFunction
@@ -10,7 +10,7 @@ import Data.List ( elemIndex )
1010
import Data.Maybe ( isNothing, fromMaybe )
1111

1212
import Agda.Syntax.Abstract.Name ( QName, qnameModule )
13-
import Agda.TypeChecking.Monad.Base
13+
import Agda.TypeChecking.Monad.Base hiding ( None )
1414
import Agda.Compiler.ToTreeless ( toTreeless )
1515
import Agda.Compiler.Backend ( getConstInfo, funInline )
1616
import Agda.Syntax.Treeless ( EvaluationStrategy(EagerEvaluation) )
@@ -20,10 +20,13 @@ import Agda.Utils.Monad (guardWithError, whenM)
2020
import Agda.Utils.Lens ( (^.) )
2121

2222
import Agda.Utils ( etaExpandCtor, treeless, pp )
23+
import Agda2Lambox.Compile.Target
2324
import Agda2Lambox.Compile.Utils
2425
import Agda2Lambox.Compile.Monad
2526
import Agda2Lambox.Compile.Term ( compileTerm )
27+
2628
import LambdaBox qualified as LBox
29+
import LambdaBox.Env
2730

2831

2932
-- | Check whether a definition is a function.
@@ -49,7 +52,7 @@ shouldCompileFunction def@Defn{theDef} | Function{..} <- theDef
4952

5053

5154
-- | Convert a function definition to a λ□ declaration.
52-
compileFunction :: Definition -> CompileM (Maybe LBox.GlobalDecl)
55+
compileFunction :: Definition -> CompileM (Maybe (LBox.GlobalDecl Untyped))
5356
compileFunction defn | not (shouldCompileFunction defn) = return Nothing
5457
compileFunction defn@Defn{theDef} = do
5558
let Function{funMutual = Just mutuals} = theDef
@@ -65,13 +68,14 @@ compileFunction defn@Defn{theDef} = do
6568
let mnames = map defName mdefs
6669

6770
-- if the function is not recursive, just compile the body
68-
if null mdefs then Just. LBox.ConstantDecl . Just <$> compileFunctionBody [] defn
71+
if null mdefs then
72+
Just . ConstantDecl . ConstantBody None . Just <$> compileFunctionBody [] defn
6973

7074
-- otherwise, take fixpoint
7175
else do
7276
let k = fromMaybe 0 $ elemIndex (defName defn) mnames
7377

74-
Just . LBox.ConstantDecl . Just . flip LBox.LFix k <$>
78+
Just . ConstantDecl . ConstantBody None . Just . flip LBox.LFix k <$>
7579
forM mdefs \def@Defn{defName} -> do
7680
body <- compileFunctionBody mnames def
7781
return LBox.Def

src/Agda2Lambox/Compile/Inductive.hs

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# LANGUAGE NamedFieldPuns, ImportQualifiedPost #-}
1+
{-# LANGUAGE NamedFieldPuns, ImportQualifiedPost, DataKinds #-}
22
-- | Convert Agda datatypes to λ□ inductive declarations
33
module Agda2Lambox.Compile.Inductive
44
( compileInductive
@@ -14,7 +14,7 @@ import Data.Maybe ( isJust, listToMaybe, fromMaybe )
1414
import Data.Traversable ( mapM )
1515

1616
import Agda.Syntax.Abstract.Name ( qnameModule, qnameName )
17-
import Agda.TypeChecking.Monad.Base
17+
import Agda.TypeChecking.Monad.Base hiding (None)
1818
import Agda.TypeChecking.Monad.Env ( withCurrentModule )
1919
import Agda.TypeChecking.Datatypes ( ConstructorInfo(..), getConstructorInfo, isDatatype )
2020
import Agda.Compiler.Backend ( getConstInfo, lookupMutualBlock )
@@ -23,13 +23,14 @@ import Agda.Syntax.Internal ( ConHead(..), unDom )
2323
import Agda.Utils.Monad ( unlessM )
2424

2525
import Agda.Utils ( isDataOrRecDef )
26+
import Agda2Lambox.Compile.Target
2627
import Agda2Lambox.Compile.Utils
2728
import Agda2Lambox.Compile.Monad
2829
import LambdaBox qualified as LBox
2930

3031

3132
-- | Toplevel conversion from a datatype/record definition to a Lambdabox declaration.
32-
compileInductive :: Definition -> CompileM (Maybe LBox.GlobalDecl)
33+
compileInductive :: Definition -> CompileM (Maybe (LBox.GlobalDecl Untyped))
3334
compileInductive defn@Defn{defName} = do
3435
mutuals <- liftTCM $ dataOrRecDefMutuals defn
3536

@@ -69,16 +70,17 @@ compileInductive defn@Defn{defName} = do
6970
, indBodies = NEL.toList bodies
7071
}
7172

72-
actuallyConvertInductive :: Definition -> TCM LBox.OneInductiveBody
73+
actuallyConvertInductive :: Definition -> TCM (LBox.OneInductiveBody Untyped)
7374
actuallyConvertInductive Defn{defName, theDef, defMutual} = case theDef of
7475
Datatype{..} -> do
7576

76-
ctors :: [LBox.ConstructorBody]
77+
ctors :: [LBox.ConstructorBody Untyped]
7778
<- forM dataCons \cname -> do
7879
DataCon arity <- getConstructorInfo cname
79-
return LBox.Ctor
80-
{ ctorName = prettyShow $ qnameName cname
81-
, ctorArgs = arity
80+
return LBox.Constructor
81+
{ cstrName = prettyShow $ qnameName cname
82+
, cstrArgs = arity
83+
, cstrTypes = None
8284
}
8385

8486
pure LBox.OneInductive
@@ -87,19 +89,23 @@ actuallyConvertInductive Defn{defName, theDef, defMutual} = case theDef of
8789
, indKElim = LBox.IntoAny -- TODO(flupe)
8890
, indCtors = ctors
8991
, indProjs = []
92+
, indTypeVars = None
9093
}
9194

9295
Record{..} -> do
9396

9497
let ConHead{conName, conFields} = recConHead
95-
fields :: [LBox.ProjectionBody] = LBox.Proj . prettyShow . qnameName . unDom <$> recFields
98+
fields :: [LBox.ProjectionBody Untyped] =
99+
flip LBox.Projection None . prettyShow . qnameName . unDom <$> recFields
96100

97101
pure LBox.OneInductive
98102
{ indName = prettyShow $ qnameName defName
99103
, indPropositional = False -- TODO(flupe)
100104
, indKElim = LBox.IntoAny -- TODO(flupe)
101-
, indCtors = [ LBox.Ctor (prettyShow $ qnameName conName) (length conFields) ]
105+
, indCtors =
106+
[ LBox.Constructor (prettyShow $ qnameName conName) (length conFields) None ]
102107
, indProjs = fields
108+
, indTypeVars = None
103109
}
104110

105111
-- { indFinite = maybe LBox.BiFinite inductionToRecKind recInduction

src/Agda2Lambox/Compile/Target.hs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{-# LANGUAGE DataKinds, GADTs #-}
2+
-- | Compile targets for the backend
3+
module Agda2Lambox.Compile.Target
4+
( Typing(..)
5+
, Target(..)
6+
, WhenTyped(..)
7+
, extract
8+
) where
9+
10+
import Control.DeepSeq ( NFData(rnf) )
11+
import Data.Kind ( Type )
12+
13+
-- | Supported targets.
14+
data Typing = Typed | Untyped
15+
16+
-- | Compile targets, indexed by the kind of target.
17+
data Target :: Typing -> Type where
18+
ToTyped :: Target Typed
19+
ToUntyped :: Target Untyped
20+
21+
-- | Type wrapper that contains a value iff we're in the typed setting.
22+
data WhenTyped a :: Typing -> Type where
23+
None :: WhenTyped a Untyped
24+
Some :: a -> WhenTyped a Typed
25+
26+
-- | Attempt retrieving value.
27+
extract :: WhenTyped a t -> Maybe a
28+
extract None = Nothing
29+
extract (Some x) = Just x
30+
31+
instance NFData (Target t) where
32+
rnf ToTyped = ()
33+
rnf ToUntyped = ()

src/Agda2Lambox/Compile/Term.hs

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import Agda.TypeChecking.Datatypes ( getConstructorData, getConstructors )
2424
import Agda.TypeChecking.Monad.Base ( TCM , liftTCM, MonadTCEnv, MonadTCM )
2525
import Agda.TypeChecking.Monad.Builtin ( getBuiltinName_ )
2626

27+
import LambdaBox.Names ( Name(..) )
2728
import LambdaBox ( Term(..) )
2829
import LambdaBox qualified as LBox
2930
import Agda2Lambox.Compile.Utils
@@ -105,12 +106,12 @@ compileTermC = \case
105106
ces <- traverse compileTermC es
106107
pure $ foldl' LApp cu ces
107108

108-
TLam t -> underBinder $ LLam <$> compileTermC t
109+
TLam t -> underBinder $ LLambda Anon <$> compileTermC t
109110

110111
TLit l -> compileLit l
111112

112-
TLet u v -> LLet <$> compileTermC u
113-
<*> underBinder (compileTermC v)
113+
TLet u v -> LLetIn Anon <$> compileTermC u
114+
<*> underBinder (compileTermC v)
114115

115116
TCase n CaseInfo{..} dt talts ->
116117
case caseErased of

src/Agda2Lambox/Compile/Utils.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ toConApp qn es = do
5959
ctrs <- getConstructors dt
6060
ind <- toInductive dt
6161
let idx = fromMaybe 0 $ qn `elemIndex` ctrs
62-
pure $ LBox.LCtor ind idx es
62+
pure $ LBox.LConstruct ind idx es
63+

0 commit comments

Comments
 (0)