microscope.tri (1108B)
1 !import bd2c14d6db6b69308a5252a8e839705a55d30e815f012900b094315263e08d81 as B 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 = B.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 count tree acc = (B.triage 16 (B.succ acc) 17 (child : count child (B.succ acc)) 18 (left right : count right (count left (B.succ acc))) 19 tree) 20 21 size tree = count tree 0 22 23 same a b = (B.triage 24 (B.triage B.true (ignored : B.false) (ignored ignored2 : B.false) b) 25 (x : B.triage B.false (y : same x y) (ignored ignored2 : B.false) b) 26 (x y : B.triage B.false (ignored : B.false) (u v : B.and? (same x u) (same y v)) b) 27 a)