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

[fix #215] handle automatic projections from let pattern of tuples #216

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Agda2Hs/Compile/Term.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Control.Monad ( unless )
import Control.Monad.Reader

import Data.List ( isPrefixOf )
import Data.Maybe ( fromMaybe, isJust )
import Data.Maybe ( fromMaybe, isJust, listToMaybe )
import qualified Data.Text as Text ( unpack )

import qualified Language.Haskell.Exts as Hs
Expand Down Expand Up @@ -46,6 +46,7 @@ isSpecialTerm q = case prettyShow q of
"Haskell.Prim.case_of_" -> Just caseOf
"Haskell.Prim.Monad.Do.Monad._>>=_" -> Just bind
"Haskell.Prim.Monad.Do.Monad._>>_" -> Just sequ
"Haskell.Prim.Tuple.Pair.fst" -> Just (const tupleProj)
"Agda.Builtin.FromNat.Number.fromNat" -> Just fromNat
"Agda.Builtin.FromNeg.Negative.fromNeg" -> Just fromNeg
"Agda.Builtin.FromString.IsString.fromString" -> Just fromString
Expand All @@ -66,6 +67,13 @@ tupleTerm cons i es = do
ts <- mapM compileTerm xs
return $ Hs.Tuple () Hs.Boxed ts

tupleProj :: Elims -> C (Hs.Exp ())
tupleProj elims = do
let t = unArg $ fromMaybe __IMPOSSIBLE__ (listToMaybe =<< allApplyElims elims)
case unSpine1 t of
Def q es | q ~~ "Haskell.Prim.Tuple.Pair.snd" -> compileTerm t
_ -> Hs.App () (hsVar "fst") <$> compileTerm t

ifThenElse :: QName -> Elims -> C (Hs.Exp ())
ifThenElse _ es = compileElims es >>= \case
-- fully applied
Expand Down
10 changes: 10 additions & 0 deletions test/Tuples.agda
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,13 @@ t3 : Bool × (Bool × Bool)
t3 = True , (False , True)

{-# COMPILE AGDA2HS t3 #-}

pair : Int × Int
pair = (1 , 2)

{-# COMPILE AGDA2HS pair #-}

test : Int
test = let (x , y) = pair in x + y

{-# COMPILE AGDA2HS test #-}
6 changes: 6 additions & 0 deletions test/golden/Tuples.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ t2 = ((True, False), True)
t3 :: (Bool, (Bool, Bool))
t3 = (True, (False, True))

pair :: (Int, Int)
pair = (1, 2)

test :: Int
test = fst pair + snd pair

Loading