-
Notifications
You must be signed in to change notification settings - Fork 0
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
Use instances for type classes #3
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
{-# OPTIONS --without-K --exact-split --safe --no-unicode #-} | ||
|
||
--https://github.com/pigworker/CS410-16/blob/master/lectures/CS410-Functor.agda | ||
|
||
module FP.Abstractions.Applicative where | ||
|
||
open import TypeTheory.Universes using (Type; 𝑢; usuc) | ||
open import TypeTheory.SimpleTypes using (OneL; <>; unit; id; _compose_) | ||
open import TypeTheory.SimpleTypes using (id) | ||
open import HoTT.Identity-Types using (refl; _≡_) | ||
open import FP.Types using (Id) | ||
open import FP.Abstractions.Functor using (Functor; FunctorId) | ||
|
||
record Applicative (F : Type 𝑢 -> Type 𝑢) : Type (usuc 𝑢) where | ||
record Applicative (F : Type 𝑢 -> Type 𝑢) {{ _ : Functor F }} : Type (usuc 𝑢) where | ||
field | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So you're enforcing that |
||
-- operations | ||
pure : forall {A : Type 𝑢} -> A -> F A | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{-# OPTIONS --without-K --exact-split --safe --no-unicode #-} | ||
|
||
module FP.Either.Instances where | ||
|
||
open import TypeTheory.Universes using (Type; 𝑢) | ||
open import TypeTheory.Sum using (_+_) | ||
open import TypeTheory.SimpleTypes using (Nat; succ; zero; id; _compose_) | ||
open import FP.List using (List; map-List; map-id; map-compose; list; _<*>_) | ||
open import FP.Abstractions.Functor using (Functor) | ||
open import FP.Abstractions.Applicative using (Applicative) | ||
open import FP.Abstractions.Monad using (Monad) | ||
|
||
module _ (E : Type 𝑢) where | ||
instance | ||
FunctorEither : Functor {𝑢} (\ A -> E + A) | ||
FunctorEither = record | ||
{ fmap = {! !} | ||
; fmap-id = {! !} | ||
; fmap-compose = {! !} | ||
} | ||
|
||
ApplicativeEither : Applicative {𝑢} (\ A -> E + A) | ||
ApplicativeEither = record | ||
{ pure = {! !} | ||
; _<*>_ = {! !} | ||
; identity = {! !} | ||
; composition = {! !} | ||
; homomorphism = {! !} | ||
; interchange = {! !} | ||
} | ||
|
||
MonadEither : Monad {𝑢} (\ A -> E + A) | ||
MonadEither = record | ||
{ return = {! !} | ||
; _>>=_ = {! !} | ||
; return-flatmap = {! !} | ||
; flatmap-return = {! !} | ||
; flatmap-compose = {! !} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here will be some proofs (instances). Cool. So we have a bit of work left to do. 👍🏼 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{-# OPTIONS --without-K --exact-split --safe --no-unicode #-} | ||
|
||
module FP.Function.Instances where | ||
|
||
open import TypeTheory.Universes using (Type; 𝑢) | ||
open import TypeTheory.SimpleTypes using (OneL; <>; unit; id; _compose_; Nat) | ||
open import TypeTheory.FunctionsProperties using (compose-id; compose-compose) | ||
open import TypeTheory.FunctionsProperties using (function-dimap-id; function-dimap-compose; compose3) | ||
open import FP.Types using (Function) | ||
open import FP.Abstractions.Functor using (Functor) | ||
open import FP.Abstractions.Profunctor using (Profunctor) | ||
|
||
instance | ||
FunctorFunction : {X : Type 𝑢} -> Functor {𝑢} (Function X) | ||
FunctorFunction {X} = record | ||
{ fmap = _compose_ | ||
; fmap-id = compose-id | ||
; fmap-compose = compose-compose | ||
} | ||
|
||
ProfunctorFunction : Profunctor {𝑢} Function | ||
ProfunctorFunction = record | ||
{ dimap = \ f g h -> compose3 f h g | ||
; dimap-id = function-dimap-id | ||
; dimap-compose = function-dimap-compose | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{-# OPTIONS --without-K --exact-split --safe --no-unicode #-} | ||
|
||
module FP.Id.Instances where | ||
|
||
open import TypeTheory.Universes using (Type; 𝑢) | ||
open import TypeTheory.SimpleTypes using (id) | ||
open import HoTT.Identity-Types using (refl) | ||
open import FP.Abstractions.Functor using (Functor) | ||
open import FP.Types using (Id) | ||
|
||
instance | ||
FunctorId : Functor {𝑢} Id | ||
FunctorId = record | ||
{ fmap = id | ||
; fmap-id = refl | ||
; fmap-compose = \ f g fa -> refl (g (f fa)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{-# OPTIONS --without-K --exact-split --safe --no-unicode #-} | ||
|
||
module FP.List.Instances where | ||
|
||
open import TypeTheory.Universes using (Type; 𝑢) | ||
open import TypeTheory.SimpleTypes using (Nat; succ; zero; id; _compose_) | ||
open import FP.List using ( | ||
List; | ||
map-List; | ||
flatMap-List; | ||
map-id; map-compose; list; _<*>_) | ||
open import FP.Abstractions.Functor using (Functor) | ||
open import FP.Abstractions.Applicative using (Applicative) | ||
open import FP.Abstractions.Monad using (Monad) | ||
|
||
instance | ||
FunctorList : Functor {𝑢} List | ||
FunctorList = record | ||
{ fmap = map-List | ||
; fmap-id = map-id | ||
; fmap-compose = map-compose | ||
} | ||
|
||
ApplicativeList : Applicative {𝑢} List | ||
ApplicativeList = record | ||
{ pure = list | ||
; _<*>_ = _<*>_ | ||
; identity = {! !} | ||
; composition = {! !} | ||
; homomorphism = {! !} | ||
; interchange = {! !} | ||
} | ||
|
||
MonadList : Monad {𝑢} List | ||
MonadList = record | ||
{ return = list | ||
; _>>=_ = \ xs f -> flatMap-List f xs | ||
; return-flatmap = {! !} | ||
; flatmap-return = {! !} | ||
; flatmap-compose = {! !} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{-# OPTIONS --without-K --exact-split --safe --no-unicode #-} | ||
|
||
module FP.Maybe.Instances where | ||
|
||
open import TypeTheory.Universes using (Type; 𝑢) | ||
open import TypeTheory.SimpleTypes using (Nat; succ; zero; id; _compose_) | ||
open import FP.List using (List; map-List; map-id; map-compose; list; _<*>_) | ||
open import FP.Maybe using ( | ||
Maybe; | ||
map-Maybe; maybe-map-id; maybe-map-compose; | ||
Just; | ||
flatMap-Maybe; | ||
maybe-flatMap-just-f; maybe-flatMap-f-just; maybe-flatMap-compose) | ||
open import FP.Abstractions.Functor using (Functor) | ||
open import FP.Abstractions.Applicative using (Applicative) | ||
open import FP.Abstractions.Monad using (Monad) | ||
|
||
instance | ||
FunctorMaybe : Functor {𝑢} Maybe | ||
FunctorMaybe = record | ||
{ fmap = map-Maybe | ||
; fmap-id = maybe-map-id | ||
; fmap-compose = maybe-map-compose | ||
} | ||
|
||
ApplicativeMaybe : Applicative {𝑢} Maybe | ||
ApplicativeMaybe = record | ||
{ pure = {! !} | ||
; _<*>_ = {! !} | ||
; identity = {! !} | ||
; composition = {! !} | ||
; homomorphism = {! !} | ||
; interchange = {! !} | ||
} | ||
|
||
MonadMaybe : Monad {𝑢} Maybe | ||
MonadMaybe = record | ||
{ return = Just | ||
; _>>=_ = flatMap-Maybe | ||
; return-flatmap = maybe-flatMap-just-f | ||
; flatmap-return = maybe-flatMap-f-just | ||
; flatmap-compose = maybe-flatMap-compose | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{-# OPTIONS --without-K --exact-split --safe --no-unicode #-} | ||
|
||
module FP.Vec.Instances where | ||
|
||
open import TypeTheory.Universes using (Type; 𝑢) | ||
open import TypeTheory.SimpleTypes using (Nat) | ||
open import FP.Vec using (Vec; vMap; vMap-id; vMap-compose; vec) | ||
open import FP.Abstractions.Functor using (Functor) | ||
open import FP.Abstractions.Applicative using (Applicative) | ||
|
||
module _ (n : Nat) where | ||
instance | ||
FunctorVec : Functor {𝑢} (\ X -> Vec X n ) | ||
FunctorVec = record | ||
{ fmap = vMap | ||
; fmap-id = vMap-id n | ||
; fmap-compose = vMap-compose n | ||
} | ||
|
||
instance | ||
FunctorVec1 : Functor {𝑢} (\ X -> Vec X 1 ) | ||
FunctorVec1 = FunctorVec 1 | ||
|
||
ApplicativeVec1 : Applicative {𝑢} (\ X -> Vec X 1 ) | ||
ApplicativeVec1 = record | ||
{ pure = vec | ||
; _<*>_ = {! !} | ||
; identity = {! !} | ||
; composition = {! !} | ||
; homomorphism = {! !} | ||
; interchange = {! !} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting that you use
--no-unicode
. Just curious, why do you not like unicode?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise, those pragmas are exactly the ones I use in my agda-algebras library. 👍🏼
By the way, I think they're maybe phasing out
--without-K
in favor of--cubical-compatible
. (I much prefer the old name as it seems to give a more precise description of what going on.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drop pragma but awoid.