-
Notifications
You must be signed in to change notification settings - Fork 247
Generic n-ary programs (zipWith, alignWith, lift, etc.) #10
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
Comments
So off the top of my head I'm struggling to come up with a generalised I don't think |
Closing as I'm afraid I can't come up with a suitable |
I think this ought to be doable using the code in #608 Edit: yep. uncurried version ( 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 |
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₁) |
I like the second one better. I am puzzled because I tried my best and I really |
Going to remove the |
Generic n-ary 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) |
I wonder of wherher the library needs some generalized zipWith-n functions.
For example, the below function is actually zipWith3,
but one list is by List and others are by List.All:
The text was updated successfully, but these errors were encountered: