cdparks/mini-core

语言: Haskell

git: https://github.com/cdparks/mini-core

核心语言的实现
An implementation of the Core language
README.md (中文)

微型芯

概观

Mini-Core最初是作为Simon Peyton Jones和David Lester的“实现功能语言:教程”中描述的核心语言的实现而开始的。我通过添加let-polymorphic类型推断和一些用于指定和匹配数据构造函数的具体语法略微分歧。

用法

mini-core将文件编译为G代码并在虚拟G-Machine中执行。

Usage: mini-core [OPTION...] file
  -h  --help         Print usage and exit
      --show-parse   Show program after parsing
      --show-types   Show types after type-checking
      --show-simple  Show program after constructor generation and lambda lifting
      --show-g-code  Show G-code after compilation
      --show-states  Dump all machine states
      --interactive  Print each machine state one at a time as program executes

示例程序

程序只是一系列超级组合器。通过减少supercombinator main来执行执行。使用标记的构造函数支持简单的代数数据类型。

-- A list is either empty or a pair containing a value and another list
data List a = Nil | Cons a (List a);

-- mini-core is non-strict; we can construct infinite data structures
infinite x = Cons x (infinite (x + 1));

-- Case expressions make a multi-way branch based on the scrutinee's
-- tag and bind its components to the names preceding the arrow

-- take yields the first n elements of a (possibly infinite) list
take n ls = if (n <= 0)
                Nil
                (case ls of {
                    Cons x xs -> Cons x (take (n - 1) xs);
                    Nil       -> Nil;
                });

-- map applys a function f to each element in a list yielding a list of the
-- same size
map f ls = case ls of {
    Cons x xs -> Cons (f x) (map f xs);
    Nil       -> Nil;
};

-- Print the squares of the first 5 natural numbers
-- As in Haskell, we use \ to introduce an anonymous function
main = map (\x -> x * x) (take 5 (infinite 1))

在此程序上运行编译器会生成以下输出:

(Cons 0 (Cons 1 (Cons 4 (Cons 9 (Cons 16 Nil)))))

使用--show-types,我们还得到以下输出:

==================== Type Inference ====================
Cons :: forall a. a -> List a -> List a
False :: Bool
Nil :: forall a. List a
True :: Bool
infinite :: Int -> List Int
main :: List Int
map :: forall a b. (a -> b) -> List a -> List b
take :: forall a. Int -> List a -> List a

参考

  • 实现功能语言:Simon Peyton Jones和David Lester的教程
  • Simon Peyton Jones实现的函数式编程语言
  • 算法W由Martin Grabmuller逐步完成
  • 在马克斯·琼斯的Haskell中输入Haskell

本文使用googletrans自动翻译,仅供参考, 原文来自github.com

en_README.md

Mini-Core

Overview

Mini-Core started as an implementation of the Core language described in Implementing Functional Languages: A Tutorial by Simon Peyton Jones & David Lester. I've diverged slightly by adding let-polymorphic type inference and some concrete syntax for specifying and matching on data constructors.

Usage

mini-core compiles a file to G-code and executes it in a virtual G-Machine.

Usage: mini-core [OPTION...] file
  -h  --help         Print usage and exit
      --show-parse   Show program after parsing
      --show-types   Show types after type-checking
      --show-simple  Show program after constructor generation and lambda lifting
      --show-g-code  Show G-code after compilation
      --show-states  Dump all machine states
      --interactive  Print each machine state one at a time as program executes

Example Program

A program is just a sequence of supercombinators. Execution proceeds by reducing the supercombinator main. Simple algebraic data types are supported using tagged constructors.

-- A list is either empty or a pair containing a value and another list
data List a = Nil | Cons a (List a);

-- mini-core is non-strict; we can construct infinite data structures
infinite x = Cons x (infinite (x + 1));

-- Case expressions make a multi-way branch based on the scrutinee's
-- tag and bind its components to the names preceding the arrow

-- take yields the first n elements of a (possibly infinite) list
take n ls = if (n <= 0)
                Nil
                (case ls of {
                    Cons x xs -> Cons x (take (n - 1) xs);
                    Nil       -> Nil;
                });

-- map applys a function f to each element in a list yielding a list of the
-- same size
map f ls = case ls of {
    Cons x xs -> Cons (f x) (map f xs);
    Nil       -> Nil;
};

-- Print the squares of the first 5 natural numbers
-- As in Haskell, we use \ to introduce an anonymous function
main = map (\x -> x * x) (take 5 (infinite 1))

Running the compiler on this program produces the following output:

(Cons 0 (Cons 1 (Cons 4 (Cons 9 (Cons 16 Nil)))))

With --show-types we also get the following output:

==================== Type Inference ====================
Cons :: forall a. a -> List a -> List a
False :: Bool
Nil :: forall a. List a
True :: Bool
infinite :: Int -> List Int
main :: List Int
map :: forall a b. (a -> b) -> List a -> List b
take :: forall a. Int -> List a -> List a

References