Skip to content

Commit

Permalink
Added capacity for algebra, defining equations and projecting functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoejp committed Oct 4, 2024
1 parent edb3bdd commit ce76ccb
Show file tree
Hide file tree
Showing 10 changed files with 515 additions and 9 deletions.
3 changes: 1 addition & 2 deletions stdlib/source/library/lux/abstract/equivalence.lux
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@

(the .public (recursive sub)
(for_any (_ it)
(-> (-> (Equivalence it)
(Equivalence it))
(-> (Change (Equivalence it))
(Equivalence it)))
(implementation
(the (= left right)
Expand Down
116 changes: 116 additions & 0 deletions stdlib/source/library/lux/math/algebra/equation.lux
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.using
[library
[lux (.except function)
[math
[number
[/64
["[0]" natural]]]]
[data
["[0]" product]
["[0]" text]
[collection
["[0]" list (.use "[1]#[0]" functor)
["?[1]" \\projection]]
["[0]" set (.only Set)]]]
[abstract
["[0]" monad]]
[macro
["[0]" template]
["[0]" syntax]]
["[0]" error (.only)
["[0]" try (.only Try)]]
["[0]" meta]]]
[//
["[0]" variable]
["[0]" formula (.only Formula)]])

(every .public Equation
[Formula Formula])

(the .public (= definition concept)
(-> Formula Formula
Equation)
[definition concept])

(the .public in_reverse
(Change Equation)
product.swapped)

(the .public (as_text [definition concept])
(text.Injection Equation)
(text (formula.as_text concept)
" = "
(formula.as_text definition)))

(template.with [,name ,formula]
[(the .public (,name parameter)
(-> Formula
(Change Equation))
(let [change (,formula parameter)]
(product.then change change)))]

[[+ formula.+]
[- formula.-]
[x formula.x]
[/ formula./]
[^ formula.^]])

(the (function'' parameters value definition)
(-> (List Formula) Formula Formula
Code)
(let [input (list.repeated (list.size parameters) (` formula.Scalar))
type (` (-> (,* input)
formula.Scalar))]
(` (is (, type)
(.function ((, (formula.as_code value))
(,* (list#each formula.as_code parameters)))
(, (formula.as_code definition)))))))

(the (function' parameters value [definition concept])
(-> (List Formula) Formula Equation
(Try Code))
(let [input (is (Set variable.Identity)
(|> parameters
(list#each formula.every_variable)
(list.mix set.union (set.empty natural.hash))))
output (is (Set variable.Identity)
(formula.every_variable value))]
(if (let [there_is_no_circularity
(not (set.sub? input output))

all_inputs_are_present_in_definition
(set.= input
(formula.every_variable definition))

only_the_concept_is_being_calculated
(set.= output
(formula.every_variable concept))]
(and there_is_no_circularity
all_inputs_are_present_in_definition
only_the_concept_is_being_calculated))
{try.#Success (function'' parameters value definition)}
{try.#Failure error.not_valid})))

(every .public (Function of)
[Formula of
Equation])

(the Function'
(type (Function (List Formula))))

(the .public function
(syntax.macro (_ [it ?list.any])
(monad.let meta.monad
[it (meta.eval Function' it)
.let [[concept parameters it] (as (-> Any
Function')
it)]]
(when (function' parameters concept it)
{try.#Success it}
(pure (list it))

{try.#Failure error}
(meta.failure error)))))
209 changes: 209 additions & 0 deletions stdlib/source/library/lux/math/algebra/formula.lux
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.using
[library
[lux (.except #Variable)
[math
[number
[/64
["[0]" natural]
["[0]" decimal]]]]
[control
["[0]" state]]
[data
["[0]" sum]
["[0]" product]
["[0]" text]
[collection
["[0]" set (.only Set)]]]
[abstract
["[0]" monad]
["[0]" equivalence (.only Equivalence)]]
[macro
["^" pattern]
["[0]" template]]
[meta
["[0]" code]]]]
[//
["[0]" variable (.only Variable)]])

... https://en.wikipedia.org/wiki/Scalar_(physics)
(every .public Scalar
Decimal)

(every .public Formula
(Rec @
(Variant
{#Constant Scalar}
{#+ @ @}
{#- @ @}
{#x @ @}
{#/ @ @}
{#^ @ @}
{#Variable Variable})))

(the .public equivalence
(Equivalence Formula)
(<| equivalence.recursive
(.function (_ equivalence))
(let [binary (product.equivalence equivalence equivalence)])
(all sum.equivalence
decimal.equivalence
binary
binary
binary
binary
binary
variable.equivalence
)))

(the .public (as_text it)
(text.Injection Formula)
(`` (when it
{#Constant it}
(by decimal.base_10 as it)

(,, (template.with [,tag ,symbol]
[{,tag parameter it}
(|> (text (as_text it)
" " ,symbol " "
(as_text parameter))
(text.enclosed ["(" ")"]))]
[[#+ "+"]
[#- "-"]
[#x "x"]
[#/ "/"]
[#^ "^"]]))

{#Variable it}
(variable.as_text it))))

(every .public Environment
Natural)

(the .public constant
(-> Scalar
Formula)
(|>> {#Constant}))

(the .public (variable name)
(-> Text
(state.State Environment Formula))
(monad.let state.monad
[it state.get
_ (state.update ++)]
(pure {#Variable it name})))

(template.the (one)
[{#Constant decimal.positive_one}])

(the (x' parameter it)
(-> Formula Formula
(Maybe Formula))
(when [parameter it]
(^.or [(..one) it]
[it (..one)])
{.#Some it}

else
{.#None}))

(the (/' parameter it)
(-> Formula Formula
(Maybe Formula))
(if (with equivalence
(= parameter it))
{.#Some (..one)}
(when it
{#x parameter' it'}
(when [(/' parameter parameter')
(/' parameter it')]
(^.or [{.#Some parameter'} _]
[_ {.#Some it'}])
{.#Some {#x parameter' it'}}

else
{.#None})

else
{.#None})))

(the (normal it)
(Change Formula)
(`` (when it
(,, (template.with [,tag ,name]
[{,tag parameter' it'}
(when (,name parameter' it')
{.#Some it}
(normal it)

else
it)]

[[#x x']
[#/ /']]))

else
it)))

(template.with [,name ,tag]
[(the .public (,name parameter it)
(-> Formula
(Change Formula))
(normal {,tag parameter it}))]

[[+ #+]
[- #-]
[x #x]
[/ #/]
[^ #^]])

(the .public new
(for_any (_ it)
(-> (state.State Environment it)
it))
(|>> (state.value natural.zero)
product.right))

(the .public (every_variable it)
(-> Formula
(Set variable.Identity))
(`` (when it
{#Constant _}
(set.empty natural.hash)

(,, (template.with [,tag]
[{,tag parameter it}
(set.union (every_variable parameter)
(every_variable it))]

[[#+]
[#-]
[#x]
[#/]
[#^]]))

{#Variable it}
(set.of_list natural.hash (list (variable.identity it))))))

(the .public (as_code it)
(-> Formula
Code)
(`` (when it
{#Constant it}
(code.decimal it)

(,, (template.with [,tag ,function]
[{,tag parameter it}
(` (,function (, (as_code parameter))
(, (as_code it))))]

[[#+ decimal.+]
[#- decimal.-]
[#x decimal.x]
[#/ decimal./]
[#^ decimal.^]]))

{#Variable it}
(variable.as_code it))))
52 changes: 52 additions & 0 deletions stdlib/source/library/lux/math/algebra/variable.lux
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.using
[library
[lux (.except Name name)
[math
[number
[/64
["[0]" natural]]]]
[data
["[0]" product]
["[0]" text]]
[abstract
["[0]" equivalence (.only Equivalence)]]
[meta
["[0]" code]]]])

(every .public Identity
Natural)

(every .public Name
Text)

(every .public Variable
[Identity Name])

(the .public identity
(-> Variable
Identity)
product.left)

(the .public name
(-> Variable
Name)
product.right)

(the .public equivalence
(Equivalence Variable)
(by equivalence.functor each ..identity natural.equivalence))

(the .public (as_text it)
(text.Injection Variable)
(text (name it)
"@"
(by natural.base_10 as (identity it))))

(the .public as_code
(-> Variable
Code)
(|>> as_text
code.local))
Loading

0 comments on commit ce76ccb

Please sign in to comment.