Algebraic Expression Languages   [9]

Calculation trace semantics

The other direction from abstract: can give elaborated "meaning" which includes more information, e.g., a trace of how the value might have been calculated.

eval = fold id sem

pprt = fold show (inf . syn)

inf o l r = par (concat [l, o, r])
par s = "(" ++ s ++ ")"


trace = fold sing comb  where sing k = [Lit k]

comb op ls@(l:_) (r:tr) = let (·) = Bop op in
  Lit (eval (l·r)) : map (·r) ls ++ map (last ls ·) tr


disp (e:es) = ("\n   " ++ e) : map ("\n = " ++) es

calc = putStrLn . concat . disp . map pprt . reverse . trace

Notes