microscope.tri (1095B)
1 !import bd2c14d6db6b69308a5252a8e839705a55d30e815f012900b094315263e08d81 * 2 !export shape 3 !export size 4 !export same 5 6 -- Inspect data OR compiled program trees, not source text. 7 -- Try shape (t t t), size (t t t), or same (t t) (t t). 8 -- Also try shape (x : x): functions are inspectable trees too. 9 -- triage dispatches directly on Leaf, Stem and Fork without visiting children. 10 -- Arithmetic and Booleans come from base; traversal is the subject of this demo. 11 shape tree = triage "Leaf" (child : "Stem") (left right : "Fork") tree 12 13 -- Count every constructor occurrence, including repeated shared branches. 14 -- This is expanded tree size, not allocated nodes or source-code length. 15 countNodes tree acc = triage 16 (succ acc) 17 (child : countNodes child <| succ acc) 18 (left right : succ acc |> countNodes left |> countNodes right) 19 tree 20 21 size tree = countNodes tree 0 22 23 same a b = triage 24 (triage true (ignored : false) (ignored ignored2 : false) b) 25 (x : triage false (y : same x y) (ignored ignored2 : false) b) 26 (x y : triage false (ignored : false) (u v : and? (same x u) (same y v)) b) 27 a