From 54e219ee24c1508713d07473cd8a3b04c7f8fe18 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Sat, 8 Jan 2022 06:37:39 -0400 Subject: Fixes for the pure-Lux JVM compiler machinery. [Part 4] --- stdlib/source/library/lux.lux | 143 ++++---- .../source/library/lux/control/parser/analysis.lux | 5 +- .../library/lux/control/parser/synthesis.lux | 56 +-- stdlib/source/library/lux/target/ruby.lux | 52 ++- .../lux/tool/compiler/language/lux/analysis.lux | 100 +----- .../compiler/language/lux/analysis/composite.lux | 98 ++++++ .../compiler/language/lux/phase/analysis/case.lux | 7 +- .../language/lux/phase/analysis/case/coverage.lux | 7 +- .../language/lux/phase/analysis/inference.lux | 5 +- .../language/lux/phase/analysis/structure.lux | 7 +- .../language/lux/phase/extension/analysis/jvm.lux | 122 +++---- .../lux/phase/extension/generation/jvm/host.lux | 377 +++++++++++++-------- .../lux/phase/extension/generation/ruby/common.lux | 3 +- .../lux/phase/generation/jvm/structure.lux | 3 +- .../language/lux/phase/generation/ruby.lux | 1 + .../language/lux/phase/generation/ruby/case.lux | 36 +- .../lux/phase/generation/ruby/function.lux | 64 ++-- .../language/lux/phase/generation/ruby/loop.lux | 2 +- .../language/lux/phase/generation/ruby/runtime.lux | 45 ++- .../lux/phase/generation/ruby/structure.lux | 3 +- .../tool/compiler/language/lux/phase/synthesis.lux | 11 +- .../compiler/language/lux/phase/synthesis/case.lux | 13 +- .../language/lux/phase/synthesis/function.lux | 7 +- .../compiler/language/lux/phase/synthesis/loop.lux | 15 +- .../language/lux/phase/synthesis/variable.lux | 19 +- .../lux/tool/compiler/language/lux/synthesis.lux | 81 ++--- .../lux/tool/compiler/meta/archive/dependency.lux | 9 +- stdlib/source/test/lux/target/ruby.lux | 103 ++++-- stdlib/source/test/lux/tool.lux | 4 +- .../compiler/language/lux/analysis/composite.lux | 76 +++++ 30 files changed, 871 insertions(+), 603 deletions(-) create mode 100644 stdlib/source/library/lux/tool/compiler/language/lux/analysis/composite.lux create mode 100644 stdlib/source/test/lux/tool/compiler/language/lux/analysis/composite.lux (limited to 'stdlib/source') diff --git a/stdlib/source/library/lux.lux b/stdlib/source/library/lux.lux index 4aed1937b..d65fa7bcb 100644 --- a/stdlib/source/library/lux.lux +++ b/stdlib/source/library/lux.lux @@ -895,11 +895,8 @@ {#None} {#Item [k v] environment'} - ({#1 - {#Some v} - - #0 - (replacement for environment')} + ({[#1] {#Some v} + [#0] (replacement for environment')} (text#= k for))} environment)) @@ -999,8 +996,8 @@ ({... Jackpot! {#Parameter id} ({id' - ({#0 {#Parameter id'} - #1 {#Parameter ("lux i64 -" 2 id')}} + ({[#0] {#Parameter id'} + [#1] {#Parameter ("lux i64 -" 2 id')}} (self_id? id))} ("lux i64 -" ("lux i64 -" depth id) 0)) @@ -1072,18 +1069,19 @@ ..#seed _ ..#expected _ ..#location _ ..#extensions _ ..#scope_type_vars _ ..#eval _] (list#mix (function'' [scope verdict] - ({#1 #1 - _ ({[..#name _ ..#inner _ ..#captured _ - ..#locals [..#counter _ - ..#mappings locals]] - (list#mix (function'' [local verdict] - ({[local _] - ({#1 #1 _ ("lux text =" ..quantification_level local)} - verdict)} - local)) - #0 - locals)} - scope)} + ({[#1] #1 + _ ({[..#name _ ..#inner _ ..#captured _ + ..#locals [..#counter _ + ..#mappings locals]] + (list#mix (function'' [local verdict] + ({[local _] + ({[#1] #1 + _ ("lux text =" ..quantification_level local)} + verdict)} + local)) + #0 + locals)} + scope)} verdict)) #0 scopes)} @@ -1094,8 +1092,8 @@ {#Item body {#End}}} {#Right [lux {#Item ({raw - ({#1 raw - #0 (..quantified raw)} + ({[#1] raw + [#0] (..quantified raw)} (initialized_quantification? lux))} ({{#End} body @@ -1126,8 +1124,8 @@ {#Item body {#End}}} {#Right [lux {#Item ({raw - ({#1 raw - #0 (..quantified raw)} + ({[#1] raw + [#0] (..quantified raw)} (initialized_quantification? lux))} ({{#End} body @@ -1301,8 +1299,8 @@ #0 {#Item x xs'} - ({#1 #1 - #0 (any? p xs')} + ({[#1] #1 + [#0] (any? p xs')} (p x))} xs)) @@ -1561,40 +1559,38 @@ (def:''' .private (spliced replace? untemplated elems) (-> Bit (-> Code ($' Meta Code)) ($' List Code) ($' Meta Code)) - ({#1 - ({{#End} - (in_meta |#End|) - - {#Item lastI inits} - (do meta_monad - [lastO ({[_ {#Form {#Item [[_ {#Symbol ["" "~+"]}] {#Item [spliced {#End}]}]}}] - (in (:List spliced)) + ({[#1] ({{#End} + (in_meta |#End|) + + {#Item lastI inits} + (do meta_monad + [lastO ({[_ {#Form {#Item [[_ {#Symbol ["" "~+"]}] {#Item [spliced {#End}]}]}}] + (in (:List spliced)) - _ - (do meta_monad - [lastO (untemplated lastI)] - (in (:List (|#Item| lastO |#End|))))} - lastI)] - (monad#mix meta_monad - (function' [leftI rightO] - ({[_ {#Form {#Item [[_ {#Symbol ["" "~+"]}] {#Item [spliced {#End}]}]}}] - (let' [g!in-module (form$ (list (text$ "lux in-module") - (text$ ..prelude_module) - (symbol$ [..prelude_module "list#composite"])))] - (in (form$ (list g!in-module (:List spliced) rightO)))) - - _ - (do meta_monad - [leftO (untemplated leftI)] - (in (|#Item| leftO rightO)))} - leftI)) - lastO - inits))} - (list#reversed elems)) - #0 - (do meta_monad - [=elems (monad#each meta_monad untemplated elems)] - (in (untemplated_list =elems)))} + _ + (do meta_monad + [lastO (untemplated lastI)] + (in (:List (|#Item| lastO |#End|))))} + lastI)] + (monad#mix meta_monad + (function' [leftI rightO] + ({[_ {#Form {#Item [[_ {#Symbol ["" "~+"]}] {#Item [spliced {#End}]}]}}] + (let' [g!in-module (form$ (list (text$ "lux in-module") + (text$ ..prelude_module) + (symbol$ [..prelude_module "list#composite"])))] + (in (form$ (list g!in-module (:List spliced) rightO)))) + + _ + (do meta_monad + [leftO (untemplated leftI)] + (in (|#Item| leftO rightO)))} + leftI)) + lastO + inits))} + (list#reversed elems)) + [#0] (do meta_monad + [=elems (monad#each meta_monad untemplated elems)] + (in (untemplated_list =elems)))} replace?)) (def:''' .private (untemplated_text value) @@ -1925,27 +1921,24 @@ (def:''' .private (digit::format digit) (-> Nat Text) - ({0 "0" - 1 "1" 2 "2" 3 "3" - 4 "4" 5 "5" 6 "6" - 7 "7" 8 "8" 9 "9" - _ ("lux io error" "@digit::format Undefined behavior.")} + ({[0] "0" + [1] "1" [2] "2" [3] "3" + [4] "4" [5] "5" [6] "6" + [7] "7" [8] "8" [9] "9" + _ ("lux io error" "@digit::format Undefined behavior.")} digit)) (def:''' .private (nat#encoded value) (-> Nat Text) - ({0 - "0" - - _ - (let' [loop ("lux type check" (-> Nat Text Text) - (function' again [input output] - (if ("lux i64 =" 0 input) - output - (again (n// 10 input) - (text#composite (|> input (n/% 10) digit::format) - output)))))] - (loop value ""))} + ({[0] "0" + _ (let' [loop ("lux type check" (-> Nat Text Text) + (function' again [input output] + (if ("lux i64 =" 0 input) + output + (again (n// 10 input) + (text#composite (|> input (n/% 10) digit::format) + output)))))] + (loop value ""))} value)) (def:''' .private (int#abs value) diff --git a/stdlib/source/library/lux/control/parser/analysis.lux b/stdlib/source/library/lux/control/parser/analysis.lux index a59bbfe13..73889de68 100644 --- a/stdlib/source/library/lux/control/parser/analysis.lux +++ b/stdlib/source/library/lux/control/parser/analysis.lux @@ -1,6 +1,6 @@ (.using [library - [lux {"-" Tuple Variant nat int rev local} + [lux {"-" nat int rev local} [abstract [monad {"+" do}]] [control @@ -30,7 +30,8 @@ [variable {"+" }]] [language [lux - ["/" analysis {"+" Variant Tuple Environment Analysis}]]]]]]] + ["/" analysis {"+" Environment Analysis} + ["[1][0]" composite]]]]]]]] ["[0]" //]) (def: (remaining_inputs asts) diff --git a/stdlib/source/library/lux/control/parser/synthesis.lux b/stdlib/source/library/lux/control/parser/synthesis.lux index 473d6371b..894f7da68 100644 --- a/stdlib/source/library/lux/control/parser/synthesis.lux +++ b/stdlib/source/library/lux/control/parser/synthesis.lux @@ -1,32 +1,32 @@ (.using - [library - [lux {"-" Tuple Variant function loop i64 local} - [abstract - [monad {"+" do}]] - [control - ["[0]" try {"+" Try}] - ["[0]" exception {"+" exception:}]] - [data - ["[0]" bit] - ["[0]" text - ["%" format {"+" format}]]] - [math - [number - ["n" nat] - ["[0]" i64] - ["[0]" frac]]] - [meta - ["[0]" symbol]] - [tool - [compiler - [reference {"+" } - [variable {"+" Register}]] - [arity {"+" Arity}] - [language - [lux - [analysis {"+" Variant Tuple Environment}] - ["/" synthesis {"+" Synthesis Abstraction}]]]]]]] - ["[0]" //]) + [library + [lux {"-" function loop i64 local} + [abstract + [monad {"+" do}]] + [control + ["[0]" try {"+" Try}] + ["[0]" exception {"+" exception:}]] + [data + ["[0]" bit] + ["[0]" text + ["%" format {"+" format}]]] + [math + [number + ["n" nat] + ["[0]" i64] + ["[0]" frac]]] + [meta + ["[0]" symbol]] + [tool + [compiler + [reference {"+" } + [variable {"+" Register}]] + [arity {"+" Arity}] + [language + [lux + [analysis {"+" Environment}] + ["/" synthesis {"+" Synthesis Abstraction}]]]]]]] + ["[0]" //]) (exception: .public (cannot_parse [input (List Synthesis)]) (exception.report diff --git a/stdlib/source/library/lux/target/ruby.lux b/stdlib/source/library/lux/target/ruby.lux index c2d0517c9..df112f23f 100644 --- a/stdlib/source/library/lux/target/ruby.lux +++ b/stdlib/source/library/lux/target/ruby.lux @@ -43,18 +43,12 @@ (abstract: .public (Code brand) Text - (implementation: .public code_equivalence + (implementation: .public equivalence (All (_ brand) (Equivalence (Code brand))) (def: (= reference subject) (# text.equivalence = (:representation reference) (:representation subject)))) - (implementation: .public code_hash - (All (_ brand) (Hash (Code brand))) - - (def: &equivalence ..code_equivalence) - (def: hash (|>> :representation (# text.hash hash)))) - (def: .public manual (-> Text Code) (|>> :abstraction)) @@ -223,8 +217,18 @@ \n+ "end" ..statement_suffix)) (type: .public Block - [(List LVar) - Statement]) + (Record + [#parameters (List Var) + #body Statement])) + + (def: (block it) + (-> Block Text) + (|> (format (|> (value@ #parameters it) + (list#each (|>> :representation)) + (text.interposed ..input_separator) + (text.enclosed' "|")) + (..nested (:representation (value@ #body it)))) + (text.enclosed ["{" "}"]))) (def: .public (apply/* arguments block func) (-> (List Expression) (Maybe Block) Expression Computation) @@ -336,19 +340,18 @@ (..nested (:representation rescue))))) (text.interposed \n+))))) - (def: .public (catch expectation body!) - (-> Expression Statement Statement) + (def: .public (catch expectation block) + (-> Expression Block Expression) (<| :abstraction - ..control_structure - (format "catch(" (:representation expectation) ") do" - (..nested (:representation body!))))) + (format "catch(" (:representation expectation) ") " + (..block block)))) (def: .public (return value) (-> Expression Statement) (:abstraction (format "return " (:representation value) ..statement_suffix))) (def: .public (raise message) - (-> Expression Computation) + (-> Expression Expression) (:abstraction (format "raise " (:representation message)))) (template [ ] @@ -374,15 +377,9 @@ (text.enclosed ["(" ")"])) (..nested (:representation body!))))) - (def: .public (lambda name args body!) - (-> (Maybe LVar) (List Var) Statement Computation) - (let [proc (|> (format (|> args - (list#each (|>> :representation)) - (text.interposed ..input_separator) - (text.enclosed' "|")) - (..nested (:representation body!))) - (text.enclosed ["{" "}"]) - (format "lambda "))] + (def: .public (lambda name block) + (-> (Maybe LVar) Block Literal) + (let [proc (format "lambda " (..block block))] (|> (case name {.#None} proc @@ -491,12 +488,13 @@ [2 [["print"] ["alias_method"]]] - - [3 - [["print"]]] ) (def: .public throw/1 (-> Expression Statement) (|>> (..apply/1 (..local "throw")) ..statement)) + +(def: .public (throw/2 tag value) + (-> Expression Expression Statement) + (..statement (..apply/2 (..local "throw") tag value))) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/analysis.lux b/stdlib/source/library/lux/tool/compiler/language/lux/analysis.lux index 1b3f70f05..0096a259a 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/analysis.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/analysis.lux @@ -27,6 +27,7 @@ ["[0]" location]]]] ["[0]" / "_" ["[1][0]" primitive {"+" Primitive}] + ["[1][0]" composite {"+" Tuple Variant Composite}] [// [phase ["[0]" extension {"+" Extension}]] @@ -37,41 +38,6 @@ ["[0]" reference {"+" Reference} ["[0]" variable {"+" Register Variable}]]]]]) -(type: .public Tag - Nat) - -(type: .public (Variant a) - (Record - [#lefts Nat - #right? Bit - #value a])) - -(def: .public (tag lefts right?) - (-> Nat Bit Nat) - (if right? - (++ lefts) - lefts)) - -(def: (lefts tag right?) - (-> Nat Bit Nat) - (if right? - (-- tag) - tag)) - -(def: .public (choice options pick) - (-> Nat Nat [Nat Bit]) - (let [right? (n.= (-- options) pick)] - [(..lefts pick right?) - right?])) - -(type: .public (Tuple a) - (List a)) - -(type: .public (Composite a) - (.Variant - {#Variant (Variant a)} - {#Tuple (Tuple a)})) - (type: .public Pattern (Rec Pattern (.Variant @@ -107,42 +73,6 @@ (type: .public Match (Match' Analysis)) -(implementation: .public (composite_equivalence (^open "/#[0]")) - (All (_ a) (-> (Equivalence a) (Equivalence (Composite a)))) - - (def: (= reference sample) - (case [reference sample] - [{#Variant [reference_lefts reference_right? reference_value]} - {#Variant [sample_lefts sample_right? sample_value]}] - (and (n.= reference_lefts sample_lefts) - (bit#= reference_right? sample_right?) - (/#= reference_value sample_value)) - - [{#Tuple reference} {#Tuple sample}] - (# (list.equivalence /#=) = reference sample) - - _ - false))) - -(implementation: .public (composite_hash super) - (All (_ a) (-> (Hash a) (Hash (Composite a)))) - - (def: &equivalence - (..composite_equivalence (# super &equivalence))) - - (def: (hash value) - (case value - {#Variant [lefts right? value]} - ($_ n.* 2 - (# n.hash hash lefts) - (# bit.hash hash right?) - (# super hash value)) - - {#Tuple members} - ($_ n.* 3 - (# (list.hash super) hash members)) - ))) - (implementation: pattern_equivalence (Equivalence Pattern) @@ -152,7 +82,7 @@ (# /primitive.equivalence = reference sample) [{#Complex reference} {#Complex sample}] - (# (composite_equivalence =) = reference sample) + (# (/composite.equivalence =) = reference sample) [{#Bind reference} {#Bind sample}] (n.= reference sample) @@ -176,7 +106,7 @@ (# /primitive.equivalence = reference sample) [{#Structure reference} {#Structure sample}] - (# (composite_equivalence =) = reference sample) + (# (/composite.equivalence =) = reference sample) [{#Reference reference} {#Reference sample}] (# reference.equivalence = reference sample) @@ -230,10 +160,6 @@ (type: .public (Application c) [c (List c)]) -(def: (last? size tag) - (-> Nat Tag Bit) - (n.= (-- size) tag)) - (template: .public (no_op value) [(|> 1 {variable.#Local} @@ -279,8 +205,8 @@ content)])] - [pattern/variant {..#Variant}] - [pattern/tuple {..#Tuple}] + [pattern/variant {/composite.#Variant}] + [pattern/tuple {/composite.#Tuple}] ) (template [ ] @@ -289,8 +215,8 @@ {} content)])] - [variant ..#Variant] - [tuple ..#Tuple] + [variant /composite.#Variant] + [tuple /composite.#Tuple] ) (template: .public (pattern/unit) @@ -317,16 +243,8 @@ {#Primitive it} (/primitive.format it) - {#Structure structure} - (case structure - {#Variant [lefts right? value]} - (format "(" (%.nat lefts) " " (%.bit right?) " " (%analysis value) ")") - - {#Tuple members} - (|> members - (list#each %analysis) - (text.interposed " ") - (text.enclosed ["[" "]"]))) + {#Structure it} + (/composite.format %analysis it) {#Reference reference} (reference.format reference) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/analysis/composite.lux b/stdlib/source/library/lux/tool/compiler/language/lux/analysis/composite.lux new file mode 100644 index 000000000..c85f332f9 --- /dev/null +++ b/stdlib/source/library/lux/tool/compiler/language/lux/analysis/composite.lux @@ -0,0 +1,98 @@ +(.using + [library + [lux {"-" Tuple Variant} + [abstract + [equivalence {"+" Equivalence}] + [hash {"+" Hash}]] + [data + ["[0]" bit ("[1]#[0]" equivalence)] + ["[0]" text + ["%" format {"+" Format}]] + [collection + ["[0]" list ("[1]#[0]" functor)]]] + [math + [number + ["n" nat]]]]]) + +(type: .public (Variant a) + (Record + [#lefts Nat + #right? Bit + #value a])) + +(type: .public (Tuple a) + (List a)) + +(type: .public (Composite a) + (.Variant + {#Variant (Variant a)} + {#Tuple (Tuple a)})) + +(type: .public Tag + Nat) + +(def: .public (tag right? lefts) + (-> Bit Nat Tag) + (if right? + (++ lefts) + lefts)) + +(def: .public (lefts right? tag) + (-> Bit Tag Nat) + (if right? + (-- tag) + tag)) + +(def: .public (choice multiplicity pick) + (-> Nat Tag [Nat Bit]) + (let [right? (n.= (-- multiplicity) pick)] + [(..lefts right? pick) + right?])) + +(implementation: .public (equivalence (^open "/#[0]")) + (All (_ a) (-> (Equivalence a) (Equivalence (Composite a)))) + + (def: (= reference sample) + (case [reference sample] + [{#Variant [reference_lefts reference_right? reference_value]} + {#Variant [sample_lefts sample_right? sample_value]}] + (and (n.= reference_lefts sample_lefts) + (bit#= reference_right? sample_right?) + (/#= reference_value sample_value)) + + [{#Tuple reference} {#Tuple sample}] + (# (list.equivalence /#=) = reference sample) + + _ + false))) + +(implementation: .public (hash super) + (All (_ a) (-> (Hash a) (Hash (Composite a)))) + + (def: &equivalence + (..equivalence (# super &equivalence))) + + (def: (hash value) + (case value + {#Variant [lefts right? value]} + ($_ n.* 2 + (# n.hash hash lefts) + (# bit.hash hash right?) + (# super hash value)) + + {#Tuple members} + ($_ n.* 3 + (# (list.hash super) hash members)) + ))) + +(def: .public (format %it it) + (All (_ a) (-> (Format a) (Format (Composite a)))) + (case it + {#Variant [lefts right? it]} + (%.format "{" (%.nat lefts) " " (%.bit right?) " " (%it it) "}") + + {#Tuple it} + (|> it + (list#each %it) + (text.interposed " ") + (text.enclosed ["[" "]"])))) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/case.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/case.lux index c2ae6155d..c6e389f6a 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/case.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/case.lux @@ -31,7 +31,8 @@ ["[1][0]" extension] [// ["/" analysis {"+" Pattern Analysis Operation Phase} - ["[1][0]" primitive]] + ["[1][0]" primitive] + ["[1][0]" composite]] [/// ["[1]" phase]]]]]]) @@ -285,7 +286,7 @@ (let [flat_sum (type.flat_variant inputT') size_sum (list.size flat_sum) num_cases (maybe.else size_sum num_tags) - idx (/.tag lefts right?)] + idx (/composite.tag right? lefts)] (.case (list.item idx flat_sum) (^multi {.#Some caseT} (n.< num_cases idx)) @@ -322,7 +323,7 @@ [idx group variantT] (///extension.lifted (meta.tag tag)) _ (//type.with_env (check.check inputT variantT)) - .let [[lefts right?] (/.choice (list.size group) idx)]] + .let [[lefts right?] (/composite.choice (list.size group) idx)]] (analyse_pattern {.#Some (list.size group)} inputT (` {(~ (code.nat lefts)) (~ (code.bit right?)) (~+ values)}) next))) _ diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/case/coverage.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/case/coverage.lux index fd07b53b5..9306b1c20 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/case/coverage.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/case/coverage.lux @@ -25,7 +25,8 @@ ["[0]" //// "_" [// ["/" analysis {"+" Pattern Operation} - ["[1][0]" primitive]] + ["[1][0]" primitive] + ["[1][0]" composite]] [/// ["[1]" phase ("[1]#[0]" monad)]]]]) @@ -138,7 +139,7 @@ ... Tuple patterns can be exhaustive if there is exhaustiveness for all of ... their sub-patterns. - {/.#Complex {/.#Tuple membersP+}} + {/.#Complex {/composite.#Tuple membersP+}} (case (list.reversed membersP+) (^or {.#End} {.#Item _ {.#End}}) (/.except ..invalid_tuple_pattern []) @@ -160,7 +161,7 @@ ... Variant patterns can be shown to be exhaustive if all the possible ... cases are handled exhaustively. - {/.#Complex {/.#Variant [lefts right? value]}} + {/.#Complex {/composite.#Variant [lefts right? value]}} (do ////.monad [value_coverage (determine value) .let [idx (if right? diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/inference.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/inference.lux index d71ad7c71..6c94f14bd 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/inference.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/inference.lux @@ -22,7 +22,8 @@ ["/[1]" // "_" ["[1][0]" extension] [// - ["/" analysis {"+" Tag Analysis Operation Phase}] + ["/" analysis {"+" Analysis Operation Phase} + [composite {"+" Tag}]] [/// ["[1]" phase ("[1]#[0]" monad)] [meta @@ -248,7 +249,7 @@ ... Turns a variant type into the kind of function type suitable for inference. (def: .public (variant tag expected_size inferT) - (-> Nat Nat Type (Operation Type)) + (-> Tag Nat Type (Operation Type)) (loop [depth 0 currentT inferT] (case currentT diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/structure.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/structure.lux index 3ba31d089..54eca8afb 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/structure.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/structure.lux @@ -32,7 +32,8 @@ ["/[1]" // "_" ["[1][0]" extension] [// - ["/" analysis {"+" Tag Analysis Operation Phase}] + ["/" analysis {"+" Analysis Operation Phase} + ["[1][0]" composite {"+" Tag}]] [/// ["[1]" phase] [meta @@ -108,7 +109,7 @@ (def: .public (sum analyse lefts right? archive) (-> Phase Nat Bit Phase) - (let [tag (/.tag lefts right?)] + (let [tag (/composite.tag right? lefts)] (function (again valueC) (do [! ///.monad] [expectedT (///extension.lifted meta.expected_type) @@ -289,7 +290,7 @@ [tag (///extension.lifted (meta.normal tag)) [idx group variantT] (///extension.lifted (meta.tag tag)) .let [case_size (list.size group) - [lefts right?] (/.choice case_size idx)] + [lefts right?] (/composite.choice case_size idx)] expectedT (///extension.lifted meta.expected_type)] (case expectedT {.#Var _} diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/analysis/jvm.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/analysis/jvm.lux index cfee738e6..49a9758bd 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/analysis/jvm.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/analysis/jvm.lux @@ -1,62 +1,63 @@ (.using - [library - [lux {"-" Type Module Primitive type char int} - ["[0]" ffi {"+" import:}] - ["[0]" meta] - [abstract - ["[0]" monad {"+" do}]] - [control - pipe - ["[0]" maybe] - ["[0]" try {"+" Try} ("[1]#[0]" monad)] - ["[0]" exception {"+" exception:}] - ["<>" parser - ["<[0]>" code {"+" Parser}] - ["<[0]>" text]]] - [data - ["[0]" product] - ["[0]" text ("[1]#[0]" equivalence) - ["%" format {"+" format}]] - [collection - ["[0]" list ("[1]#[0]" mix monad monoid)] - ["[0]" array] - ["[0]" dictionary {"+" Dictionary}]]] - [math - [number - ["n" nat]]] - [target - ["[0]" jvm "_" - ["[0]!" reflection] - [encoding - [name {"+" External}]] - ["[1]" type {"+" Type Argument Typed} ("[1]#[0]" equivalence) - ["[0]" category {"+" Void Value' Value Return' Return Primitive Object Class Array Var Parameter Method}] - ["[0]" box] - ["[0]" reflection] - ["[0]" descriptor] - ["[0]" signature] - ["[1]_[0]" parser] - ["[1]_[0]" alias {"+" Aliasing}] - ["[0]T" lux {"+" Mapping}]]]] - ["[0]" type - ["[0]" check {"+" Check} ("[1]#[0]" monad)]]]] - ["[0]" // "_" - ["[1][0]" lux {"+" custom}] - ["/[1]" // - ["[1][0]" bundle] + [library + [lux {"-" Type Module Primitive type char int} + ["[0]" ffi {"+" import:}] + ["[0]" meta] + [abstract + ["[0]" monad {"+" do}]] + [control + pipe + ["[0]" maybe] + ["[0]" try {"+" Try} ("[1]#[0]" monad)] + ["[0]" exception {"+" exception:}] + ["<>" parser + ["<[0]>" code {"+" Parser}] + ["<[0]>" text]]] + [data + ["[0]" product] + ["[0]" text ("[1]#[0]" equivalence) + ["%" format {"+" format}]] + [collection + ["[0]" list ("[1]#[0]" mix monad monoid)] + ["[0]" array] + ["[0]" dictionary {"+" Dictionary}]]] + [math + [number + ["n" nat]]] + [target + ["[0]" jvm "_" + ["[0]!" reflection] + [encoding + [name {"+" External}]] + ["[1]" type {"+" Type Argument Typed} ("[1]#[0]" equivalence) + ["[0]" category {"+" Void Value' Value Return' Return Primitive Object Class Array Var Parameter Method}] + ["[0]" box] + ["[0]" reflection] + ["[0]" descriptor] + ["[0]" signature] + ["[1]_[0]" parser] + ["[1]_[0]" alias {"+" Aliasing}] + ["[0]T" lux {"+" Mapping}]]]] + ["[0]" type + ["[0]" check {"+" Check} ("[1]#[0]" monad)]]]] + ["[0]" // "_" + ["[1][0]" lux {"+" custom}] + ["/[1]" // + ["[1][0]" bundle] + ["/[1]" // "_" + [analysis + ["[0]A" type] + ["[0]A" inference] + ["[0]" scope]] ["/[1]" // "_" - [analysis - ["[0]A" type] - ["[0]A" inference] - ["[0]" scope]] - ["/[1]" // "_" - ["[1][0]" analysis {"+" Analysis Operation Phase Handler Bundle}] - ["[1][0]" synthesis] - [/// - ["[0]" phase ("[1]#[0]" monad)] - [meta - [archive {"+" Archive} - [descriptor {"+" Module}]]]]]]]]) + ["[1][0]" analysis {"+" Analysis Operation Phase Handler Bundle} + ["[1]/[0]" composite]] + ["[1][0]" synthesis] + [/// + ["[0]" phase ("[1]#[0]" monad)] + [meta + [archive {"+" Archive} + [descriptor {"+" Module}]]]]]]]]) (import: java/lang/ClassLoader) @@ -1980,9 +1981,10 @@ {/////analysis.#Case (/////analysis.unit) [[/////analysis.#when {/////analysis.#Complex - {/////analysis.#Tuple (|> arity - list.indices - (list#each (|>> (n.+ 2) {/////analysis.#Bind})))}} + {/////analysis/composite.#Tuple + (|> arity + list.indices + (list#each (|>> (n.+ 2) {/////analysis.#Bind})))}} /////analysis.#then bodyA] diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/jvm/host.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/jvm/host.lux index 7c70c99ed..dd3816d77 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/jvm/host.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/jvm/host.lux @@ -7,20 +7,23 @@ ["[0]" maybe] ["[0]" exception {"+" exception:}] ["<>" parser - ["" text] - ["" synthesis {"+" Parser}]]] + ["<[0]>" text] + ["<[0]>" synthesis {"+" Parser}]]] [data ["[0]" product] ["[0]" text ("[1]#[0]" equivalence)] [collection - ["[0]" list ("[1]#[0]" monad monoid)] + ["[0]" list ("[1]#[0]" monad mix monoid)] ["[0]" dictionary {"+" Dictionary}] ["[0]" set {"+" Set}] ["[0]" sequence]] ["[0]" format "_" ["[1]" binary]]] + [macro + ["[0]" template]] [math [number + ["n" nat] ["[0]" i32]]] [target [jvm @@ -49,6 +52,7 @@ ["///" jvm ["[1][0]" runtime {"+" Operation Bundle Phase Handler}] ["[1][0]" reference] + ["[1][0]" value] [function [field [variable @@ -64,7 +68,7 @@ [/// ["[1]" phase] ["[1][0]" reference - ["[2][0]" variable {"+" Variable}]] + ["[2][0]" variable {"+" Variable Register}]] [meta ["[0]" archive {"+" Archive} ["[0]" artifact] @@ -322,7 +326,7 @@ (template [ ] [(def: .public (Parser (Type )) - (.then .text))] + (.then .text))] [var Var parser.var] [class category.Class parser.class] @@ -338,7 +342,7 @@ (def: .public object_array (Parser (Type Object)) (do <>.monad - [arrayJT (.then parser.array .text)] + [arrayJT (.then parser.array .text)] (case (parser.array? arrayJT) {.#Some elementJT} (case (parser.object? elementJT) @@ -354,7 +358,7 @@ (def: (primitive_array_length_handler jvm_primitive) (-> (Type Primitive) Handler) (..custom - [.any + [.any (function (_ extension_name generate archive arrayS) (do //////.monad [arrayG (generate archive arrayS)] @@ -366,7 +370,7 @@ (def: array::length::object Handler (..custom - [($_ <>.and ..object_array .any) + [($_ <>.and ..object_array .any) (function (_ extension_name generate archive [elementJT arrayS]) (do //////.monad [arrayG (generate archive arrayS)] @@ -378,7 +382,7 @@ (def: (new_primitive_array_handler jvm_primitive) (-> Primitive_Array_Type Handler) (..custom - [.any + [.any (function (_ extension_name generate archive [lengthS]) (do //////.monad [lengthG (generate archive lengthS)] @@ -389,7 +393,7 @@ (def: array::new::object Handler (..custom - [($_ <>.and ..object .any) + [($_ <>.and ..object .any) (function (_ extension_name generate archive [objectJT lengthS]) (do //////.monad [lengthG (generate archive lengthS)] @@ -400,7 +404,7 @@ (def: (read_primitive_array_handler jvm_primitive loadG) (-> (Type Primitive) (Bytecode Any) Handler) (..custom - [($_ <>.and .any .any) + [($_ <>.and .any .any) (function (_ extension_name generate archive [idxS arrayS]) (do //////.monad [arrayG (generate archive arrayS) @@ -414,7 +418,7 @@ (def: array::read::object Handler (..custom - [($_ <>.and ..object_array .any .any) + [($_ <>.and ..object_array .any .any) (function (_ extension_name generate archive [elementJT idxS arrayS]) (do //////.monad [arrayG (generate archive arrayS) @@ -428,7 +432,7 @@ (def: (write_primitive_array_handler jvm_primitive storeG) (-> (Type Primitive) (Bytecode Any) Handler) (..custom - [($_ <>.and .any .any .any) + [($_ <>.and .any .any .any) (function (_ extension_name generate archive [idxS valueS arrayS]) (do //////.monad [arrayG (generate archive arrayS) @@ -445,7 +449,7 @@ (def: array::write::object Handler (..custom - [($_ <>.and ..object_array .any .any .any) + [($_ <>.and ..object_array .any .any .any) (function (_ extension_name generate archive [elementJT idxS valueS arrayS]) (do //////.monad [arrayG (generate archive arrayS) @@ -549,7 +553,7 @@ (def: object::class Handler (..custom - [.text + [.text (function (_ extension_name generate archive [class]) (do //////.monad [] @@ -560,7 +564,7 @@ (def: object::instance? Handler (..custom - [($_ <>.and .text .any) + [($_ <>.and .text .any) (function (_ extension_name generate archive [class objectS]) (do //////.monad [objectG (generate archive objectS)] @@ -577,7 +581,7 @@ (def: object::cast Handler (..custom - [($_ <>.and .text .text .any) + [($_ <>.and .text .text .any) (function (_ extension_name generate archive [from to valueS]) (do //////.monad [valueG (generate archive valueS)] @@ -640,7 +644,7 @@ (def: get::static Handler (..custom - [($_ <>.and .text .text .text) + [($_ <>.and .text .text .text) (function (_ extension_name generate archive [class field unboxed]) (do //////.monad [.let [$class (type.class class (list))]] @@ -656,7 +660,7 @@ (def: put::static Handler (..custom - [($_ <>.and .text .text .text .any) + [($_ <>.and .text .text .text .any) (function (_ extension_name generate archive [class field unboxed valueS]) (do //////.monad [valueG (generate archive valueS) @@ -678,7 +682,7 @@ (def: get::virtual Handler (..custom - [($_ <>.and .text .text .text .any) + [($_ <>.and .text .text .text .any) (function (_ extension_name generate archive [class field unboxed objectS]) (do //////.monad [objectG (generate archive objectS) @@ -697,7 +701,7 @@ (def: put::virtual Handler (..custom - [($_ <>.and .text .text .text .any .any) + [($_ <>.and .text .text .text .any .any) (function (_ extension_name generate archive [class field unboxed valueS objectS]) (do //////.monad [valueG (generate archive valueS) @@ -724,7 +728,7 @@ (def: input (Parser Input) - (.tuple (<>.and ..value .any))) + (.tuple (<>.and ..value .any))) (def: (generate_input generate archive [valueT valueS]) (-> Phase Archive Input (Operation (Typed (Bytecode Any)))) @@ -751,7 +755,7 @@ (def: invoke::static Handler (..custom - [($_ <>.and ..class .text ..return (<>.some ..input)) + [($_ <>.and ..class .text ..return (<>.some ..input)) (function (_ extension_name generate archive [class method outputT inputsTS]) (do [! //////.monad] [inputsTG (monad.each ! (generate_input generate archive) inputsTS)] @@ -764,7 +768,7 @@ [(def: Handler (..custom - [($_ <>.and ..class .text ..return .any (<>.some ..input)) + [($_ <>.and ..class .text ..return .any (<>.some ..input)) (function (_ extension_name generate archive [class method outputT objectS inputsTS]) (do [! //////.monad] [objectG (generate archive objectS) @@ -817,36 +821,72 @@ (def: annotation_parameter (Parser (/.Annotation_Parameter Synthesis)) - (.tuple (<>.and .text .any))) + (.tuple (<>.and .text .any))) (def: annotation (Parser (/.Annotation Synthesis)) - (.tuple (<>.and .text (<>.some ..annotation_parameter)))) + (.tuple (<>.and .text (<>.some ..annotation_parameter)))) (def: argument (Parser Argument) - (.tuple (<>.and .text ..value))) + (.tuple (<>.and .text ..value))) + +(def: .public (hidden_method_body arity body) + (-> Nat Synthesis Synthesis) + (case [arity body] + (^or [0 _] + [1 _]) + body + + (^or [2 {//////synthesis.#Control {//////synthesis.#Branch {//////synthesis.#Let _ 2 hidden}}}] + [2 {//////synthesis.#Control {//////synthesis.#Branch {//////synthesis.#Exec _ hidden}}}]) + hidden + + [_ {//////synthesis.#Control {//////synthesis.#Branch {//////synthesis.#Case _ path}}}] + (loop [path (: Path path)] + (case path + (^or {//////synthesis.#Pop} + {//////synthesis.#Access _} + {//////synthesis.#Bind _} + {//////synthesis.#Bit_Fork _} + {//////synthesis.#I64_Fork _} + {//////synthesis.#F64_Fork _} + {//////synthesis.#Text_Fork _} + {//////synthesis.#Alt _}) + body + + {//////synthesis.#Seq _ next} + (again next) + + {//////synthesis.#Then hidden} + hidden)) + + _ + body)) (def: overriden_method_definition (Parser [(Environment Synthesis) (/.Overriden_Method Synthesis)]) - (.tuple (do <>.monad - [_ (.text! /.overriden_tag) - ownerT ..class - name .text - strict_fp? .bit - annotations (.tuple (<>.some ..annotation)) - vars (.tuple (<>.some ..var)) - self_name .text - arguments (.tuple (<>.some ..argument)) - returnT ..return - exceptionsT (.tuple (<>.some ..class)) - [environment body] (.function 1 - (.tuple .any))] - (in [environment - [ownerT name - strict_fp? annotations vars - self_name arguments returnT exceptionsT - body]])))) + (.tuple (do <>.monad + [_ (.text! /.overriden_tag) + ownerT ..class + name .text + strict_fp? .bit + annotations (.tuple (<>.some ..annotation)) + vars (.tuple (<>.some ..var)) + self_name .text + arguments (.tuple (<>.some ..argument)) + returnT ..return + exceptionsT (.tuple (<>.some ..class)) + [environment _ _ body] (<| (.function 1) + (.loop (<>.exactly 0 .any)) + .tuple + (<>.after .any) + .any)] + (in [environment + [ownerT name + strict_fp? annotations vars + self_name arguments returnT exceptionsT + (..hidden_method_body (list.size arguments) body)]])))) (def: (normalize_path normalize) (-> (-> Synthesis Synthesis) @@ -872,8 +912,11 @@ _ (undefined)))) +(type: Mapping + (Dictionary Synthesis Variable)) + (def: (normalize_method_body mapping) - (-> (Dictionary Variable Variable) Synthesis Synthesis) + (-> Mapping Synthesis Synthesis) (function (again body) (case body (^template [] @@ -890,7 +933,7 @@ (^ (//////synthesis.variable var)) (|> mapping - (dictionary.value var) + (dictionary.value body) (maybe.else var) //////synthesis.variable) @@ -916,16 +959,16 @@ (//////synthesis.loop/again (list#each again updatesS+)) (^ (//////synthesis.function/abstraction [environment arity bodyS])) - (//////synthesis.function/abstraction [(list#each (function (_ local) - (case local - (^ (//////synthesis.variable local)) + (//////synthesis.function/abstraction [(list#each (function (_ captured) + (case captured + (^ (//////synthesis.variable var)) (|> mapping - (dictionary.value local) - (maybe.else local) + (dictionary.value captured) + (maybe.else var) //////synthesis.variable) _ - local)) + captured)) environment) arity bodyS]) @@ -984,26 +1027,34 @@ {.#Left returnT} (case (type.primitive? returnT) {.#Left returnT} - ($_ _.composite - (_.checkcast returnT) - _.areturn) + (case (type.class? returnT) + {.#Some class_name} + ($_ _.composite + (_.checkcast returnT) + _.areturn) + + {.#None} + _.areturn) {.#Right returnT} - (cond (or (# type.equivalence = type.boolean returnT) - (# type.equivalence = type.byte returnT) - (# type.equivalence = type.short returnT) - (# type.equivalence = type.int returnT) - (# type.equivalence = type.char returnT)) - _.ireturn - - (# type.equivalence = type.long returnT) - _.lreturn - - (# type.equivalence = type.float returnT) - _.freturn - - ... (# type.equivalence = type.double returnT) - _.dreturn)))) + (template.let [(unwrap_primitive ) + [($_ _.composite + (///value.unwrap ) + )]] + (`` (cond (~~ (template [ ] + [(# type.equivalence = returnT) + (unwrap_primitive )] + + [_.ireturn type.boolean] + [_.ireturn type.byte] + [_.ireturn type.short] + [_.ireturn type.int] + [_.ireturn type.char] + [_.freturn type.float] + [_.lreturn type.long])) + + ... (# type.equivalence = type.double returnT) + (unwrap_primitive _.dreturn type.double))))))) (def: (method_dependencies archive method) (-> Archive (/.Overriden_Method Synthesis) (Operation (Set artifact.Dependency))) @@ -1027,15 +1078,124 @@ all_closure_dependencies all_method_dependencies))))) +(def: (prepare_argument lux_register argumentT jvm_register) + (-> Register (Type Value) Register [Register (Bytecode Any)]) + (case (type.primitive? argumentT) + {.#Left argumentT} + [(n.+ 1 jvm_register) + (if (n.= lux_register jvm_register) + (_#in []) + ($_ _.composite + (_.aload jvm_register) + (_.astore lux_register)))] + + {.#Right argumentT} + (template.let [(wrap_primitive ) + [[(n.+ jvm_register) + ($_ _.composite + ( jvm_register) + (///value.wrap ) + (_.astore lux_register))]]] + (`` (cond (~~ (template [ ] + [(# type.equivalence = argumentT) + (wrap_primitive )] + + [1 _.iload type.boolean] + [1 _.iload type.byte] + [1 _.iload type.short] + [1 _.iload type.int] + [1 _.iload type.char] + [1 _.fload type.float] + [2 _.lload type.long])) + + ... (# type.equivalence = type.double argumentT) + (wrap_primitive 2 _.dload type.double)))))) + +(def: .public (prepare_arguments offset types) + (-> Nat (List (Type Value)) (Bytecode Any)) + (|> types + list.enumeration + (list#mix (function (_ [lux_register type] [jvm_register before]) + (let [[jvm_register' after] (prepare_argument (n.+ offset lux_register) type jvm_register)] + [jvm_register' + ($_ _.composite + before + after)])) + (: [Register (Bytecode Any)] + [offset + (_#in [])])) + product.right)) + +(def: (normalized_method global_mapping [environment method]) + (-> Mapping [(Environment Synthesis) (/.Overriden_Method Synthesis)] + (/.Overriden_Method Synthesis)) + (let [[ownerT name strict_fp? annotations vars self_name arguments returnT exceptionsT body] method + local_mapping (|> environment + list.enumeration + (list#each (function (_ [foreign_id capture]) + [(//////synthesis.variable/foreign foreign_id) + (|> global_mapping + (dictionary.value capture) + maybe.trusted)])) + (dictionary.of_list //////synthesis.hash))] + [ownerT name + strict_fp? annotations vars + self_name arguments returnT exceptionsT + (normalize_method_body local_mapping body)])) + +(def: (total_environment overriden_methods) + (-> (List [(Environment Synthesis) (/.Overriden_Method Synthesis)]) + (List Synthesis)) + (|> overriden_methods + ... Get all the environments. + (list#each product.left) + ... Combine them. + list#conjoint + ... Remove duplicates. + (set.of_list //////synthesis.hash) + set.list)) + +(def: (global_mapping total_environment) + (-> (List Synthesis) Mapping) + (|> total_environment + ... Give them names as "foreign" variables. + list.enumeration + (list#each (function (_ [id capture]) + [capture {//////variable.#Foreign id}])) + (dictionary.of_list //////synthesis.hash))) + +(def: (method_definition phase archive artifact_id method) + (-> Phase Archive artifact.ID (/.Overriden_Method Synthesis) (Operation (Resource Method))) + (let [[ownerT name strict_fp? annotations vars self_name arguments returnT exceptionsT bodyS] method] + (do //////.monad + [bodyG (//////generation.with_context artifact_id + (phase archive bodyS)) + .let [argumentsT (list#each product.right arguments) + methodT (type.method [vars argumentsT returnT exceptionsT])]] + (in (method.method ($_ modifier#composite + method.public + method.final + (if strict_fp? + method.strict + modifier#identity)) + name + methodT + (list) + {.#Some ($_ _.composite + (prepare_arguments 1 argumentsT) + bodyG + (returnG returnT))}))))) + (def: class::anonymous Handler (..custom [($_ <>.and ..class - (.tuple (<>.some ..class)) - (.tuple (<>.some ..input)) - (.tuple (<>.some ..overriden_method_definition))) - (function (_ extension_name generate archive [super_class super_interfaces + (.tuple (<>.some ..class)) + (.tuple (<>.some ..input)) + (.tuple (<>.some ..overriden_method_definition))) + (function (_ extension_name generate archive [super_class + super_interfaces inputsTS overriden_methods]) (do [! //////.monad] @@ -1044,62 +1204,12 @@ .let [[module_id artifact_id] context anonymous_class_name (///runtime.class_name context) class (type.class anonymous_class_name (list)) - total_environment (|> overriden_methods - ... Get all the environments. - (list#each product.left) - ... Combine them. - list#conjoint - ... Remove duplicates. - (set.of_list //////synthesis.hash) - set.list) - global_mapping (|> total_environment - ... Give them names as "foreign" variables. - list.enumeration - (list#each (function (_ [id capture]) - [capture {//////variable.#Foreign id}])) - (dictionary.of_list //////synthesis.hash)) - normalized_methods (list#each (function (_ [environment - [ownerT name - strict_fp? annotations vars - self_name arguments returnT exceptionsT - body]]) - (let [local_mapping (|> environment - list.enumeration - (list#each (function (_ [foreign_id capture]) - [{//////variable.#Foreign foreign_id} - (|> global_mapping - (dictionary.value capture) - maybe.trusted)])) - (dictionary.of_list //////variable.hash))] - [ownerT name - strict_fp? annotations vars - self_name arguments returnT exceptionsT - (normalize_method_body local_mapping body)])) - overriden_methods)] + total_environment (..total_environment overriden_methods) + global_mapping (..global_mapping total_environment)] inputsTI (monad.each ! (generate_input generate archive) inputsTS) - method_definitions (monad.each ! (function (_ [ownerT name - strict_fp? annotations vars - self_name arguments returnT exceptionsT - bodyS]) - (do ! - [bodyG (//////generation.with_context artifact_id - (generate archive bodyS))] - (in (method.method ($_ modifier#composite - method.public - method.final - (if strict_fp? - method.strict - modifier#identity)) - name - (type.method [(list) - (list#each product.right arguments) - returnT - exceptionsT]) - (list) - {.#Some ($_ _.composite - bodyG - (returnG returnT))})))) - normalized_methods) + methods! (|> overriden_methods + (list#each (normalized_method global_mapping)) + (monad.each ! (method_definition generate archive artifact_id))) bytecode (<| (# ! each (format.result class.writer)) //////.lifted (class.class version.v6_0 ($_ modifier#composite class.public class.final) @@ -1108,10 +1218,11 @@ (list#each (|>> ..reflection name.internal) super_interfaces) (foreign.variables total_environment) (list& (..with_anonymous_init class total_environment super_class inputsTI) - method_definitions) + methods!) (sequence.sequence))) - _ (//////generation.execute! [anonymous_class_name bytecode]) - _ (//////generation.save! artifact_id {.#None} [anonymous_class_name bytecode])] + .let [artifact [anonymous_class_name bytecode]] + _ (//////generation.execute! artifact) + _ (//////generation.save! artifact_id {.#None} artifact)] (anonymous_instance generate archive class total_environment)))])) (def: bundle::class diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/ruby/common.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/ruby/common.lux index a65557eeb..2328c2f2a 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/ruby/common.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/ruby/common.lux @@ -88,7 +88,8 @@ (^template [ ] [(^ ( value)) ( statement expression archive value)]) - ([synthesis.branch/let //case.let!] + ([synthesis.branch/exec //case.exec!] + [synthesis.branch/let //case.let!] [synthesis.branch/if //case.if!] [synthesis.loop/scope //loop.scope!] [synthesis.loop/again //loop.again!]) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/structure.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/structure.lux index 5a1ec9ea6..7827fee86 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/structure.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/structure.lux @@ -21,8 +21,9 @@ ["[1][0]" runtime {"+" Operation Phase Generator}] ["[1][0]" primitive] ["///[1]" //// "_" - [analysis {"+" Variant Tuple}] ["[1][0]" synthesis {"+" Synthesis}] + [analysis + [composite {"+" Variant Tuple}]] [/// ["[0]" phase]]]]) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby.lux index 02170332c..ca563e3e1 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby.lux @@ -49,6 +49,7 @@ ([////synthesis.variant /structure.variant] [////synthesis.tuple /structure.tuple] + [////synthesis.branch/exec /case.exec] [////synthesis.branch/let /case.let] [////synthesis.branch/if /case.if] [////synthesis.branch/get /case.get] diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/case.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/case.lux index 766b4fa43..400e47cfb 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/case.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/case.lux @@ -1,6 +1,6 @@ (.using [library - [lux {"-" case let if symbol} + [lux {"-" case exec let if symbol} [abstract ["[0]" monad {"+" do}]] [control @@ -48,6 +48,24 @@ (-> Register LVar) (|>> (///reference.foreign //reference.system) :expected)) +(def: .public (exec expression archive [this that]) + (Generator [Synthesis Synthesis]) + (do ///////phase.monad + [this (expression archive this) + that (expression archive that)] + (in (|> (_.array (list this that)) + (_.item (_.int +1)))))) + +(def: .public (exec! statement expression archive [this that]) + (Generator! [Synthesis Synthesis]) + (do ///////phase.monad + [this (expression archive this) + that (statement expression archive that)] + (in ($_ _.then + (_.statement this) + that + )))) + (def: .public (let expression archive [valueS register bodyS]) (Generator [Synthesis Register Synthesis]) (do ///////phase.monad @@ -56,7 +74,7 @@ ... TODO: Find some way to do 'let' without paying the price of the closure. (in (|> bodyO _.return - (_.lambda {.#None} (list (..register register))) + [(list (..register register))] (_.lambda {.#None}) (_.apply_lambda/* (list valueO)))))) (def: .public (let! statement expression archive [valueS register bodyS]) @@ -217,8 +235,10 @@ ..peek)]) (again then))) {.#Item item})] - (in {.#Some (_.cond clauses - ..fail!)}))]) + (in {.#Some (list#mix (function (_ [when then] else) + (_.if when then else)) + ..fail! + clauses)}))]) ([/////synthesis.#I64_Fork (<| //primitive.i64 .int)] [/////synthesis.#F64_Fork (<| //primitive.f64)] [/////synthesis.#Text_Fork (<| //primitive.text)]) @@ -272,8 +292,10 @@ ..peek)]) (again then))) {.#Item item})] - (in (_.cond clauses - ..fail!)))]) + (in (list#mix (function (_ [when then] else) + (_.if when then else)) + ..fail! + clauses)))]) ([/////synthesis.#I64_Fork (<| //primitive.i64 .int)] [/////synthesis.#F64_Fork (<| //primitive.f64)] [/////synthesis.#Text_Fork (<| //primitive.text)]) @@ -356,5 +378,5 @@ (|> case (case! true statement expression archive) (# ///////phase.monad each - (|>> (_.lambda {.#None} (list)) + (|>> [(list)] (_.lambda {.#None}) (_.apply_lambda/* (list)))))) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/function.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/function.lux index ccad8ba2a..16dab0814 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/function.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/function.lux @@ -1,6 +1,6 @@ (.using [library - [lux {"-" Variant Tuple function} + [lux {"-" function} [abstract ["[0]" monad {"+" do}]] [data @@ -19,7 +19,7 @@ ["/[1]" // "_" ["[1][0]" reference] ["//[1]" /// "_" - [analysis {"+" Variant Tuple Environment Abstraction Application Analysis}] + [analysis {"+" Environment Abstraction Application Analysis}] [synthesis {"+" Synthesis}] ["[1][0]" generation {"+" Context}] ["//[1]" /// "_" @@ -52,12 +52,12 @@ _ [(_.set (list @self) (_.lambda {.#None} - (|> (list.enumeration inits) - (list#each (|>> product.left ..capture))) - (let [@self (_.local self)] - ($_ _.then - (_.set (list @self) function_definition) - (_.return @self))))) + [(|> (list.enumeration inits) + (list#each (|>> product.left ..capture))) + (let [@self (_.local self)] + ($_ _.then + (_.set (list @self) function_definition) + (_.return @self)))])) (_.apply_lambda/* inits @self)]))) (def: input @@ -91,29 +91,31 @@ initialize_self! (list.indices arity)) [declaration instatiation] (with_closure closureO+ function_name - (_.lambda {.#None} (list (_.variadic @curried)) - ($_ _.then - (_.set (list @num_args) (_.the "length" @curried)) - (_.cond (list [(|> @num_args (_.= arityO)) - (<| (_.then initialize!) - //loop.with_scope - body!)] - [(|> @num_args (_.> arityO)) - (let [slice (.function (_ from to) - (_.array_range from to @curried)) - arity_args (_.splat (slice (_.int +0) limitO)) - output_func_args (_.splat (slice arityO @num_args))] - (_.return (|> @self - (_.apply_lambda/* (list arity_args)) - (_.apply_lambda/* (list output_func_args)))))]) - ... (|> @num_args (_.< arityO)) - (let [@missing (_.local "missing")] - (_.return (_.lambda {.#None} (list (_.variadic @missing)) - (_.return (|> @self - (_.apply_lambda/* (list (_.splat (|> (_.array (list)) - (_.do "concat" (list @curried) {.#None}) - (_.do "concat" (list @missing) {.#None}))))))))))) - )))] + (_.lambda {.#None} + [(list (_.variadic @curried)) + ($_ _.then + (_.set (list @num_args) (_.the "length" @curried)) + (<| (_.if (|> @num_args (_.= arityO)) + (<| (_.then initialize!) + //loop.with_scope + body!)) + (_.if (|> @num_args (_.> arityO)) + (let [slice (.function (_ from to) + (_.array_range from to @curried)) + arity_args (_.splat (slice (_.int +0) limitO)) + output_func_args (_.splat (slice arityO @num_args))] + (_.return (|> @self + (_.apply_lambda/* (list arity_args)) + (_.apply_lambda/* (list output_func_args)))))) + ... (|> @num_args (_.< arityO)) + (let [@missing (_.local "missing")] + (_.return (_.lambda {.#None} + [(list (_.variadic @missing)) + (_.return (|> @self + (_.apply_lambda/* (list (_.splat (|> (_.array (list)) + (_.do "concat" (list @curried) {.#None}) + (_.do "concat" (list @missing) {.#None})))))))])))) + )]))] _ (/////generation.execute! declaration) _ (/////generation.save! function_artifact {.#None} declaration)] (in instatiation))) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/loop.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/loop.lux index b4dbf8248..9ccd0151e 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/loop.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/loop.lux @@ -77,7 +77,7 @@ (do [! ///////phase.monad] [body! (scope! statement expression archive [start initsS+ bodyS])] (in (|> body! - (_.lambda {.#None} (list)) + [(list)] (_.lambda {.#None}) (_.apply_lambda/* (list))))))) (def: .public (again! statement expression archive argsS+) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/runtime.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/runtime.lux index af0f3338c..d82d5e7ba 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/runtime.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/runtime.lux @@ -171,12 +171,12 @@ ($_ _.then (_.set (list last_index_right) (..last_index tuple)) (_.set (list right_index) (_.+ (_.int +1) lefts)) - (_.cond (list [(_.= last_index_right right_index) - (_.return (_.item right_index tuple))] - [(_.> last_index_right right_index) - ... Needs recursion. - ]) - (_.return (_.array_range right_index (..tuple_size tuple) tuple))) + (<| (_.if (_.= last_index_right right_index) + (_.return (_.item right_index tuple))) + (_.if (_.> last_index_right right_index) + ... Needs recursion. + ) + (_.return (_.array_range right_index (..tuple_size tuple) tuple))) ))))) (def: .public variant_tag_field "_lux_tag") @@ -219,24 +219,21 @@ (_.- (_.int +1)))) (_.set (list sum) actual##value))] (<| (_.while (_.bool true)) - (_.cond (list [(_.= expected##lefts actual##lefts) - (_.if (_.= expected##right? actual##right?) - (_.return actual##value) - mismatch!)] - - [(_.< expected##lefts actual##lefts) - (_.if (_.= ..unit actual##right?) - recur! - mismatch!)] - - [(_.= ..unit expected##right?) - (_.return (sum//make (|> actual##lefts - (_.- expected##lefts) - (_.- (_.int +1))) - actual##right? - actual##value))]) - - mismatch!)))) + (_.if (_.= expected##lefts actual##lefts) + (_.if (_.= expected##right? actual##right?) + (_.return actual##value) + mismatch!)) + (_.if (_.< expected##lefts actual##lefts) + (_.if (_.= ..unit actual##right?) + recur! + mismatch!)) + (_.if (_.= ..unit expected##right?) + (_.return (sum//make (|> actual##lefts + (_.- expected##lefts) + (_.- (_.int +1))) + actual##right? + actual##value))) + mismatch!))) (def: runtime//adt Statement diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/structure.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/structure.lux index 72c620827..27361d558 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/structure.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/structure.lux @@ -9,7 +9,8 @@ ["[1][0]" runtime {"+" Operation Phase Generator}] ["[1][0]" primitive] ["///[1]" //// "_" - [analysis {"+" Variant Tuple}] + [analysis + [composite {"+" Variant Tuple}]] ["[1][0]" synthesis {"+" Synthesis}] ["//[1]" /// "_" ["[1][0]" phase ("[1]#[0]" monad)]]]]) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis.lux index 68f3c22d4..e8aec1a83 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis.lux @@ -19,7 +19,8 @@ ["/[1]" // "_" ["/" synthesis {"+" Synthesis Phase}] ["[1][0]" analysis {"+" Analysis} - ["[2][0]" primitive]] + ["[2][0]" primitive] + ["[2][0]" composite]] [/// ["[0]" phase ("[1]#[0]" monad)] [reference {"+"} @@ -58,12 +59,12 @@ {///analysis.#Structure structure} (/.with_currying? false (case structure - {///analysis.#Variant variant} + {///composite.#Variant variant} (do phase.monad - [valueS (optimization' (value@ ///analysis.#value variant))] - (in (/.variant (with@ ///analysis.#value valueS variant)))) + [valueS (optimization' (value@ ///composite.#value variant))] + (in (/.variant (with@ ///composite.#value valueS variant)))) - {///analysis.#Tuple tuple} + {///composite.#Tuple tuple} (|> tuple (monad.each phase.monad optimization') (phase#each (|>> /.tuple))))) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/case.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/case.lux index 29d478d0b..10d71f730 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/case.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/case.lux @@ -22,7 +22,8 @@ [// ["/" synthesis {"+" Path Synthesis Operation Phase}] ["[1][0]" analysis {"+" Pattern Match Analysis} - ["[2][0]" primitive]] + ["[2][0]" primitive] + ["[2][0]" composite]] [/// ["[1]" phase ("[1]#[0]" monad)] ["[1][0]" reference @@ -63,7 +64,7 @@ /.with_new_local thenC) - {///analysis.#Complex {///analysis.#Variant [lefts right? value_pattern]}} + {///analysis.#Complex {///composite.#Variant [lefts right? value_pattern]}} (<| (///#each (|>> {/.#Seq {/.#Access {/.#Side (if right? {.#Right lefts} {.#Left lefts})}}})) @@ -71,7 +72,7 @@ (when> [(new> (not end?) [])] [(///#each ..clean_up)]) thenC) - {///analysis.#Complex {///analysis.#Tuple tuple}} + {///analysis.#Complex {///composite.#Tuple tuple}} (let [tuple::last (-- (list.size tuple))] (list#mix (function (_ [tuple::lefts tuple::member] nextC) (.case tuple::member @@ -192,7 +193,7 @@ ))) (def: (get patterns @selection) - (-> (///analysis.Tuple ///analysis.Pattern) Register (List /.Member)) + (-> (///composite.Tuple ///analysis.Pattern) Register (List /.Member)) (loop [lefts 0 patterns patterns] (with_expansions [ (as_is (list)) @@ -215,7 +216,7 @@ (list ) ) - {///analysis.#Complex {///analysis.#Tuple sub_patterns}} + {///analysis.#Complex {///composite.#Tuple sub_patterns}} (case (get sub_patterns @selection) {.#End} @@ -264,7 +265,7 @@ (.list)]]) (def: .public (synthesize_get synthesize archive input patterns @member) - (-> Phase Archive Synthesis (///analysis.Tuple ///analysis.Pattern) Register (Operation Synthesis)) + (-> Phase Archive Synthesis (///composite.Tuple ///analysis.Pattern) Register (Operation Synthesis)) (case (..get patterns @member) {.#End} (..synthesize_case synthesize archive input (!get patterns @member)) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/function.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/function.lux index e2380b282..5e171165e 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/function.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/function.lux @@ -19,7 +19,8 @@ ["[0]" // "_" ["[1][0]" loop {"+" Transform}] ["//[1]" /// "_" - ["[1][0]" analysis {"+" Environment Analysis}] + ["[1][0]" analysis {"+" Environment Analysis} + ["[1]/[0]" composite]] ["/" synthesis {"+" Path Abstraction Synthesis Operation Phase}] [/// [arity {"+" Arity}] @@ -143,12 +144,12 @@ (case expression {/.#Structure structure} (case structure - {////analysis.#Variant [lefts right? subS]} + {////analysis/composite.#Variant [lefts right? subS]} (|> subS (grow environment) (phase#each (|>> [lefts right?] /.variant))) - {////analysis.#Tuple membersS+} + {////analysis/composite.#Tuple membersS+} (|> membersS+ (monad.each phase.monad (grow environment)) (phase#each (|>> /.tuple)))) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/loop.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/loop.lux index 8b9273084..eea8ea951 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/loop.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/loop.lux @@ -12,7 +12,8 @@ [number ["n" nat]]]]] [//// - ["[0]" analysis {"+" Environment}] + ["[0]" analysis {"+" Environment} + ["[1]/[0]" composite]] ["/" synthesis {"+" Path Abstraction Synthesis}] [/// [arity {"+" Arity}] @@ -84,14 +85,14 @@ {/.#Structure structure} (case structure - {analysis.#Variant variant} + {analysis/composite.#Variant variant} (do maybe.monad - [value' (|> variant (value@ analysis.#value) (again false))] + [value' (|> variant (value@ analysis/composite.#value) (again false))] (in (|> variant - (with@ analysis.#value value') + (with@ analysis/composite.#value value') /.variant))) - {analysis.#Tuple tuple} + {analysis/composite.#Tuple tuple} (|> tuple (monad.each maybe.monad (again false)) (maybe#each (|>> /.tuple)))) @@ -190,11 +191,11 @@ matches (monad.each ! (function (_ match) (case match - (^ {/.#Structure {analysis.#Tuple (list when then)}}) + (^ {/.#Structure {analysis/composite.#Tuple (list when then)}}) (do ! [when (again false when) then (again return? then)] - (in {/.#Structure {analysis.#Tuple (list when then)}})) + (in {/.#Structure {analysis/composite.#Tuple (list when then)}})) _ (again false match))) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/variable.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/variable.lux index 375919eca..79321ad28 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/variable.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/variable.lux @@ -20,7 +20,8 @@ ["n" nat]]]]] [//// ["/" synthesis {"+" Path Synthesis}] - ["[0]" analysis] + ["[0]" analysis + ["[1]/[0]" composite]] [/// [arity {"+" Arity}] ["[0]" reference @@ -112,11 +113,11 @@ {/.#Structure structure} {/.#Structure (case structure - {analysis.#Variant [lefts right value]} - {analysis.#Variant [lefts right (again value)]} + {analysis/composite.#Variant [lefts right value]} + {analysis/composite.#Variant [lefts right (again value)]} - {analysis.#Tuple tuple} - {analysis.#Tuple (list#each again tuple)})} + {analysis/composite.#Tuple tuple} + {analysis/composite.#Tuple (list#each again tuple)})} {/.#Reference reference} (case reference @@ -335,17 +336,17 @@ {/.#Structure structure} (case structure - {analysis.#Variant [lefts right value]} + {analysis/composite.#Variant [lefts right value]} (do try.monad [[redundancy value] (optimization' [redundancy value])] (in [redundancy - {/.#Structure {analysis.#Variant [lefts right value]}}])) + {/.#Structure {analysis/composite.#Variant [lefts right value]}}])) - {analysis.#Tuple tuple} + {analysis/composite.#Tuple tuple} (do try.monad [[redundancy tuple] (..list_optimization optimization' [redundancy tuple])] (in [redundancy - {/.#Structure {analysis.#Tuple tuple}}]))) + {/.#Structure {analysis/composite.#Tuple tuple}}]))) {/.#Reference reference} (case reference diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/synthesis.lux b/stdlib/source/library/lux/tool/compiler/language/lux/synthesis.lux index c27fa9a81..2bd2bd22e 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/synthesis.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/synthesis.lux @@ -1,38 +1,39 @@ (.using - [library - [lux {"-" Primitive Scope i64} - [abstract - [monad {"+" do}] - [equivalence {"+" Equivalence}] - [hash {"+" Hash}]] - [control - [pipe {"+" case>}] - ["[0]" maybe] - ["[0]" exception {"+" exception:}]] - [data - ["[0]" sum] - ["[0]" product] - ["[0]" bit ("[1]#[0]" equivalence)] - ["[0]" text ("[1]#[0]" equivalence) - ["%" format {"+" Format format}]] - [collection - ["[0]" list ("[1]#[0]" functor)] - ["[0]" dictionary {"+" Dictionary}]]] - [math - [number - ["[0]" i64] - ["n" nat] - ["i" int] - ["f" frac]]]]] - [// - ["[0]" analysis {"+" Environment Composite Analysis}] - [phase - ["[0]" extension {"+" Extension}]] - [/// - [arity {"+" Arity}] - ["[0]" phase] - ["[0]" reference {"+" Reference} - ["[0]" variable {"+" Register Variable}]]]]) + [library + [lux {"-" Primitive Scope i64} + [abstract + [monad {"+" do}] + [equivalence {"+" Equivalence}] + [hash {"+" Hash}]] + [control + [pipe {"+" case>}] + ["[0]" maybe] + ["[0]" exception {"+" exception:}]] + [data + ["[0]" sum] + ["[0]" product] + ["[0]" bit ("[1]#[0]" equivalence)] + ["[0]" text ("[1]#[0]" equivalence) + ["%" format {"+" Format format}]] + [collection + ["[0]" list ("[1]#[0]" functor)] + ["[0]" dictionary {"+" Dictionary}]]] + [math + [number + ["[0]" i64] + ["n" nat] + ["i" int] + ["f" frac]]]]] + [// + ["[0]" analysis {"+" Environment Analysis} + ["[1]/[0]" composite {"+" Composite}]] + [phase + ["[0]" extension {"+" Extension}]] + [/// + [arity {"+" Arity}] + ["[0]" phase] + ["[0]" reference {"+" Reference} + ["[0]" variable {"+" Register Variable}]]]]) (type: .public Resolver (Dictionary Variable Variable)) @@ -238,8 +239,8 @@ {} content)])] - [variant analysis.#Variant] - [tuple analysis.#Tuple] + [variant analysis/composite.#Variant] + [tuple analysis/composite.#Tuple] ) (template [ ] @@ -350,12 +351,12 @@ {#Structure structure} (case structure - {analysis.#Variant [lefts right? content]} + {analysis/composite.#Variant [lefts right? content]} (|> (%synthesis content) (format (%.nat lefts) " " (%.bit right?) " ") (text.enclosed ["{" "}"])) - {analysis.#Tuple members} + {analysis/composite.#Tuple members} (|> members (list#each %synthesis) (text.interposed " ") @@ -775,7 +776,7 @@ [[{ reference'} { sample'}] (# = reference' sample')]) ([#Primitive ..primitive_equivalence] - [#Structure (analysis.composite_equivalence =)] + [#Structure (analysis/composite.equivalence =)] [#Reference reference.equivalence] [#Control (control_equivalence =)] [#Extension (extension.equivalence =)]) @@ -799,7 +800,7 @@ [{ value} (# hash value)]) ([#Primitive ..primitive_hash] - [#Structure (analysis.composite_hash again_hash)] + [#Structure (analysis/composite.hash again_hash)] [#Reference reference.hash] [#Control (..control_hash again_hash)] [#Extension (extension.hash again_hash)]))))) diff --git a/stdlib/source/library/lux/tool/compiler/meta/archive/dependency.lux b/stdlib/source/library/lux/tool/compiler/meta/archive/dependency.lux index f3e1c6d83..7b0065dc4 100644 --- a/stdlib/source/library/lux/tool/compiler/meta/archive/dependency.lux +++ b/stdlib/source/library/lux/tool/compiler/meta/archive/dependency.lux @@ -23,7 +23,8 @@ ["[0]" reference {"+" Constant}] [language [lux - ["[0]" analysis] + ["[0]" analysis + ["[1]/[0]" composite]] ["[0]" synthesis {"+" Synthesis Path}] ["[0]" generation {"+" Context Operation}]]] [meta @@ -79,12 +80,12 @@ {synthesis.#Structure value} (case value - {analysis.#Variant value} + {analysis/composite.#Variant value} (|> value - (value@ analysis.#value) + (value@ analysis/composite.#value) references) - {analysis.#Tuple value} + {analysis/composite.#Tuple value} (|> value (list#each references) list#conjoint)) diff --git a/stdlib/source/test/lux/target/ruby.lux b/stdlib/source/test/lux/target/ruby.lux index 86a817703..7723cd776 100644 --- a/stdlib/source/test/lux/target/ruby.lux +++ b/stdlib/source/test/lux/target/ruby.lux @@ -5,7 +5,9 @@ ["[0]" ffi] [abstract [monad {"+" do}] - ["[0]" predicate]] + ["[0]" predicate] + [\\specification + ["$[0]" equivalence]]] [control ["[0]" maybe ("[1]#[0]" functor)] ["[0]" try {"+" Try} ("[1]#[0]" functor)]] @@ -306,7 +308,7 @@ (|> ($_ /.then (/.set (list $foreign) (/.+ $foreign $foreign)) (/.return $foreign)) - (/.lambda {.#None} (list $foreign)) + [(list $foreign)] (/.lambda {.#None}) (/.apply_lambda/* (list (/.float float/0)))))) )) (_.cover [/.Access] @@ -316,7 +318,7 @@ (/.set (list $foreign) (/.array (list $foreign))) (/.set (list @) (/.+ @ @)) (/.return @)) - (/.lambda {.#None} (list $foreign)) + [(list $foreign)] (/.lambda {.#None}) (/.apply_lambda/* (list (/.float float/0)))))) (expression (|>> (:as Frac) (f.= (f.+ float/0 float/0))) (let [@ (/.item field $foreign)] @@ -324,7 +326,7 @@ (/.set (list $foreign) (/.hash (list [field $foreign]))) (/.set (list @) (/.+ @ @)) (/.return @)) - (/.lambda {.#None} (list $foreign)) + [(list $foreign)] (/.lambda {.#None}) (/.apply_lambda/* (list (/.float float/0)))))) )) ))) @@ -359,7 +361,7 @@ (/.set (list $inner_index) (/.+ (/.int +1) $inner_index)) )) (/.return $output)) - (/.lambda {.#None} (list $input)) + [(list $input)] (/.lambda {.#None}) (/.apply_lambda/* (list (/.int input))))))) (_.cover [/.next] (let [expected (i.* (.int (n.- expected_inner_iterations full_inner_iterations)) input)] @@ -375,7 +377,7 @@ (/.set (list $output) (/.+ $input $output)) )) (/.return $output)) - (/.lambda {.#None} (list $input)) + [(list $input)] (/.lambda {.#None}) (/.apply_lambda/* (list (/.int input))))))) ))) @@ -400,7 +402,7 @@ (/.set (list $index) (/.+ (/.int +1) $index)) )) (/.return $output)) - (/.lambda {.#None} (list $input)) + [(list $input)] (/.lambda {.#None}) (/.apply_lambda/* (list (/.int input)))))) (_.cover [/.for_in] (expression (|>> (:as Int) (i.= expected)) @@ -409,7 +411,7 @@ (/.for_in $index (/.array (list.repeated iterations (/.int input))) (/.set (list $output) (/.+ $index $output))) (/.return $output)) - (/.lambda {.#None} (list $input)) + [(list $input)] (/.lambda {.#None}) (/.apply_lambda/* (list (/.int input)))))) ..test|label ))) @@ -420,13 +422,19 @@ [expected random.safe_frac dummy (random.only (|>> (f.= expected) not) random.safe_frac) - $ex (# ! each /.local (random.ascii/lower 10))] + $ex (# ! each /.local (random.ascii/lower 10)) + + expected_tag random.int + dummy_tag (random.only (|>> (i.= expected_tag) not) + random.int) + .let [expected_tag (/.int expected_tag) + dummy_tag (/.int dummy_tag)]] ($_ _.and (_.cover [/.begin] (expression (|>> (:as Frac) (f.= expected)) (|> (/.begin (/.return (/.float expected)) (list [(list) $ex (/.return (/.float dummy))])) - (/.lambda {.#None} (list)) + [(list)] (/.lambda {.#None}) (/.apply_lambda/* (list))))) (_.cover [/.Rescue /.throw/1] (expression (|>> (:as Frac) (f.= expected)) @@ -434,8 +442,37 @@ (/.throw/1 (/.string "")) (/.return (/.float dummy))) (list [(list) $ex (/.return (/.float expected))])) - (/.lambda {.#None} (list)) + [(list)] (/.lambda {.#None}) + (/.apply_lambda/* (list))))) + (_.cover [/.raise] + (expression (|>> (:as Frac) (f.= expected)) + (|> (/.begin ($_ /.then + (/.statement (/.raise (/.string ""))) + (/.return (/.float dummy))) + (list [(list) $ex (/.return (/.float expected))])) + [(list)] (/.lambda {.#None}) (/.apply_lambda/* (list))))) + (_.cover [/.catch /.throw/2] + (and (expression (|>> (:as Frac) (f.= expected)) + (<| (/.apply_lambda/* (list)) + (/.lambda {.#None}) [(list)] + /.return + (/.catch expected_tag) [(list)] + (/.throw/2 expected_tag (/.float expected)))) + (expression (|>> (:as Frac) (f.= expected)) + (<| (/.apply_lambda/* (list)) + (/.lambda {.#None}) [(list)] + /.return + (/.catch expected_tag) [(list)] + /.statement (/.catch dummy_tag) [(list)] + (/.throw/2 expected_tag (/.float expected)))) + (expression (|>> (:as Frac) (f.= expected)) + (<| (/.apply_lambda/* (list)) + (/.lambda {.#None}) [(list)] + /.return + (/.catch dummy_tag) [(list)] + /.statement (/.catch expected_tag) [(list)] + (/.throw/2 expected_tag (/.float expected)))))) ))) (def: test|function @@ -456,18 +493,18 @@ (_.cover [/.lambda /.return] (and (expression (|>> (:as Frac) (f.= float/0)) (|> (/.return (/.float float/0)) - (/.lambda {.#None} (list)) + [(list)] (/.lambda {.#None}) (/.apply_lambda/* (list)))) (expression (|>> (:as Frac) f.nat (n.= iterations)) - (|> (/.lambda {.#Some $self} (list $arg/0) - (/.return (/.? (/.< (/.int (.int iterations)) $arg/0) - (/.apply_lambda/* (list (/.+ (/.int +1) $arg/0)) $self) - $arg/0))) + (|> (/.return (/.? (/.< (/.int (.int iterations)) $arg/0) + (/.apply_lambda/* (list (/.+ (/.int +1) $arg/0)) $self) + $arg/0)) + [(list $arg/0)] (/.lambda {.#Some $self}) (/.apply_lambda/* (list (/.int +0))))))) (_.cover [/.apply_lambda/*] (expression (|>> (:as Frac) (f.= ($_ f.+ float/0 float/1 float/2))) (|> (/.return ($_ /.+ $arg/0 $arg/1 $arg/2)) - (/.lambda {.#None} (list $arg/0 $arg/1 $arg/2)) + [(list $arg/0 $arg/1 $arg/2)] (/.lambda {.#None}) (/.apply_lambda/* (list (/.float float/0) (/.float float/1) (/.float float/2)))))) (_.cover [/.function] (expression (|>> (:as Frac) f.nat (n.= iterations)) @@ -477,7 +514,7 @@ (/.apply/1 $self (/.+ (/.int +1) $arg/0)) $arg/0))) (/.return (/.apply/1 $self (/.int +0)))) - (/.lambda {.#None} (list)) + [(list)] (/.lambda {.#None}) (/.apply_lambda/* (list))))) (_.cover [/.apply/1] (expression (|>> (:as Frac) (f.= float/0)) @@ -485,7 +522,7 @@ (/.function $self (list $arg/0) (/.return $arg/0)) (/.return (/.apply/1 $self (/.float float/0)))) - (/.lambda {.#None} (list)) + [(list)] (/.lambda {.#None}) (/.apply_lambda/* (list))))) (_.cover [/.apply/2] (expression (|>> (:as Frac) (f.= ($_ f.+ float/0 float/1))) @@ -493,15 +530,7 @@ (/.function $self (list $arg/0 $arg/1) (/.return ($_ /.+ $arg/0 $arg/1))) (/.return (/.apply/2 $self (/.float float/0) (/.float float/1)))) - (/.lambda {.#None} (list)) - (/.apply_lambda/* (list))))) - (_.cover [/.apply/3] - (expression (|>> (:as Frac) (f.= ($_ f.+ float/0 float/1 float/2))) - (|> ($_ /.then - (/.function $self (list $arg/0 $arg/1 $arg/2) - (/.return ($_ /.+ $arg/0 $arg/1 $arg/2))) - (/.return (/.apply/3 $self (/.float float/0) (/.float float/1) (/.float float/2)))) - (/.lambda {.#None} (list)) + [(list)] (/.lambda {.#None}) (/.apply_lambda/* (list))))) (_.cover [/.apply/*] (expression (|>> (:as Frac) (f.= ($_ f.+ float/0 float/1 float/2))) @@ -509,7 +538,7 @@ (/.function $self (list $arg/0 $arg/1 $arg/2) (/.return ($_ /.+ $arg/0 $arg/1 $arg/2))) (/.return (/.apply/* (list (/.float float/0) (/.float float/1) (/.float float/2)) {.#None} $self))) - (/.lambda {.#None} (list)) + [(list)] (/.lambda {.#None}) (/.apply_lambda/* (list))))) ... (_.cover [/.new] ... (let [$this (/.local "this")] @@ -544,7 +573,7 @@ (|> (/.if (/.bool ???) (/.return (/.float float/0)) (/.return (/.float float/1))) - (/.lambda {.#None} (list)) + [(list)] (/.lambda {.#None}) (/.apply_lambda/* (list))))) (_.cover [/.when] (expression (|>> (:as Frac) (f.= (if ??? float/0 float/1))) @@ -552,7 +581,7 @@ (/.when (/.bool ???) (/.return (/.float float/0))) (/.return (/.float float/1))) - (/.lambda {.#None} (list)) + [(list)] (/.lambda {.#None}) (/.apply_lambda/* (list))))) ))) @@ -571,19 +600,20 @@ (|> ($_ /.then (/.statement (/.+ $arg/0 $arg/0)) (/.return $arg/0)) - (/.lambda {.#None} (list $arg/0)) + [(list $arg/0)] (/.lambda {.#None}) (/.apply_lambda/* (list (/.float float/0)))))) (_.cover [/.then] (expression (|>> (:as Frac) (f.= float/0)) (|> ($_ /.then (/.return $arg/0) (/.return $arg/1)) - (/.lambda {.#None} (list $arg/0 $arg/1)) + [(list $arg/0 $arg/1)] (/.lambda {.#None}) (/.apply_lambda/* (list (/.float float/0) (/.float float/1)))))) ..test|exception - ..test|function ..test|branching ..test|loop + (_.for [/.Block] + ..test|function) (_.for [/.Location] ..test/location) ))) @@ -632,10 +662,13 @@ (<| (_.covering /._) (_.for [/.Code]) ($_ _.and + (_.for [/.equivalence] + ($equivalence.spec /.equivalence ..random_expression)) + (_.cover [/.code /.manual] (|> (/.manual (/.code expected)) (: /.Expression) - (# /.code_equivalence = expected))) + (# /.equivalence = expected))) (_.for [/.Expression] ..test|expression) (_.for [/.Statement] diff --git a/stdlib/source/test/lux/tool.lux b/stdlib/source/test/lux/tool.lux index e2cbc50b6..4e3bad586 100644 --- a/stdlib/source/test/lux/tool.lux +++ b/stdlib/source/test/lux/tool.lux @@ -11,7 +11,8 @@ [lux ... ["[1][0]" syntax] ["[1][0]" analysis "_" - ["[1]/[0]" primitive]] + ["[1]/[0]" primitive] + ["[1]/[0]" composite]] ... [phase ... ["[1][0]" analysis] ... ["[1][0]" synthesis]] @@ -25,6 +26,7 @@ /version.test /reference.test /analysis/primitive.test + /analysis/composite.test ... /syntax.test ... /analysis.test ... /synthesis.test diff --git a/stdlib/source/test/lux/tool/compiler/language/lux/analysis/composite.lux b/stdlib/source/test/lux/tool/compiler/language/lux/analysis/composite.lux new file mode 100644 index 000000000..8c74718b8 --- /dev/null +++ b/stdlib/source/test/lux/tool/compiler/language/lux/analysis/composite.lux @@ -0,0 +1,76 @@ +(.using + [library + [lux "*" + ["_" test {"+" Test}] + [abstract + [monad {"+" do}] + [\\specification + ["$[0]" equivalence] + ["$[0]" hash]]] + [data + ["[0]" bit ("[1]#[0]" equivalence)] + ["[0]" text ("[1]#[0]" equivalence) + ["%" format {"+" format}]]] + [math + ["[0]" random {"+" Random} ("[1]#[0]" monad)] + [number + ["n" nat]]]]] + [\\library + ["[0]" /]]) + +(def: test|tag + Test + (do [! random.monad] + [multiplicity (# ! each (n.max 2) random.nat) + tag (# ! each (n.% multiplicity) random.nat) + lefts random.nat + right? random.bit] + ($_ _.and + (_.cover [/.tag /.lefts] + (and (|> lefts + (/.tag right?) + (/.lefts right?) + (n.= lefts)) + (|> tag + (/.lefts right?) + (/.tag right?) + (n.= tag)))) + (_.cover [/.choice] + (let [[lefts right?] (/.choice multiplicity tag)] + (if right? + (n.= (-- tag) lefts) + (n.= tag lefts)))) + ))) + +(def: .public (random multiplicity it) + (All (_ a) + (-> Nat (Random a) (Random (/.Composite a)))) + ($_ random.or + ($_ random.and + (random#each (n.% (-- multiplicity)) random.nat) + random.bit + it) + (random.list multiplicity it) + )) + +(def: .public test + Test + (let [random (..random 3 random.nat)] + (<| (_.covering /._) + (_.for [/.Composite /.Variant /.Tuple]) + ($_ _.and + (_.for [/.equivalence] + ($equivalence.spec (/.equivalence n.equivalence) random)) + (_.for [/.hash] + ($hash.spec (/.hash n.hash) random)) + + (_.for [/.Tag] + ..test|tag) + + (do random.monad + [left random + right random] + (_.cover [/.format] + (bit#= (# (/.equivalence n.equivalence) = left right) + (text#= (/.format %.nat left) (/.format %.nat right))))) + )))) -- cgit v1.2.3