Skip to content

Commit

Permalink
Compile Agda sections to Haskell sections
Browse files Browse the repository at this point in the history
  • Loading branch information
UlfNorell committed Nov 3, 2020
1 parent 97d77fd commit fd72b4c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
16 changes: 8 additions & 8 deletions Main.hs
Expand Up @@ -329,13 +329,9 @@ compileType t = do
t -> genericDocError =<< text "Bad Haskell type:" <?> prettyTCM t

compileTerm :: Term -> TCM (Hs.Exp ())
compileTerm v =
compileTerm v = do
case unSpine v of
Var x es -> do
cxt <- getContext
reportSDoc "agda2hs.compile" 10 $ text "context:" <+> fsep
[ prettyTCM (Var i []) | i <- reverse [0..length cxt - 1] ]
(`app` es) . Hs.Var () . Hs.UnQual () . hsName =<< showTCM (Var x [])
Var x es -> (`app` es) . Hs.Var () . Hs.UnQual () . hsName =<< showTCM (Var x [])
Def f es
| Just semantics <- isSpecialTerm f -> semantics f es
| otherwise -> (`app` es) . Hs.Var () =<< hsQName f
Expand All @@ -350,10 +346,14 @@ compileTerm v =
hsLambda (absName b) <$> underAbstr_ b compileTerm
Lam v b | visible v ->
-- System-inserted lambda, no need to preserve the name.
-- TODO: most likely a section, so should really reconstruct that.
underAbstraction_ b $ \ body -> do
x <- showTCM (Var 0 [])
hsLambda x <$> compileTerm body
let hsx = Hs.Var () $ Hs.UnQual () (hsName x)
body <- compileTerm body
return $ case body of
Hs.InfixApp _ a op b
| a == hsx -> Hs.RightSection () op b -- System-inserted visible lambdas can only come from sections
_ -> hsLambda x body -- so we know x is not free in b.
Lam _ b -> underAbstraction_ b compileTerm
t -> genericDocError =<< text "bad term:" <?> prettyTCM t
where
Expand Down
2 changes: 2 additions & 0 deletions test/AllTests.agda
Expand Up @@ -3,12 +3,14 @@ module AllTests where

import Issue14
import Pragmas
import Sections
import Test
import Tuples

{-# FOREIGN AGDA2HS
import Issue14
import Pragmas
import Sections
import Test
import Tuples
#-}
17 changes: 17 additions & 0 deletions test/Sections.agda
@@ -0,0 +1,17 @@

module Sections where

open import Haskell.Prelude

test₁ : Nat Nat
test₁ = 5 +_

test₂ : Nat Nat
test₂ = _+ 5

test₃ : Nat Nat
test₃ = _+_ 5

{-# COMPILE AGDA2HS test₁ #-}
{-# COMPILE AGDA2HS test₂ #-}
{-# COMPILE AGDA2HS test₃ #-}
1 change: 1 addition & 0 deletions test/golden/AllTests.hs
Expand Up @@ -2,6 +2,7 @@ module AllTests where

import Issue14
import Pragmas
import Sections
import Test
import Tuples

4 changes: 2 additions & 2 deletions test/golden/Issue14.hs
Expand Up @@ -4,8 +4,8 @@ constid :: a -> b -> b
constid x = \ x -> x

sectionTest₁ :: Integer -> Integer -> Integer
sectionTest₁ n = \ section -> section + n
sectionTest₁ n = (+ n)

sectionTest₂ :: Integer -> Integer -> Integer
sectionTest₂ section = \ section₁ -> section₁ + section
sectionTest₂ section = (+ section)

11 changes: 11 additions & 0 deletions test/golden/Sections.hs
@@ -0,0 +1,11 @@
module Sections where

test₁ :: Integer -> Integer
test₁ = (5 +)

test₂ :: Integer -> Integer
test₂ = (+ 5)

test₃ :: Integer -> Integer
test₃ = (5 +)

0 comments on commit fd72b4c

Please sign in to comment.