haskell

This magazine is from a federated server and may be incomplete. Browse more on the original instance.

jaror,
@jaror@kbin.social avatar

Anyone want to guess how many errors this code generates:

{-# LANGUAGE DerivingVia #-}

module T where

newtype OrdList1 a = OrdList1 [a]
  deriving (Functor, Foldable, Traversable) via []

newtype OrdList2 a = OrdList2 [a]
  deriving (Functor, Foldable, Traversable) via Maybe

#haskell

jaror,
@jaror@kbin.social avatar
jaror,
@jaror@kbin.social avatar
jaror,
@jaror@kbin.social avatar

The GitHub repo of the #haskell basement and foundation packages was just archived, while it currently has 3764 indirect reverse dependencies including cryptonite, pandoc, and accelerate. I don't know why and I don't know what will happen now, but I am concerned.

kosmikus,
@kosmikus@functional.cafe avatar

@mangoiv @jaror I don't really see the problem? To my knowledge, Vincent has not really done any active Haskell work in quite a while, nor should anyone be forced to. Isn't it more honest to archive the repositories to reflect that state properly than to continue providing some illusion that these are active libraries? People can still use the code and fork / revive. Isn't this how open-source projects are supposed to work?

jaror,
@jaror@kbin.social avatar

@kosmikus @mangoiv

The thing I'm concerned about is that there are still a lot of packages depending on it and there doesn't seem to be a fork yet. So, important fixes like making all these packages build on 32-bit platforms are now in limbo.

Also, I'm concerned that this goes unnoticed. Hackage does not currently show maintenance status very well, so people might think these packages are still suitable to use in new projects.

jaror, (edited )
@jaror@kbin.social avatar

Brzozowski derivatives are neat, but good old denotational semantics of regular expressions can be very elegant too:

data RE = Empty | Eps | Ch Char | App RE RE | Alt RE RE | Star RE

foldRE :: p -> p -> (Char -> p) -> (p -> p -> p) -> (p -> p -> p) -> (p -> p) -> RE -> p
foldRE emp eps ch app alt star = go where
  go = case
    Empty -> emp
    Eps -> eps
    Ch c -> ch c
    App p q -> app (go p) (go q)
    Alt p q -> alt (go p) (go q)
    Star p -> star (go p)

recognise :: RE -> String -> [String]
recognise =
  foldRE (pure empty) pure (c -> case x : xs | c == x -> [xs]; _ -> [])
    (>=>) (liftA2 (<|>)) (p -> fix (t -> liftA2 (<|>) pure (p >=> t)))

jaror,
@jaror@kbin.social avatar

@mangoiv perhaps it is slightly easier to read like this?

data RE = Empty | Eps | Ch Char | App RE RE | Alt RE RE | Star RE

data REalg a = REalg
  { emp :: a
  , eps :: a
  , ch :: Char -> a
  , app :: a -> a -> a
  , alt :: a -> a -> a
  , star :: a -> a
  }

foldRE :: REalg a -> RE -> a
foldRE alg = go where
  go = case
    Empty -> emp alg
    Eps -> eps alg
    Ch c -> ch alg c
    App p q -> app alg (go p) (go q)
    Alt p q -> alt alg (go p) (go q)
    Star p -> star alg (go p)

recognise :: RE -> StateT String [] ()
recognise = foldRE REalg
  { emp = empty
  , eps = pure ()
  , ch = c -> StateT (case x : xs | c == x -> [((), xs)]; _ -> [])
  , app = (*>)
  , alt = (<|>)
  , star = p -> fix (t -> p *> t <|> pure ())
  }

mangoiv,
@mangoiv@functional.cafe avatar

@jaror oh yeah much better. Lovely, thanks ;)

jaror, (edited )
@jaror@kbin.social avatar

Which error message do you find easier to read?

GHC-style:

Hangman.hs:46:32: error:
    * Couldn't match type '[Char]' with 'Char'
      Expected type: Char
        Actual type: String
    * In the first argument of 'makeGuess', namely 'letterInput'
      In the first argument of 'gameLoop', namely
        '(makeGuess letterInput gs)'
      In the expression: gameLoop (makeGuess letterInput gs)
   |
46 |       else gameLoop (makeGuess letterInput gs)
   |                                ^^^^^^^^^^^

Elm-style:

-- Type mismatch ---------------------------------------------------- Hangman.hs 

46 |       else gameLoop (makeGuess letterInput gs)
                                    ^^^^^^^^^^^
Couldn't match type '[Char]' with 'Char'

Actual type: 
 
    String

Expected type: 

    Char

jaror,
@jaror@kbin.social avatar
jaror,
@jaror@kbin.social avatar

I finally found a decent definition of free alternatives:

newtype Free f a = Free { alts :: [Free' f a] } deriving Functor
instance Applicative (Free f) where
  pure = Free . pure . Pure
  Free ps <*> q0 = asum $ map (`apL` q0) ps where
    apL (Pure f) q = f <$> q
    apL (Free' x p) q = Free $ pure $ Free' x $ flip <$> p <*> q
instance Alternative (Free f) where
  empty = Free empty
  Free ps <|> Free qs = Free (ps <|> qs)
data Free' f a = Pure a | forall x. Free' (f x) (Free f (x -> a))
deriving instance Functor (Free' f)

#haskell

jaror,
@jaror@kbin.social avatar

Exercise: why did I not write an instance Applicative (Free' f) and used deriving (Functor, Applicative, Alternative) via Compose [] (Free' f) to get the instances for Free?

jaror,
@jaror@kbin.social avatar
jaror,
@jaror@kbin.social avatar

The fastest effect system approaches translate operations into a fast concrete monad like IO or State.

https://gist.github.com/noughtmare/cccda38eb7c67c1ea6df6a3377f1da0d

jaror,
@jaror@kbin.social avatar

DatatypeContexts as implemented today in GHC are much more reasonable than I thought.

https://discourse.haskell.org/t/datatypecontexts-has-something-changed-since-it-was-marked-deprecated/7560/7?u=jaror

#haskell

jaror,
@jaror@kbin.social avatar

Sometimes ImpredicativeTypes can be a good replacement for DeepSubsumption: https://stackoverflow.com/a/76936438/15207568

jaror,
@jaror@kbin.social avatar

Lenses are not always better:

fresh :: FDState s c -> (Int, FDState s c)
fresh = id &&& id
  >>> first (^. nextId)
  >>> ((i, s) -> (i, s & alive %~ Set.insert i))
  >>> second (nextId %~ (+ 1))

fresh :: FDState s c -> (Int, FDState s c)
fresh s@FDState {_nextId = i, _alive = as} =
  (i, s {_nextId = i + 1, _alive = Set.insert i as})

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • uselessserver093
  • Food
  • aaaaaaacccccccce
  • [email protected]
  • test
  • CafeMeta
  • testmag
  • MUD
  • RhythmGameZone
  • RSS
  • dabs
  • Socialism
  • KbinCafe
  • TheResearchGuardian
  • Ask_kbincafe
  • oklahoma
  • feritale
  • SuperSentai
  • KamenRider
  • All magazines