Skip to content
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

Reify state #338

Open
Kyuuhachi opened this issue Apr 28, 2020 · 2 comments
Open

Reify state #338

Kyuuhachi opened this issue Apr 28, 2020 · 2 comments
Labels
question Further information is requested

Comments

@Kyuuhachi
Copy link

I have a program in which I want to run an effectful function several times with different arguments, evaluate which is best, and continue as if only that one had happened. As far as I know, this can't be done with NonDet (as there's no way to communicate between branches to choose one) or any other existing effect. Is something like that possible? I have no idea how state threading works, but I would imagine an API akin to

choose :: Ord c => [Sem r (c, a)] -> Sem r a
choose alts = do
  st <- getState
  values <- forM alts $ \alt -> do
    setState st
    (c, v) <- alt
    st' <- getState
    pure (st', c, v)
  let (st', _, v) = maximumBy (comparing (\(_, c, _) -> c)) values
  setState st'
  pure v
@Kyuuhachi
Copy link
Author

Kyuuhachi commented Apr 30, 2020

I've managed to cobble something together, but I have no idea whether I'm doing it right.

data Opti m a where
  Opti :: Ord c => [m (c, a)] -> Opti m a
makeSem ''Opti

runOpti :: Sem (Opti : r) a -> Sem r a
runOpti
  = interpretH \case
    Opti ms -> do
      ms' <- mapM runT ms
      ins <- getInspectorT
      vals <- forM ms' \m' -> do
                x <- raise (runOpti m')
                case inspect ins x of
                  Just (c, v) ->
                    pure (c, v <$ x)
                  Nothing ->
                    error "runOpti: fail"
      pure $ snd $ maximumBy (comparing fst) vals

(Renamed it from choose because that's apparently already used by NonDet.)

@TheMatten TheMatten added the question Further information is requested label Jun 21, 2020
@Heimdell
Copy link

Just remember that certain effects (Lift IO) cannot be undone anyway, and you have to keep it like Sem (xs ++ [Opt, Final M]) a.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants