-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add built-in support for Delay monad
- Loading branch information
1 parent
e492bf8
commit 984821a
Showing
13 changed files
with
114 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{-# OPTIONS --sized-types #-} | ||
|
||
module Haskell.Extra.Delay where | ||
|
||
open import Agda.Builtin.Size public | ||
|
||
open import Haskell.Prelude | ||
open import Haskell.Prim.Thunk | ||
open import Haskell.Extra.Refinement | ||
|
||
private variable | ||
x y z : a | ||
@0 i : Size | ||
|
||
data Delay (a : Set) (@0 i : Size) : Set where | ||
now : a → Delay a i | ||
later : Thunk (Delay a) i → Delay a i | ||
|
||
data HasResult (x : a) : Delay a i → Set where | ||
now : HasResult x (now x) | ||
later : HasResult x (y .force) → HasResult x (later y) | ||
|
||
runDelay : {@0 x : a} (y : Delay a ∞) → @0 HasResult x y → a | ||
runDelay (now x) now = x | ||
runDelay (later y) (later p) = runDelay (y .force) p | ||
|
||
runDelaySound : {@0 x : a} (y : Delay a ∞) → (@0 hr : HasResult x y) → runDelay y hr ≡ x | ||
runDelaySound (now x) now = refl | ||
runDelaySound (later y) (later hr) = runDelaySound (y .force) hr | ||
|
||
-- tryDelay and unDelay cannot and should not be compiled to Haskell, | ||
-- so they are marked as erased. | ||
@0 tryDelay : (y : Delay a ∞) → Nat → Maybe (∃ a (λ x → HasResult x y)) | ||
tryDelay (now x) _ = Just (x ⟨ now ⟩) | ||
tryDelay (later y) zero = Nothing | ||
tryDelay (later y) (suc n) = fmap (mapRefine later) (tryDelay (y .force) n) | ||
|
||
@0 unDelay : (y : Delay a ∞) (n : Nat) → @0 {IsJust (tryDelay y n)} → a | ||
unDelay y n {p} = fromJust (tryDelay y n) {p} .value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 #-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
module Fail.MatchOnDelay where | ||
|
||
open import Haskell.Prelude | ||
open import Haskell.Extra.Delay | ||
|
||
bad : Delay a ∞ → Bool | ||
bad (now x) = True | ||
bad (later x) = False | ||
|
||
{-# COMPILE AGDA2HS bad #-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,4 +64,5 @@ import ModuleParametersImports | |
import Coerce | ||
import Inlining | ||
import EraseType | ||
import Delay | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
test/Fail/MatchOnDelay.agda:7,1-4 | ||
constructor `now` not supported in patterns |