Skip to content

Commit

Permalink
Merge pull request #1035 from andrew-johnson-4/lsts-apply
Browse files Browse the repository at this point in the history
Lsts apply
  • Loading branch information
andrew-johnson-4 authored Dec 6, 2024
2 parents c84a27b + 3a79322 commit 91a048c
Show file tree
Hide file tree
Showing 12 changed files with 22,390 additions and 22,344 deletions.
44,550 changes: 22,287 additions & 22,263 deletions BOOTSTRAP/cli.c

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lambda_mountain"
version = "1.19.37"
version = "1.19.38"
authors = ["Andrew <andrew@subarctic.org>"]
license = "MIT"
description = "Typed Macro Assembler (backed by Coq proofs-of-correctness)"
Expand Down
4 changes: 4 additions & 0 deletions PLUGINS/FRONTEND/LSTS/lsts-parse.lsts
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,10 @@ let lsts-parse-lhs-one(tokens: List<Token>): Tuple<AST,List<Token>> = (
let base-rest = lsts-parse-lit(tokens);
tokens = base-rest.second;
base-rest.first;
} else if non-zero(tokens) && lsts-parse-head(tokens)==c"[" {
let base-rest = lsts-parse-lhs-list(tokens);
tokens = base-rest.second;
base-rest.first;
} else if non-zero(tokens) && lsts-parse-head(tail(tokens))==c"{" {
let loc = head(tokens).location;
let tag = lsts-parse-head(tokens); tokens = tail(tokens);
Expand Down
2 changes: 1 addition & 1 deletion SRC/apply-or-cons-and-specialize.lm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
apply-or-cons-and-specialize := λ(: function-name String)(: ft Type)(: pt Type)(: blame AST). (: (
(let r (Tuple( TAny TAny )))
(if (is-arrow ft) (
(set r (apply( function-name ft pt 1_u64 blame )))
(set r (apply-blame( function-name ft pt 1_u64 blame )))
) (
(if (&&( (non-zero ft) (non-zero pt) )) (
(set r (Tuple(
Expand Down
6 changes: 3 additions & 3 deletions SRC/apply-plural.lm
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

apply-plural := λ(: function-name String)(: ft Type)(: pt Type)(: blame AST). (: (
apply-plural := λ(: function-name String)(: ft Type)(: pt Type). (: (
(let r (: LEOF List<Type>))
(match ft (
()
( (TAnd( t1 t2 )) (
(set r (+(
(apply-plural( function-name t1 pt blame ))
(apply-plural( function-name t2 pt blame ))
(apply-plural( function-name t1 pt ))
(apply-plural( function-name t2 pt ))
)))
))
( (TGround( 'Array_s (LCons( _ (LCons(
Expand Down
61 changes: 1 addition & 60 deletions SRC/apply.lm
Original file line number Diff line number Diff line change
@@ -1,64 +1,5 @@
apply := λ(: function-name String)(: ft Type)(: pt Type)(: blame AST). (: (
(apply( function-name ft pt 0_u64 blame ))
) Tuple<Type,Type>);

apply := λ(: function-name String)(: ft Type)(: pt Type)(: do-specialize U64)(: blame AST). (: (
(apply( function-name ft pt do-specialize blame 1_u64 ))
) Tuple<Type,Type>);

apply := λ(: function-name String)(: ft Type)(: pt Type)(: do-specialize U64)(: blame AST)(: hard U64). (: (
(let r TAny)
(let rs (apply-plural( function-name ft pt blame )))
(if (not(is-t( ft 'Hook_s ))) (
(set rs (reduce-plural rs))
) ())
(if (&&( (==( (.length rs) 0_u64 )) hard )) (
(print 'Function\sApplication\sYielded\sNo\sMatches\n_s)
(print function-name)(print '\nWith\sArgument\s:\s_s)(print pt)(print '\n_s)
(print (location-of( blame )))(print '\n_s)
(print 'Options:\s_s)(print ft)(print '\n_s)
(exit 1_u64)
) ())
(if (&&( (>( (.length rs) 1_u64 )) (not(is-t( ft 'Hook_s ))) )) (
(print 'Function\sApplication\sYielded\sAn\sIrreducible\sPlurality\sOf\sMatches\n_s)
(print function-name)(print '\s:\s_s)(print ft)(print '\n_s)
(print 'With\sArgument\s_s)(print pt)(print '\n_s)
(print (location-of( blame )))(print '\n_s)
(let rs-copy rs)
(while (non-zero( rs-copy )) (
(print 'Matched\s_s)
(match rs-copy (
()
( (LCons( hd tl )) (
(print hd)
(set rs-copy tl)
))
))
(print '\n_s)
))
(exit 1_u64)
) ())
(for-each (sft in rs) (
(let frt (range sft))
(let fpt (domain sft))
(let ctx (unify( fpt pt )))
(set ctx (normalize ctx))
(let closed-type (substitute( ctx sft )))
(set ft closed-type)
(set r (guess-representation(substitute( ctx frt ))))
(if (&&( do-specialize (is-open sft) )) (
(if (is-open closed-type) (
(print 'Application\sDid\sNot\sClose\sBefore\sSpecialization:\n_s)
(print 'Function\s_s)(print function-name)(print '\s:\s_s)(print sft)(print '\n_s)
(print 'Argument\s_s)(print pt)(print '\n_s)
(print 'Result\s_s)(print closed-type)(print '\n_s)
(print (location-of( blame )))(print '\n_s)
(exit 1_u64)
) ())
(try-specialize( function-name sft ctx closed-type ))
) ())
))
(Tuple( ft r ))
(apply-blame( function-name ft pt 0_u64 blame ))
) Tuple<Type,Type>);

apply := λ(: ctx Context)(: term AST). (: (
Expand Down
64 changes: 64 additions & 0 deletions SRC/apply.lsts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

let apply-blame(function-name: CString, function-type: Type, parameters: Type, do-specialize: U64, blame: AST): Tuple<Type,Type> = (
let r = apply(function-name, function-type, parameters, do-specialize);
if not(non-zero(r.second)) {
fail("Function Application Yielded No Matches\n"
"\{function-name}\n"
"With Arguments \{parameters}\n"
"At \{location-of(blame)}\n"
"With Candidates:\n"
"\{function-type}\n")
};
if is-t(r.second, c"Error: Did Not Close Before Specialization") {
fail("Application\sDid\sNot\sClose\sBefore\sSpecialization:\n"
"Function \{function-name}: \{function-type}\n"
"Argument \{parameters}\n"
"At \{location-of(blame)}\n");
};
if is-t(r.second, c"Error: Function Application Yielded An Irreducible Plurality Of Matches") {
fail("Function Application Yielded An Irreducible Plurality Of Matches\n"
"\{function-name}\n"
"With Arguments \{parameters}\n"
"At \{location-of(blame)}\n"
"Matched Candidates:\n"
"\{r.first}\n")
};
r
);

let apply(function-name: CString, function-type: Type, parameters: Type, do-specialize: U64): Tuple<Type,Type> = (
let r = TAny {};
let rs = apply-plural(function-name, function-type, parameters);
if not(is-t( function-type, c"Hook" )) {
rs = reduce-plural(rs);
};
if rs.length > 1 && not(is-t( function-type, c"Hook" )) {
r = t1(c"Error: Function Application Yielded An Irreducible Plurality Of Matches");
function-type = head(rs);
for candidate in tail(rs) {
function-type = TAnd { close(r), close(candidate) };
};
} else {
for sft in rs { # iterate in case this is a hook
let frt = range(sft);
let fpt = domain(sft);
let ctx = normalize(unify(fpt, parameters));
let closed-type = substitute( ctx, sft );
function-type = closed-type;
if not(is-t( r, c"Error: Did Not Close Before Specialization")) {
r = substitute(ctx, frt);
};
if do-specialize && is-open(sft) {
if is-open(closed-type) {
r = t1(c"Error: Did Not Close Before Specialization");
};
try-specialize(function-name, sft, ctx, closed-type);
}
};
if is-t(function-type, c"Prop") {
r = and(r, cons-root(parameters));
};
};
Tuple { function-type, r }
);

7 changes: 7 additions & 0 deletions SRC/cons-root.lsts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

let cons-root(tt: Type): Type = (
match tt {
TGround{tag:c"Cons", parameters:[tl..hd..]} => cons-root(hd);
_ => tt;
};
);
3 changes: 3 additions & 0 deletions SRC/index-types.lm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import SRC/index-definitions.lm;
import SRC/quick-prop.lsts;
import SRC/cmp.lsts;

import SRC/cons-root.lsts;
import SRC/is-t.lm;
import SRC/is-and.lsts;
import SRC/index-concrete-type-instances.lm;
import SRC/type-index.lm;
import SRC/infer-expr.lm;
Expand Down Expand Up @@ -83,6 +85,7 @@ import SRC/cons-head.lm;
import SRC/cons-tail.lm;
import SRC/parameter-number.lm;
import SRC/apply.lm;
import SRC/apply.lsts;
import SRC/apply-plural.lm;
import SRC/reduce-plural.lm;
import SRC/apply-or-cons-and-specialize.lm;
Expand Down
22 changes: 9 additions & 13 deletions SRC/infer-expr.lm
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ infer-expr-one := λ(: tctx TContext)(: term AST)(: scoped IsScoped)(: hint Type
(let vt (typeof v))
(if (is-arrow vt) () (
(let gt (and( (t1 'GlobalVariable_s) (without-representation vt) )))
(maybe-specialize( 'mov_s (typeof-var-raw( term tctx 'mov_s )) (t3( 'Cons_s vt gt )) term ))
(maybe-specialize( 'mov_s (typeof-var-raw( term tctx 'mov_s )) (t3( 'Cons_s vt gt )) ))
))
))
))
Expand Down Expand Up @@ -84,7 +84,7 @@ infer-expr-one := λ(: tctx TContext)(: term AST)(: scoped IsScoped)(: hint Type
))
) ())
(if (is-t( tt 'SmartString_s )) () (
(maybe-specialize( 'intern_s (typeof-var-raw( term tctx 'intern_s )) tt term ))
(maybe-specialize( 'intern_s (typeof-var-raw( term tctx 'intern_s )) tt ))
))
))
( (App( (Var( 'gensym-label_s _ )) (Var( lname _ )) )) (
Expand Down Expand Up @@ -142,8 +142,8 @@ infer-expr-one := λ(: tctx TContext)(: term AST)(: scoped IsScoped)(: hint Type
()
( (TGround( 'Array_s (LCons( _ (LCons( TAny LEOF )) )) )) () )
( (TGround( 'Array_s (LCons( TAny (LCons( array-base LEOF )) )) )) (
(maybe-specialize( 'open_s (typeof-var-raw( term tctx 'open_s )) deref-type term ))
(maybe-specialize( 'push-deref_s (typeof-var-raw( term tctx 'push-deref_s )) deref-type term ))
(maybe-specialize( 'open_s (typeof-var-raw( term tctx 'open_s )) deref-type ))
(maybe-specialize( 'push-deref_s (typeof-var-raw( term tctx 'push-deref_s )) deref-type ))
(set deref-type (and( array-base (t1 'StackVariable_s) )))
))
( _ (do-specialize( 'open_s (typeof-var-raw( term tctx 'open_s )) deref-type term )) )
Expand Down Expand Up @@ -183,7 +183,7 @@ infer-expr-one := λ(: tctx TContext)(: term AST)(: scoped IsScoped)(: hint Type
)))
(ascript-normal( term (t1 'Nil_s) ))

(maybe-specialize( 'del_s (typeof-var-raw( term tctx 'del_s )) tt term ))
(maybe-specialize( 'del_s (typeof-var-raw( term tctx 'del_s )) tt ))
))
( (App( l r )) (
(let l-used Unused)
Expand Down Expand Up @@ -228,9 +228,6 @@ infer-expr-one := λ(: tctx TContext)(: term AST)(: scoped IsScoped)(: hint Type
(let function-type (.2 ftrt))
(ascript-normal( l function-type ))
(set rt (.1 ftrt))
(if (is-t( (typeof-var-raw( term tctx lt-fname )) 'Prop_s )) (
(set rt (and( rt (typeof r) )))
) ())
))
( _ (
(if (is( used Unused )) (set r-used Unused) ())
Expand Down Expand Up @@ -261,7 +258,6 @@ infer-expr-one := λ(: tctx TContext)(: term AST)(: scoped IsScoped)(: hint Type
(if (==( (var-name-if-var l) 'Rc_s )) (
(maybe-specialize( 'inc_s (typeof-var-raw( term tctx 'inc_s ))
(t2( 'Rc_s (p2(slot( (p2(slot( (p1 (typeof r)) 'Array_s ))) 'Tuple_s ))) )) # invert Rc constructor to get type parameter
term
))
) ())

Expand All @@ -276,7 +272,7 @@ infer-expr-one := λ(: tctx TContext)(: term AST)(: scoped IsScoped)(: hint Type
(set ct (and( ct (t2( 'CaseNumber_s (t1(to-string(index-of-tag l-name))) )) )))
(set ct (and( ct (t2( 'Sized_s (t1(to-string struct-size)) )) )))
(set ct (and( ct (t2( 'FieldsSized_s (t1(to-string fields-size)) )) )))
(maybe-specialize( 'push_s (typeof-var-raw( term tctx 'push_s )) ct term ))
(maybe-specialize( 'push_s (typeof-var-raw( term tctx 'push_s )) ct ))
))
( _ () )
))
Expand All @@ -299,7 +295,7 @@ infer-expr-one := λ(: tctx TContext)(: term AST)(: scoped IsScoped)(: hint Type
(let rt (typeof rhs))
(ascript-normal( term (enrich( (t3( 'Arrow_s lt rt )) tlt )) ))
(if (is-t( tlt 'Blob_s )) () (
(maybe-specialize( 'cdecl::return_s (typeof-var-raw( term tctx 'cdecl::return_s )) rt term ))
(maybe-specialize( 'cdecl::return_s (typeof-var-raw( term tctx 'cdecl::return_s )) rt ))
))
))
( (Var( v vtk )) (
Expand All @@ -321,7 +317,7 @@ infer-expr-one := λ(: tctx TContext)(: term AST)(: scoped IsScoped)(: hint Type

(if (is-arrow tt) () (
(let ct (denormalize tt))
(maybe-specialize( 'push_s (typeof-var-raw( term tctx 'push_s )) ct term ))
(maybe-specialize( 'push_s (typeof-var-raw( term tctx 'push_s )) ct ))
))
) ())
))
Expand All @@ -336,7 +332,7 @@ infer-expr-one := λ(: tctx TContext)(: term AST)(: scoped IsScoped)(: hint Type
))
))
(if (>( (sizeof-type(typeof term)) 0_u64 )) (
(maybe-specialize( 'push_s (typeof-var-raw( term tctx 'push_s )) (typeof term) term ))
(maybe-specialize( 'push_s (typeof-var-raw( term tctx 'push_s )) (typeof term) ))
) ())
tctx
) TContext);
Expand Down
7 changes: 7 additions & 0 deletions SRC/is-and.lsts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

let is-and(tt: Type): U64 = (
match tt {
TAnd {} => 1;
_ => 0;
}
);
6 changes: 3 additions & 3 deletions SRC/maybe-specialize.lm
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

maybe-specialize := λ(: function-name String)(: ft Type)(: pt Type)(: blame AST). (: (
(apply( function-name ft pt 1_u64 blame 0_u64 )) ()
maybe-specialize := λ(: function-name String)(: ft Type)(: pt Type). (: (
(apply( function-name ft pt 1_u64 )) ()
) Nil);

do-specialize := λ(: function-name String)(: ft Type)(: pt Type)(: blame AST). (: (
(apply( function-name ft pt 1_u64 blame 1_u64 )) ()
(apply-blame( function-name ft pt 1_u64 blame )) ()
) Nil);

0 comments on commit 91a048c

Please sign in to comment.