-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #239 from granule-project/dev-minor
v0.9.5.0
- Loading branch information
Showing
121 changed files
with
1,474 additions
and
503 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
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
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,54 @@ | ||
-- Example of using algebraic effects and handlers in Granule to capture | ||
-- non-determinism effects | ||
|
||
-- Uses the builtin `call` and `handle` operations. | ||
-- See https://granule-project.github.io/docs/modules/Primitives.html#algebraiceffectsandhandlers | ||
|
||
import List | ||
|
||
-- # Coin toss game effect operations | ||
|
||
data Labels = Toss | Drop | ||
|
||
-- Operations | ||
data GameOps : Set Labels -> Type -> Type where | ||
FlipCoin : forall {r : Type} . () -> (Bool -> r) [2] -> GameOps {Toss} r; | ||
Fumble : forall {r : Type} . () -> (Void -> r) [0] -> GameOps {Drop} r | ||
|
||
-- -- Functoriality of operations | ||
fmap_gameops : forall {a b : Type, l : Set Labels} | ||
. (a -> b) [0..Inf] -> GameOps l a -> GameOps l b | ||
fmap_gameops [f] (FlipCoin () [k]) = FlipCoin () [f . k]; | ||
fmap_gameops [f] (Fumble () [k]) = Fumble () [f . k] | ||
|
||
-- -- Handler to list monad | ||
handler : forall {a : Type, l : Set Labels} . GameOps l (List a) -> List a | ||
handler (FlipCoin () [k]) = join_list (Next (k True) (Next (k False) Empty)); | ||
handler (Fumble () [k]) = Empty | ||
|
||
-- # Examples | ||
|
||
foo : Bool <Eff (Set Labels) GameOps {Toss}> | ||
foo = call FlipCoin () | ||
|
||
-- Two coin flips, all good | ||
example1 : (Bool, Bool) <Eff (Set Labels) GameOps {Toss}> | ||
example1 = let | ||
x <- call FlipCoin (); | ||
y <- call FlipCoin () | ||
in pure (x, y) | ||
|
||
-- -- Two coin flips, attempt, but fumble in the middle | ||
example2 : (Bool, Bool) <Eff (Set Labels) GameOps {Toss,Drop}> | ||
example2 = let | ||
x <- call FlipCoin (); | ||
a <- call Fumble (); | ||
y <- call FlipCoin () | ||
in let () = drop @Void a in pure (x, y) | ||
|
||
-- -- Easy runner | ||
go : forall {e : Set Labels, a : Type} . a <Eff (Set Labels) GameOps e> -> List a | ||
go = handle [/\{a,b,l}.fmap_gameops] [/\{l}.handler] (return_list) | ||
|
||
main : List (Bool, Bool) | ||
main = go example1 |
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,46 @@ | ||
-- Example of using algebraic effects and handlers in Granule to capture | ||
-- state effects | ||
|
||
-- Uses the builtin `call` and `handle` operations. | ||
-- See https://granule-project.github.io/docs/modules/Primitives.html#algebraiceffectsandhandlers | ||
|
||
import State | ||
|
||
-- # State operations | ||
|
||
data Labels = Get | Put | ||
|
||
-- Operations | ||
data StateOps (s : Type) : (Set Labels) -> Type -> Type where | ||
GetOp : forall {r : Type} . () -> (s -> r) [1] -> StateOps s {Get} r; | ||
PutOp : forall {r : Type} . s [0..Inf] -> (() -> r) [1] -> StateOps s {Put} r | ||
|
||
-- Functoriality of operations | ||
fmap_stateops : forall {s a b : Type, l : Set Labels} | ||
. (a -> b) [0..Inf] -> StateOps s l a -> StateOps s l b | ||
fmap_stateops [f] (GetOp () [k]) = GetOp () [f . k]; | ||
fmap_stateops [f] (PutOp [s] [k]) = PutOp [s] [f . k] | ||
|
||
-- Handler to state monad | ||
stateHandler : forall {s r : Type, l : Set Labels} | ||
. StateOps s l (State s r) -> State s r | ||
stateHandler (GetOp () [k]) = join_state (State (\([s] : s [0..Inf]) -> (k s, [s]))); | ||
stateHandler (PutOp s [k]) = join_state (State (\([_] : s [0..Inf]) -> (k (), s))) | ||
|
||
-- # Examples | ||
|
||
incr : Int <Eff (Set Labels) (StateOps Int) {Get, Put}> | ||
incr = let | ||
y <- call GetOp (); | ||
[z] <- pure (moveInt y); | ||
() <- call PutOp [z + 1] in | ||
pure z | ||
|
||
-- Handle state wrapped | ||
handleState : forall {a b : Type, e : Set Labels, s : Type} | ||
. a <Eff (Set Labels) (StateOps s) e> | ||
-> State s a | ||
handleState = handle [/\{a,b,l}.fmap_stateops] [/\{l}.stateHandler] (return_state) | ||
|
||
main : (Int, Int []) | ||
main = runState (handleState incr) [42] |
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 |
---|---|---|
@@ -1,15 +1,15 @@ | ||
import Vec | ||
import List | ||
|
||
listToVec : ∀ {a : Type} . List a → exists {n : Nat} . Vec n a | ||
listToVec Empty = pack < 0 , Nil > as exists {n : Nat} . Vec n a; | ||
listToVec : ∀ {a : Type} . List a → ∃ {n : Nat} . Vec n a | ||
listToVec Empty = pack < 0 , Nil > as ∃ {n : Nat} . Vec n a; | ||
listToVec (Next x xs) = | ||
unpack < m , v > = listToVec xs | ||
in pack < m + 1 , (Cons x v) > as exists {n : Nat} . Vec n a | ||
in pack < m + 1 , (Cons x v) > as ∃ {n : Nat} . Vec n a | ||
|
||
vecToList : ∀ {a : Type, n : Nat} . Vec n a → List a | ||
vecToList Nil = Empty; | ||
vecToList (Cons x xs) = Next x (vecToList xs) | ||
|
||
iso : forall {a : Type} . List a -> List a | ||
iso : ∀ {a : Type} . List a -> List a | ||
iso x = unpack < n , list > = listToVec x in vecToList list |
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,14 @@ | ||
-- A function expecting a rank-1 polymorphic function which it reuses at | ||
-- two different types (`Char` and `()`) | ||
foo : (forall {b : Type} . b -> (Int, b)) [2] -> ((Int, Char), (Int, ())) | ||
foo [k] = ((k @ Char) 'a', (k @ ()) ()) | ||
|
||
-- Application of `foo` with a reusable type abstraction function | ||
main : ((Int, Char), (Int, ())) | ||
main = foo [(/\(t : Type) -> \(x : t) -> (42, x))] | ||
|
||
-- Mixing existentials and rankN forall | ||
unpacker : forall {f : Type → Type, a : Type} . | ||
(exists {a : Type} . f a) → (forall {t : Type} . f t → a) → a | ||
unpacker e f = | ||
unpack < b , e' > = e in (f @ b) e' |
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
Oops, something went wrong.