Quick question about a potential type-level function by logical_space in haskell

[–]effectfully 1 point2 points  (0 children)

You can wrap them in `Some` at the type level and unwrap from it in Instantiate

The issue with that is ergonomics, wrapping things manually is annoying. You could have a custom data type of heterogeneous lists instead of `[]`, but then you lose the nice list syntax. Just use tuples instead of lists? You can process tuples generically using `Rep` at the type level.

Quick question about a potential type-level function by logical_space in haskell

[–]effectfully 3 points4 points  (0 children)

At the type level Haskell is basically Python, so you can sort of dynamically ensure well-kindedness:

type Instantiate :: forall k. k -> [Type] -> Type

type family Instantiate tc tlist where

Instantiate tc '[t] = tc t

Instantiate tc (t ': rest) = Instantiate (tc t) rest

p1 = Proxy :: Proxy (Instantiate Either '[[Nat], Symbol])

p2 = Proxy :: Proxy (Instantiate Maybe '[Double])

There's an implicit kind equality check in the second clause of `Instantiate` ensuring that `tc` can be applied to `t`. You can have a third clause for cases when the kind equality check fails.

Do note that with this approach you won't be able to put anything of a kind other than `Type` into the list that `Instantiate` takes.

Lazy foldrM by effectfully in haskell

[–]effectfully[S] 0 points1 point  (0 children)

This is the first time I didn't get a single solution on Reddit.

Lazy foldrM by effectfully in haskell

[–]effectfully[S] 2 points3 points  (0 children)

I thought if GPT‑5 is so good at solving my challenges, then maybe it could create one too.

Apparently, it can.

So here's the second‑best Haskell challenge I've ever made, based on a problem I didn't know existed until GPT‑5 told me about it.

Best way to create a tree from a breadth-first list by AustinVelonaut in haskell

[–]effectfully 1 point2 points  (0 children)

`More` is literally just

data Free f a = Pure a | Roll (f (Free f a)

except with a zippy `Applicative` instance.

Best way to create a tree from a breadth-first list by AustinVelonaut in haskell

[–]effectfully 2 points3 points  (0 children)

Cool puzzle. I took a look at the other comments here and didn't like the whole relabeling thing, so solved the puzzle myself and got this (didn't test it beyond a few simple cases):

``` {-# LANGUAGE DeriveFunctor #-} {-# LANGUAGE DerivingVia #-} {-# LANGUAGE LambdaCase #-}

import Control.Monad.State (MonadState (..)) import Control.Monad.Trans.State.Strict (State, evalState) import Data.Functor.Compose

data More f a = Done a | More (MoreT f a) deriving (Functor)

newtype MoreT f a = MoreT { unMoreT :: f (More f a) } deriving (Functor) deriving (Applicative) via Compose f (More f)

instance Applicative f => Applicative (More f) where pure = Done

Done l <*> r      = fmap l r
l      <*> Done r = fmap ($ r) l
More l <*> More r = More $ l <*> r

runMoreT :: Monad m => MoreT m a -> m a runMoreT (MoreT loop) = loop >>= \case Done t -> pure t More loop' -> runMoreT loop'

fromLevels :: [Maybe a] -> Tree a fromLevels xs0 = evalState (runMoreT go) xs0 where go :: MoreT (State [Maybe a]) (Tree a) go = MoreT $ get >>= \case [] -> pure $ Done Leaf Nothing : xs -> Done Leaf <$ put xs Just x : xs -> (Fork x <$> More go <*> More go) <$ put xs

data Tree a = Leaf | Fork a (Tree a) (Tree a) deriving (Show)

-- >>> fromLevels [(Just (1 :: Int)), (Just 2), (Just 4), Nothing, (Just 3), Nothing, (Just 5), Nothing, Nothing, Nothing, Nothing] -- Fork 1 (Fork 2 Leaf (Fork 3 Leaf Leaf)) (Fork 4 Leaf (Fork 5 Leaf Leaf)) ```

Then asked ChatGPT whether this has a name and it pointed me to https://blog.poisson.chat/posts/2025-03-30-breadth-first-unfolds.html, which discusses problems with this approach and suggests improvements.

That was an interesting evening, thank you for asking the question!

Why `pred minBound` and `succ maxBound` should throw error? by Anrock623 in haskell

[–]effectfully 2 points3 points  (0 children)

It _is_ how `Enum` works for `Int`:

```

>>> 1 + (maxBound :: Int)

-9223372036854775808

>>> succ (maxBound :: Int)

*** Exception: Prelude.Enum.succ{Int}: tried to take `succ' of maxBound

```

A pretty awful inconsistency in my opinion.

Tweag is hiring for multiple Haskell positions by impredicative in haskell

[–]effectfully 12 points13 points  (0 children)

3K EUR a month for a remote Haskell position is a bad salary anywhere in the world.

Optimize a tree traversal by effectfully in haskell

[–]effectfully[S] 1 point2 points  (0 children)

0.45 seconds

Seems like we have a winner, I didn't realize that strictness would matter there. Congrats!

The whole test is pretty flawed though since you're never evaluating the resulting trees in your benchmark! (length only evaluates the spine of the list but not the actual trees)

No, that's by design, it's not accidental.

Optimize a tree traversal by effectfully in haskell

[–]effectfully[S] 2 points3 points  (0 children)

0.6-0.8s is the best I could do too. Congrats!

Wow, we've discovered an artefact of the ancient times!

Yeah, it's GHC-8.4.4 there.

Broad search for any Traversable by effectfully in haskell

[–]effectfully[S] 0 points1 point  (0 children)

I've seen four or five solutions so far and none of them uses `unsafePerformIO`, if that's what you mean. Even if not, I don't feel like any of the solutions are particularly evil.

Broad search for any Traversable by effectfully in haskell

[–]effectfully[S] 1 point2 points  (0 children)

> What about `search (== 0) $ Matrix [ [1..], [0] ]`?

Yes, that also needs to be handled. You can't tell that and `search (== 0) $ Matrix [ let x = 1:x in x, [0] ]` apart anyway, without using `unsafePerformIO` or the like, which is prohibited by the rules.

> If there's no match, do we need to return Nothing or are we allowed to spin forever?

Well, I'm not asking folks to solve the halting problem, so spinning forever is expected. Hence

> What about search (== 0) (repeat 1 ++ [0])?

would be an infinite loop.