From 72950a540be3dc49a107700c77c0195db16a4f58 Mon Sep 17 00:00:00 2001
From: Eduardo Julian
Date: Wed, 23 May 2018 02:04:47 -0400
Subject: - Migrated special-form analysis to stdlib.
---
new-luxc/source/luxc/lang/analysis/type.lux | 27 -
new-luxc/source/luxc/lang/extension.lux | 114 --
new-luxc/source/luxc/lang/extension/analysis.lux | 19 -
.../source/luxc/lang/extension/analysis/common.lux | 448 -------
.../luxc/lang/extension/analysis/host.jvm.lux | 1218 --------------------
new-luxc/source/luxc/lang/synthesis.lux | 8 -
6 files changed, 1834 deletions(-)
delete mode 100644 new-luxc/source/luxc/lang/analysis/type.lux
delete mode 100644 new-luxc/source/luxc/lang/extension.lux
delete mode 100644 new-luxc/source/luxc/lang/extension/analysis.lux
delete mode 100644 new-luxc/source/luxc/lang/extension/analysis/common.lux
delete mode 100644 new-luxc/source/luxc/lang/extension/analysis/host.jvm.lux
delete mode 100644 new-luxc/source/luxc/lang/synthesis.lux
(limited to 'new-luxc/source/luxc')
diff --git a/new-luxc/source/luxc/lang/analysis/type.lux b/new-luxc/source/luxc/lang/analysis/type.lux
deleted file mode 100644
index 6d89dd5ef..000000000
--- a/new-luxc/source/luxc/lang/analysis/type.lux
+++ /dev/null
@@ -1,27 +0,0 @@
-(.module:
- lux
- (lux (control monad)
- [macro]
- (lang (type ["tc" check])))
- (luxc ["&" lang]
- (lang ["la" analysis #+ Analysis])))
-
-## These 2 analysers are somewhat special, since they require the
-## means of evaluating Lux expressions at compile-time for the sake of
-## computing Lux type values.
-(def: #export (analyse-check analyse eval type value)
- (-> &.Analyser &.Eval Code Code (Meta Analysis))
- (do macro.Monad
- [actualT (eval Type type)
- #let [actualT (:! Type actualT)]
- _ (&.infer actualT)]
- (&.with-type actualT
- (analyse value))))
-
-(def: #export (analyse-coerce analyse eval type value)
- (-> &.Analyser &.Eval Code Code (Meta Analysis))
- (do macro.Monad
- [actualT (eval Type type)
- _ (&.infer (:! Type actualT))]
- (&.with-type Any
- (analyse value))))
diff --git a/new-luxc/source/luxc/lang/extension.lux b/new-luxc/source/luxc/lang/extension.lux
deleted file mode 100644
index 254dd18ca..000000000
--- a/new-luxc/source/luxc/lang/extension.lux
+++ /dev/null
@@ -1,114 +0,0 @@
-(.module:
- lux
- (lux (control [monad #+ do]
- ["ex" exception #+ exception:])
- (data ["e" error]
- [text]
- (coll (dictionary ["dict" unordered #+ Dict])))
- [macro])
- [//]
- (// ["la" analysis]
- ["ls" synthesis]))
-
-(do-template []
- [(exception: #export ( {message Text})
- message)]
-
- [Unknown-Analysis]
- [Unknown-Synthesis]
- [Unknown-Translation]
- [Unknown-Statement]
-
- [Cannot-Define-Analysis-More-Than-Once]
- [Cannot-Define-Synthesis-More-Than-Once]
- [Cannot-Define-Translation-More-Than-Once]
- [Cannot-Define-Statement-More-Than-Once]
- )
-
-(type: #export Analysis
- (-> (-> Code (Meta Code))
- (-> Type Code (Meta Any))
- (List Code) (Meta Code)))
-
-(type: #export Synthesis
- (-> (-> la.Analysis ls.Synthesis) (List Code) Code))
-
-(type: #export Syntheses (Dict Text Synthesis))
-
-(type: #export Translation
- (-> (List Code) (Meta Code)))
-
-(type: #export Statement
- (-> (List Code) (Meta Any)))
-
-(type: #export Extensions
- {#analysis (Dict Text Analysis)
- #synthesis Syntheses
- #translation (Dict Text Translation)
- #statement (Dict Text Statement)})
-
-(def: #export fresh
- Extensions
- {#analysis (dict.new text.Hash)
- #synthesis (dict.new text.Hash)
- #translation (dict.new text.Hash)
- #statement (dict.new text.Hash)})
-
-(def: get
- (Meta Extensions)
- (function (_ compiler)
- (#e.Success [compiler
- (|> compiler (get@ #.extensions) (:! Extensions))])))
-
-(def: (set extensions)
- (-> Extensions (Meta Any))
- (function (_ compiler)
- (#e.Success [(set@ #.extensions (:! Nothing extensions) compiler)
- []])))
-
-(do-template [ ]
- [(def: #export ( name)
- (-> Text (Meta ))
- (do macro.Monad
- [extensions ..get]
- (case (dict.get name (get@ extensions))
- (#.Some extension)
- (wrap extension)
-
- #.None
- (//.throw name))))]
-
- [find-analysis Analysis #analysis Unknown-Analysis]
- [find-synthesis Synthesis #synthesis Unknown-Synthesis]
- [find-translation Translation #translation Unknown-Translation]
- [find-statement Statement #statement Unknown-Statement]
- )
-
-(do-template [ ]
- [(def: #export
-
- )
-
- (def: #export
- (Meta )
- (|> ..get
- (:: macro.Monad map (get@ ))))]
-
- [no-syntheses all-syntheses Syntheses #synthesis (dict.new text.Hash)]
- )
-
-(do-template [ ]
- [(def: #export ( name extension)
- (-> Text (Meta Any))
- (do macro.Monad
- [extensions ..get
- _ (//.assert name
- (not (dict.contains? name (get@ extensions))))
- _ (..set (update@ (dict.put name extension) extensions))]
- (wrap [])))]
-
- [install-analysis Analysis #analysis Cannot-Define-Analysis-More-Than-Once]
- [install-synthesis Synthesis #synthesis Cannot-Define-Synthesis-More-Than-Once]
- [install-translation Translation #translation Cannot-Define-Translation-More-Than-Once]
- [install-statement Statement #statement Cannot-Define-Statement-More-Than-Once]
- )
diff --git a/new-luxc/source/luxc/lang/extension/analysis.lux b/new-luxc/source/luxc/lang/extension/analysis.lux
deleted file mode 100644
index 79fa3af88..000000000
--- a/new-luxc/source/luxc/lang/extension/analysis.lux
+++ /dev/null
@@ -1,19 +0,0 @@
-(.module:
- lux
- (lux (data [text]
- (coll [list "list/" Functor]
- (dictionary ["dict" unordered #+ Dict]))))
- [//]
- [/common]
- [/host])
-
-(def: realize
- (-> /common.Bundle (Dict Text //.Analysis))
- (|>> dict.entries
- (list/map (function (_ [name proc]) [name (proc name)]))
- (dict.from-list text.Hash)))
-
-(def: #export defaults
- (Dict Text //.Analysis)
- (realize (dict.merge /common.procedures
- /host.procedures)))
diff --git a/new-luxc/source/luxc/lang/extension/analysis/common.lux b/new-luxc/source/luxc/lang/extension/analysis/common.lux
deleted file mode 100644
index f22cdcdd1..000000000
--- a/new-luxc/source/luxc/lang/extension/analysis/common.lux
+++ /dev/null
@@ -1,448 +0,0 @@
-(.module:
- lux
- (lux (control [monad #+ do]
- ["ex" exception #+ exception:])
- (concurrency [atom #+ Atom])
- (data [text]
- text/format
- (coll [list "list/" Functor]
- [array]
- (dictionary ["dict" unordered #+ Dict])))
- [macro]
- (macro [code])
- (lang (type ["tc" check]))
- [io])
- (luxc ["&" lang]
- (lang ["la" analysis]
- (analysis ["&." common]
- [".A" function]
- [".A" case]
- [".A" type])))
- [///])
-
-(do-template []
- [(exception: #export ( {message Text})
- message)]
-
- [Incorrect-Procedure-Arity]
- [Invalid-Syntax]
- )
-
-## [Utils]
-(type: #export Bundle
- (Dict Text (-> Text ///.Analysis)))
-
-(def: #export (install name unnamed)
- (-> Text (-> Text ///.Analysis)
- (-> Bundle Bundle))
- (dict.put name unnamed))
-
-(def: #export (prefix prefix bundle)
- (-> Text Bundle Bundle)
- (|> bundle
- dict.entries
- (list/map (function (_ [key val]) [(format prefix " " key) val]))
- (dict.from-list text.Hash)))
-
-(def: #export (wrong-arity proc expected actual)
- (-> Text Nat Nat Text)
- (format " Procedure: " (%t proc) "\n"
- " Expected Arity: " (|> expected nat-to-int %i) "\n"
- " Actual Arity: " (|> actual nat-to-int %i)))
-
-(def: (simple proc inputsT+ outputT)
- (-> Text (List Type) Type ///.Analysis)
- (let [num-expected (list.size inputsT+)]
- (function (_ analyse eval args)
- (let [num-actual (list.size args)]
- (if (n/= num-expected num-actual)
- (do macro.Monad
- [_ (&.infer outputT)
- argsA (monad.map @
- (function (_ [argT argC])
- (&.with-type argT
- (analyse argC)))
- (list.zip2 inputsT+ args))]
- (wrap (la.procedure proc argsA)))
- (&.throw Incorrect-Procedure-Arity (wrong-arity proc num-expected num-actual)))))))
-
-(def: #export (nullary valueT proc)
- (-> Type Text ///.Analysis)
- (simple proc (list) valueT))
-
-(def: #export (unary inputT outputT proc)
- (-> Type Type Text ///.Analysis)
- (simple proc (list inputT) outputT))
-
-(def: #export (binary subjectT paramT outputT proc)
- (-> Type Type Type Text ///.Analysis)
- (simple proc (list subjectT paramT) outputT))
-
-(def: #export (trinary subjectT param0T param1T outputT proc)
- (-> Type Type Type Type Text ///.Analysis)
- (simple proc (list subjectT param0T param1T) outputT))
-
-## [Analysers]
-## "lux is" represents reference/pointer equality.
-(def: (lux//is proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (do macro.Monad
- [[var-id varT] (&.with-type-env tc.var)]
- ((binary varT varT Bool proc)
- analyse eval args))))
-
-## "lux try" provides a simple way to interact with the host platform's
-## error-handling facilities.
-(def: (lux//try proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (case args
- (^ (list opC))
- (do macro.Monad
- [[var-id varT] (&.with-type-env tc.var)
- _ (&.infer (type (Either Text varT)))
- opA (&.with-type (type (io.IO varT))
- (analyse opC))]
- (wrap (la.procedure proc (list opA))))
-
- _
- (&.throw Incorrect-Procedure-Arity (wrong-arity proc +1 (list.size args))))))
-
-(def: (lux//function proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (case args
- (^ (list [_ (#.Symbol ["" func-name])]
- [_ (#.Symbol ["" arg-name])]
- body))
- (functionA.analyse-function analyse func-name arg-name body)
-
- _
- (&.throw Incorrect-Procedure-Arity (wrong-arity proc +3 (list.size args))))))
-
-(def: (lux//case proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (case args
- (^ (list input [_ (#.Record branches)]))
- (caseA.analyse-case analyse input branches)
-
- _
- (&.throw Incorrect-Procedure-Arity (wrong-arity proc +2 (list.size args))))))
-
-(def: (lux//in-module proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval argsC+)
- (case argsC+
- (^ (list [_ (#.Text module-name)] exprC))
- (&.with-current-module module-name
- (analyse exprC))
-
- _
- (&.throw Invalid-Syntax (format "Procedure: " proc "\n"
- " Inputs:" (|> argsC+
- list.enumerate
- (list/map (function (_ [idx argC])
- (format "\n " (%n idx) " " (%code argC))))
- (text.join-with "")) "\n")))))
-
-(do-template [ ]
- [(def: ( proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (case args
- (^ (list typeC valueC))
- ( analyse eval typeC valueC)
-
- _
- (&.throw Incorrect-Procedure-Arity (wrong-arity proc +2 (list.size args))))))]
-
- [lux//check typeA.analyse-check]
- [lux//coerce typeA.analyse-coerce])
-
-(def: (lux//check//type proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (case args
- (^ (list valueC))
- (do macro.Monad
- [_ (&.infer (type Type))
- valueA (&.with-type Type
- (analyse valueC))]
- (wrap valueA))
-
- _
- (&.throw Incorrect-Procedure-Arity (wrong-arity proc +1 (list.size args))))))
-
-(def: lux-procs
- Bundle
- (|> (dict.new text.Hash)
- (install "is" lux//is)
- (install "try" lux//try)
- (install "function" lux//function)
- (install "case" lux//case)
- (install "check" lux//check)
- (install "coerce" lux//coerce)
- (install "check type" lux//check//type)
- (install "in-module" lux//in-module)))
-
-(def: io-procs
- Bundle
- (<| (prefix "io")
- (|> (dict.new text.Hash)
- (install "log" (unary Text Any))
- (install "error" (unary Text Nothing))
- (install "exit" (unary Int Nothing))
- (install "current-time" (nullary Int)))))
-
-(def: bit-procs
- Bundle
- (<| (prefix "bit")
- (|> (dict.new text.Hash)
- (install "and" (binary Nat Nat Nat))
- (install "or" (binary Nat Nat Nat))
- (install "xor" (binary Nat Nat Nat))
- (install "left-shift" (binary Nat Nat Nat))
- (install "logical-right-shift" (binary Nat Nat Nat))
- (install "arithmetic-right-shift" (binary Int Nat Int))
- )))
-
-(def: int-procs
- Bundle
- (<| (prefix "int")
- (|> (dict.new text.Hash)
- (install "+" (binary Int Int Int))
- (install "-" (binary Int Int Int))
- (install "*" (binary Int Int Int))
- (install "/" (binary Int Int Int))
- (install "%" (binary Int Int Int))
- (install "=" (binary Int Int Bool))
- (install "<" (binary Int Int Bool))
- (install "min" (nullary Int))
- (install "max" (nullary Int))
- (install "to-nat" (unary Int Nat))
- (install "to-frac" (unary Int Frac))
- (install "char" (unary Int Text)))))
-
-(def: deg-procs
- Bundle
- (<| (prefix "deg")
- (|> (dict.new text.Hash)
- (install "+" (binary Deg Deg Deg))
- (install "-" (binary Deg Deg Deg))
- (install "*" (binary Deg Deg Deg))
- (install "/" (binary Deg Deg Deg))
- (install "%" (binary Deg Deg Deg))
- (install "=" (binary Deg Deg Bool))
- (install "<" (binary Deg Deg Bool))
- (install "scale" (binary Deg Nat Deg))
- (install "reciprocal" (binary Deg Nat Deg))
- (install "min" (nullary Deg))
- (install "max" (nullary Deg))
- (install "to-frac" (unary Deg Frac)))))
-
-(def: frac-procs
- Bundle
- (<| (prefix "frac")
- (|> (dict.new text.Hash)
- (install "+" (binary Frac Frac Frac))
- (install "-" (binary Frac Frac Frac))
- (install "*" (binary Frac Frac Frac))
- (install "/" (binary Frac Frac Frac))
- (install "%" (binary Frac Frac Frac))
- (install "=" (binary Frac Frac Bool))
- (install "<" (binary Frac Frac Bool))
- (install "smallest" (nullary Frac))
- (install "min" (nullary Frac))
- (install "max" (nullary Frac))
- (install "not-a-number" (nullary Frac))
- (install "positive-infinity" (nullary Frac))
- (install "negative-infinity" (nullary Frac))
- (install "to-deg" (unary Frac Deg))
- (install "to-int" (unary Frac Int))
- (install "encode" (unary Frac Text))
- (install "decode" (unary Text (type (Maybe Frac)))))))
-
-(def: text-procs
- Bundle
- (<| (prefix "text")
- (|> (dict.new text.Hash)
- (install "=" (binary Text Text Bool))
- (install "<" (binary Text Text Bool))
- (install "concat" (binary Text Text Text))
- (install "index" (trinary Text Text Nat (type (Maybe Nat))))
- (install "size" (unary Text Nat))
- (install "hash" (unary Text Nat))
- (install "replace-once" (trinary Text Text Text Text))
- (install "replace-all" (trinary Text Text Text Text))
- (install "char" (binary Text Nat (type (Maybe Nat))))
- (install "clip" (trinary Text Nat Nat (type (Maybe Text))))
- )))
-
-(def: (array//get proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (do macro.Monad
- [[var-id varT] (&.with-type-env tc.var)]
- ((binary (type (Array varT)) Nat (type (Maybe varT)) proc)
- analyse eval args))))
-
-(def: (array//put proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (do macro.Monad
- [[var-id varT] (&.with-type-env tc.var)]
- ((trinary (type (Array varT)) Nat varT (type (Array varT)) proc)
- analyse eval args))))
-
-(def: (array//remove proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (do macro.Monad
- [[var-id varT] (&.with-type-env tc.var)]
- ((binary (type (Array varT)) Nat (type (Array varT)) proc)
- analyse eval args))))
-
-(def: array-procs
- Bundle
- (<| (prefix "array")
- (|> (dict.new text.Hash)
- (install "new" (unary Nat Array))
- (install "get" array//get)
- (install "put" array//put)
- (install "remove" array//remove)
- (install "size" (unary (type (Ex [a] (Array a))) Nat))
- )))
-
-(def: math-procs
- Bundle
- (<| (prefix "math")
- (|> (dict.new text.Hash)
- (install "cos" (unary Frac Frac))
- (install "sin" (unary Frac Frac))
- (install "tan" (unary Frac Frac))
- (install "acos" (unary Frac Frac))
- (install "asin" (unary Frac Frac))
- (install "atan" (unary Frac Frac))
- (install "cosh" (unary Frac Frac))
- (install "sinh" (unary Frac Frac))
- (install "tanh" (unary Frac Frac))
- (install "exp" (unary Frac Frac))
- (install "log" (unary Frac Frac))
- (install "ceil" (unary Frac Frac))
- (install "floor" (unary Frac Frac))
- (install "round" (unary Frac Frac))
- (install "atan2" (binary Frac Frac Frac))
- (install "pow" (binary Frac Frac Frac))
- )))
-
-(def: (atom-new proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (case args
- (^ (list initC))
- (do macro.Monad
- [[var-id varT] (&.with-type-env tc.var)
- _ (&.infer (type (Atom varT)))
- initA (&.with-type varT
- (analyse initC))]
- (wrap (la.procedure proc (list initA))))
-
- _
- (&.throw Incorrect-Procedure-Arity (wrong-arity proc +1 (list.size args))))))
-
-(def: (atom-read proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (do macro.Monad
- [[var-id varT] (&.with-type-env tc.var)]
- ((unary (type (Atom varT)) varT proc)
- analyse eval args))))
-
-(def: (atom//compare-and-swap proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (do macro.Monad
- [[var-id varT] (&.with-type-env tc.var)]
- ((trinary (type (Atom varT)) varT varT Bool proc)
- analyse eval args))))
-
-(def: atom-procs
- Bundle
- (<| (prefix "atom")
- (|> (dict.new text.Hash)
- (install "new" atom-new)
- (install "read" atom-read)
- (install "compare-and-swap" atom//compare-and-swap)
- )))
-
-(type: (Box ! a)
- (#.Primitive "#Box" (#.Cons ! (#.Cons a #.Nil))))
-
-(def: (box//new proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (case args
- (^ (list initC))
- (do macro.Monad
- [[var-id varT] (&.with-type-env tc.var)
- _ (&.infer (type (All [!] (Box ! varT))))
- initA (&.with-type varT
- (analyse initC))]
- (wrap (la.procedure proc (list initA))))
-
- _
- (&.throw Incorrect-Procedure-Arity (wrong-arity proc +1 (list.size args))))))
-
-(def: (box//read proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (do macro.Monad
- [[thread-id threadT] (&.with-type-env tc.var)
- [var-id varT] (&.with-type-env tc.var)]
- ((unary (type (Box threadT varT)) varT proc)
- analyse eval args))))
-
-(def: (box//write proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (do macro.Monad
- [[thread-id threadT] (&.with-type-env tc.var)
- [var-id varT] (&.with-type-env tc.var)]
- ((binary varT (type (Box threadT varT)) Any proc)
- analyse eval args))))
-
-(def: box-procs
- Bundle
- (<| (prefix "box")
- (|> (dict.new text.Hash)
- (install "new" box//new)
- (install "read" box//read)
- (install "write" box//write)
- )))
-
-(def: process-procs
- Bundle
- (<| (prefix "process")
- (|> (dict.new text.Hash)
- (install "parallelism-level" (nullary Nat))
- (install "schedule" (binary Nat (type (io.IO Any)) Any))
- )))
-
-(def: #export procedures
- Bundle
- (<| (prefix "lux")
- (|> (dict.new text.Hash)
- (dict.merge lux-procs)
- (dict.merge bit-procs)
- (dict.merge int-procs)
- (dict.merge deg-procs)
- (dict.merge frac-procs)
- (dict.merge text-procs)
- (dict.merge array-procs)
- (dict.merge math-procs)
- (dict.merge atom-procs)
- (dict.merge box-procs)
- (dict.merge process-procs)
- (dict.merge io-procs))))
diff --git a/new-luxc/source/luxc/lang/extension/analysis/host.jvm.lux b/new-luxc/source/luxc/lang/extension/analysis/host.jvm.lux
deleted file mode 100644
index 9ef06a4b1..000000000
--- a/new-luxc/source/luxc/lang/extension/analysis/host.jvm.lux
+++ /dev/null
@@ -1,1218 +0,0 @@
-(.module:
- [lux #- char]
- (lux (control [monad #+ do]
- ["p" parser]
- ["ex" exception #+ exception:])
- (concurrency ["A" atom])
- (data ["e" error]
- [maybe]
- [product]
- [bool "bool/" Eq]
- [text "text/" Eq]
- (text format
- ["l" lexer])
- (coll [list "list/" Fold Functor Monoid]
- [array]
- (dictionary ["dict" unordered #+ Dict])))
- [macro "macro/" Monad]
- (macro [code]
- ["s" syntax])
- (lang [type]
- (type ["tc" check]))
- [host])
- (luxc ["&" lang]
- (lang ["&." host]
- ["la" analysis]
- (analysis ["&." common]
- [".A" inference])))
- ["@" //common]
- [///]
- )
-
-(do-template []
- [(exception: #export ( {message Text})
- message)]
-
- [Wrong-Syntax]
-
- [JVM-Type-Is-Not-Class]
-
- [Non-Interface]
- [Non-Object]
- [Non-Array]
- [Non-Throwable]
- [Non-JVM-Type]
-
- [Unknown-Class]
- [Primitives-Cannot-Have-Type-Parameters]
- [Primitives-Are-Not-Objects]
- [Invalid-Type-For-Array-Element]
-
- [Unknown-Field]
- [Mistaken-Field-Owner]
- [Not-Virtual-Field]
- [Not-Static-Field]
- [Cannot-Set-Final-Field]
-
- [No-Candidates]
- [Too-Many-Candidates]
-
- [Cannot-Cast]
-
- [Cannot-Possibly-Be-Instance]
-
- [Cannot-Convert-To-Class]
- [Cannot-Convert-To-Parameter]
- [Cannot-Convert-To-Lux-Type]
- [Unknown-Type-Var]
- [Type-Parameter-Mismatch]
- [Cannot-Correspond-Type-With-Class]
- )
-
-(def: (wrong-syntax procedure args)
- (-> Text (List Code) Text)
- (format "Procedure: " procedure "\n"
- "Arguments: " (%code (code.tuple args))))
-
-(do-template [ ]
- [(def: #export Type (#.Primitive (list)))]
-
- ## Boxes
- [Boolean "java.lang.Boolean"]
- [Byte "java.lang.Byte"]
- [Short "java.lang.Short"]
- [Integer "java.lang.Integer"]
- [Long "java.lang.Long"]
- [Float "java.lang.Float"]
- [Double "java.lang.Double"]
- [Character "java.lang.Character"]
- [String "java.lang.String"]
-
- ## Primitives
- [boolean "boolean"]
- [byte "byte"]
- [short "short"]
- [int "int"]
- [long "long"]
- [float "float"]
- [double "double"]
- [char "char"]
- )
-
-(def: conversion-procs
- @.Bundle
- (<| (@.prefix "convert")
- (|> (dict.new text.Hash)
- (@.install "double-to-float" (@.unary Double Float))
- (@.install "double-to-int" (@.unary Double Integer))
- (@.install "double-to-long" (@.unary Double Long))
- (@.install "float-to-double" (@.unary Float Double))
- (@.install "float-to-int" (@.unary Float Integer))
- (@.install "float-to-long" (@.unary Float Long))
- (@.install "int-to-byte" (@.unary Integer Byte))
- (@.install "int-to-char" (@.unary Integer Character))
- (@.install "int-to-double" (@.unary Integer Double))
- (@.install "int-to-float" (@.unary Integer Float))
- (@.install "int-to-long" (@.unary Integer Long))
- (@.install "int-to-short" (@.unary Integer Short))
- (@.install "long-to-double" (@.unary Long Double))
- (@.install "long-to-float" (@.unary Long Float))
- (@.install "long-to-int" (@.unary Long Integer))
- (@.install "long-to-short" (@.unary Long Short))
- (@.install "long-to-byte" (@.unary Long Byte))
- (@.install "char-to-byte" (@.unary Character Byte))
- (@.install "char-to-short" (@.unary Character Short))
- (@.install "char-to-int" (@.unary Character Integer))
- (@.install "char-to-long" (@.unary Character Long))
- (@.install "byte-to-long" (@.unary Byte Long))
- (@.install "short-to-long" (@.unary Short Long))
- )))
-
-(do-template [ ]
- [(def:
- @.Bundle
- (<| (@.prefix )
- (|> (dict.new text.Hash)
- (@.install "+" (@.binary ))
- (@.install "-" (@.binary ))
- (@.install "*" (@.binary ))
- (@.install "/" (@.binary ))
- (@.install "%" (@.binary ))
- (@.install "=" (@.binary Boolean))
- (@.install "<" (@.binary Boolean))
- (@.install "and" (@.binary ))
- (@.install "or" (@.binary ))
- (@.install "xor" (@.binary ))
- (@.install "shl" (@.binary Integer ))
- (@.install "shr" (@.binary Integer ))
- (@.install "ushr" (@.binary Integer ))
- )))]
-
- [int-procs "int" Integer]
- [long-procs "long" Long]
- )
-
-(do-template [ ]
- [(def:
- @.Bundle
- (<| (@.prefix )
- (|> (dict.new text.Hash)
- (@.install "+" (@.binary ))
- (@.install "-" (@.binary ))
- (@.install "*" (@.binary ))
- (@.install "/" (@.binary ))
- (@.install "%" (@.binary ))
- (@.install "=" (@.binary Boolean))
- (@.install "<" (@.binary Boolean))
- )))]
-
- [float-procs "float" Float]
- [double-procs "double" Double]
- )
-
-(def: char-procs
- @.Bundle
- (<| (@.prefix "char")
- (|> (dict.new text.Hash)
- (@.install "=" (@.binary Character Character Boolean))
- (@.install "<" (@.binary Character Character Boolean))
- )))
-
-(def: #export boxes
- (Dict Text Text)
- (|> (list ["boolean" "java.lang.Boolean"]
- ["byte" "java.lang.Byte"]
- ["short" "java.lang.Short"]
- ["int" "java.lang.Integer"]
- ["long" "java.lang.Long"]
- ["float" "java.lang.Float"]
- ["double" "java.lang.Double"]
- ["char" "java.lang.Character"])
- (dict.from-list text.Hash)))
-
-(def: (array-length proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (case args
- (^ (list arrayC))
- (do macro.Monad
- [_ (&.infer Nat)
- [var-id varT] (&.with-type-env tc.var)
- arrayA (&.with-type (type (Array varT))
- (analyse arrayC))]
- (wrap (la.procedure proc (list arrayA))))
-
- _
- (&.throw @.Incorrect-Procedure-Arity (@.wrong-arity proc +1 (list.size args))))))
-
-(def: (array-new proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (case args
- (^ (list lengthC))
- (do macro.Monad
- [lengthA (&.with-type Nat
- (analyse lengthC))
- expectedT macro.expected-type
- [level elem-class] (: (Meta [Nat Text])
- (loop [analysisT expectedT
- level +0]
- (case analysisT
- (#.Apply inputT funcT)
- (case (type.apply (list inputT) funcT)
- (#.Some outputT)
- (recur outputT level)
-
- #.None
- (&.throw Non-Array (%type expectedT)))
-
- (^ (#.Primitive "#Array" (list elemT)))
- (recur elemT (n/inc level))
-
- (#.Primitive class _)
- (wrap [level class])
-
- _
- (&.throw Non-Array (%type expectedT)))))
- _ (if (n/> +0 level)
- (wrap [])
- (&.throw Non-Array (%type expectedT)))]
- (wrap (la.procedure proc (list (code.nat (n/dec level)) (code.text elem-class) lengthA))))
-
- _
- (&.throw @.Incorrect-Procedure-Arity (@.wrong-arity proc +1 (list.size args))))))
-
-(def: (check-jvm objectT)
- (-> Type (Meta Text))
- (case objectT
- (#.Primitive name _)
- (macro/wrap name)
-
- (#.Named name unnamed)
- (check-jvm unnamed)
-
- (#.Var id)
- (macro/wrap "java.lang.Object")
-
- (^template []
- ( env unquantified)
- (check-jvm unquantified))
- ([#.UnivQ]
- [#.ExQ])
-
- (#.Apply inputT funcT)
- (case (type.apply (list inputT) funcT)
- (#.Some outputT)
- (check-jvm outputT)
-
- #.None
- (&.throw Non-Object (%type objectT)))
-
- _
- (&.throw Non-Object (%type objectT))))
-
-(def: (check-object objectT)
- (-> Type (Meta Text))
- (do macro.Monad
- [name (check-jvm objectT)]
- (if (dict.contains? name boxes)
- (&.throw Primitives-Are-Not-Objects name)
- (macro/wrap name))))
-
-(def: (box-array-element-type elemT)
- (-> Type (Meta [Type Text]))
- (case elemT
- (#.Primitive name #.Nil)
- (let [boxed-name (|> (dict.get name boxes)
- (maybe.default name))]
- (macro/wrap [(#.Primitive boxed-name #.Nil)
- boxed-name]))
-
- (#.Primitive name _)
- (if (dict.contains? name boxes)
- (&.throw Primitives-Cannot-Have-Type-Parameters name)
- (macro/wrap [elemT name]))
-
- _
- (&.throw Invalid-Type-For-Array-Element (%type elemT))))
-
-(def: (array-read proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (case args
- (^ (list arrayC idxC))
- (do macro.Monad
- [[var-id varT] (&.with-type-env tc.var)
- _ (&.infer varT)
- arrayA (&.with-type (type (Array varT))
- (analyse arrayC))
- ?elemT (&.with-type-env
- (tc.read var-id))
- [elemT elem-class] (box-array-element-type (maybe.default varT ?elemT))
- idxA (&.with-type Nat
- (analyse idxC))]
- (wrap (la.procedure proc (list (code.text elem-class) idxA arrayA))))
-
- _
- (&.throw @.Incorrect-Procedure-Arity (@.wrong-arity proc +2 (list.size args))))))
-
-(def: (array-write proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (case args
- (^ (list arrayC idxC valueC))
- (do macro.Monad
- [[var-id varT] (&.with-type-env tc.var)
- _ (&.infer (type (Array varT)))
- arrayA (&.with-type (type (Array varT))
- (analyse arrayC))
- ?elemT (&.with-type-env
- (tc.read var-id))
- [valueT elem-class] (box-array-element-type (maybe.default varT ?elemT))
- idxA (&.with-type Nat
- (analyse idxC))
- valueA (&.with-type valueT
- (analyse valueC))]
- (wrap (la.procedure proc (list (code.text elem-class) idxA valueA arrayA))))
-
- _
- (&.throw @.Incorrect-Procedure-Arity (@.wrong-arity proc +3 (list.size args))))))
-
-(def: array-procs
- @.Bundle
- (<| (@.prefix "array")
- (|> (dict.new text.Hash)
- (@.install "length" array-length)
- (@.install "new" array-new)
- (@.install "read" array-read)
- (@.install "write" array-write)
- )))
-
-(def: (object//null proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (case args
- (^ (list))
- (do macro.Monad
- [expectedT macro.expected-type
- _ (check-object expectedT)]
- (wrap (la.procedure proc (list))))
-
- _
- (&.throw @.Incorrect-Procedure-Arity (@.wrong-arity proc +0 (list.size args))))))
-
-(def: (object//null? proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (case args
- (^ (list objectC))
- (do macro.Monad
- [_ (&.infer Bool)
- [objectT objectA] (&common.with-unknown-type
- (analyse objectC))
- _ (check-object objectT)]
- (wrap (la.procedure proc (list objectA))))
-
- _
- (&.throw @.Incorrect-Procedure-Arity (@.wrong-arity proc +1 (list.size args))))))
-
-(def: (object//synchronized proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (case args
- (^ (list monitorC exprC))
- (do macro.Monad
- [[monitorT monitorA] (&common.with-unknown-type
- (analyse monitorC))
- _ (check-object monitorT)
- exprA (analyse exprC)]
- (wrap (la.procedure proc (list monitorA exprA))))
-
- _
- (&.throw @.Incorrect-Procedure-Arity (@.wrong-arity proc +2 (list.size args))))))
-
-(host.import java/lang/Object
- (equals [Object] boolean))
-
-(host.import java/lang/ClassLoader)
-
-(host.import #long java/lang/reflect/Type
- (getTypeName [] String))
-
-(host.import java/lang/reflect/GenericArrayType
- (getGenericComponentType [] java/lang/reflect/Type))
-
-(host.import java/lang/reflect/ParameterizedType
- (getRawType [] java/lang/reflect/Type)
- (getActualTypeArguments [] (Array java/lang/reflect/Type)))
-
-(host.import (java/lang/reflect/TypeVariable d)
- (getName [] String)
- (getBounds [] (Array java/lang/reflect/Type)))
-
-(host.import (java/lang/reflect/WildcardType d)
- (getLowerBounds [] (Array java/lang/reflect/Type))
- (getUpperBounds [] (Array java/lang/reflect/Type)))
-
-(host.import java/lang/reflect/Modifier
- (#static isStatic [int] boolean)
- (#static isFinal [int] boolean)
- (#static isInterface [int] boolean)
- (#static isAbstract [int] boolean))
-
-(host.import java/lang/reflect/Field
- (getDeclaringClass [] (java/lang/Class Object))
- (getModifiers [] int)
- (getGenericType [] java/lang/reflect/Type))
-
-(host.import java/lang/reflect/Method
- (getName [] String)
- (getModifiers [] int)
- (getDeclaringClass [] (Class Object))
- (getTypeParameters [] (Array (TypeVariable Method)))
- (getGenericParameterTypes [] (Array java/lang/reflect/Type))
- (getGenericReturnType [] java/lang/reflect/Type)
- (getGenericExceptionTypes [] (Array java/lang/reflect/Type)))
-
-(host.import (java/lang/reflect/Constructor c)
- (getModifiers [] int)
- (getDeclaringClass [] (Class c))
- (getTypeParameters [] (Array (TypeVariable (Constructor c))))
- (getGenericParameterTypes [] (Array java/lang/reflect/Type))
- (getGenericExceptionTypes [] (Array java/lang/reflect/Type)))
-
-(host.import (java/lang/Class c)
- (getName [] String)
- (getModifiers [] int)
- (#static forName [String boolean ClassLoader] #try (Class Object))
- (isAssignableFrom [(Class Object)] boolean)
- (getTypeParameters [] (Array (TypeVariable (Class c))))
- (getGenericInterfaces [] (Array java/lang/reflect/Type))
- (getGenericSuperclass [] java/lang/reflect/Type)
- (getDeclaredField [String] #try Field)
- (getConstructors [] (Array (Constructor Object)))
- (getDeclaredMethods [] (Array Method)))
-
-(def: (load-class name)
- (-> Text (Meta (Class Object)))
- (do macro.Monad
- [class-loader &host.class-loader]
- (case (Class::forName [name false class-loader])
- (#e.Success [class])
- (wrap class)
-
- (#e.Error error)
- (&.throw Unknown-Class name))))
-
-(def: (sub-class? super sub)
- (-> Text Text (Meta Bool))
- (do macro.Monad
- [super (load-class super)
- sub (load-class sub)]
- (wrap (Class::isAssignableFrom [sub] super))))
-
-(def: (object//throw proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (case args
- (^ (list exceptionC))
- (do macro.Monad
- [_ (&.infer Nothing)
- [exceptionT exceptionA] (&common.with-unknown-type
- (analyse exceptionC))
- exception-class (check-object exceptionT)
- ? (sub-class? "java.lang.Throwable" exception-class)
- _ (: (Meta Any)
- (if ?
- (wrap [])
- (&.throw Non-Throwable exception-class)))]
- (wrap (la.procedure proc (list exceptionA))))
-
- _
- (&.throw @.Incorrect-Procedure-Arity (@.wrong-arity proc +1 (list.size args))))))
-
-(def: (object//class proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (case args
- (^ (list classC))
- (case classC
- [_ (#.Text class)]
- (do macro.Monad
- [_ (&.infer (#.Primitive "java.lang.Class" (list (#.Primitive class (list)))))
- _ (load-class class)]
- (wrap (la.procedure proc (list (code.text class)))))
-
- _
- (&.throw Wrong-Syntax (wrong-syntax proc args)))
-
- _
- (&.throw @.Incorrect-Procedure-Arity (@.wrong-arity proc +1 (list.size args))))))
-
-(def: (object//instance? proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (case args
- (^ (list classC objectC))
- (case classC
- [_ (#.Text class)]
- (do macro.Monad
- [_ (&.infer Bool)
- [objectT objectA] (&common.with-unknown-type
- (analyse objectC))
- object-class (check-object objectT)
- ? (sub-class? class object-class)]
- (if ?
- (wrap (la.procedure proc (list (code.text class))))
- (&.throw Cannot-Possibly-Be-Instance (format object-class " !<= " class))))
-
- _
- (&.throw Wrong-Syntax (wrong-syntax proc args)))
-
- _
- (&.throw @.Incorrect-Procedure-Arity (@.wrong-arity proc +2 (list.size args))))))
-
-(def: type-descriptor
- (-> java/lang/reflect/Type Text)
- (java/lang/reflect/Type::getTypeName []))
-
-(def: (java-type-to-class type)
- (-> java/lang/reflect/Type (Meta Text))
- (cond (host.instance? Class type)
- (macro/wrap (Class::getName [] (:! Class type)))
-
- (host.instance? ParameterizedType type)
- (java-type-to-class (ParameterizedType::getRawType [] (:! ParameterizedType type)))
-
- ## else
- (&.throw Cannot-Convert-To-Class (type-descriptor type))))
-
-(type: Mappings
- (Dict Text Type))
-
-(def: fresh-mappings Mappings (dict.new text.Hash))
-
-(def: (java-type-to-lux-type mappings java-type)
- (-> Mappings java/lang/reflect/Type (Meta Type))
- (cond (host.instance? TypeVariable java-type)
- (let [var-name (TypeVariable::getName [] (:! TypeVariable java-type))]
- (case (dict.get var-name mappings)
- (#.Some var-type)
- (macro/wrap var-type)
-
- #.None
- (&.throw Unknown-Type-Var var-name)))
-
- (host.instance? WildcardType java-type)
- (let [java-type (:! WildcardType java-type)]
- (case [(array.read +0 (WildcardType::getUpperBounds [] java-type))
- (array.read +0 (WildcardType::getLowerBounds [] java-type))]
- (^or [(#.Some bound) _] [_ (#.Some bound)])
- (java-type-to-lux-type mappings bound)
-
- _
- (macro/wrap Any)))
-
- (host.instance? Class java-type)
- (let [java-type (:! (Class Object) java-type)
- class-name (Class::getName [] java-type)]
- (macro/wrap (case (array.size (Class::getTypeParameters [] java-type))
- +0
- (#.Primitive class-name (list))
-
- arity
- (|> (list.n/range +0 (n/dec arity))
- list.reverse
- (list/map (|>> (n/* +2) n/inc #.Bound))
- (#.Primitive class-name)
- (type.univ-q arity)))))
-
- (host.instance? ParameterizedType java-type)
- (let [java-type (:! ParameterizedType java-type)
- raw (ParameterizedType::getRawType [] java-type)]
- (if (host.instance? Class raw)
- (do macro.Monad
- [paramsT (|> java-type
- (ParameterizedType::getActualTypeArguments [])
- array.to-list
- (monad.map @ (java-type-to-lux-type mappings)))]
- (macro/wrap (#.Primitive (Class::getName [] (:! (Class Object) raw))
- paramsT)))
- (&.throw JVM-Type-Is-Not-Class (type-descriptor raw))))
-
- (host.instance? GenericArrayType java-type)
- (do macro.Monad
- [innerT (|> (:! GenericArrayType java-type)
- (GenericArrayType::getGenericComponentType [])
- (java-type-to-lux-type mappings))]
- (wrap (#.Primitive "#Array" (list innerT))))
-
- ## else
- (&.throw Cannot-Convert-To-Lux-Type (type-descriptor java-type))))
-
-(def: (correspond-type-params class type)
- (-> (Class Object) Type (Meta Mappings))
- (case type
- (#.Primitive name params)
- (let [class-name (Class::getName [] class)
- class-params (array.to-list (Class::getTypeParameters [] class))
- num-class-params (list.size class-params)
- num-type-params (list.size params)]
- (cond (not (text/= class-name name))
- (&.throw Cannot-Correspond-Type-With-Class
- (format "Class = " class-name "\n"
- "Type = " (%type type)))
-
- (not (n/= num-class-params num-type-params))
- (&.throw Type-Parameter-Mismatch
- (format "Expected: " (%i (nat-to-int num-class-params)) "\n"
- " Actual: " (%i (nat-to-int num-type-params)) "\n"
- " Class: " class-name "\n"
- " Type: " (%type type)))
-
- ## else
- (macro/wrap (|> params
- (list.zip2 (list/map (TypeVariable::getName []) class-params))
- (dict.from-list text.Hash)))
- ))
-
- _
- (&.throw Non-JVM-Type (%type type))))
-
-(def: (object//cast proc)
- (-> Text ///.Analysis)
- (function (_ analyse eval args)
- (case args
- (^ (list valueC))
- (do macro.Monad
- [toT macro.expected-type
- to-name (check-jvm toT)
- [valueT valueA] (&common.with-unknown-type
- (analyse valueC))
- from-name (check-jvm valueT)
- can-cast? (: (Meta Bool)
- (case [from-name to-name]
- (^template [