Commit 08fb81a3 by Michael Hanus

### Nice interpreter and pretty ICurry output, Arithmetic example added

parent 09fb28ea
Arithmetic.curry 0 → 100644
 ------------------------------------------------------------------------------ -- Example with arithmetic computations to demonstrate the treatment -- of external operations. Note that this works with the -- ICurry interpreter in order to avoid the compilation -- of the complete Curry prelude. ------------------------------------------------------------------------------ (+) :: Int -> Int -> Int x + y = (prim_Int_plus \$# y) \$# x -- The primitive addition `prim_Int_plus` is treated by the ICurry interpreter. prim_Int_plus :: Int -> Int -> Int prim_Int_plus external (*) :: Int -> Int -> Int x * y = (prim_Int_mult \$# y) \$# x -- The primitive addition `prim_Int_mult` is treated by the ICurry interpreter. prim_Int_mult :: Int -> Int -> Int prim_Int_mult external ------------------------------------------------------------------------------ -- Combining arithmetic and non-determinism: coin :: Int coin = 0 coin = 1 coinCoin = coin + coin double :: Int -> Int double x = x + x doubleCoin = double coin ------------------------------------------------------------------------------ -- Higher-order with arithmetic operations. map :: (a -> b) -> [a] -> [b] map _ [] = [] map f (x : xs) = f x : map f xs inc123 :: [Int] inc123 = normalForm (map (1+) [1,2,3]) foldr :: (a -> b -> b) -> b -> [a] -> b foldr _ z [] = z foldr f z (x:xs) = f x (foldr f z xs) fac5 :: Int fac5 = foldr (*) 1 [1,2,3,4,5] ------------------------------------------------------------------------------
 ... ... @@ -93,6 +93,6 @@ one can wrap it with the function `normalForm`, e.g., mymain = normalForm (reverse [1,2,3]) The current version of the interpreter supports only the prelude operations `normalForm` and `\$!`. operations `normalForm`, `\$#`, and `\$!`. ----------------------------------------------------------------------------
 ... ... @@ -349,17 +349,17 @@ testIdTrue :: Prop testIdTrue = evalFunND exampleProg "idTrue" <~> "True" testCoinList :: Prop testCoinList = evalFunND exampleProg "coinList" <~> ("(: 1 [])" ? "(: 2 [])") testCoinList = evalFunND exampleProg "coinList" <~> ("(1 : [])" ? "(2 : [])") testCoinCoinList :: Prop testCoinCoinList = evalFunND exampleProg "coinCoinList" <~> ("(: 1 (: 1 []))" ? "(: 2 (: 1 []))" ? "(: 1 (: 2 []))" ? "(: 2 (: 2 []))") ("(1 : (1 : []))" ? "(2 : (1 : []))" ? "(1 : (2 : []))" ? "(2 : (2 : []))") testPerm123 :: Prop testPerm123 = evalFunND exampleProg "perm123" <~> ("(: 1 (: 2 (: 3 [])))" ? "(: 1 (: 3 (: 2 [])))" ? "(: 2 (: 1 (: 3 [])))" ? "(: 2 (: 3 (: 1 [])))" ? "(: 3 (: 1 (: 2 [])))" ? "(: 3 (: 2 (: 1 [])))") ("(1 : (2 : (3 : [])))" ? "(1 : (3 : (2 : [])))" ? "(2 : (1 : (3 : [])))" ? "(2 : (3 : (1 : [])))" ? "(3 : (1 : (2 : [])))" ? "(3 : (2 : (1 : [])))") ------------------------------------------------------------------------------
 ... ... @@ -132,7 +132,7 @@ replaceNode (Graph nodes mx) oldnid newid = -- Shows the expression represented by the graph starting with a given node. -- In order to visualize sharing, shared subexpressions are shown -- by let expressions. -- as let expressions. showGraphExp :: Graph -> NodeID -> String showGraphExp g nid = showExp [] 10 nid where ... ... @@ -150,11 +150,20 @@ showGraphExp g nid = showExp [] 10 nid then "" else "let {" ++ intercalate " ; " (map showLetDecl arglets) ++ "} in ") ++ unwords (f : map (\a -> if a `elem` alllets then showVar a else showExp alllets (d-1) a) args) ++ showCall f (map (\a -> if a `elem` alllets then showVar a else showExp alllets (d-1) a) args) ++ ")" where showCall f args = if isInfixOp f then case args of [a1,a2] -> unwords [a1,f,a2] _ -> unwords (('(' : f ++ ")") : args) else unwords (f : args) where isInfixOp = all (`elem` "!@#\$%^&*+-=<>?./|\\:") arglets = nub (concatMap (\a -> let occs = occursInGraphExp d a ni in if occs < 2 || isConst a then [] else [a]) ... ...