Skip to content

Commit

Permalink
Shorter syntax for anonymous sums/variants. Also, new angle constants.
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoejp committed Aug 14, 2023
1 parent 403d5a4 commit caab080
Show file tree
Hide file tree
Showing 29 changed files with 682 additions and 614 deletions.
4 changes: 4 additions & 0 deletions lux-bootstrapper/src/lux/analyser.clj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@

(&/$Variant (&/$Item [command-meta command] parameters))
(|case command
(&/$Bit ?right)
(&/with-analysis-meta location exo-type
(&&lux/analyse-variant analyse (&/$Right exo-type) 0 ?right parameters))

(&/$Nat lefts)
(|let [(&/$Item [_ (&/$Bit ?right)] parameters*) parameters]
(&/with-analysis-meta location exo-type
Expand Down
30 changes: 21 additions & 9 deletions lux-bootstrapper/src/lux/analyser/case.clj
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,19 @@
_
(&/fail-with-loc (str "[Pattern-matching Error] Tuples require tuple-types: " (&type/show-type value-type))))))

(defn ^:private anonymous_variant [analyse-pattern
value-type kont
lefts right? ?values]
(let [idx (if right? (inc lefts) lefts)]
(|do [value-type* (adjust-type value-type)
case-type (&type/sum-at idx value-type*)
[=test =kont] (case (int (&/|length ?values))
0 (analyse-pattern &/$None case-type unit-tuple kont)
1 (analyse-pattern &/$None case-type (&/|head ?values) kont)
;; 1+
(analyse-pattern &/$None case-type (&/T [(&/T ["" -1 -1]) (&/$Tuple ?values)]) kont))]
(return (&/T [($VariantTestAC (&/T [lefts right? =test])) =kont])))))

