Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutual Definition Message/Intuition #1174

Closed
mariari opened this issue Jul 1, 2022 · 2 comments
Closed

Mutual Definition Message/Intuition #1174

mariari opened this issue Jul 1, 2022 · 2 comments

Comments

@mariari
Copy link
Member

mariari commented Jul 1, 2022

When trying to make my tak benchmark fast I ran into an issue.

module tak;

open import Prelude;
open import Data.Nat;
open import Data.Nat.Ord;
open import Complex;
     import Data.Bool;
open import Data.Ord;

-:   ;
-① (suc x)  x;
-① zero     zero;


inductive Unit {
  unit : Unit;
};

inductive Thunk (A : Type) {
  box : (Unit  A)  Thunk A;
};

call : {A : Type}  Thunk A  A;
call (box thunk)  thunk unit;

if : {A : Type}  Data.Bool.Bool  (Thunk A)  (Thunk A)  A;
if Data.Bool.true  then else  call then;
if Data.Bool.false then else  call else;


terminating
tak-then :       Unit -> ;
tak-then x y z unit 
  (tak (tak (-① x) y z)
       (tak (-① y) z x)
       (tak (-① z) x y));

tak-else :   Unit  ;
tak-else z unit  z;

terminating
tak :       ;
tak x y z 
  if-func (isLT (compare y x))
          (box (tak-then x y z))
          (box (tak-else z));

end;

with the following error message

Symbol not in scope: tak

splitting up the function leads into the issue where I need to call tak from tak-then or stage it in a way that is similar to placing jump labels by hand.

module tak;

open import Prelude;
open import Data.Nat;
open import Data.Nat.Ord;
open import Complex;
     import Data.Bool;
open import Data.Ord;

-:   ;
-① (suc x)  x;
-① zero     zero;


inductive Unit {
  unit : Unit;
};

inductive Thunk (A : Type) {
  box : (Unit  A)  Thunk A;
};

call : {A : Type}  Thunk A  A;
call (box thunk)  thunk unit;

if : {A : Type}  Data.Bool.Bool  (Thunk A)  (Thunk A)  A;
if Data.Bool.true  then else  call then;
if Data.Bool.false then else  call else;

tak-else :   Unit  ;
tak-else z unit  z;

terminating
tak-branch : (      Unit  )        ;
tak-branch tak-then x y z 
  if (isLT (compare y x))
     (box (tak-then x y z))
     (box (tak-else z));

terminating
tak-then :       Unit  ;
tak-then x y z unit 
   tak-branch tak-then
              (tak-branch tak-then (-① x) y z)
              (tak-branch tak-then (-① y) z x)
              (tak-branch tak-then (-① z) x y);


terminating
tak :       ;
tak  tak-branch tak-then;

end;

where I have to pass in the recursive branch tak-then to tak-branch then setup my tak function to be the curried version of trak-branch basically, doing label jump points by hand.

As a side note, the explicit box call in tak-branch can be replaced to the tak-then and tak-else call once local lambdas are added to the language.

@mariari mariari mentioned this issue Jul 13, 2022
@mariari mariari changed the title Mutual Functions Mutual Definition Support Jul 1, 2022
@mariari mariari changed the title Mutual Definition Support Mutual Definition Message/Intuition Jul 1, 2022
@mariari
Copy link
Member Author

mariari commented Jul 1, 2022

Upon further reading, there is a way to express mutual functions

terminating tak      :       ;
terminating tak-then :       Unit -> ;
            tak-else :   Unit  ;

tak-then x y z unit 
  (tak (tak (-① x) y z)
       (tak (-① y) z x)
       (tak (-① z) x y));

tak-else z unit  z;

tak x y z 
  if (isLT (compare y x))
     (box (tak-then x y z))
     (box (tak-else z));

The above does work, however I was unsure of the convention on how to achieve this. Might be good to have a comment about it, since I think due to how the language ingests terms, the information should be around about the unknown symbol.

@paulcadman
Copy link
Collaborator

We'll clarify mutually recursive functions in the documentation.

@cwgoes cwgoes transferred this issue from another repository Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants