From 514d03851b20c2f8b818ee26194a93515a685ae5 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Sun, 6 Sep 2015 19:52:07 -0400 Subject: - Added type-inference when constructing tuples. --- src/lux/analyser.clj | 7 +++-- src/lux/analyser/base.clj | 4 +-- src/lux/analyser/host.clj | 10 +++---- src/lux/analyser/lux.clj | 66 ++++++++++++++++++++++++++--------------------- src/lux/compiler/lux.clj | 4 +-- src/lux/type.clj | 12 ++++++++- 6 files changed, 60 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/lux/analyser.clj b/src/lux/analyser.clj index 552ccd77d..fbc360628 100644 --- a/src/lux/analyser.clj +++ b/src/lux/analyser.clj @@ -529,8 +529,11 @@ (fn [state] (|case (try ((aba1 analyse eval! compile-module compile-token exo-type ?token) state) (catch Error e - (prn e) - (assert false (prn-str 'analyse-basic-ast (&/show-ast token))))) + (prn 'analyse-basic-ast/Error-1 e) + (prn 'analyse-basic-ast/Error-2 (&/show-ast token)) + (prn 'analyse-basic-ast/Error-3 (&type/show-type exo-type)) + (throw e)) + ) (&/$Right state* output) (return* state* output) diff --git a/src/lux/analyser/base.clj b/src/lux/analyser/base.clj index 8c52748d7..414d005f1 100644 --- a/src/lux/analyser/base.clj +++ b/src/lux/analyser/base.clj @@ -126,9 +126,9 @@ ) ;; [Exports] -(defn expr-type [syntax+] +(defn expr-type* [syntax+] (|let [[_ type] syntax+] - (return type))) + type)) (defn analyse-1 [analyse exo-type elem] (|do [output (analyse exo-type elem)] diff --git a/src/lux/analyser/host.clj b/src/lux/analyser/host.clj index c6c5cb39b..0b333ce07 100644 --- a/src/lux/analyser/host.clj +++ b/src/lux/analyser/host.clj @@ -209,14 +209,12 @@ (defn analyse-jvm-aastore [analyse ?array ?idx ?elem] (|do [=array (analyse-1+ analyse ?array) - =elem (analyse-1+ analyse ?elem) - =array-type (&&/expr-type =array)] - (return (&/|list (&/T (&/V &&/$jvm-aastore (&/T =array ?idx =elem)) =array-type))))) + =elem (analyse-1+ analyse ?elem)] + (return (&/|list (&/T (&/V &&/$jvm-aastore (&/T =array ?idx =elem)) (&&/expr-type* =array)))))) (defn analyse-jvm-aaload [analyse ?array ?idx] - (|do [=array (analyse-1+ analyse ?array) - =array-type (&&/expr-type =array)] - (return (&/|list (&/T (&/V &&/$jvm-aaload (&/T =array ?idx)) =array-type))))) + (|do [=array (analyse-1+ analyse ?array)] + (return (&/|list (&/T (&/V &&/$jvm-aaload (&/T =array ?idx)) (&&/expr-type* =array)))))) (defn ^:private analyse-modifiers [modifiers] (&/fold% (fn [so-far modif] diff --git a/src/lux/analyser/lux.clj b/src/lux/analyser/lux.clj index 375c82f27..62202c1c9 100644 --- a/src/lux/analyser/lux.clj +++ b/src/lux/analyser/lux.clj @@ -28,23 +28,31 @@ ;; [Exports] (defn analyse-tuple [analyse exo-type ?elems] - (|do [exo-type* (&type/actual-type exo-type)] - (|case exo-type* - (&/$TupleT ?members) - (|do [=elems (&/map2% (fn [elem-t elem] - (&&/analyse-1 analyse elem-t elem)) - ?members ?elems)] + (|do [unknown? (&type/unknown? exo-type)] + (if unknown? + (|do [=elems (&/map% #(|do [=analysis (analyse-1+ analyse %)] + (return =analysis)) + ?elems) + _ (&type/check exo-type (&/V &/$TupleT (&/|map &&/expr-type* =elems)))] (return (&/|list (&/T (&/V &&/$tuple =elems) exo-type)))) + (|do [exo-type* (&type/actual-type exo-type)] + (|case exo-type* + (&/$TupleT ?members) + (|do [=elems (&/map2% (fn [elem-t elem] + (&&/analyse-1 analyse elem-t elem)) + ?members ?elems)] + (return (&/|list (&/T (&/V &&/$tuple =elems) + exo-type)))) + + (&/$UnivQ _) + (&type/with-var + (fn [$var] + (|do [exo-type** (&type/apply-type exo-type* $var)] + (analyse-tuple analyse exo-type** ?elems)))) - (&/$UnivQ _) - (&type/with-var - (fn [$var] - (|do [exo-type** (&type/apply-type exo-type* $var)] - (analyse-tuple analyse exo-type** ?elems)))) - - _ - (fail (str "[Analyser Error] Tuples require tuple-types: " (&type/show-type exo-type*)))))) + _ + (fail (str "[Analyser Error] Tuples require tuple-types: " (&type/show-type exo-type*)))))))) (defn ^:private analyse-variant-body [analyse exo-type ?values] (|do [output (|case ?values @@ -206,8 +214,7 @@ (->> top-outer (&/get$ &/$closure) (&/get$ &/$mappings) (&/|get name))) (&/|list)) (&/|reverse inner) scopes)] - ((|do [btype (&&/expr-type =local) - _ (&type/check exo-type btype)] + ((|do [_ (&type/check exo-type (&&/expr-type* =local))] (return (&/|list =local))) (&/set$ &/$envs (&/|++ inner* outer) state)))) )))) @@ -271,8 +278,8 @@ macro-expansion #(-> macro (.apply ?args) (.apply %)) ;; :let [_ (prn 'MACRO-EXPAND|POST (&/ident->text real-name))] ;; :let [_ (when (or (= "defsig" (aget real-name 1)) - ;; ;; (= "type" (aget real-name 1)) - ;; ;; (= &&/$struct r-name) + ;; ;; (= "..?" (aget real-name 1)) + ;; ;; (= "try$" (aget real-name 1)) ;; ) ;; (->> (&/|map &/show-ast macro-expansion) ;; (&/|interpose "\n") @@ -297,8 +304,7 @@ _ (&/assert! (> num-branches 0) "[Analyser Error] Can't have empty branches in \"case'\" expression.") _ (&/assert! (even? num-branches) "[Analyser Error] Unbalanced branches in \"case'\" expression.") =value (analyse-1+ analyse ?value) - =value-type (&&/expr-type =value) - =match (&&case/analyse-branches analyse exo-type =value-type (&/|as-pairs ?branches))] + =match (&&case/analyse-branches analyse exo-type (&&/expr-type* =value) (&/|as-pairs ?branches))] (return (&/|list (&/T (&/V &&/$case (&/T =value =match)) exo-type))))) @@ -376,11 +382,10 @@ (if ? (fail (str "[Analyser Error] Can't redefine " (str module-name ";" ?name))) (|do [=value (&/with-scope ?name - (analyse-1+ analyse ?value)) - =value-type (&&/expr-type =value)] + (analyse-1+ analyse ?value))] (|case =value [(&&/$var (&/$Global ?r-module ?r-name)) _] - (|do [_ (&&module/def-alias module-name ?name ?r-module ?r-name =value-type) + (|do [_ (&&module/def-alias module-name ?name ?r-module ?r-name (&&/expr-type* =value)) ;; :let [_ (println 'analyse-def/ALIAS (str module-name ";" ?name) '=> (str ?r-module ";" ?r-name)) ;; _ (println)] ] @@ -412,16 +417,17 @@ _ (&&module/declare-tags module-name tags def-type)] (return (&/|list)))) -(defn analyse-import [analyse compile-module compile-token ?path] +(defn analyse-import [analyse compile-module compile-token path] + ;; (prn 'analyse-import path) (|do [module-name &/get-module-name - _ (if (= module-name ?path) - (fail (str "[Analyser Error] Module can't import itself: " ?path)) + _ (if (= module-name path) + (fail (str "[Analyser Error] Module can't import itself: " path)) (return nil))] (&/save-module - (|do [already-compiled? (&&module/exists? ?path) - ;; :let [_ (prn 'analyse-import module-name ?path already-compiled?)] - _ (&&module/add-import ?path) - _ (&/when% (not already-compiled?) (compile-module ?path))] + (|do [already-compiled? (&&module/exists? path) + ;; :let [_ (prn 'analyse-import module-name path already-compiled?)] + _ (&&module/add-import path) + _ (&/when% (not already-compiled?) (compile-module path))] (return (&/|list)))))) (defn analyse-export [analyse compile-token name] diff --git a/src/lux/compiler/lux.clj b/src/lux/compiler/lux.clj index 6a02ed21d..c17d10494 100644 --- a/src/lux/compiler/lux.clj +++ b/src/lux/compiler/lux.clj @@ -176,8 +176,8 @@ ))) (defn compile-def [compile ?name ?body] - (|do [=value-type (&a/expr-type ?body) - :let [def-type (cond (&type/type= &type/Type =value-type) + (|do [:let [=value-type (&a/expr-type* ?body) + def-type (cond (&type/type= &type/Type =value-type) "type" :else diff --git a/src/lux/type.clj b/src/lux/type.clj index 8300d470c..f067867d8 100644 --- a/src/lux/type.clj +++ b/src/lux/type.clj @@ -395,7 +395,7 @@ (->> state (&/get$ &/$type-vars) (&/get$ &/$mappings)))] (fn [state] (return* (&/update$ &/$type-vars #(->> % - (&/update$ &/$counter dec) + ;; (&/update$ &/$counter dec) (&/set$ &/$mappings (&/|remove id mappings*))) state) nil))) @@ -949,3 +949,13 @@ _ (fail (str "[Type Error] Type is not named: " (show-type type))) )) + +(defn unknown? [type] + "(-> Type (Lux Bool))" + (|case type + (&/$VarT id) + (|do [? (bound? id)] + (return (not ?))) + + _ + (return false))) -- cgit v1.2.3