monads

Monadic substitution under binders

喜夏-厌秋 提交于 2019-12-06 01:36:16
In the following Agda code, I have a term language based on de Bruijn indices. I can define substitution over terms in the usual de Bruijn indices way, using renaming to allow the substitution to proceed under a binder. module Temp where data Type : Set where unit : Type _⇾_ : Type → Type → Type -- A context is a snoc-list of types. data Cxt : Set where ε : Cxt _∷_ : Cxt → Type → Cxt -- Context membership. data _∈_ (τ : Type) : Cxt → Set where here : ∀ {Γ} → τ ∈ Γ ∷ τ there : ∀ {Γ τ′} → τ ∈ Γ → τ ∈ Γ ∷ τ′ infix 3 _∈_ data Term (Γ : Cxt) : Type → Set where var : ∀ {τ} → τ ∈ Γ → Term Γ τ 〈〉 :

Can IO actions be sequenced while keeping the logic in a pure function?

守給你的承諾、 提交于 2019-12-06 00:26:45
问题 I have the following code which grabs two pages of data from a paginated API endpoint. I'd like to modify query function to keep getting pages until it finds no more data (so replace take 2 in the code below with something which looks at the API response). My question is wether it is possible to achieve this without changing query function to an IO function. And if so, how would I go about it. If not, is there a way of doing this without writing recursive function? Here is the code: #!/usr

Is there something like Java Stream's “peek” operation in Scala?

折月煮酒 提交于 2019-12-06 00:02:05
问题 In Java you can call peek(x -> println(x)) on a Stream and it will perform the action for each element and return the original stream, unlike foreach which is Unit. Is there something similar in Scala, ideally something which works on all Monady types, allowing you to "pass through" the original Monad while performing a side effect action? (Logging, e.g.) It is of course easily implemented: def tap[A, U](a: A)(action: (A) => U): A = { action(a) a } but I'm hoping for something a bit more

Termination-checking substitution via (monadic) join and fmap

杀马特。学长 韩版系。学妹 提交于 2019-12-05 21:30:59
I'm using sized types, and have a substitution function for typed terms which termination-checks if I give a definition directly, but not if I factor it via (monadic) join and fmap. {-# OPTIONS --sized-types #-} module Subst where open import Size To show the problem, it's enough to have unit and sums. I have data types of Trie and Term , and I use tries with a codomain of Term inside Term , as part of the elimination form for sums. data Type : Set where 𝟏 : Type _+_ : Type → Type → Type postulate Cxt : Set → Set -- Every value in the trie is typed in a context obtained by extending Γ. data

How do I implement Reader using free monads?

☆樱花仙子☆ 提交于 2019-12-05 21:05:41
问题 Ok, so I have figured out how to implement Reader (and ReaderT , not shown) using the operational package: {-# LANGUAGE GADTs, ScopedTypeVariables #-} import Control.Monad.Operational data ReaderI r a where Ask :: ReaderI r r type Reader r a = Program (ReaderI r) a ask :: Reader r r ask = singleton Ask runReader :: forall r a. Reader r a -> r -> a runReader = interpretWithMonad evalI where evalI :: forall b. ReaderI r b -> (r -> b) evalI Ask = id But I can't figure out for my life how to do

About the function monad

China☆狼群 提交于 2019-12-05 19:50:28
问题 I have some confusion with the function monad. The function monad is defined as follow: instance Monad ((->) r) where return x = \_ -> x h >>= f = \w -> f (h w) w I tried to play around with it by writing a binding operation: ( (*2) >>= (+10) ) 3 (return 3) :: ((->) Int) But it caused errors. And I also try to rewrite a function AddStuff into the binding operations. addStuff = do a <- (*2) b <- (+10) return (a+b) then convert this function into addStuff' w = (*2) w >>= (\a -> (+10) w >>= (\b

Scala and cats: Implicit conversion to identity monad

房东的猫 提交于 2019-12-05 19:04:50
I have a function that computes sum of squares for numeric types as shown below. import cats.syntax.functor._ import cats.syntax.applicative._ import cats.{Id, Monad} import scala.language.higherKinds object PowerOfMonads { /** * Ultimate sum of squares method * * @param x First value in context * @param y Second value in context * @tparam F Monadic context * @tparam T Type parameter in the Monad * @return Sum of squares of first and second values in the Monadic context */ def sumOfSquares[F[_]: Monad, A, T >: A](x: F[A], y: F[A])(implicit num: Numeric[T]) : F[T] = { def square(value:T): T =

What purpose does the complexity of `Except` serve in Haskell?

痞子三分冷 提交于 2019-12-05 18:00:02
I understand (I think) that there is a close relationship between Either and Except in Haskell, and that it is easy to convert from one to the other. But I'm a bit confused about best practices for handling errors in Haskell and under what circumstances and scenarios I would choose one over the other. For example, in the example provided in Control.Monad.Except , Either is used in the definition type LengthMonad = Either LengthError so that calculateLength "abc" is Right 3 If instead one were to define type LengthMonad = Except LengthError then calculateLength "abc" would be ExceptT (Identity

What does Haskell call the Hom Functor/Monad?

心不动则不痛 提交于 2019-12-05 16:34:45
I'd like to use it in my code and would rather not duplicate it, but since it involves only massively generic words like "function" or "composition" I can't find it by searching. To be completely specific, I'm looking for instance Functor (x->) where fmap f p = f . p This is the basic reader (or environment) monad, usually referred to as ((->) e) . (This is (e ->) written as a partially applied function instead of as a section; the latter syntax is problematic to parse.) You can get it by importing Control.Monad.Reader or Control.Monad.Instances . 来源: https://stackoverflow.com/questions

Help me understand this Scala code: scalaz IO Monad

家住魔仙堡 提交于 2019-12-05 16:23:48
Here's the code I'm trying to understand (it's from http://apocalisp.wordpress.com/2010/10/17/scalaz-tutorial-enumeration-based-io-with-iteratees/ ): object io { sealed trait IO[A] { def unsafePerformIO: A } object IO { def apply[A](a: => A): IO[A] = new IO[A] { def unsafePerformIO = a } } implicit val IOMonad = new Monad[IO] { def pure[A](a: => A): IO[A] = IO(a) def bind[A,B](a: IO[A], f: A => IO[B]): IO[B] = IO { implicitly[Monad[Function0]].bind(() => a.unsafePerformIO, (x:A) => () => f(x).unsafePerformIO)() } } } This code is used like this (I'm assuming an import io._ is implied) def