packages feed

ruler-core-1.0: examples/Sum.rul

{
{-# LANGUAGE BangPatterns #-}
module Sum where

import Control.Monad.Error
}

itf S
  visit v1
    inh l :: [Int]
  visit v2
    syn s :: Int

{
sum' = sem sum : S monad Either String
         visit v1
           clause sumNil
             lhs.s = 0
             match [] = lhs.l

           clause sumCons
             tl.l = loc.xs
             lhs.s = loc.x + tl.s
             match (loc.x : loc.xs) = lhs.l
             attach v1 of tl : S = sum'
}

{
test :: [Int] -> Either String Int
test xs = do
  let inh1 = Inh_S_v1 { l_Inh_S = xs }
  syn1 <- invoke_S_v1 sum' inh1
  let inh2 = Inh_S_v2 { }
  syn2 <- invoke_S_v2 (next_S_v1 syn1) inh2
  let s = s_Syn_S syn2
  return s

main :: IO ()
main = putStrLn (show (test [1..100000]))
}