sbp / idris-bi

Idris Binary Integer Arithmetic, porting PArith, NArith, and ZArith from Coq
https://github.com/idris-lang/Idris-dev/issues/3976
36 stars 5 forks source link

Reformulate case-using functions #6

Closed clayrat closed 7 years ago

clayrat commented 7 years ago

If we define bipMinus as

bipMinusHelp : Bim -> Bip
bipMinusHelp (BimP c) = c
bipMinusHelp _        = U

bipMinus : (a, b: Bip) -> Bip
bipMinus a b = bipMinusHelp (bimMinus a b)

then the proof of subSuccR simplifies a lot, since now we can use rewrite:

subSuccR : (p, q: Bip) -> bipMinus p (bipSucc q) = bipPred (bipMinus p q)
subSuccR p q = rewrite subMaskSuccR p q in 
               rewrite subMaskCarrySpec p q in 
               aux (bimMinus p q)
  where
  aux : (m : Bim) -> bipMinusHelp (bimPred m) = bipPred (bipMinusHelp m) 
  aux  BimO        = Refl
  aux (BimP  U   ) = Refl
  aux (BimP (O _)) = Refl
  aux (BimP (I _)) = Refl
  aux  BimM        = Refl

So I propose to redefine all case-using functions (bipMinus, bipMin, bipMax, bipSqrtRemStep etc) to this form, at least until https://github.com/idris-lang/Idris-dev/issues/4001 is fixed.

Another inconvenience is that we can't put these helpers under where, as there currently is no way to refer to them (see https://github.com/idris-lang/Idris-dev/issues/3991).

clayrat commented 7 years ago

Another approach might be using with blocks:

bipMinus : (a, b: Bip) -> Bip
bipMinus a b with (bimMinus a b)
  bipMinus a b | BimP c = c
  bipMinus a b | _      = U

As these automatically get elaborated into helper functions, it also allows rewrites to work, but we can't proceed with aux for the same reason - with blocks can't be referred to.

sbp commented 7 years ago

My preference is for using top-level helper functions instead of where or with, but since you're writing 99% of the proofs at the moment I think you should choose ultimately whatever style suits you best whilst we work around idris-lang/Idris-dev#4001. In any case, a top-level helper function is what I used in my Bin proof.

clayrat commented 7 years ago

What I'm saying is that top-level helpers is apparently the only way forward for now, where/with is unusable due to https://github.com/idris-lang/Idris-dev/issues/3991