Iter 16e: == polymorphic over Int / Bool / Str / Unit
Lifts the Int-only restriction on `==`. Declared type becomes
forall a. Fn(a, a) -> Bool; codegen monomorphises and dispatches:
Int → icmp eq i64
Bool → icmp eq i1
Str → call @strcmp + icmp eq i32 0
Unit → constant true (operands still emitted for side effects)
ADT / Fn / other → CodegenError::Internal
This unblocks 16c's build_eq for non-Int lit patterns. == joins
__unreachable__ as the second polymorphic builtin (same Forall
machinery).
- check/builtins.rs: == registered as Forall(a, Fn(a, a) -> Bool).
- codegen: lower_eq dispatch table; @strcmp declared in IR header
alongside @printf/@GC_malloc/@puts.
- examples/eq_demo.{ailx,ail.json}: covers all four supported
scalars including a Str-lit-pattern match.
- IR snapshots refreshed: only +declare i32 @strcmp(ptr, ptr) in
the header; every define body bit-identical.
- e2e + check + codegen tests: 124 → 133 (+9, of which +1 is the
e2e fixture and the rest exercise the new dispatch / typecheck
surface).
Other comparison ops (<, <=, >, >=, !=) remain Int-only — out of
scope for this iter.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1137,3 +1137,29 @@ fn unreachable_demo() {
|
||||
let lines: Vec<&str> = stdout.lines().collect();
|
||||
assert_eq!(lines, vec!["4", "5"]);
|
||||
}
|
||||
|
||||
/// Iter 16e: polymorphic `==`. Properties protected:
|
||||
/// (1) `==` typechecks at Int / Bool / Str / Unit (the fixture
|
||||
/// uses every supported case directly via `(app == ...)`);
|
||||
/// (2) codegen dispatches each arg-type to the right LLVM shape
|
||||
/// — `icmp eq i64`, `icmp eq i1`, `@strcmp + icmp eq i32 0`,
|
||||
/// constant `i1 true` — and the binary's stdout matches the
|
||||
/// boolean truth-table for those operators;
|
||||
/// (3) 16c's `build_eq` desugar of `(pat-lit "hi")` over a `Str`
|
||||
/// scrutinee now goes through, since `==` no longer rejects
|
||||
/// non-Int args at typecheck.
|
||||
#[test]
|
||||
fn eq_demo() {
|
||||
let stdout = build_and_run("eq_demo.ail.json");
|
||||
let lines: Vec<&str> = stdout.lines().collect();
|
||||
assert_eq!(
|
||||
lines,
|
||||
vec![
|
||||
"true", "false", // == on Int
|
||||
"false", "true", // == on Bool
|
||||
"true", "false", // == on Str
|
||||
"true", // == on Unit
|
||||
"1", "2", "0", // classify_str: Str lit-pattern desugar
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ target triple = "<NORMALIZED>"
|
||||
declare i32 @printf(ptr, ...)
|
||||
declare i32 @puts(ptr)
|
||||
declare ptr @GC_malloc(i64)
|
||||
declare i32 @strcmp(ptr, ptr)
|
||||
|
||||
@ail_hello_main_clos = private unnamed_addr constant { ptr, ptr } { ptr @ail_hello_main_adapter, ptr null }
|
||||
define i8 @ail_hello_main() {
|
||||
|
||||
@@ -7,6 +7,7 @@ target triple = "<NORMALIZED>"
|
||||
declare i32 @printf(ptr, ...)
|
||||
declare i32 @puts(ptr)
|
||||
declare ptr @GC_malloc(i64)
|
||||
declare i32 @strcmp(ptr, ptr)
|
||||
|
||||
@ail_list_sum_list_clos = private unnamed_addr constant { ptr, ptr } { ptr @ail_list_sum_list_adapter, ptr null }
|
||||
@ail_list_main_clos = private unnamed_addr constant { ptr, ptr } { ptr @ail_list_main_adapter, ptr null }
|
||||
|
||||
@@ -7,6 +7,7 @@ target triple = "<NORMALIZED>"
|
||||
declare i32 @printf(ptr, ...)
|
||||
declare i32 @puts(ptr)
|
||||
declare ptr @GC_malloc(i64)
|
||||
declare i32 @strcmp(ptr, ptr)
|
||||
|
||||
@ail_max3_max_clos = private unnamed_addr constant { ptr, ptr } { ptr @ail_max3_max_adapter, ptr null }
|
||||
@ail_max3_max3_clos = private unnamed_addr constant { ptr, ptr } { ptr @ail_max3_max3_adapter, ptr null }
|
||||
|
||||
@@ -7,6 +7,7 @@ target triple = "<NORMALIZED>"
|
||||
declare i32 @printf(ptr, ...)
|
||||
declare i32 @puts(ptr)
|
||||
declare ptr @GC_malloc(i64)
|
||||
declare i32 @strcmp(ptr, ptr)
|
||||
|
||||
@ail_sum_sum_clos = private unnamed_addr constant { ptr, ptr } { ptr @ail_sum_sum_adapter, ptr null }
|
||||
@ail_sum_main_clos = private unnamed_addr constant { ptr, ptr } { ptr @ail_sum_main_adapter, ptr null }
|
||||
|
||||
@@ -7,6 +7,7 @@ target triple = "<NORMALIZED>"
|
||||
declare i32 @printf(ptr, ...)
|
||||
declare i32 @puts(ptr)
|
||||
declare ptr @GC_malloc(i64)
|
||||
declare i32 @strcmp(ptr, ptr)
|
||||
|
||||
@ail_ws_lib_add_clos = private unnamed_addr constant { ptr, ptr } { ptr @ail_ws_lib_add_adapter, ptr null }
|
||||
@ail_ws_main_main_clos = private unnamed_addr constant { ptr, ptr } { ptr @ail_ws_main_main_adapter, ptr null }
|
||||
|
||||
Reference in New Issue
Block a user