Giter Club home page Giter Club logo

Comments (4)

yurrriq avatar yurrriq commented on September 23, 2024

We should use idris-testing or, better yet, Specdris (#43).

from idris.

yurrriq avatar yurrriq commented on September 23, 2024

Edit: I should've mentioned this right away. Thanks for starting this discussion!

That's not the real issue here though. Let me try to elaborate.

Altering the bespoke assertEq to be more specific is not the correct approach here.

assertEq : Eq a => String -> (given : a) -> (expected : a) -> IO ()

It specifies that assertEq means to compare two values of type a, under the constraint that a has an implementation of Eq. The implementation you linked does not satisfy that invariant, because the type checker cannot be sure that b has an implementation of Eq. The solution to your generic implementation then is to add the constraint:

accumulate : Eq b => (a -> b) -> List a -> List b

What all that said, the tests expect the solution to have the following signature, as per the stub.

accumulate : (Int -> Int) -> List Int -> List Int

Notably, the following test fails to type check when you alter the signature of accumulate, as in the linked solution.

testDecrementFunctionAddsOneToAllInput : IO ()
testDecrementFunctionAddsOneToAllInput = assertEq "decrement function subtracts 1 from input" (accumulate (\x => x - 1) [1,2,3]) [0,1,2]
When checking right hand side of testDecrementFunctionAddsOneToAllInput with expected type
        IO ()

When checking argument smaller to function Prelude.Nat.-:
        Can't find a value of type
                LTE 1 x

So if we want to support a more generic accumulate (I can see arguments both ways), the necessary changes are more broad in scope than just assertEq.

Hot take re: making accumulate generic

  • Pros: more traditional FP, good learning exercise, intro to type constraints
  • Cons: could be too much too soon (intro to type constraints), need to change most/all of test (not a big deal, obviously)

I've had a long day/week. Does this blathering make any sense?

from idris.

ktosiek avatar ktosiek commented on September 23, 2024

It does make sense.

I still believe generalising the signature should be accepted by tests, even if the stub stays the same.

The type errors you've cited stem from the compiler either not specialising b (the empty list case), or selecting a wrong type for numbers (the LTE 1 x case). Both cases could be solved by specifying the type of lists used in tests. That's what my proposition really does, by specialising the assertEq helper.

from idris.

yurrriq avatar yurrriq commented on September 23, 2024

Ok, Iā€™d be happy to review/merge a PR.

from idris.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.