-
Notifications
You must be signed in to change notification settings - Fork 0
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 #1 from sourceCode4/containers
Containers
- Loading branch information
Showing
3 changed files
with
91 additions
and
44 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: project | ||
depend: agda2hs | ||
depend: agda2hs, standard-library | ||
include: src | ||
flags: --erase-record-parameters |
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,43 +1,43 @@ | ||
module Data.Free where | ||
|
||
open import Haskell.Prelude hiding (pure) | ||
import Haskell.Prelude using (pure) | ||
import Haskell.Prim.Functor as Functor | ||
open import Agda.Builtin.Sigma using (_,_) | ||
open import Haskell.Prelude hiding (pure; _,_) | ||
open import Data.Container | ||
open import Level as L | ||
|
||
{- Add cases to extend for more functors -} | ||
data PosFunctor : Set where | ||
pList : PosFunctor | ||
pMaybe : PosFunctor | ||
Container00 : Set₁ | ||
Container00 = Container L.zero L.zero | ||
|
||
toFunctor : PosFunctor → (Set → Set) | ||
toFunctor pList = List | ||
toFunctor pMaybe = Maybe | ||
|
||
data Free (F : PosFunctor) (A : Set) : Set where | ||
data Free (F : Container00) (A : Set) : Set where | ||
pure : A → Free F A | ||
free : toFunctor F (Free F A) → Free F A | ||
free : ⟦ F ⟧ (Free F A) → Free F A | ||
|
||
iFunctorPosFunctor : (F : PosFunctor) → Functor (toFunctor F) | ||
iFunctorPosFunctor pList = iFunctorList | ||
iFunctorPosFunctor pMaybe = iFunctorMaybe | ||
-- definition of map for Container00 with erased implicit arguments | ||
cmap : {@0 C : Container00} {@0 A B : Set} → (A → B) → (x : ⟦ C ⟧ A) → ⟦ C ⟧ B | ||
cmap f (posits , vals) = (posits , f ∘ vals) | ||
|
||
instance | ||
{-# TERMINATING #-} | ||
iFunctorFree : {F : PosFunctor} → ⦃ Functor (toFunctor F) ⦄ → Functor (Free F) | ||
iFunctorFree : {@0 F : Container00} → ⦃ Functor ⟦ F ⟧ ⦄ → Functor (Free F) | ||
iFunctorFree .fmap {a = A} {b = B} f = go | ||
where | ||
go : {F : PosFunctor} → {{ Functor (toFunctor F) }} → Free F A → Free F B | ||
go : {@0 F : Container00} → ⦃ Functor ⟦ F ⟧ ⦄ → Free F A → Free F B | ||
go (pure v) = pure $ f v | ||
go (free ffa) = free (go <$> ffa) | ||
|
||
{-# TERMINATING #-} | ||
iApplicativeFree : {F : PosFunctor} → ⦃ Functor (toFunctor F) ⦄ → Applicative (Free F) | ||
iApplicativeFree .Applicative.pure = pure | ||
iApplicativeFree : {@0 F : Container00} → ⦃ Functor ⟦ F ⟧ ⦄ → Applicative (Free F) | ||
iApplicativeFree .Applicative.pure = pure | ||
iApplicativeFree ._<*>_ (pure f) (pure b) = pure (f b) | ||
iApplicativeFree ._<*>_ (pure f) (free ffb) = free $ fmap f <$> ffb | ||
iApplicativeFree ._<*>_ (free ffa) mb = free $ (_<*> mb) <$> ffa | ||
|
||
{-# TERMINATING #-} | ||
iMonadFree : {F : PosFunctor} → ⦃ Functor (toFunctor F) ⦄ → Monad (Free F) | ||
iMonadFree : {@0 F : Container00} → ⦃ Functor ⟦ F ⟧ ⦄ → Monad (Free F) | ||
iMonadFree ._>>=_ (pure a) f = f a | ||
iMonadFree ._>>=_ (free fa) f = free (fmap (_>>= f) fa) | ||
iMonadFree ._>>=_ (free fa) f = free (fmap (_>>= f) fa) | ||
|
||
iFunctorContainer : {@0 C : Container00} → Functor ⟦ C ⟧ | ||
iFunctorContainer = record { fmap = cmap } |
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