如何在haskell中顯示派生樹? (how can i show a derivation tree in haskell?)


問題描述

如何在haskell中顯示派生樹? (how can i show a derivation tree in haskell?)

我正在做一個構建 while 語言派生樹的練習。我的 while 語言實現由代數數據類型組成,例如“Aexp”(算術表達式)“Bexp”(布爾表達式)和“Stm”(語句):

type  Var   =  String
data  Aexp  =  N Integer
        |  V Var
        |  Add Aexp Aexp
        |  Mult Aexp Aexp
        |  Sub Aexp Aexp
        deriving (Show, Eq)

data  Bexp  =  TRUE
        |  FALSE
        |  Eq Aexp Aexp
        |  Le Aexp Aexp
        |  Neg Bexp
        |  And Bexp Bexp
        deriving (Show, Eq)

data  Stm   =  Ass Var Aexp
        |  Skip
        |  Comp Stm Stm
        |  If Bexp Stm Stm
        |  While Bexp Stm
        |  Repeat Stm Bexp
        deriving Show

在這些代數數據類型之後,我創建了更多的代數數據類型來表示while語言程序的派生樹

type  State  =  Var ‑> Z

data Config = Inter Stm State  ‑‑ <S, s>
        | Final State      ‑‑ s

data Transition = Config :‑‑>:  State

data DerivTree = AssNS     Transition
           | SkipNS    Transition
           | CompNS    Transition DerivTree DerivTree
           | IfTTNS    Transition DerivTree
           | IfFFNS    Transition DerivTree
           | WhileTTNS Transition DerivTree DerivTree
           | WhileFFNS Transition
           | RepeatTTNS Transition 
           | RepeatFFNS Transition DerivTree DerivTree

我怎樣才能顯示這種派生樹??

<z:=x, s> ‑> s'    <x:=,s1> ‑> s''
‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑
    <z:=x; x:=y,s> ‑> s''             <y:=z,s''> ‑> s'''
    ‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑
            <z:=x; x:=y; y:=z, s> ‑> s'''

每個構造函數的期望值如下所示:

在此處輸入圖片描述


參考解法

方法 1:

Here is a simple example of using the boxes package to produce something derivation‑tree‑like. You should be able to adapt it to your needs without too much trouble ‑‑ either by producing a Tree String, or by using the ideas inside pp to produce a Box directly from your derivation trees. (I chose to use Tree String here rather than your type mainly to avoid having to figure out all the pretty‑printing details of a data type with lots of constructors.)

import Data.Tree
import Text.PrettyPrint.Boxes

pp :: Tree String ‑> Box
pp (Node here []      ) = text here
pp (Node here children) = vcat center1 [premises, separator, conclusion]
    where
    premises   = hsep 4 bottom (map pp children)
    conclusion = text here
    width      = max (cols premises) (cols conclusion)
    separator  = text (replicate width '‑')

sampleTree :: Tree String
sampleTree = Node "<z:=x; x:=y; y:=z, s> ‑> s'''"
    [Node "<z:=x; x:=y,s> ‑> s''"
        [Node "<z:=x, s> ‑> s'" []
        ,Node "<x:=,s1> ‑> s''" []
        ]
    ,Node "<y:=z, s''> ‑> s'''" []
    ]

Here's a sample run in ghci:

*Main> printBox (pp sampleTree)
<z:=x, s> ‑> s'    <x:=,s1> ‑> s''                       
‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑                       
      <z:=x; x:=y,s> ‑> s''           <y:=z, s''> ‑> s'''
‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑
              <z:=x; x:=y; y:=z, s> ‑> s'''              

(by eduardogrDaniel Wagner)

參考文件

  1. how can i show a derivation tree in haskell? (CC BY‑SA 2.5/3.0/4.0)

#functional-programming #haskell #semantics #algebraic-data-types #tree






相關問題

有沒有辦法在 C 中進行柯里化? (Is there a way to do currying in C?)

Scala 的產量是多少? (What is Scala's yield?)

功能性 javascript 和網絡瀏覽器 javascript 版本 (Functional javascript and web browser javascript versions)

從元組列表中獲取唯一路徑 (Getting Unique Paths from list of tuple)

用函數作為參數定義函數 (Defining a function with a function as an argument)

如何使用函數 VB.NET 插入數據庫? (How to use Function VB.NET Insert To Database?)

Python中列表的模式匹配 (Pattern matching of lists in Python)

如何在haskell中顯示派生樹? (how can i show a derivation tree in haskell?)

編寫一個可任意調用次數的 curried javascript 函數,該函數在最後一次函數調用時返回一個值 (Writing a curried javascript function that can be called an arbitrary number of times that returns a value on the very last function call)

我應該總是給我的函數一個返回值嗎? (Should I always give a return value to my function?)

如何在 JavaScript 中實現動態回調參數 (How can I achieve dynamic callback arguments in JavaScript)

Haskell 是否有一個函數可以創建將函數應用於列表的每個變體 (Haskell Is there a function for creating every variation of applying a function to a list)







留言討論