Confusing behavior when a binding form is also an expression

オープン
#97 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
45/100
issue の種類
ドキュメント
明瞭さ
明確に書かれている
活発さ
停滞
領域
documentation

調査の方向性

ドキュメントの lc-bind+patterns の例から始め、list と listp の使い方を報告された LCP の例と比較してください。Redex が binding と use を構文的に区別することを簡潔に説明し、この動作と異なる形式が存在する理由が読者に明確になった時点で、ドキュメントは完成したとみなしてください。

索引モデルが issue の本文から書いたものです。

説明

I was confused by some behavior when working on a test case for the issue I opened yesterday. I formulated a simple lambda-calculus language with pattern-matching on tuples (shown below). It is essentially the same as the lc-bind+patterns example in the docs, except I used tuple where lc-bind+patterns uses list and listp not understanding the importance of that distinction.

What I've inferred is that having a binding form (that is, something with a #:exports clause) that also looks like an expression is trouble. Identifiers that look like uses will be interpreted as bindings.

I don't think this behavior is itself an issue, but I could see it tripping up another person like me that tries to use the same syntax for both pattens and expressions. Pointing the behavior out in the documentation would have helped me; maybe a sentence like

"Variable uses and bindings must always be distinguished syntactically, which is why we use list and listp instead of just list."

or

"Redex determines whether an identifier is a binding or a use syntactically; as a consequence, if a form may contain both types of variables some form of distinguishing syntax must be used. Here, we use list and listp to mark that when an identifier appears in an expression (signified by list) it is a reference, while when it appears in a pattern (listp) it is a binding."

would do the trick?

#lang racket

(require redex)

(define-language LCP
  (M (M M) X (λ P M) (tuple M ...) b)
  (b number)
  (V (λ P M) b (tuple V ...))
  (X variable-not-otherwise-mentioned)
  (P X b (tuple P ...))
  (E hole (E M) (V E) (tuple V ... E M ...))
  (ρ ([X V] ...))
  (⊥ bot)
  #:binding-forms
  (tuple P ...) #:exports (shadow P ...)
  (λ P M #:refers-to P))

(define ->
  (reduction-relation
   LCP
   (==> ((λ P M) V)
        (subst-all ρ M)
        (where ρ (match P V)))
   with
   [(--> (in-hole E x) (in-hole E y))
    (==> x y)]))

(define-metafunction LCP
  match : P V -> ρ or ⊥
  [(match b b) ()]
  [(match X V) ([X V])]
  [(match (tuple P ..._1) (tuple V ..._1))
   ([X_bind V_bind] ... ...)
   (where (([X_bind V_bind] ...) ...) ((match P V) ...))]
  [(match P V) bot])

(define-metafunction LCP
  subst-all : ρ M -> M
  [(subst-all () M) M]
  [(subst-all ([X_0 V_0] [X_1 V_1] ...) M)
   (subst-all ([X_1 V_1] ...) (substitute M X_0 V_0))])

(define-term t
    (λ (tuple x 2)
      (tuple x x)))

(term (substitute t x hello) #:lang LCP)
;; => '(λ (tuple x«0» 2) (tuple x«1» x«2»))
主要言語
Racket
スター
113
フォーク
43
PR マージ指標
30日以内にマージされた PR はありません

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

racket/redex のほかの issue

racket/redex の issue をすべて見る

似ている issue

Documentation の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。