carbon-lang icon indicating copy to clipboard operation
carbon-lang copied to clipboard

Implement comparison operators

Open oraqlle opened this issue 2 years ago • 10 comments

The explorer, both local and on Godbolt are giving an error that they do not recognize the < character. Here's the code I've tested.

// main.carbon

package main api;

fn Fibonacci(limit: i32) {
  var (a: i32, b: i32) = (0, 1);
  while (a < limit) {
    Print(a, " ");
    let next: i32 = a + b;
    a = b;
    b = next;
  }
  Print("\n");
}

fn Main() -> i32 {
    Fibonacci(9);
    return 0;
}

And it gives the following error

$ carbon-explorer main.carbon
COMPILATION ERROR: main.carbon:5: invalid character '\x3C' in source file.

I am not sure why this error is occurring and any help would be greatly appreciated. Regards.

Misc.

oraqlle avatar Jul 22 '22 06:07 oraqlle

CC-ing some explorer folks @geoffromer @pk19604014 @SlaterLatiao

chandlerc avatar Jul 22 '22 07:07 chandlerc

To explain what's going on here, we didn't implement comparison operators yet because we were wanting to just do it once.

As described in the arithmetic design, the plan is to use interfaces to provide math. I think this is currently possible with impl, and would be reminiscent (but a little different, maybe needing external impl) if compared to what's currently done for ImplicitAs (which note the impls in the prelude: https://github.com/carbon-language/carbon-lang/blob/trunk/explorer/data/prelude.carbon -- there's supporting code in the interpreter)

It may still be necessary to add some intrinsic calls to do the integer comparisons, but I think the leaning would be to put that behind an interface rather than calling directly. Eventually we need to start using those interfaces, and intrinsics are a way to do that (similar to what's done for Heap.New/Delete).

jonmeow avatar Jul 23 '22 01:07 jonmeow

Is there anything left to do here? Should we close this as WAI and awaiting the generics implementation?

chandlerc avatar Jul 23 '22 02:07 chandlerc

Okay so is it just a case that at this stage integers (and any other types) don't have an implementation for comparisons?

oraqlle avatar Jul 23 '22 05:07 oraqlle

Correct. I've retitled this issue to make it clearer what's going on. :)

jonmeow avatar Jul 23 '22 12:07 jonmeow

More specific link, comparison operator extensibility: https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/expressions/comparison_operators.md#extensibility

jonmeow avatar Jul 25 '22 15:07 jonmeow

Is there anything left to do here? Should we close this as WAI and awaiting the generics implementation?

It sounds like @jonmeow is saying the Explorer might have enough support for generics to implement this now, if someone wants to take that on.

geoffromer avatar Jul 26 '22 01:07 geoffromer

I've started taking a crack at implementing the interface approach. (Well, actually I'm starting out with generalizing == but I imagine comparison would be a straightforward enough follow up to that.)

tkadur avatar Jul 26 '22 01:07 tkadur

I've started taking a crack at implementing the interface approach. (Well, actually I'm starting out with generalizing == but I imagine comparison would be a straightforward enough follow up to that.)

You may want to take a look at #1751 if you haven't already.

geoffromer avatar Jul 26 '22 23:07 geoffromer

This was mostly done in https://github.com/carbon-language/carbon-lang/pull/1883 but it looks like != is still missing.

jonmeow avatar Aug 24 '22 21:08 jonmeow

This was mostly done in #1883 but it looks like != is still missing.

This seems to be done in #2146

jlgoniba avatar Sep 15 '22 08:09 jlgoniba

Thanks, yes this is probably done now.

jonmeow avatar Sep 15 '22 15:09 jonmeow