3

I've seen it here and there in Idris source code, but I have yet to find an explanation for what it means. The closest I've gotten is finding that it's called RigCount in the syntax reference. But what is it?

test : (0 a : b) -> b
test x = x
--      ^^^ Error

As it happens the above example throws an error: While processing right hand side of test. x is not accessible in this context.

Changing this to a 1 makes it type check, but it doesn't give any more indication what it means.

test : (1 a : b) -> b
test x = x
Aron
  • 8,696
  • 6
  • 33
  • 59

2 Answers2

7

This is a multiplicity.

Idris2 has three multiplicities:

  • Erased (0) - not available at runtime
  • Linear (1) - can be used once at runtime
  • Unrestricted (no multiplicity tag) - can be used any number of times
Cactus
  • 27,075
  • 9
  • 69
  • 149
michaelmesser
  • 3,601
  • 2
  • 19
  • 42
4

To elaborate on the error message, in the first example, (0 a : b) says there are 0 of a (or x) available at runtime. You'd need it to be available at runtime to return it. Hence, it "is not accessible in this context".

You can make it available in this context in two ways:

  • change its multiplicity, which is what you do when you write (1 a : b), which says there are exactly one of these available at runtime. You could also leave off the multiplicity, as (a : b), which means any number are available at runtime, or
  • change the context. You can also give module-level objects multiplicities, so if you marked test as erased, like
    0 test : (0 a : b) -> b
    
    then you can return x because you're stating the result of test is also erased.
joel
  • 6,359
  • 2
  • 30
  • 55
  • 1
    Interesting! Thank you for that. So if I give a value a multiplicity of 1 is that equivalent to a linear type? – Aron Jun 22 '22 at 22:04
  • @Aron yes it is – joel Jun 22 '22 at 22:24
  • 1
    @Aron you have to be a bit careful about _where_ the value is linear. In your example, I can use the _result_ of `test` in a non-linear context. The `1` in this case simply means it's only linear _within_ `test` – joel Jun 22 '22 at 22:28