diff options
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/lux/data/struct/tree.lux | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/stdlib/source/lux/data/struct/tree.lux b/stdlib/source/lux/data/struct/tree.lux new file mode 100644 index 000000000..7b7828d73 --- /dev/null +++ b/stdlib/source/lux/data/struct/tree.lux @@ -0,0 +1,54 @@ +## Copyright (c) Eduardo Julian. All rights reserved. +## 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 http://mozilla.org/MPL/2.0/. + +(;module: + lux + (lux (control monad + eq) + (data (struct [list "" Monad<List>])) + [compiler] + (macro [ast] + ["s" syntax #+ syntax: Syntax]))) + +## [Types] +(type: #export (Tree a) + {#value a + #children (List (Tree a))}) + +## [Values] +(def: #export (flatten tree) + (All [a] (-> (Tree a) (List a))) + (#;Cons (get@ #value tree) + (join (map flatten (get@ #children tree))))) + +(def: #export (leaf value) + (All [a] (-> a (Tree a))) + {#value value + #children (list)}) + +(def: #export (branch value children) + (All [a] (-> a (List (Tree a)) (Tree a))) + {#value value + #children children}) + +## [Syntax] +(type: #rec Tree-AST + [AST (List Tree-AST)]) + +(def: (tree^ _) + (-> Unit (Syntax Tree-AST)) + (s;record (s;seq s;any (s;tuple (s;some (lambda [state] ((tree^ []) state))))))) + +(syntax: #export (tree type {root (tree^ [])}) + (wrap (list (` (: (Tree (~ type)) + (~ (loop [[value children] root] + (` {#value (~ value) + #children (list (~@ (map recur children)))})))))))) + +## [Structs] +(struct: #export (Eq<Tree> Eq<a>) (All [a] (-> (Eq a) (Eq (Tree a)))) + (def: (= tx ty) + (and (:: Eq<a> = (get@ #value tx) (get@ #value ty)) + (:: (list;Eq<List> (Eq<Tree> Eq<a>)) = (get@ #children tx) (get@ #children ty))))) |