Skip to content

Instantly share code, notes, and snippets.

@nomeata
Created May 16, 2021 17:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nomeata/b9d3e18a49f0c320c0d1f0e7b72a9c73 to your computer and use it in GitHub Desktop.
Save nomeata/b9d3e18a49f0c320c0d1f0e7b72a9c73 to your computer and use it in GitHub Desktop.
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
module Z where
import Prelude hiding (zip)
zip :: forall a b. [a] -> [b] -> [(a,b)]
zip [] _ = []
zip _ [] = []
zip (x:xs) (y:ys) = (x,y) : zip xs ys
zip2 :: forall a b. [a] -> [b] -> [(a,b)]
zip2 xs ys = (foo xs) (bar ys)
where
foo :: [a] -> Z a b -> [(a,b)]
foo [] = \_ -> []
foo (x:xs) = \r -> runZ r (foo xs) x
bar :: [b] -> Z a b
bar [] = Z $ \xys x -> []
bar (y:ys) = Z $ \xys x -> (x,y) : xys (bar ys)
newtype Z a b = Z { runZ :: (Z a b -> [(a, b)]) -> a -> [(a, b)] }
newtype a -&> b = Hyp { invoke :: (b -&> a) -> b }
-- a -&> b == (b -&> a) -> b
-- == ((a -&> b) -> a) -> b
--
-- Z a b == (Z a b -> [(a, b)]) -> (a -> [(a, b)])
--
-- Z a b = [(a, b)] -&> (a -> [(a,b)])
zip3 :: forall a b. [a] -> [b] -> [(a,b)]
zip3 xs ys = invoke (foo xs) (bar ys)
where
foo :: [a] -> (a -> [(a,b)]) -&> [(a,b)]
foo [] = Hyp $ \_ -> []
foo (x:xs) = Hyp $ \r -> invoke r (foo xs) x
bar :: [b] -> [(a, b)] -&> (a -> [(a,b)])
bar [] = Hyp $ \xys x -> []
bar (y:ys) = Hyp $ \xys x -> (x,y) : invoke xys (bar ys)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment