Code Monkey home page Code Monkey logo

fcore's Introduction

fcore

A Haskell implementation of the Core functional programming language from SPJ's "Implementing Functional Languages" book

Core language overview

A Core language program is a set of supercombinator definitions, with an entry point named main, e.g.:

main = double 21
double x = x + x

Supercombinators can have local definitions via let (non-recursive) and letrec (recursive):

quadruple x = let twice_x = x + x
               in twice_x + twice_x
infinite n = letrec ns = ons n ns
                 in ns

bnf

Note: I modified the language syntax somewhat to fit my personal style. Some modifications include:

  • Lambdas (from \x. x+1 to \x -> x + 1)
  • Case alternatives (from <0> -> ... to {0} -> ...)

Please look at the examples or tests for up-to-date syntax.

Lambdas

double_list xs = map (\ x -> 2*x) xs

Data types

Data types are represented uniformly with a family of constructors:

Cons{tag,arity}

For example:

Red = Cons{1,0}
Green = Cons{2,0}
Blue = Cons{3,0}

Rect = Cons{4,2}
Polar = Cons{5,2}

Leaf = Cons{6,1}
Branch = Cons{7,2}

MkNumPair = Cons{8,2}

So, in Core, instead of:

Branch (Leaf 3) (Leaf 4)

one would write:

Cons{7,2} (Cons{6,1} 3) (Cons{6,1} 4)

This, of course, is easily extendable to actual type names through a map of types to Conss, but this is just an implementation detail.

Pattern matching

Patern matching is performed via the case expression that takes a tag and an arbitrary number of arguments:

Red = Cons{1,0}
Green = Cons{2,0}
Blue = Cons{3,0}

isRed c = case c of
    {1} -> True ;
    {2} -> False ;
    {3} -> False

Leaf = Cons{1,1}
Branch = Cons{2,2}

depth t = case t of
    {1} n -> 0 ;
    {2} t1 t2 -> 1 + max (depth t1) (depth t2)

Operators

Precedence Asociativity Operator
6 Left Application
5 Right *
None /
4 Right +
None -
3 None == ~= > >= < <=
2 Right &
1 Right |

Note: Unary - is provided via the negate function.

fcore's People

Contributors

ivan-ristovic avatar

Stargazers

Vuk Amidzic avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.