Difference between revisions of "Yhc/API/Core"

From HaskellWiki
< Yhc‎ | API
Jump to navigation Jump to search
Line 93: Line 93:
   
 
Here the linker ignores the import statements and the module name, of course you can do linking driven by the import statements easily.
 
Here the linker ignores the import statements and the module name, of course you can do linking driven by the import statements easily.
  +
 
=== SHOUT AT EVERYONE ===
  +
 
How do you change all strings and characters into uppercase?
  +
 
<haskell>
 
import Yhc.Core
  +
 
shout :: Core -> Core
 
shout core = mapUnderCore f core
 
where
 
f (CoreChr x) = CoreChr (toUpper x)
 
f (CoreStr x) = CoreStr (map toUpper x)
 
f x = x
 
</haskell>
   
 
=== The 42 counter ===
 
=== The 42 counter ===
Line 111: Line 126:
 
is42 (CoreInteger 42) = True
 
is42 (CoreInteger 42) = True
 
is42 _ = False
 
is42 _ = False
</haskell>
 
 
=== SHOUT AT EVERYONE ===
 
 
How do you change all strings and characters into uppercase?
 
 
<haskell>
 
import Yhc.Core
 
 
shout :: Core -> Core
 
shout core = mapUnderCore f core
 
where
 
f (CoreChr x) = CoreChr (toUpper x)
 
f (CoreStr x) = CoreStr (map toUpper x)
 
f x = x
 
 
</haskell>
 
</haskell>
   

Revision as of 21:56, 15 October 2006

Part of Yhc

(Download)

Yhc Core is a way of dumping and using the internal representation of Yhc in an external project.

Haddock Documentation

Overview

Yhc.Core is a simple core Haskell-like language, feature case statements, let statements, top level lambda's and data values. Much of the syntactic sugar present in Haskell has gone (list comprehensions, typeclasses, overloaded names, nested lambdas)

The strengths of Yhc.Core are:

  • Simple representation of Haskell
  • Relatively simple to relate Core to original Haskell
  • Source locations are preseved
  • Minimal name mangling
  • Few synactic forms

The weaknesses are:

  • Yhc cannot compile Yhc Core files, format is write only (fix is being worked on)
  • Types are not present (hard to fix, a lot of work)

The intended users:

  • Analysis tools
  • Compiler backends

Generating Core

Yhc Core files are stored as .ycr files in a binary format. They can be generated by passing the -corep flag to the compiler, yhc -corep Main.hs. If Core files are required for the standard libraries these can be build using scons core=1. To view the Core output, yhc -core Main.hs, with the output going to the screen in a pretty printed format.

The Core output

For an example of the Core output, taking the following program:

head2 (x:xs) = x

map2 f [] = []
map2 f (x:xs) = f x : map2 f xs

test x = map2 head2 x

Generates:

Sample.head2 v220 =
    case v220 of
        (:) v221 v222 -> v221
        _ -> Prelude.error Sample._LAMBDA228

Sample._LAMBDA228 =
    "Sample: Pattern match failure in function at 9:1-9:15."

Sample.map2 v223 v224 =
    case v224 of
        [] -> []
        (:) v225 v226 -> (:) (v223 v225) (Sample.map2 v223 v226)

Sample.test v227 = Sample.map2 Sample.head2 v227

Note that all names have been fully qualified, there are no infix operators, all pattern matches have been converted to case's.

Some little samples

Hello World, for Core

The following snipet loads a Core file and prints it out.

import Yhc.Core

showFile x :: FilePath -> String
showFile x = liftM show (loadCore x)

The linker

Since all Core programs are fully qualified, linking is really easy:

import Yhc.Core

linker :: [Core] -> Core
linker xs = foldr f (Core "" [] [] []) xs
   where f (Core _ _ x1 x2) (Core _ _ y1 y2) = Core "" [] (x1++y1) (x2++y2)

Here the linker ignores the import statements and the module name, of course you can do linking driven by the import statements easily.

SHOUT AT EVERYONE

How do you change all strings and characters into uppercase?

import Yhc.Core

shout :: Core -> Core
shout core = mapUnderCore f core
    where
        f (CoreChr x) = CoreChr (toUpper x)
        f (CoreStr x) = CoreStr (map toUpper x)
        f x = x

The 42 counter

How many of your functions contain the literal 42? How many times does it occur per function?

import Yhc.Core

main x = putStr $ unlines [name ++ ": " ++ show count | (name,count) <- count42 x]

count42 :: Core -> [(String,Int)]
count42 core = [(name, n) | func@(Func name _ _) <- coreFuncs core,
                            let n = length $ filter is42 $ allCore func, n /= 0]

is42 :: CoreExpr -> Bool
is42 (CoreInt 42) = True
is42 (CoreInteger 42) = True
is42 _ = False

See Also

Users

  • Catch - Case and Termination Checker for Haskell by Neil Mitchell. This takes Core as an input.
  • Dr Haskell - give hints to beginners about useful Haskell functions/idioms.