<div dir="ltr">Two things:<div><br></div><div>1.</div><div>In your first attempt you did not re-define GCD (or you skipped that from your e-mail, but that seems to be the important part). The original definition can't work because [GCD x] isn't even a type so nothing can ever return a value of this type.</div><div>You need to define something like (I'll stubbornly keep calling this P for similarity with other examples but you're free to call it whatever):</div><div>P : Bin → Set</div><div>P a = (b : Bin) → GCD a b</div><div><br></div><div>2. Giving P explicitly can help improve error messages a lot. You can try something like this:</div><div><span class="gmail-im" style="color:rgb(80,0,80)">gcd =  <-rec _ P gc</span><br></div><div><span class="gmail-im" style="color:rgb(80,0,80)"><br></span></div><div><span class="gmail-im" style="color:rgb(80,0,80)">I haven't look in much detail so maybe this is not actually helpful. Sorry if it's not.</span></div><div><br></div><div><br></div><div><font color="#500050">> </font>WellFounded recursion thing is also available directly for the signature of </div><div>> gcd : (a b : Bin) → GCD a b,</div><div><br></div><div>It's only available for signatures of form [(x : X) → P a] for well-founded X, so yes, that's the case. By unifying the two types you get exactly the P I wrote above.</div></div><br><div class="gmail_quote"><div dir="ltr">On Fri, 10 Aug 2018 at 13:35, Sergei Meshveliani <<a href="mailto:mechvel@botik.ru">mechvel@botik.ru</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Fri, 2018-08-10 at 14:36 +0300, Sergei Meshveliani wrote:<br>
> On Thu, 2018-08-09 at 23:15 +0100, Arseniy Alekseyev wrote:<br>
> > Something like this, I think:<br>
> > <br>
> > <br>
> > P : Bin → Set <br>
> > <br>
> > P _y = (x : Bin) → Bin <br>
> > <br>
> > <br>
> > <br>
> > gc :  (y : Bin) → (∀ y' → y' < y → P y') → P y<br>
> > <br>
> > <br>
> > then after applying <-rec you get something of type [(y : Bin) → P y],<br>
> > which is just gcd with arguments swapped.<br>
> > <br>
> > <br>
> > (I wrote P in a general form so that it's more similar to "dependent"<br>
> > examples, but of course you don't need to)<br>
> > <br>
> <br>
> <br>
> <br>
> Thank you. <br>
> This works with the result of  Bin.<br>
> I try to extend this to   <br>
>                      gcd : (a b : Bin) -> GCD a b,<br>
> and fail.<br>
> <br>
<br>
<br>
Below       r<x = rem< x y x≢0<br>
was a typo,<br>
it needs to be replaced with  r<x = rem< y x x≢0.<br>
<br>
But this  does not change the report of Agda.<br>
<br>
<br>
Another attempt<br>
===============<br>
<br>
I change the signatures to  <br>
<br>
------------------------------------------<br>
BB = Bin × Bin<br>
<br>
_<p_ :  Rel BB Level.zero<br>
_<p_ =  _<_ on proj₁<br>
<br>
open import Induction.WellFounded using (WellFounded)<br>
<br>
postulate  <p-wellFounded : WellFounded _<p_<br>
<br>
open All <p-wellFounded using () renaming (wfRec to <-rec)<br>
<br>
record GCD (pr : BB) : Set   -- contrived<br>
       where<br>
       constructor gcd′<br>
       a = proj₁ pr<br>
       b = proj₂ pr<br>
<br>
       field  res       : Bin<br>
              divides-a : ∃ (\q → res * q ≡ a)<br>
              divides-b : ∃ (\q → res * q ≡ b)<br>
              -- and maximality axiom<br>
<br>
gcd : (pr : BB) → GCD pr<br>
----------------------------------------------<br>
<br>
Now  gcd  is  on  Bin × Bin,  and your approach works,<br>
the code is type-checked: <br>
<br>
gcd =  <-rec _ _ gc<br>
  where<br>
  gc :  (pr : BB) → (∀ pr' → pr' <p pr → GCD pr') → GCD pr<br>
  gc (x , y) gcRec<br>
             with x ≟ 0#<br>
<br>
  ... | no x≢0 =  f<br>
                  where<br>
                  f : GCD (x , y)<br>
                  f = liftGCD (gcRec (r , x) r<x)<br>
                      where<br>
                      r   = rem y x x≢0<br>
                      r<x = rem< y x x≢0<br>
<br>
                      postulate  liftGCD : GCD (r , x) → GCD (x , y)<br>
  ... | ...<br>
-------------------------------------------------<br>
<br>
<br>
All right:  I can convert from  GCD-auxiliary (a , b)  <br>
to                              GCD a b.<br>
<br>
But I have an impression that the WellFounded recursion thing is also<br>
available directly for the signature of<br>
<br>
          gcd : (a b : Bin) → GCD a b,<br>
<br>
only am missing something.<br>
-- ?<br>
<br>
Regards,<br>
<br>
------<br>
Sergei<br>
<br>
<br>
<br>
<br>
<br>
<br>
> ------------------------------------------------------------------<br>
> postulate<br>
>   rem  :  Bin → (y : Bin) → y ≢ 0# → Bin   -- remainder of x by y.<br>
> <br>
>   rem< :  (x y : Bin) → (y≢0 : y ≢ 0#) → rem x y y≢0 < y<br>
> <br>
> <br>
> record GCD (a b : Bin) : Set   -- contrived<br>
>        where<br>
>        constructor gcd′<br>
> <br>
>        field  res       : Bin<br>
>               divides-a : ∃ (\q → res * q ≡ a)<br>
>               divides-b : ∃ (\q → res * q ≡ b)<br>
>               --<br>
>               -- and maximality axiom<br>
> <br>
> -- Without using termination proof:<br>
> --<br>
> {-# TERMINATING #-}<br>
> gcd : (a b : Bin) → GCD a b<br>
> gcd x y<br>
>       with x ≟ 0#<br>
> ...   | yes x≡0 =  gcd′ y (0# , y*0≡x) (1# , y*1≡y)<br>
>                    where<br>
>                    postulate  y*0≡x : y * 0# ≡ x<br>
>                               y*1≡y : y * 1# ≡ y<br>
> <br>
> ...   | no x≢0 =   liftGCD (gcd r x)<br>
>                    where<br>
>                    r = rem y x x≢0<br>
> <br>
>                    postulate  liftGCD : GCD r x → GCD x y<br>
> ---------------------------------------------------------------<br>
> <br>
> The second argument is divided by the first one in the loop -- this way<br>
> it is easier to use.<br>
> <br>
> This is type-checked.<br>
> <br>
> Now try WellFounded. As I understand, the approach is to reduce to a<br>
> function of a single argument:<br>
> <br>
> --------------------------------------------------------------- <br>
> gcd : (a : Bin) → GCD a<br>
> gcd =  <-rec _ _ gc<br>
>   where<br>
>   gc :  (x : Bin) → (∀ x' → x' < x → GCD x') → GCD x<br>
>   gc x gcRec<br>
>        with x ≟ 0#<br>
>   ...  | yes x≡0 =  f<br>
>                     where<br>
>                     f : GCD x<br>
>                     f y =  gcd′ y (0# , y*0≡x) (1# , y*1≡y)<br>
>                            where<br>
>                            postulate y*0≡x : y * 0# ≡ x<br>
>                                      y*1≡y : y * 1# ≡ y<br>
> <br>
>   ...  | no x≢0 =  f<br>
>                    where<br>
>                    f : GCD x<br>
>                          f y = liftGCD (gcRec r r<x x)<br>
>                                where<br>
>                          r   = rem y x x≢0<br>
>                                r<x = rem< x y x≢0<br>
> <br>
>                          postulate  liftGCD : GCD r x → GCD x y<br>
> ---------------------------------------------------------------<br>
> <br>
> Agda type-checks the function  gc, <br>
> but it reports that  (<-rec _ _ gc)  does not return a value of the type<br>
> GCD a.<br>
> <br>
> Then I try <br>
> <br>
>  gcd :  Bin → (Bin → Set)<br>
>  gcd =  <-rec _ _ gc<br>
>   where<br>
>   postulate <br>
>     gc :  (x : Bin) → (∀ x' → x' < x → Bin → Set) → Bin → Set<br>
>  <br>
> (which goal adequacy I do not understand). <br>
> It is type-checked,<br>
> but I fail to implement this version of gc.<br>
> <br>
> Can anybody advise, please?<br>
> <br>
> ------<br>
> Sergei <br>
> <br>
> <br>
> <br>
> <br>
> > On Thu, 9 Aug 2018 at 20:40, Sergei Meshveliani <<a href="mailto:mechvel@botik.ru" target="_blank">mechvel@botik.ru</a>><br>
> > wrote:<br>
> > <br>
> >         Thank you.<br>
> >         After this sample of  downFrom  I was able to program  divMod<br>
> >         for Bin.<br>
> >         But I am stuck with  gcd  for  Bin.<br>
> >         Consider a contrived simple version:<br>
> >         <br>
> >         ------------------------------------------------------<br>
> >         postulate<br>
> >           rem  :  Bin → (y : Bin) → y ≢ 0# → Bin    -- remainder of x<br>
> >         by y.<br>
> >         <br>
> >           rem< :  (x y : Bin) → (y≢0 : y ≢ 0#) → rem x y y≢0 < y<br>
> >         <br>
> >         gcd : Bin → Bin → Bin<br>
> >         gcd x y<br>
> >               with y ≟ 0#<br>
> >         ...   | yes _  =  x<br>
> >         ...   | no y≢0 =  gcd y (rem x y y≢0)<br>
> >         <br>
> >         This lacks termination proof.<br>
> >         The argument pair  (x , y)  is replaced in recursion with<br>
> >         (y , r),<br>
> >         where  r < y.  So, it is needed well-founded recursion: <br>
> >         <br>
> >         gcd' : Bin → Bin → Bin<br>
> >         gcd' =  <-rec _ _ gc<br>
> >           where<br>
> >           postulate<br>
> >            gc :  Bin → (b : Bin) → (∀ x y → y < b → Bin) → Bin<br>
> >         -- ??<br>
> >         <br>
> >         <br>
> >         I do not guess what signature to set for  gc.  <br>
> >         I set a hole "?" for  gc,  and the type checker shows<br>
> >         <br>
> >               Induction.WellFounded.WfRec _<_ (λ _ → Bin → Bin)<br>
> >               .Relation.Unary._.⊆′ (λ _ → Bin → Bin)<br>
> >         -- ?<br>
> >         <br>
> >         Can anybody help, please?<br>
> >         <br>
> >         Thanks,<br>
> >         <br>
> >         ------<br>
> >         Sergei  <br>
> >         <br>
> >         <br>
> >         <br>
> >         On Wed, 2018-08-08 at 17:49 +0200, Sandro Stucki wrote:<br>
> >         > > Can anybody demonstrate it on the following example?<br>
> >         > <br>
> >         > Here you go:<br>
> >         > <br>
> >         ><br>
> >         --------------------------------------------------------------<br>
> >         > open import Function   using (_∘_; _on_)<br>
> >         > open import Data.List  using (List; []; _∷_)<br>
> >         > open import Data.Bin   using (Bin; toBits; pred; _<_; less;<br>
> >         toℕ)<br>
> >         > open import Data.Digit using (Bit)<br>
> >         > import Data.Nat      as Nat<br>
> >         > import Induction.Nat as NatInd<br>
> >         > open import Induction.WellFounded<br>
> >         > <br>
> >         > open Bin<br>
> >         > <br>
> >         > predBin : Bin → Bin<br>
> >         > predBin = pred ∘ toBits<br>
> >         > <br>
> >         > postulate<br>
> >         >   predBin-< :  (bs : List Bit) -> predBin (bs 1#) < (bs 1#)<br>
> >         > <br>
> >         > -- The strict order on binary naturals implies the strict<br>
> >         order on the<br>
> >         > -- corresponding unary naturals.<br>
> >         > <⇒<ℕ : ∀ {b₁ b₂} → b₁ < b₂ → (Nat._<_ on toℕ) b₁ b₂<br>
> >         > <⇒<ℕ (less lt) = lt<br>
> >         > <br>
> >         > -- We can derive well-foundedness of _<_ on binary naturals<br>
> >         from<br>
> >         > -- well-foundedness of _<_ on unary naturals.<br>
> >         > <-wellFounded : WellFounded _<_<br>
> >         > <-wellFounded =<br>
> >         >   Subrelation.wellFounded <⇒<ℕ (Inverse-image.wellFounded<br>
> >         toℕ<br>
> >         > NatInd.<-wellFounded)<br>
> >         > <br>
> >         > open All <-wellFounded using () renaming (wfRec to <-rec)<br>
> >         > <br>
> >         > downFrom : Bin → List Bin     -- x ∷ x-1 ∷ x-2 ∷ ... ∷ 0# ∷<br>
> >         []<br>
> >         > downFrom = <-rec _ _ df<br>
> >         >   where<br>
> >         >     df : (b : Bin) → (∀ b′ → b′ < b → List Bin) → List Bin<br>
> >         >     df 0#      dfRec = 0# ∷ []<br>
> >         >     df (bs 1#) dfRec = (bs 1#) ∷ (dfRec (predBin (bs 1#))<br>
> >         (predBin-< bs))<br>
> >         ><br>
> >         --------------------------------------------------------------<br>
> >         > <br>
> >         > In order to use well-founded induction, we first have to<br>
> >         prove that<br>
> >         > the strict order < is indeed well-founded. Thankfully, the<br>
> >         standard<br>
> >         > library already contains such a proof for the strict order<br>
> >         on (unary)<br>
> >         > naturals as well as a collection of combinators for deriving<br>
> >         > well-foundedness of relations from others (in this case the<br>
> >         strict<br>
> >         > order on unary naturals).<br>
> >         > <br>
> >         > The core of the implementation of `downFrom' via<br>
> >         well-founded<br>
> >         > recursion is the function `df', which has the same signature<br>
> >         as<br>
> >         > `downFrom' except for the additional argument `dfRec', which<br>
> >         serves as<br>
> >         > the 'induction hypothesis'. The argument `dfRec' is itself a<br>
> >         function<br>
> >         > with (almost) the same signature as `downFrom' allowing us<br>
> >         to make<br>
> >         > recursive calls (i.e. take a recursive step), provided we<br>
> >         can prove<br>
> >         > that the first argument of the recursive call (i.e. the<br>
> >         argument to<br>
> >         > the induction hypothesis) is smaller than the first argument<br>
> >         of the<br>
> >         > enclosing call to `df'. The proof that this is indeed the<br>
> >         case is<br>
> >         > passed to `dfRec' as an additional argument of type b′ < b.<br>
> >         > <br>
> >         > The following answer on Stackoverflow contains a nice<br>
> >         explanation on<br>
> >         > how all of this is implemented in Agda under the hood:<br>
> >         > <a href="https://stackoverflow.com/a/19667260" rel="noreferrer" target="_blank">https://stackoverflow.com/a/19667260</a><br>
> >         > <br>
> >         > Cheers<br>
> >         > /Sandro<br>
> >         > <br>
> >         > <br>
> >         > On Wed, Aug 8, 2018 at 12:13 PM Sergei Meshveliani<br>
> >         <<a href="mailto:mechvel@botik.ru" target="_blank">mechvel@botik.ru</a>> wrote:<br>
> >         > ><br>
> >         > > On Tue, 2018-08-07 at 20:51 +0300, Sergei Meshveliani<br>
> >         wrote:<br>
> >         > > > Dear all,<br>
> >         > > ><br>
> >         > > > I am trying to understand how to use WellFounded of<br>
> >         Standard library.<br>
> >         > > ><br>
> >         > > > Can anybody demonstrate it on the following example?<br>
> >         > > ><br>
> >         > > ><br>
> >         --------------------------------------------------------------<br>
> >         > > > open import Function  using (_∘_)<br>
> >         > > > open import Data.List using (List; []; _∷_)<br>
> >         > > > open import Data.Bin  using (Bin; toBits; pred)<br>
> >         > > ><br>
> >         > > > open Bin<br>
> >         > > ><br>
> >         > > > predBin : Bin → Bin<br>
> >         > > > predBin = pred ∘ toBits<br>
> >         > > ><br>
> >         > > > downFrom : Bin → List Bin     -- x ∷ x-1 ∷ x-2 ∷ ... ∷<br>
> >         0# ∷ []<br>
> >         > > > downFrom 0#      =  0# ∷ []<br>
> >         > > > downFrom (bs 1#) =  (bs 1#) ∷ (downFrom (predBin (bs<br>
> >         1#)))<br>
> >         > > ><br>
> >         --------------------------------------------------------------<br>
> >         > > ><br>
> >         > > > downFrom  is not recognized as terminating.<br>
> >         > > > How to reorganize it with using items from<br>
> >         > > > Induction/*, WellFounded.agda ?<br>
> >         > ><br>
> >         > ><br>
> >         > ><br>
> >         > > I presumed also that it is already given the property<br>
> >         > ><br>
> >         > >   postulate<br>
> >         > >     predBin-< :  (bs : List Bit) -> predBin (bs 1#) < (bs<br>
> >         1#)<br>
> >         > ><br>
> >         > > (I do not mean to deal here with its proof).<br>
> >         > ><br>
> >         > > --<br>
> >         > > SM<br>
> >         > ><br>
> >         > ><br>
> >         > > _______________________________________________<br>
> >         > > Agda mailing list<br>
> >         > > <a href="mailto:Agda@lists.chalmers.se" target="_blank">Agda@lists.chalmers.se</a><br>
> >         > > <a href="https://lists.chalmers.se/mailman/listinfo/agda" rel="noreferrer" target="_blank">https://lists.chalmers.se/mailman/listinfo/agda</a><br>
> >         > <br>
> >         <br>
> >         <br>
> >         _______________________________________________<br>
> >         Agda mailing list<br>
> >         <a href="mailto:Agda@lists.chalmers.se" target="_blank">Agda@lists.chalmers.se</a><br>
> >         <a href="https://lists.chalmers.se/mailman/listinfo/agda" rel="noreferrer" target="_blank">https://lists.chalmers.se/mailman/listinfo/agda</a><br>
> <br>
> <br>
> _______________________________________________<br>
> Agda mailing list<br>
> <a href="mailto:Agda@lists.chalmers.se" target="_blank">Agda@lists.chalmers.se</a><br>
> <a href="https://lists.chalmers.se/mailman/listinfo/agda" rel="noreferrer" target="_blank">https://lists.chalmers.se/mailman/listinfo/agda</a><br>
<br>
<br>
</blockquote></div>