Sergei Meshveliani
2018-08-06 12:20:24 UTC
Dear all,
I have a certain problem with termination proof.
Consider the example: division with remainder for binary natural
numbers (the code is contrived).
-------------------------------------------------------------------
record DivMod (dividend divisor : Bin) : Set where
constructor result
field
quotient : Bin
remainder : Bin
equality : dividend â¡ remainder + quotient * divisor
rem<divisor : remainder < divisor
-- suc, +, âž; *, < are of Bin, 1+, +n, âžn; *n, <n are of â.
â£_⣠: Bin â â
⣠x ⣠= number of bits
divMod : (a b : Bin) â b ⢠0# â DivMod a b
divMod a b bâ¢0 =
aux a (toâ a) â€-refl
where
aux : (a : Bin) â cnt â toâ a †cnt â DivMod a b
aux a 0 aNâ€0 =
result 0# 0# aâ¡0+0*b 0<b
where
aâ¡0 = derived from aNâ€0
aux 0# _ _ = result 0# 0# 0â¡0+0*b 0<b
aux a (1+ cnt) aNâ€1+cnt = -- here a ⢠0#
let
aN = toâ a
e = ⣠a ⣠âžn ⣠b ⣠...
d = (2^ e) * b -- 2^ e is by prepending several
-- zero bits.
a' = a âž d
a'N = toâ a'
a'Nâ€cnt : a'N â€n cnt
a'Nâ€cnt =
because a, d ⢠0#, and toââžhomo.
(result q r a'â¡r+q*b r<b) = aux a' cnt a'Nâ€cnt
in
restore a b q r a' r<b
------------------------------------------------------------------
This means that divMod a b is reduced to (divMod (a âž d) b),
where d = 2^e * b for a certain appropriate e : â.
The counter cnt = (toâ a) : â is reduced to a smaller value by this
step.
Due to the structural decrement (suc cnt) -> cnt, Agda decides that
the recursion is terminating.
But this may lead to exponential performance cost, because the unary
representation of cnt is large, and the evaluation (suc cnt) -> snt
takes place not only in the proof part but also in regular evaluation.
To fix this, I change the counter to
cnt = ⣠a ⣠: â = number of bits in a.
Then one needs to prove messy lemmata in order to prove that
⣠a ⣠>n ⣠a' ⣠in the loop.
The question is
does there exist a way for this example to join
* a normal code performance (as in the second method version),
* simple termination proof
?
I see in Standard library Induction/WellFounded.agda.
It is difficult to understand of how to apply this
(the impression is that it will complicate the program).
Can this tool be used to solve the above problem with divMod for Bin ?
Somehow to prove that _>_ is well-founded on Bin
(it is useful for many other programs),
use the tools from WellFounded.agda, and prove termination of divMod
in a simple way and without loosing run-time performance.
Can anybody, please, demonstrate, how this will change the above code
for divMod ?
Another question
----------------
Has it sense the language semantic extension with WellFoundedOrder ?
The property IsWellFoundedOrder concerns any StrictPartialOrder,
and its simple definition is
appended to this letter.
Imagine that the type checker of Agda-extended
sees in the above loop
a a' : Bin and a>a' : a > a',
sees the instance of StrictPartialOrder for _â¡_ and _>_ on Bin,
sees the proof for IsWellFoundedOrder for this instance,
and concludes that divMod is terminating -- by the _implicit axiom_
of termination by a well-founded ordering.
With this, the proofs will be much simpler, simpler than with applying
the constructs of Induction of Library.
Agda is able to automatically find an argument in recursion that
decreases structurally. Similarly can it be able to find the argument
decreasing by _<_ and fit for well-founded recursion?
Agda sees that a is replaced with a' in the call, and it needs to
find where in the scope it is proved a > a'. At least this place for
a>a' can be marked, may be, by some pragma.
Thank you in advance for explanation,
------
Sergei
I have a certain problem with termination proof.
Consider the example: division with remainder for binary natural
numbers (the code is contrived).
-------------------------------------------------------------------
record DivMod (dividend divisor : Bin) : Set where
constructor result
field
quotient : Bin
remainder : Bin
equality : dividend â¡ remainder + quotient * divisor
rem<divisor : remainder < divisor
-- suc, +, âž; *, < are of Bin, 1+, +n, âžn; *n, <n are of â.
â£_⣠: Bin â â
⣠x ⣠= number of bits
divMod : (a b : Bin) â b ⢠0# â DivMod a b
divMod a b bâ¢0 =
aux a (toâ a) â€-refl
where
aux : (a : Bin) â cnt â toâ a †cnt â DivMod a b
aux a 0 aNâ€0 =
result 0# 0# aâ¡0+0*b 0<b
where
aâ¡0 = derived from aNâ€0
aux 0# _ _ = result 0# 0# 0â¡0+0*b 0<b
aux a (1+ cnt) aNâ€1+cnt = -- here a ⢠0#
let
aN = toâ a
e = ⣠a ⣠âžn ⣠b ⣠...
d = (2^ e) * b -- 2^ e is by prepending several
-- zero bits.
a' = a âž d
a'N = toâ a'
a'Nâ€cnt : a'N â€n cnt
a'Nâ€cnt =
because a, d ⢠0#, and toââžhomo.
(result q r a'â¡r+q*b r<b) = aux a' cnt a'Nâ€cnt
in
restore a b q r a' r<b
------------------------------------------------------------------
This means that divMod a b is reduced to (divMod (a âž d) b),
where d = 2^e * b for a certain appropriate e : â.
The counter cnt = (toâ a) : â is reduced to a smaller value by this
step.
Due to the structural decrement (suc cnt) -> cnt, Agda decides that
the recursion is terminating.
But this may lead to exponential performance cost, because the unary
representation of cnt is large, and the evaluation (suc cnt) -> snt
takes place not only in the proof part but also in regular evaluation.
To fix this, I change the counter to
cnt = ⣠a ⣠: â = number of bits in a.
Then one needs to prove messy lemmata in order to prove that
⣠a ⣠>n ⣠a' ⣠in the loop.
The question is
does there exist a way for this example to join
* a normal code performance (as in the second method version),
* simple termination proof
?
I see in Standard library Induction/WellFounded.agda.
It is difficult to understand of how to apply this
(the impression is that it will complicate the program).
Can this tool be used to solve the above problem with divMod for Bin ?
Somehow to prove that _>_ is well-founded on Bin
(it is useful for many other programs),
use the tools from WellFounded.agda, and prove termination of divMod
in a simple way and without loosing run-time performance.
Can anybody, please, demonstrate, how this will change the above code
for divMod ?
Another question
----------------
Has it sense the language semantic extension with WellFoundedOrder ?
The property IsWellFoundedOrder concerns any StrictPartialOrder,
and its simple definition is
appended to this letter.
Imagine that the type checker of Agda-extended
sees in the above loop
a a' : Bin and a>a' : a > a',
sees the instance of StrictPartialOrder for _â¡_ and _>_ on Bin,
sees the proof for IsWellFoundedOrder for this instance,
and concludes that divMod is terminating -- by the _implicit axiom_
of termination by a well-founded ordering.
With this, the proofs will be much simpler, simpler than with applying
the constructs of Induction of Library.
Agda is able to automatically find an argument in recursion that
decreases structurally. Similarly can it be able to find the argument
decreasing by _<_ and fit for well-founded recursion?
Agda sees that a is replaced with a' in the call, and it needs to
find where in the scope it is proved a > a'. At least this place for
a>a' can be marked, may be, by some pragma.
Thank you in advance for explanation,
------
Sergei