mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-17 14:57:03 +00:00
44 lines
1.4 KiB
Mathematica
44 lines
1.4 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
%
|
|
% Main authors: conway, fjh.
|
|
%
|
|
% This file provides a 'tree' data type.
|
|
% The code generater uses this to build a tree of instructions and
|
|
% then flatten them into a list.
|
|
%
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module tree.
|
|
:- import_module list.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- interface.
|
|
:- type tree(T) ---> empty
|
|
; node(T)
|
|
; tree(tree(T), tree(T)).
|
|
|
|
:- pred tree__flatten(tree(T), list(T)).
|
|
:- mode tree__flatten(in, out) is det.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
tree__flatten(T, L) :-
|
|
tree__flatten_2(T, [], L).
|
|
|
|
:- pred tree__flatten_2(tree(T), list(T), list(T)).
|
|
:- mode tree__flatten_2(in, in, out) is det.
|
|
% flatten_2(T, L0, L) is true iff L is the list that results from
|
|
% traversing T left-to-right depth-first, and then appending L0.
|
|
tree__flatten_2(empty, L, L).
|
|
tree__flatten_2(node(T), L, [T|L]).
|
|
tree__flatten_2(tree(T1,T2), L0, L) :-
|
|
tree__flatten_2(T2, L0, L1),
|
|
tree__flatten_2(T1, L1, L).
|
|
|
|
%-----------------------------------------------------------------------------%
|