haskell

Control.MonadPlus.Free without unnecessary distribution

梦想与她 提交于 2020-01-03 11:31:49
问题 I'm trying to use a free monad to build an EDSL for constructing AND/OR decision trees like Prolog, with >>= mapped to AND, and mplus mapped to OR. I want to be able to describe something like A AND (B OR C) AND (D OR E) , but I don't want distributivity to turn this into (A AND B AND D) OR (A AND B AND E) OR (A AND C AND D) OR (A AND C AND E) . Ultimately, I want to transform the AND/OR nodes into reified constraints in a constraint solver, without causing the combinatorial explosion in the

Haskell lexical layout rule implementation

天大地大妈咪最大 提交于 2020-01-03 11:13:09
问题 I have been working on a pet language, which has Haskell-like syntax. One of the neat things which Haskell does, which I have been trying to replicate, is its insertion of {, } and ; tokens based on code layout, before the parsing step. I found http://www.haskell.org/onlinereport/syntax-iso.html, which includes a specification of how to implement the layout program, and have made a version of it (modified, of course, for my (much simpler) language). Unfortunately, I am getting an incorrect

Haskell lexical layout rule implementation

戏子无情 提交于 2020-01-03 11:12:12
问题 I have been working on a pet language, which has Haskell-like syntax. One of the neat things which Haskell does, which I have been trying to replicate, is its insertion of {, } and ; tokens based on code layout, before the parsing step. I found http://www.haskell.org/onlinereport/syntax-iso.html, which includes a specification of how to implement the layout program, and have made a version of it (modified, of course, for my (much simpler) language). Unfortunately, I am getting an incorrect

haskell : making a superclass of Num

杀马特。学长 韩版系。学妹 提交于 2020-01-03 11:00:35
问题 I want to make a superclass of Num, called Linear class Linear a where add :: a -> a -> a instance (Num a) => Linear a where add = (+) I get the error : Illegal instance declaration for `Linear a' (All instance types must be of the form (T a1 ... an) where a1 ... an are *distinct type variables*, and each type variable appears at most once in the instance head. Use -XFlexibleInstances if you want to disable this.) In the instance declaration for `Linear a' From what I understand, something

Number type boundaries in Common LISP and Stack flowing over in GHCI

北慕城南 提交于 2020-01-03 10:55:28
问题 First question ever here, and newbie in both Common LISP and Haskell, please be kind. I have a function in Common LISP - code below - which is intended to tell whether the area of a triangle is an integral number (integer?). (defun area-int-p (a b c) (let* ((s (/ (+ a b c) 2)) (area (sqrt (* s (- s a) (- s b) (- s c))))) (if (equal (ceiling area) (floor area)) t nil))) This is supposed to use Heron's formula to calculate the area of the triangle, given the size of the three sides, and decide

How to get two values from one, point free, in Haskell?

杀马特。学长 韩版系。学妹 提交于 2020-01-03 10:44:55
问题 For example, given a value, v , and a function f , is there a way to get (f v,v) point free? 回答1: Alternatively, note that for functions h and f with appropriate types, h >>= f = \w -> f (h w) w so you can write f >>= (,) 回答2: import Control.Arrow (g &&& f) v = (g v, f v) -- ergo, (id &&& f) v = (v, f v) (f &&& id) v = (f v, v) 回答3: How about using the Applicative instance for (->) liftA2 (,) id :: (a -> b) -> a -> (a, b) For example liftA2 (,) id succ 5 >>> (5,6) 来源: https://stackoverflow

Haskell, range downto without step [duplicate]

天大地大妈咪最大 提交于 2020-01-03 10:44:15
问题 This question already has answers here : Decrementing ranges in Haskell (3 answers) Closed 4 years ago . Why in Haskell is not working range downto without step [7..1] => [] but working only this [7,6..1] => [7,6,5,4,3,2,1] 回答1: 3.10. Arithmetic sequences [...] Arithmetic sequences satisfy these identities: [...] [ e1..e3 ] = enumFromTo e1 e3 [...] 6.3.4 The Enum Class For the types Int and Integer, the enumeration functions have the following meaning: [...] The sequence enumFromTo e1 e3 is

Haskell, range downto without step [duplicate]

淺唱寂寞╮ 提交于 2020-01-03 10:44:10
问题 This question already has answers here : Decrementing ranges in Haskell (3 answers) Closed 4 years ago . Why in Haskell is not working range downto without step [7..1] => [] but working only this [7,6..1] => [7,6,5,4,3,2,1] 回答1: 3.10. Arithmetic sequences [...] Arithmetic sequences satisfy these identities: [...] [ e1..e3 ] = enumFromTo e1 e3 [...] 6.3.4 The Enum Class For the types Int and Integer, the enumeration functions have the following meaning: [...] The sequence enumFromTo e1 e3 is

Generalized Newtype Deriving

不问归期 提交于 2020-01-03 10:06:27
问题 Haskell can derive the instance for MonadState s in T1 below but not in T2 which is however a very similar type. In which way should I modify the code for T2 so that the instance for MonadState s can be automatically derived? {-# LANGUAGE GeneralizedNewtypeDeriving #-} import Control.Monad.Reader import Control.Monad.State newtype T1 r s a = T1 { runT1 :: ReaderT r (State s) a } deriving (Monad, MonadReader r, MonadState s) newtype T2 r s a = T2 { runT2 :: StateT r (State s) a } deriving

Generalized Newtype Deriving

社会主义新天地 提交于 2020-01-03 10:06:25
问题 Haskell can derive the instance for MonadState s in T1 below but not in T2 which is however a very similar type. In which way should I modify the code for T2 so that the instance for MonadState s can be automatically derived? {-# LANGUAGE GeneralizedNewtypeDeriving #-} import Control.Monad.Reader import Control.Monad.State newtype T1 r s a = T1 { runT1 :: ReaderT r (State s) a } deriving (Monad, MonadReader r, MonadState s) newtype T2 r s a = T2 { runT2 :: StateT r (State s) a } deriving