6

In Idris, is it possible to rewrite all functions using "with" to use "case" instead of "with" ?

If not, could you give a counter example ?

jhegedus
  • 20,244
  • 16
  • 99
  • 167

1 Answers1

6

Not possible. When you pattern match with with, the types in the context are updated with information extracted from the matched constructor. case doesn't cause such updating.

As an example, the following works with with but not with case:

import Data.So

-- here (n == 10) in the goal type is rewritten to True or False
-- after the match
maybeTen : (n : Nat) -> Maybe (So (n == 10))
maybeTen n with (n == 10)
  maybeTen n | False = Nothing
  maybeTen n | True  = Just Oh

-- Here the context knows nothing about the result of (n == 10)
-- after the "case" match, so we can't fill in the rhs
maybeTen' : (n : Nat) -> Maybe (So (n == 10))
maybeTen' n = case (n == 10) of
  True  => ?a 
  False => ?b
András Kovács
  • 29,931
  • 3
  • 53
  • 99
  • So basically `with` is a kind of `case` that has a "stronger" connection to the type-level than simple `case` has. Very loosely speaking. – jhegedus Feb 25 '16 at 16:12
  • 2
    IIRC `with` desugars to a new top-level definition that has the extra arguments compared to the parent function, and it's fundamentally the function definitions that enforce/update all the type dependencies in the argument patterns. – András Kovács Feb 25 '16 at 16:15
  • So this suggests that it is not possible to have multiple `with`-s. Is that correct? For example n==10 and n==9 at the same time, would that make sense ? – jhegedus Feb 25 '16 at 16:24
  • You can use any number of `with`-s (but one at a time in Idris, in Agda we can do any number of introductions with a single `with`, which is convenient). See [example](http://lpaste.net/153319). It's also not an issue that `n == 10` and `n == 9` can't be both true; in that case you can derive a contradiction, i. e. [`Void`](http://www.idris-lang.org/docs/current/base_doc/docs/[builtins].html#Void), which is our way of telling Idris that a case branch is unreachable. – András Kovács Feb 25 '16 at 16:49