Last active
November 22, 2021 20:42
-
-
Save CodaFi/d4efd98697130bd35f5c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// Yoneda.swift | |
// Basis | |
// | |
// Created by Robert Widmann on 12/11/14. | |
// Copyright (c) 2014 Robert Widmann. All rights reserved. | |
// | |
import Basis | |
public struct Yoneda<F : Functor> { | |
let runYoneda : (F.A -> F.B) -> F.FB | |
} | |
public func liftYoneda<F : Functor>(a : F) -> Yoneda<F> { | |
return Yoneda({ f in F.fmap(f)(a) }) | |
} | |
public func lowerYoneda<F : Functor>(y : Yoneda<F>) -> F { | |
return unsafeCoerce(y.runYoneda(unsafeCoerce)) as F | |
} | |
public struct Coyoneda<F : Contravariant> { | |
let contraFun : (F.A -> F.B) | |
let contra : F.FB | |
init(_ contraFun : (F.A -> F.B), _ contra : F.FB) { | |
self.contraFun = contraFun | |
self.contra = contra | |
} | |
} | |
public func liftCoyoneda<F : Contravariant>(a : F.FB) -> Coyoneda<F> { | |
return Coyoneda(unsafeCoerce, a) | |
} | |
public func lowerCoyoneda<F : Contravariant>(c : Coyoneda<F>) -> F { | |
return F.contramap(c.contraFun)(c.contra) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment