Coder Social home page Coder Social logo

applideveloper / egison Goto Github PK

View Code? Open in Web Editor NEW

This project forked from egison/egison

0.0 0.0 0.0 1.11 MB

Egison is a purely functional programming language that realized non-linear pattern-matching against unfree data types. We can directly pattern-match against a wide range of data types such as lists, multisets, sets, trees and graphs with Egison.

Home Page: http://www.egison.org

License: MIT License

egison's Introduction

The Egison Programming Language

Egison is the pattern-matching-oriented, purely functional programming language. We can directly represent pattern-matching against lists, multisets, sets, trees, graphs and any kind of data types. This is the repository of the interpreter of Egison.

For more information, visit Egison website. You can try Egison online!

If you get interested in Egison, please contact Satoshi Egi or tweet to @Egison_Lang.

Getting Started!

At first, you should install Haskell Platform.

After you installed Haskell Platform, run the following commands on the terminal.

% cabal update
% cabal install egison

Now, you can try Egison.

% egison
Egison Version X.X.X(C) 2011-2014 Satoshi Egi
http://www.egison.org
Welcome to Egison Interpreter!
> ^D
Leaving Egison Interpreter.

If you are a beginner of Egison, it would be better to install egison-tutorial.

% cabal update
% cabal install egison-tutorial
% egison-tutorial
Egison Tutorial for Version X.X.X (C) 2013-2014 Satoshi Egi
http://www.egison.org
Welcome to Egison Tutorial!
==============================
List of sections in the tutorial
1: Calculate numbers
2: Basics of functional programming
3: Define your own functions
4: Basic of pattern-matching
5: Pattern-matching against infinite collections
6: Writing scripts in Egison
==============================
Choose a section to learn.
(1-6): 5
====================
We can write a pattern-matching against infinite lists even if that has infinite results.
Note that Egison really enumerates all pairs of two natural numbers in the following example.

Examples:
  (take 10 (match-all nats (set integer) [<cons $m <cons $n _>> [m n]]))
====================
>

We can try it also online. Enjoy!

Demonstrations

We can do non-linear pattern-matching against unfree data types in Egison. An unfree data type is a data type whose data have no canonical form, a standard way to represent that object. It enables us to write more elegant programs.

Twin Primes

We can use pattern-matching for enumeration. The following code enumerates all twin primes from the infinite list of prime numbers with pattern-matching!

(define $twin-primes
  (match-all primes (list integer)
    [<join _ <cons $p <cons ,(+ p 2) _>>>
     [p (+ p 2)]]))

;; Enumerate first 10 twin primes
(take 10 twin-primes)
;=>{[3 5] [5 7] [11 13] [17 19] [29 31] [41 43] [59 61] [71 73] [101 103] [107 109]}

Poker Hands

The following code is the program that determines poker-hands written in Egison. All hands are expressed in a single pattern.

(define $poker-hands
  (lambda [$cs]
    (match cs (multiset card)
      {[<cons <card $s $n>
         <cons <card ,s ,(- n 1)>
          <cons <card ,s ,(- n 2)>
           <cons <card ,s ,(- n 3)>
            <cons <card ,s ,(- n 4)>
             <nil>>>>>>
        <Straight-Flush>]
       [<cons <card _ $n>
         <cons <card _ ,n>
          <cons <card _ ,n>
            <cons <card _ ,n>
              <cons _
                <nil>>>>>>
        <Four-of-Kind>]
       [<cons <card _ $m>
         <cons <card _ ,m>
          <cons <card _ ,m>
           <cons <card _ $n>
            <cons <card _ ,n>
              <nil>>>>>>
        <Full-House>]
       [<cons <card $s _>
         <cons <card ,s _>
           <cons <card ,s _>
             <cons <card ,s _>
               <cons <card ,s _>
                 <nil>>>>>>
        <Flush>]
       [<cons <card _ $n>
         <cons <card _ ,(- n 1)>
          <cons <card _ ,(- n 2)>
           <cons <card _ ,(- n 3)>
            <cons <card _ ,(- n 4)>
             <nil>>>>>>
        <Straight>]
       [<cons <card _ $n>
         <cons <card _ ,n>
          <cons <card _ ,n>
           <cons _
            <cons _
             <nil>>>>>>
        <Three-of-Kind>]
       [<cons <card _ $m>
         <cons <card _ ,m>
          <cons <card _ $n>
            <cons <card _ ,n>
             <cons _
               <nil>>>>>>
        <Two-Pair>]
       [<cons <card _ $n>
         <cons <card _ ,n>
          <cons _
           <cons _
            <cons _
             <nil>>>>>>
        <One-Pair>]
       [<cons _
         <cons _
          <cons _
           <cons _
            <cons _
             <nil>>>>>>
        <Nothing>]})))

(poker-hands {<Card <Club> 12>
              <Card <Club> 10>
              <Card <Club> 13>
              <Card <Club> 1>
              <Card <Club> 11>});=><Straight-Flush>

(poker-hands {<Card <Diamond> 1>
              <Card <Club> 2>
              <Card <Club> 1>
              <Card <Heart> 1>
              <Card <Diamond> 2>});=><Full-House>

(poker-hands {<Card <Diamond> 4>
              <Card <Club> 2>
              <Card <Club> 5>
              <Card <Heart> 1>
              <Card <Diamond> 3>});=><Straight>

(poker-hands {<Card <Diamond> 4>
              <Card <Club> 10>
              <Card <Club> 5>
              <Card <Heart> 1>
              <Card <Diamond> 3>});=><Nothing>

Isn't it exciting? The pattern-matching of Egison is very powerful. We can use it for pattern-matching against graphs or tree-structures such as XML. Egison is not famous at all now. Please help us to make Egison popular.

Comparison with Related Work

There are a lot of existing work for pattern-matching.

The advantage of Egison is that it realized all of the following features at the same time.

  • Modularization of the way of pattern-matching for each data type
  • Pattern-matching with multiple results (backtracking)
  • Non-linear pattern-matching

It enables us to express non-linear pattern-matching against unfree data types intuitively as above demonstrations.

Furthermore, Egison realized the following feature. We can even modularize patterns like functions keeping above features.

  • Non-linear pattern-matching with lexical scoping

The Pattern-Matching Mechanism section in Egison developer's manual explains how we realized that.

Please read our paper on arXiv.org for details.

Note for Developers

How to Run Test

% cabal install --enable-tests
% cabal test

How to Profile the Interpreter

% sudo apt-get install haskell-platform-doc haskell-platform-prof
% cabal install --enable-library-profiling --enable-executable-profiling
% egison +RTS -p -RTS -l sample/sequence.egi
% cat egison.prof

Acknowledgement

I thank Ryo Tanaka, Takahisa Watanabe, Takuya Kuwahara and Kentaro Honda for their help to implement the interpreter.

License

Copyright (c) 2011-2014, Satoshi Egi

Egison is released under the MIT license.

I used husk-scheme by Justin Ethier as reference to implement the base part of the previous version of the interpreter.

Sponsors

Egison is sponsored by Rakuten, Inc. and Rakuten Institute of Technology.

egison's People

Contributors

egisatoshi avatar alpicola avatar xenophobia avatar bfontaine avatar ggreif avatar takei-shg avatar tomoasleep avatar rns 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.