Giter VIP home page Giter VIP logo

Comments (8)

gallais avatar gallais commented on August 18, 2024 2

Generic n-ary lift for applicatives following a discussion with @Zambonifofex

open import Category.Applicative using (RawApplicative) hiding (module RawApplicative)

module _ (Test :  {ℓ}  Set Set ℓ)
         (raw-applicative :  RawApplicative (Test {ℓ})) where

open import Data.Unit
open import Level using (_⊔_; Lift)
open import Data.Nat.Base
open import Data.Product
open import Data.Product.Nary.NonDependent
open import Function
open import Function.Nary.NonDependent

module App {ℓ} = Category.Applicative.RawApplicative (raw-applicative ℓ)

Productκ :  n {l} (as : Sets n (lreplicate n l))  Set l
Productκ zero    as       = Lift _ ⊤
Productκ (suc n) (a , as) = a × Productκ n as

toProductκ :  n {l} {as : Sets n (lreplicate n l)}  Product⊤ n as  Productκ n as
toProductκ zero    _        = _
toProductκ (suc n) (v , vs) = v , toProductκ n vs

fromProductκ :  n {l} {as : Sets n (lreplicate n l)}  Productκ n as  Product⊤ n as
fromProductκ zero    _        = _
fromProductκ (suc n) (v , vs) = v , fromProductκ n vs

curryκₙ :  n {l} {as : Sets n (lreplicate n l)} {r} {b : Set r} 
          (Productκ n as  b)  as ⇉ b
curryκₙ n f = curry⊤ₙ n (f ∘ toProductκ n)

uncurryκₙ :  n {l} {as : Sets n (lreplicate n l)} {r} {b : Set r} 
            as ⇉ b  (Productκ n as  b)
uncurryκₙ n f = uncurry⊤ₙ n f ∘ fromProductκ n


sequenceA :  n {l} {as : Sets n (lreplicate n l)} 
            Productκ n (Test <$> as)  Test (Productκ n as)
sequenceA zero    p        = App.pure p
sequenceA (suc n) (ta , p) = _,_ App.<$> ta App.⊛ sequenceA n p

lift :  n {l} {R : Set l} {As : Sets n (lreplicate n l)} 
       As ⇉ R  (Test <$> As) ⇉ Test R
lift n f = curryκₙ n (λ ps  uncurryκₙ n f App.<$> sequenceA n ps)

from agda-stdlib.

jespercockx avatar jespercockx commented on August 18, 2024 1

How about

open import Level using (Level)
open import Function
open import Function.Nary.NonDependent
open import Data.Nat
open import Data.Product using (_×_; _,_; proj₁; proj₂) renaming (curry to curry₂; uncurry to uncurry₂)
open import Data.List using (List; []; _∷_; map) renaming (zipWith to zipWith₂)

variable
  l : Level
  R : Set l

zipWith :  n {ls} {as : Sets n ls} 
          Arrows n as R  Arrows n (List <$> as) (List R)
zipWith zero f = []
zipWith (suc zero) f = map f
zipWith (suc (suc n)) f xs₀ xs₁ = zipWith (suc n) id (zipWith₂ f xs₀ xs₁)

or alternatively

zipWith :  n {ls} {as : Sets n ls} 
          Arrows n as R  Arrows n (List <$> as) (List R)
zipWith zero f = []
zipWith (suc zero) f = map f
zipWith (suc (suc n)) f xs₀ xs₁ = zipWith (suc n) (uncurry₂ f) (zipWith₂ _,_ xs₀ xs₁)

from agda-stdlib.

MatthewDaggitt avatar MatthewDaggitt commented on August 18, 2024

So off the top of my head I'm struggling to come up with a generalised zipWith-n function which is both sufficiently general and easy to reason about. Do you have such an implementation?

I don't think zip3with is common or useful enough to warrant being included in the standard library.

from agda-stdlib.

MatthewDaggitt avatar MatthewDaggitt commented on August 18, 2024

Closing as I'm afraid I can't come up with a suitable zipWith-n function.

from agda-stdlib.

gallais avatar gallais commented on August 18, 2024

I think this ought to be doable using the code in #608

Edit: yep. uncurried version (_<$>_ is map over Sets using a level polymorphic endofunctor on Set l):

zw-aux :  n {ls} {as : Sets n ls} 
         (Product n as  R) 
         (Product n (List <$> as)  List R)
zw-aux 0       f as         = []
zw-aux 1       f (as , _)   = map (f ∘ (_, tt)) as
zw-aux (suc n) f (as , ass) = zipWith _$_ fs as
  where fs = zw-aux n (flip (curry f)) ass

from agda-stdlib.

gallais avatar gallais commented on August 18, 2024

I like the second one better. I am puzzled because I tried my best and I really
could not see how to implement it.

from agda-stdlib.

MatthewDaggitt avatar MatthewDaggitt commented on August 18, 2024

Going to remove the v1.1 milestone for this as I don't think it's urgent.

from agda-stdlib.

MatthewDaggitt avatar MatthewDaggitt commented on August 18, 2024

Fixed by 58462cf. Congrats to @gallais for fixing by far the oldest issue in the library!

from agda-stdlib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.