(defn ^:private analyse-pattern [var?? value-type pattern kont]
(|let [[meta pattern*] pattern]
(|case pattern*
Expand Down Expand Up @@ -359,16 +372,15 @@
(&/$None)
(analyse-tuple-pattern analyse-pattern pattern value-type ?members kont)))

(&/$Variant (&/$Item [_ (&/$Bit right?)] ?values))
(anonymous_variant analyse-pattern
value-type kont
0 right? ?values)

(&/$Variant (&/$Item [_ (&/$Nat lefts)] (&/$Item [_ (&/$Bit right?)] ?values)))
(let [idx (if right? (inc lefts) lefts)]
(|do [value-type* (adjust-type value-type)
case-type (&type/sum-at idx value-type*)
[=test =kont] (case (int (&/|length ?values))
0 (analyse-pattern &/$None case-type unit-tuple kont)
1 (analyse-pattern &/$None case-type (&/|head ?values) kont)
;; 1+
(analyse-pattern &/$None case-type (&/T [(&/T ["" -1 -1]) (&/$Tuple ?values)]) kont))]
(return (&/T [($VariantTestAC (&/T [lefts right? =test])) =kont]))))
(anonymous_variant analyse-pattern
value-type kont
lefts right? ?values)

(&/$Variant (&/$Item [_ (&/$Identifier ?ident)] ?values))
(|do [[=module =name] (&&/resolved-ident ?ident)
Expand Down
84 changes: 42 additions & 42 deletions stdlib/source/library/lux.lux
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{9 #1
[..prelude "Any"]
{8 #0
{0 #0}
{#0}
{4 #0 1}}})
#1)

Expand All @@ -27,59 +27,59 @@
{9 #1
[..prelude "Nothing"]
{7 #0
{0 #0}
{#0}
{4 #0 1}}})
#1)

(.def# Bit
(.is_type#
{9 #1
[..prelude "Bit"]
{0 #0 "#Bit" {0 #0}}})
{#0 "#Bit" {#0}}})
#1)

(.def# I64
(.is_type#
{9 #1
[..prelude "I64"]
{7 #0
{0 #0}
{0 #0 "#I64" {0 #1 {4 #0 1} {0 #0}}}}})
{#0}
{#0 "#I64" {#1 {4 #0 1} {#0}}}}})
#1)

(.def# Nat
(.is_type#
{9 #1
[..prelude "Nat"]
{0 #0 "#I64" {0 #1 {0 #0 "#Nat" {0 #0}} {0 #0}}}})
{#0 "#I64" {#1 {#0 "#Nat" {#0}} {#0}}}})
#1)

(.def# Int
(.is_type#
{9 #1
[..prelude "Int"]
{0 #0 "#I64" {0 #1 {0 #0 "#Int" {0 #0}} {0 #0}}}})
{#0 "#I64" {#1 {#0 "#Int" {#0}} {#0}}}})
#1)

(.def# Rev
(.is_type#
{9 #1
[..prelude "Rev"]
{0 #0 "#I64" {0 #1 {0 #0 "#Rev" {0 #0}} {0 #0}}}})
{#0 "#I64" {#1 {#0 "#Rev" {#0}} {#0}}}})
#1)

(.def# Frac
(.is_type#
{9 #1
[..prelude "Frac"]
{0 #0 "#Frac" {0 #0}}})
{#0 "#Frac" {#0}}})
#1)

(.def# Text
(.is_type#
{9 #1
[..prelude "Text"]
{0 #0 "#Text" {0 #0}}})
{#0 "#Text" {#0}}})
#1)

(.def# Symbol
Expand All @@ -105,7 +105,7 @@
{9 #1
[..prelude "List"]
{7 #0
{0 #0}
{#0}
{1 #0
... End
Any
Expand All @@ -126,7 +126,7 @@
{9 #1
[..prelude "Maybe"]
{7 #0
{0 #0}
{#0}
{1 #0
... None
Any
Expand All @@ -142,7 +142,7 @@
{9 #1
[..prelude "Quantification"]
{7 #0
{0 #0}
{#0}
{2 #0
{9 #0 {4 #0 1} List}
{4 #0 1}}}})
Expand All @@ -168,9 +168,9 @@
({Type
({Type_Pair
{9 #0
{0 #0 ["" {0 #0}]}
{#0 ["" {#0}]}
{7 #0
{0 #0}
{#0}
{1 #0
... Nominal
{2 #0 Text {9 #0 Type List}}
Expand Down Expand Up @@ -204,7 +204,7 @@
... Named
{2 #0 Symbol Type}}}}}}}}}}}}}}
(.is_type# {2 #0 Type Type}))}
(.is_type# {9 #0 {0 #0 ["" {0 #0}]} {4 #0 0}}))})
(.is_type# {9 #0 {#0 ["" {#0}]} {4 #0 0}}))})
#1)

... (every .public Label
Expand All @@ -226,15 +226,15 @@
(.def# Tag
(.is_type#
{9 #1 [..prelude "Tag"]
{0 #0 "#Tag" {0 #0}}})
{#0 "#Tag" {#0}}})
#1)

... (every .public Slot
... (Nominal "#Slot"))
(.def# Slot
(.is_type#
{9 #1 [..prelude "Slot"]
{0 #0 "#Slot" {0 #0}}})
{#0 "#Slot" {#0}}})
#1)

(.def# tag
Expand All @@ -251,36 +251,36 @@

(.def# list_tags
(.is# {9 #0 Symbol List}
{0 #1 [[..prelude "#End"]
{0 #1 [[..prelude "#Item"]
{0 #0}]}]})
{#1 [[..prelude "#End"]
{#1 [[..prelude "#Item"]
{#0}]}]})
#0)
(.def# #End (..tag [{0 #1 [0 #0 ..list_tags]} List]) #1)
(.def# #Item (..tag [{0 #1 [0 #1 ..list_tags]} List]) #1)
(.def# #End (..tag [{#1 [0 #0 ..list_tags]} List]) #1)
(.def# #Item (..tag [{#1 [0 #1 ..list_tags]} List]) #1)

(.def# maybe_tags
(.is# {9 #0 Symbol List}
{0 #1 [[..prelude "#None"]
{0 #1 [[..prelude "#Some"]
{0 #0}]}]})
{#1 [[..prelude "#None"]
{#1 [[..prelude "#Some"]
{#0}]}]})
#0)
(.def# #None (..tag [{0 #1 [0 #0 ..maybe_tags]} Maybe]) #1)
(.def# #Some (..tag [{0 #1 [0 #1 ..maybe_tags]} Maybe]) #1)
(.def# #None (..tag [{#1 [0 #0 ..maybe_tags]} Maybe]) #1)
(.def# #Some (..tag [{#1 [0 #1 ..maybe_tags]} Maybe]) #1)

(.def# type_tags
(.is# {9 #0 Symbol List}
{0 #1 [[..prelude "#Nominal"]
{0 #1 [[..prelude "#Sum"]
{0 #1 [[..prelude "#Product"]
{0 #1 [[..prelude "#Function"]
{0 #1 [[..prelude "#Parameter"]
{0 #1 [[..prelude "#Var"]
{0 #1 [[..prelude "#Opaque"]
{0 #1 [[..prelude "#Universal"]
{0 #1 [[..prelude "#Existential"]
{0 #1 [[..prelude "#Apply"]
{0 #1 [[..prelude "#Named"]
{0 #0}]}]}]}]}]}]}]}]}]}]}]})
{#1 [[..prelude "#Nominal"]
{#1 [[..prelude "#Sum"]
{#1 [[..prelude "#Product"]
{#1 [[..prelude "#Function"]
{#1 [[..prelude "#Parameter"]
{#1 [[..prelude "#Var"]
{#1 [[..prelude "#Opaque"]
{#1 [[..prelude "#Universal"]
{#1 [[..prelude "#Existential"]
{#1 [[..prelude "#Apply"]
{#1 [[..prelude "#Named"]
{#0}]}]}]}]}]}]}]}]}]}]}]})
#0)
(.def# #Nominal (..tag [{#Some [0 #0 ..type_tags]} Type]) #1)
(.def# #Sum (..tag [{#Some [1 #0 ..type_tags]} Type]) #1)
Expand Down Expand Up @@ -874,7 +874,7 @@
(.is# Type
{#Named [..prelude "Lux"]
({Lux
{#Apply {0 #0 ["" {#End}]}
{#Apply {#0 ["" {#End}]}
{#Universal {#End}
{#Product
... info
Expand Down Expand Up @@ -917,7 +917,7 @@
{#Sum Text {#Product Lux Any}}}}}
... host
Any}}}}}}}}}}}}}}}
{#Apply {0 #0 ["" {#End}]} {#Parameter 0}})})
{#Apply {#0 ["" {#End}]} {#Parameter 0}})})
.public)

(.def# lux_slots
Expand Down
16 changes: 10 additions & 6 deletions stdlib/source/library/lux/abstract/comonad/free.lux
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@
[//
[functor (.only Functor)]]])

(every .public (Free F a)
[a (F (Free F a))])
(every .public (Free ! of)
[of (! (Free ! of))])

(the .public (functor dsl)
(All (_ F) (-> (Functor F) (Functor (Free F))))
(All (_ !)
(-> (Functor !)
(Functor (Free !))))
(implementation
(the (each f [head tail])
[(f head) (of dsl each (each f) tail)])))
(the (each $ [head tail])
[($ head) (of dsl each (each $) tail)])))

(the .public (comonad dsl)
(All (_ F) (-> (Functor F) (CoMonad (Free F))))
(All (_ !)
(-> (Functor !)
(CoMonad (Free !))))
(implementation
(the functor (..functor dsl))

Expand Down
27 changes: 15 additions & 12 deletions stdlib/source/library/lux/abstract/functor.lux
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,21 @@
(|>> !it
((it $))))))

(every .public (Recursive it)
(All (self of)
(it self of)))

(the .public (recursive it)
(All (_ of)
(-> (-> (Functor of)
(Functor of))
(Functor of)))
(implementation
(the (each $ !it)
((it each) $ !it))))
(for .old
(these)

(these (every .public (Recursive it)
(All (self of)
(it self of)))

(the .public (recursive it)
(All (_ of)
(-> (-> (Functor of)
(Functor of))
(Functor of)))
(implementation
(the (each $ !it)
((it each) $ !it))))))

(every .public (Then outer inner)
(All (_ of)
Expand Down
4 changes: 2 additions & 2 deletions stdlib/source/library/lux/control/concatenative.lux
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@
(the .public left
(All (_ a b) (=> [a] [(Or a b)]))
(function (_ [stack l])
[stack {0 #0 l}]))
[stack {#0 l}]))

(the .public right
(All (_ a b) (=> [b] [(Or a b)]))
(function (_ [stack r])
[stack {0 #1 r}]))
[stack {#1 r}]))

(with_template [<input> <output> <word> <func>]
[(`` (the .public <word>
Expand Down
4 changes: 2 additions & 2 deletions stdlib/source/library/lux/control/parser.lux
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@
(function (_ tokens)
(when (left tokens)
{try.#Success [tokens' output]}
{try.#Success [tokens' {0 #0 output}]}
{try.#Success [tokens' {#0 output}]}

{try.#Failure _}
(when (right tokens)
{try.#Success [tokens' output]}
{try.#Success [tokens' {0 #1 output}]}
{try.#Success [tokens' {#1 output}]}

<handle_failure!>))))

Expand Down
Loading

0 comments on commit caab080

Please sign in to comment.