Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile lambdas #5

Merged
merged 1 commit into from Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions Main.hs
Expand Up @@ -226,6 +226,8 @@ compileTerm builtins v =
Def f es -> (`app` es) . Hs.Var () =<< hsQName builtins f
Con h i es -> (`app` es) . Hs.Con () =<< hsQName builtins (conName h)
Lit (LitNat n) -> return $ Hs.Lit () $ Hs.Int () n (show n)
Lam v b | visible v -> hsLambda (absName b) <$> underAbstraction_ b (compileTerm builtins)
Lam _ b -> underAbstraction_ b (compileTerm builtins)
t -> genericDocError =<< text "bad term:" <?> prettyTCM t
where
app :: Hs.Exp () -> Elims -> TCM (Hs.Exp ())
Expand Down Expand Up @@ -262,6 +264,14 @@ tApp :: Hs.Type () -> [Hs.Type ()] -> Hs.Type ()
tApp (Hs.TyCon () (Hs.Special () Hs.ListCon{})) [a] = Hs.TyList () a
tApp t vs = foldl (Hs.TyApp ()) t vs

hsLambda :: String -> Hs.Exp () -> Hs.Exp ()
hsLambda x e =
case e of
Hs.Lambda l ps b -> Hs.Lambda l (p : ps) b
_ -> Hs.Lambda () [p] e
where
p = Hs.PVar () $ hsName x

-- FOREIGN pragmas --------------------------------------------------------

type Code = (Hs.Module Hs.SrcSpanInfo, [Hs.Comment])
Expand Down
1 change: 0 additions & 1 deletion README.md
Expand Up @@ -4,7 +4,6 @@ Compiles a subset of Agda to readable Haskell code. See Test.agda for an example

### Future work

- Compile lambdas
- Compile if/then/else
- Literals in patterns
- Use some Haskell syntax ADT and a proper pretty printing library
Expand Down
10 changes: 10 additions & 0 deletions Test.agda
Expand Up @@ -61,6 +61,16 @@ map f (x ∷ xs) = f x ∷ map f xs

{-# COMPILE AGDA2HS map #-}

plus3 : List Nat → List Nat
plus3 = map (λ n → n + 3)

{-# COMPILE AGDA2HS plus3 #-}

doubleLambda : Nat → Nat → Nat
doubleLambda = λ a b → a + 2 * b

{-# COMPILE AGDA2HS doubleLambda #-}

assoc : (a b c : Nat) → a + (b + c) ≡ (a + b) + c
assoc zero b c = refl
assoc (suc a) b c rewrite assoc a b c = refl
Expand Down