Personal tools

Yhc/API/Core

From HaskellWiki

< Yhc | API(Difference between revisions)
Jump to: navigation, search
(Blog Posts)
Line 1: Line 1:
 
{{Yhc}}
 
{{Yhc}}
   
Yhc Core is a way of dumping and using the internal representation of Yhc in an external project. The code related to this, including data types, is found in src/compiler/Core directory of the Yhc source code.
+
Yhc Core is a way of dumping and using the internal representation of Yhc in an external project.
   
 
== Haddock Documentation ==
 
== Haddock Documentation ==
Line 7: Line 7:
 
* http://www.cs.york.ac.uk/fp/yhc/snapshot/docs/Yhc-Core.html
 
* http://www.cs.york.ac.uk/fp/yhc/snapshot/docs/Yhc-Core.html
   
== Blog Posts ==
+
== Overview ==
   
The following sequence of blog posts probably gives a useful introduction to Yhc Core.
+
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)
   
* http://yhc06.blogspot.com/2005/12/yhc-core.html
+
The strengths of Yhc.Core are:
* http://yhc06.blogspot.com/2006/07/yhc-core-v2.html
+
* http://yhc06.blogspot.com/2006/09/yhccore-api-available.html
+
* 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 ==
 
== Generating Core ==
   
To generate Core, simply give the compiler the option <tt>-core</tt> or <tt>-corep</tt>. For example, when compiling
+
Yhc Core files are stored as .ycr files in a binary format. They can be generated by passing the -corep flag to the compiler, <tt>yhc -corep Main.hs</tt>. If Core files are required for the standard libraries these can be build using <tt>scons core=1</tt>. To view the Core output, <tt>yhc -core Main.hs</tt>, 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:
  +
  +
<haskell>
  +
head2 (x:xs) = x
  +
  +
map2 f [] = []
  +
map2 f (x:xs) = f x : map f xs
  +
  +
test x = map2 head2 x
  +
</haskell>
   
module FibMain where
+
Generates:
   
main xs = pam daeh xs
+
<haskell>
  +
Sample.head2 v220 =
  +
case v220 of
  +
(:) v221 v222 -> v221
  +
_ -> Prelude.error Sample._LAMBDA228
   
daeh (x:xs) = x
+
Sample._LAMBDA228 =
  +
"Sample: Pattern match failure in function at 9:1-9:15."
   
pam f [] = []
+
Sample.map2 v223 v224 =
pam f (x:xs) = f x : pam f xs
+
case v224 of
  +
[] -> []
  +
(:) v225 v226 -> (:) (v223 v225) (Sample.pam v223 v226)
   
You get the output
+
Sample.test v227 = Sample.map2 Sample.head2 v227
  +
</haskell>
   
$ yhc -core FibMain.hs
 
====== Human Readable Core:
 
FibMain.pam v220 v221 =
 
case v221 of
 
Prelude.[] -> (Prelude.[])
 
Prelude.: v222 v223 ->
 
(Prelude.: (YHC.Internal._apply1 v220 v222) (FibMain.pam v220 v223))
 
   
FibMain.daeh v224 =
+
== Some little samples ==
case v224 of
 
Prelude.: v225 v226 -> v225
 
_ -> (Prelude.error (LAMBDA228))
 
   
LAMBDA228 =
+
=== Hello World, for Core ===
(prim_STRING "FibMain: Pattern match failure in function at 7:1-7:15.")
 
   
FibMain.main v227 = (FibMain.pam FibMain.daeh v227)
+
The following snipet loads a Core file and prints it out.
   
The <tt>-core</tt> option gives human readable Core output, in a style very similar to Haskell. The <tt>-corep</tt> option gives a <tt>deriving Show</tt> output of the Core, which can be read back in with <tt>read</tt> very easily. The machine Core also contains source code locations for statements.
+
<haskell>
  +
import Yhc.Core
  +
showFile x :: FilePath -> String
  +
showFile x = liftM show (loadCore x)
  +
</haskell>
   
== Compared to [[GHC]] Core ==
+
== See Also ==
   
* '''Types''' - Yhc has none, GHC has lots.
+
* [http://www.haskell.org/ghc/docs/latest/html/users_guide/ext-core.html GHC Core]
* '''Familiarity''' - Yhc looks like Haskell, GHC is somewhat related, but not as close.
 
* '''Name mangling''' - Yhc preserves names much better.
 
* '''Source locations''' - Yhc keeps these, GHC doesn't.
 
   
 
== Users ==
 
== Users ==

Revision as of 21:36, 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.

Contents

1 Haddock Documentation

2 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

3 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.

4 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 : map 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.pam v223 v226)
 
Sample.test v227 = Sample.map2 Sample.head2 v227


5 Some little samples

5.1 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)

6 See Also

7 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.