Questions tagged [equational-reasoning]

Equational reasoning consists in manipulating definitions in referentially transparent code as if they were mathematical equations.

21 questions
46
votes
1 answer

If return a = return b then does a=b?

Can you prove that if return a = return b then a=b? When I use =, I mean in the laws and proofs sense, not the Eq class sense. Every monad that I know seems to satisfy this, and I can't think of a valid monad that wouldn't (Const a is a functor and…
PyRulez
  • 9,505
  • 9
  • 37
  • 82
14
votes
3 answers

Is it possible to use church encodings without breaking equational reasoning?

Mind this program: {-# LANGUAGE RankNTypes #-} import Prelude hiding (sum) type List h = forall t . (h -> t -> t) -> t -> t sum_ :: (Num a) => List a -> a sum_ = \ list -> list (+) 0 toList :: [a] -> List a toList = \ list cons nil -> foldr cons…
MaiaVictor
  • 45,122
  • 42
  • 127
  • 254
13
votes
3 answers

Haskell: Equation Expander 1+(1+(1+(1+(…))))=∞

Does there exist a equation expander for Haskell? Something like foldr.com: 1+(1+(1+(1+(…))))=∞ I am new to Haskell I am having trouble understanding why certain equations are more preferable than others. I think it would help if I could see the…
user295190
6
votes
0 answers

Problems with equational proofs and interface resolution in Idris

I'm trying to model Agda style equational reasoning proofs for Setoids (types with an equivalence relation). My setup is as follows: infix 1 :=: interface Equality a where (:=:) : a -> a -> Type interface Equality a => VerifiedEquality a where …
Rodrigo Ribeiro
  • 2,980
  • 1
  • 16
  • 25
6
votes
2 answers

Haskell - How to transform map sum (map (x:) xss) to map (x+) (map sum xss)

Reading "Thinking Functionally with Haskell" I came across a part of a program calculation that required that map sum (map (x:) xss) be rewritten as map (x+) (map sum xss) Intuitively I know that it makes sense ... if you have some lists that you…
Barry Rogerson
  • 558
  • 2
  • 15
6
votes
3 answers

Am I using sound equational reasoning about a definition of filter in terms of foldr?

well, this is the definition of the filter function using foldr: myFilter p xs = foldr step [] xs where step x ys | p x = x : ys | otherwise = ys so for example let's say i have this function: myFilter odd…
Azriel
  • 407
  • 2
  • 8
3
votes
1 answer

Keeping track of "state" when writing equality proofs that are long chains of transitively linked steps

I was writing the following proof in Idris: n : Nat n = S (k + k) lemma: n * n = ((k * n) + k) + (1 + (((k * n) + k) + 0)) lemma = sym $ rewrite plusZeroRightNeutral ((k * n) + k) in rewrite plusAssociative ((k * n) + k) 1 ((k * n)…
Cactus
  • 25,576
  • 9
  • 60
  • 130
3
votes
1 answer

To prove equality of two function definitions inductively

How do I do the induction to establish the statement moll n = doll n, with moll 0 = 1 --(m.1) moll n = moll ( n-1) + n --(m.2) doll n = sol 0 n --(d.1) where sol acc 0 = acc…
letter
  • 89
  • 7
3
votes
1 answer

reduce(x+y, xs) and sum(xs) are not equivalent in python?

I would expect the two to mean the same, from a functional equational standpoint, however: x = [1, 2, 3] y = ['a', 'b', 'c'] reduce(lambda x, y: x + y, zip(x, y)) # works sum(zip(x, y)) # fails Why is sum failing here?
Henry Henrinson
  • 4,929
  • 6
  • 35
  • 73
3
votes
2 answers

Haskell - How to transform maximum (xs ++ map (x+) xs) to max (maximum xs) (x + maximum xs)

One of the excercises in "Thinking Functionally With Haskell" is about making a program more efficient using the fusion law. I am having some trouble trying to replicate the answer. A part of the calculation requires that you transform maximum (xs…
Barry Rogerson
  • 558
  • 2
  • 15
3
votes
3 answers

Understanding different foldr statments

I understand simple foldr statements like foldr (+) 0 [1,2,3] However, I'm having trouble with more complicated foldr statements, namely ones that take 2 parameters in the function, and with / and - computations. Could anyone explain the steps that…
p0ny
  • 197
  • 9
2
votes
1 answer

How to prove this Haskell code using equational reasoning

I found this exercise on equational reasoning and proofs in Haskell. The following code is given: type Stack = [Int] type Code = [Op] data Op = PUSH Int | ADD deriving (Show) -- -- Stack machine -- exec :: Code -> Stack -> Stack exec [ ] s = s exec…
user4424299
2
votes
2 answers

Proof by induction with multiple lists

I am following the Functional Programming in Scala lecture on Coursera and at the end of the video 5.7, Martin Odersky asks to prove by induction the correctness of the following equation : (xs ++ ys) map f = (xs map f) ++ (ys map f) How to handle…
1
vote
1 answer

(sequence .) . flip fmap === forM?

Is (sequence .) . flip fmap the same as forM? I ask here because pointfree.io seems not to say the same...
Enlico
  • 12,203
  • 5
  • 28
  • 59
1
vote
1 answer

Mapping a strict vs. a lazy function

(head . map f) xs = (f . head) xs It works for every xs list when f is strict. Can anyone give me example, why with non-strict f it doesnt work?
beja
  • 29
  • 2
1
2