GithubHelp home page GithubHelp logo

No Nested ":" allowed? about idris2 HOT 5 CLOSED

Gadersd avatar Gadersd commented on June 9, 2024
No Nested ":" allowed?

from idris2.

Comments (5)

dunhamsteve avatar dunhamsteve commented on June 9, 2024

There is a syntax error in line 9. Due to some issues in the parser, it's getting eaten and the failure is propagating up and giving the wrong parse error.

Here, Idris is expecting a term:

    AppendSame : (val : Nat) -> Broadcastable (v1 : Vect n Nat) (v2 : Vect n Nat) -> Broadcastable (val :: v1) (val :: v2)
                                              ^^^^^^^^^^^^^^^^^

You can put v1 there. Idris automatically adds {0 v1 : _} -> at the beginning. If you want to specify that v1 and v2 are the same length, you'll have to manually write out the implicits:

import Data.Vect

data Broadcastable : (Vect n1 Nat) -> (Vect n2 Nat) -> Type where
    Empty : Broadcastable [] []
    AppendSame : {0 v1 : Vect n Nat} -> {0 v2 : Vect n Nat} -> (val : Nat) -> Broadcastable v1 v2 -> Broadcastable (val :: v1) (val :: v2)

Hopefully that unblocks you, but IMO we need a better parse error here.

from idris2.

dunhamsteve avatar dunhamsteve commented on June 9, 2024

I prefer the solution I gave above, by you could also do this:

data Broadcastable : (Vect n1 Nat) -> (Vect n2 Nat) -> Type where
    Empty : Broadcastable [] []
    AppendSame : (val : Nat) -> Broadcastable (the (Vect n Nat) v1) (the (Vect n Nat) v2) -> Broadcastable (val :: v1) (val :: v2)

The the function is identity with the type made explicit. It is used in Idris where an inline type annotation might be used in another language. If you do this, and type :ti AppendSame in the repl, you'll see that Idris adds the two implicits that I wrote above, but in the opposite order.

from idris2.

Gadersd avatar Gadersd commented on June 9, 2024

Thanks! Broadcastable is now working properly.
However, I have one other similar issue. For my other type:

data MatrixMultiplicable : (Vect n1 Nat) -> (Vect n2 Nat) -> Type where
    Dim2 : ([r1, c1]: Vect 2 Nat) -> ([c1, c2]: Vect 2 Nat) -> MatrixMultiplicable [r1, c1] [c1, c2]

I get the error

Error: Expected a type declaration.

main:16:10--16:11
 12 |     AppendOneLeft : (right: Nat) -> (b: Broadcastable v1 v2) -> Broadcastable (1::v1) (right::v2)
 13 |     AppendOneRight : (left: Nat) -> (b: Broadcastable v1 v2) -> Broadcastable (left::v1) (1::v2)
 14 | 
 15 | data MatrixMultiplicable : (Vect n1 Nat) -> (Vect n2 Nat) -> Type where
 16 |     Dim2 : ([r1, c1]: Vect 2 Nat) -> ([c1, c2]: Vect 2 Nat) -> MatrixMultiplicable [r1, c1] [c1, c2]
               ^

Error: Undefined name unsafePerformIO. 

(Interactive):1:1--1:1
 1 | module Main

How might this be resolved?

from idris2.

andrevidela avatar andrevidela commented on June 9, 2024

This is because pattern matching on the left side of : is not supported.

This should work although it does not do what you're looking for wrt to line-column checking:

data MatrixMultiplicable : (Vect n1 Nat) -> (Vect n2 Nat) -> Type where
    Dim2 : (v1: Vect 2 Nat) -> (v2: Vect 2 Nat) -> MatrixMultiplicable v1 v2

The most naive way of checking that the size of two matrices match is to write multiplication directly with the following type:

Matrix : Nat -> Nat -> Type -> Type
Matrix a b ty = Vect a (Vect b ty)

multiply : Matrx a b ty -> Matrix b c ty -> Matrix a c ty
multiply = ?etc

from idris2.

Gadersd avatar Gadersd commented on June 9, 2024

I resolved the issue by parameterizing the matrix dimensions as follows

data MatrixMultiplicable : (Vect n1 Nat) -> (Vect n2 Nat) -> Type where
    Dim2 : (r1: Nat) -> (c1: Nat) -> (c2: Nat) -> MatrixMultiplicable [r1, c1] [c1, c2]

from idris2.

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.