Skip to content

Commit

Permalink
Add built-in support for Delay monad
Browse files Browse the repository at this point in the history
  • Loading branch information
jespercockx committed Jan 16, 2024
1 parent e492bf8 commit 57d6c4a
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/Haskell/Extra/Delay.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{-# OPTIONS --sized-types #-}

module Haskell.Extra.Delay where

open import Haskell.Prim.Thunk

data Delay (a : Set) (@0 i : Size) : Set where
now : a Delay a i
later : Thunk (Delay a) i Delay a i
2 changes: 2 additions & 0 deletions src/Agda2Hs/Compile/Term.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ isSpecialCon = prettyShow >>> \case
"Haskell.Prim.Tuple._,_" -> Just tupleTerm
"Haskell.Prim.Tuple._×_×_._,_,_" -> Just tupleTerm
"Haskell.Extra.Erase.Erased" -> Just (\_ _ _ -> erasedTerm)
"Haskell.Extra.Delay.Delay.now" -> Just $ \_ _ -> compileErasedApp
"Haskell.Extra.Delay.Delay.later" -> Just $ \_ _ -> compileErasedApp
_ -> Nothing

tupleTerm :: ConHead -> ConInfo -> Elims -> C (Hs.Exp ())
Expand Down
7 changes: 7 additions & 0 deletions src/Agda2Hs/Compile/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ isSpecialType = prettyShow >>> \case
"Haskell.Prim.Tuple._×_" -> Just tupleType
"Haskell.Prim.Tuple._×_×_" -> Just tupleType
"Haskell.Extra.Erase.Erase" -> Just erasedType
"Haskell.Extra.Delay.Delay" -> Just delayType
_ -> Nothing

tupleType :: QName -> Elims -> C (Hs.Type ())
Expand All @@ -55,6 +56,12 @@ tupleType q es = do
erasedType :: QName -> Elims -> C (Hs.Type ())
erasedType _ _ = return $ Hs.TyTuple () Hs.Boxed []

delayType :: QName -> Elims -> C (Hs.Type ())
delayType _ (Apply a : _) = compileType (unArg a)
delayType _ (_ : _) = __IMPOSSIBLE__
delayType _ [] = genericDocError =<< text "Cannot compile unapplied Delay type"


-- | Add a class constraint to a Haskell type.
constrainType
:: Hs.Asst () -- ^ The class assertion.
Expand Down
2 changes: 2 additions & 0 deletions test/AllTests.agda
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import Coerce
import Inlining
import EraseType
import Issue257
import Delay

{-# FOREIGN AGDA2HS
import Issue14
Expand Down Expand Up @@ -133,4 +134,5 @@ import ModuleParametersImports
import Coerce
import Inlining
import EraseType
import Delay
#-}
23 changes: 23 additions & 0 deletions test/Delay.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

module Delay where

open import Haskell.Prelude
open import Haskell.Prim.Thunk
open import Haskell.Extra.Delay

open import Agda.Builtin.Size

postulate
div : Int Int Int
mod : Int Int Int

even : Int Bool
even x = mod x 2 == 0

collatz : {@0 j} Int Delay Int j
collatz x =
if x == 0 then now 0
else if even x then later (λ where .force collatz (div x 2))
else later λ where .force collatz (3 * x + 1)

{-# COMPILE AGDA2HS collatz #-}
1 change: 1 addition & 0 deletions test/golden/AllTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ import ModuleParametersImports
import Coerce
import Inlining
import EraseType
import Delay

7 changes: 7 additions & 0 deletions test/golden/Delay.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Delay where

collatz :: Int -> Int
collatz x
= if x == 0 then 0 else
if even x then collatz (div x 2) else collatz (3 * x + 1)

0 comments on commit 57d6c4a

Please sign in to comment.