[G1-Sync] Manual knowledge update
This commit is contained in:
@@ -1,91 +1,352 @@
|
||||
---
|
||||
id: wiki-2026-0508-domain-specific-languages
|
||||
title: Domain Specific Languages
|
||||
id: wiki-2026-0508-dsl
|
||||
title: Domain-Specific Languages (DSL)
|
||||
category: 10_Wiki/Topics
|
||||
status: needs_review
|
||||
status: verified
|
||||
canonical_id: self
|
||||
aliases: [P-Reinforce-AUTO-DSLS-001]
|
||||
aliases: [DSL, internal DSL, external DSL, fluent API, embedded DSL, NL-to-DSL]
|
||||
duplicate_of: none
|
||||
source_trust_level: A
|
||||
confidence_score: 0.96
|
||||
tags: [auto-reinforced, dsl, domain-specific-language, metaprogramming, abstraction, syntax, compiler]
|
||||
confidence_score: 0.9
|
||||
verification_status: applied
|
||||
tags: [dsl, programming-languages, abstraction, fluent-api, ddd, sql, regex, llm-dsl]
|
||||
raw_sources: []
|
||||
last_reinforced: 2026-04-20
|
||||
last_reinforced: 2026-05-10
|
||||
github_commit: pending
|
||||
inferred_by: Claude Opus 4.7 (auto-normalize 2026-05-08)
|
||||
tech_stack:
|
||||
language: unspecified
|
||||
framework: unspecified
|
||||
language: any
|
||||
framework: ANTLR / PEG / Tree-sitter
|
||||
---
|
||||
|
||||
# [[Domain-Specific-Languages|Domain-Specific-Languages]]
|
||||
# DSL (Domain-Specific Language)
|
||||
|
||||
## 📌 한 줄 통찰 (The Karpathy Summary)
|
||||
> "맞춤형 대화 수단: 범용 언어(C, Java)의 거추장스러움을 벗어던지고, 특정 도메인(SQL, HTML)의 문제 해결에만 최적화된 문법을 설계하여 비전문가도 최소한의 표현으로 복잡한 논리를 기술하게 만드는 언어적 도구."
|
||||
## 매 한 줄
|
||||
> **"매 specific domain 의 optimized 의 language"**. 매 SQL, regex, HTML, CSS 의 omnipresent. 매 internal (embedded) vs external. 매 modern: 매 LLM 의 NL → DSL 의 best interface. 매 IaC 의 Terraform / config DSL 의 boom.
|
||||
|
||||
## 📖 구조화된 지식 (Synthesized Content)
|
||||
특수 목적 언어(Domain-Specific-Languages, DSL)는 특정 영역의 문제 해결에 특화된 프로그래밍 언어나 사양 언어입니다.
|
||||
## 매 핵심
|
||||
|
||||
1. **유형 구분**:
|
||||
* **External DSL**: 완전히 새로운 문법을 가진 컴파일러/인터프리터 필요 (예: SQL, CSS, Regex).
|
||||
* **Internal DSL (Embedded)**: 기존 언어(Ruby, Kotlin, TS)의 문법 안에서 유연하게 정의 (예: [[CSS-in-JS|CSS-in-JS]], Gradle, [[Testing|Testing]] frameworks).
|
||||
2. **왜 중요한가?**:
|
||||
* **High Abstraction**: 도메인 전문가와 개발자 사이의 소통 비용 정책을 줄이고, 선언적 방식(What)으로 로직 정책을 기술할 수 있기 때문임. (Communication와 연결)
|
||||
### 매 type
|
||||
|
||||
## ⚠️ 모순 및 업데이트 (Contradictions & Updates)
|
||||
- **과거 데이터와의 충돌**: 과거에는 DSL을 만드는 것이 '바퀴를 재발명하는 것'처럼 비효율적이라 여겼으나, 현대 정책은 복잡한 설정 정책(Config)이나 인프라 제어 정책(IaC)을 위해 목적에 맞는 DSL 정책 모델을 구축하는 것이 유지보수성 정책 면에서 압도적으로 유리함을 인정함(RL Update).
|
||||
- **정책 변화(RL Update)**: 이제는 단순 텍스트 기반 DSL 정책을 넘어, AI 가 자연어 정책을 DSL 정책으로 변환(Natural-Language-to-DSL)하여 시스템을 제어하는 인간-AI 인터페이스 정책의 핵심 기술로 진화 중임. ([[Natural-Language-Processing|Natural-Language-Processing]] (NLP)와 연결)
|
||||
#### External DSL
|
||||
- 매 own grammar.
|
||||
- 매 parser / compiler 필요.
|
||||
- 예: SQL, regex, CSS, GraphQL, Gherkin, HCL (Terraform).
|
||||
|
||||
## 🔗 지식 연결 (Graph)
|
||||
- Communication, Natural-Language-[[Processing|Processing]] (NLP), [[Technical-Architecture|Technical-Architecture]], Standard-Operating-Procedure, [[Logic|Logic]]
|
||||
- **Key Examples**: SQL, HTML, Markdown, Terraform (HCL), RxJS.
|
||||
---
|
||||
#### Internal DSL (Embedded)
|
||||
- 매 host language 의 문법 안.
|
||||
- 매 fluent API.
|
||||
- 예: jQuery, RSpec, Gradle (Kotlin DSL), zod schema.
|
||||
|
||||
## 🤖 LLM 활용 힌트 (How to Use This Knowledge)
|
||||
### 매 design principle
|
||||
1. **Domain-aligned terminology**.
|
||||
2. **Declarative over imperative** ("what" not "how").
|
||||
3. **Composable**.
|
||||
4. **Readable by domain expert**.
|
||||
5. **Constrained** (less Turing-complete = better verification).
|
||||
|
||||
**언제 이 지식을 쓰는가:**
|
||||
- *(TODO)*
|
||||
### 매 famous DSL
|
||||
- **SQL**: 매 query.
|
||||
- **Regex**: 매 pattern.
|
||||
- **CSS**: 매 styling.
|
||||
- **HTML / JSX**: 매 markup.
|
||||
- **GraphQL**: 매 query.
|
||||
- **Cypher** (Neo4j): 매 graph.
|
||||
- **HCL** (Terraform): 매 IaC.
|
||||
- **Gherkin**: 매 BDD.
|
||||
- **Mermaid / PlantUML**: 매 diagram.
|
||||
- **dbt SQL**: 매 transformation.
|
||||
|
||||
**언제 쓰면 안 되는가:**
|
||||
- *(TODO)*
|
||||
### 매 modern (LLM era)
|
||||
|
||||
## 🧪 검증 상태 (Validation)
|
||||
#### NL-to-DSL
|
||||
- 매 natural language → 매 SQL / regex / etc.
|
||||
- 매 GitHub Copilot regex helper.
|
||||
- 매 Text2SQL 의 mature.
|
||||
|
||||
- **정보 상태:** needs_review
|
||||
- **출처 신뢰도:** A
|
||||
- **검토 이유:** *(P-Reinforce Phase 1 자동 정규화. 본문 검증 필요.)*
|
||||
#### LLM-driven DSL
|
||||
- 매 LLM 의 better understand 매 specific DSL.
|
||||
- 매 internal company DSL 의 valuable.
|
||||
|
||||
## 🧬 중복 검사 (Duplicate Check)
|
||||
### 매 tooling
|
||||
- **ANTLR**: 매 parser generator.
|
||||
- **PEG.js / Peggy**: 매 PEG-based.
|
||||
- **Tree-sitter**: 매 modern incremental.
|
||||
- **Lark** (Python).
|
||||
- **MPS** (JetBrains): 매 projectional.
|
||||
|
||||
- **기존 유사 문서:** *(TODO: 인덱서 클러스터 리포트 참조)*
|
||||
- **처리 방식:** UPDATE (자동 정규화)
|
||||
- **처리 이유:** Phase 1 정규화 — 옛 템플릿/누락 필드 보강.
|
||||
### 매 응용
|
||||
1. **Configuration** (Helm, Terraform).
|
||||
2. **Workflow** (Airflow, GitHub Actions YAML).
|
||||
3. **Test specification** (Gherkin, RSpec).
|
||||
4. **Build** (Gradle, Bazel).
|
||||
5. **Domain modeling** (DDD ubiquitous language).
|
||||
6. **Hardware** (Verilog, VHDL).
|
||||
7. **Math / AI** (TLA+, Lean).
|
||||
|
||||
## 🕓 변경 이력 (Changelog)
|
||||
## 💻 패턴
|
||||
|
||||
| 날짜 | 변경 내용 | 처리 방식 | 신뢰도 |
|
||||
|------|-----------|-----------|--------|
|
||||
| 2026-05-08 | P-Reinforce Phase 1 정규화 (frontmatter + 헤더 표준화) | UPDATE | A |
|
||||
### Internal DSL (TypeScript fluent)
|
||||
```ts
|
||||
// 매 query builder
|
||||
class QueryBuilder<T> {
|
||||
private filters: Filter[] = [];
|
||||
private sort: Sort[] = [];
|
||||
private limitN = 100;
|
||||
|
||||
where(field: keyof T, op: Op, value: unknown): this {
|
||||
this.filters.push({ field, op, value });
|
||||
return this; // 매 chain
|
||||
}
|
||||
|
||||
orderBy(field: keyof T, dir: 'asc' | 'desc'): this {
|
||||
this.sort.push({ field, dir });
|
||||
return this;
|
||||
}
|
||||
|
||||
limit(n: number): this {
|
||||
this.limitN = n;
|
||||
return this;
|
||||
}
|
||||
|
||||
build(): SQL {
|
||||
return `SELECT * FROM ${this.table} WHERE ${this.filters} ORDER BY ${this.sort} LIMIT ${this.limitN}`;
|
||||
}
|
||||
}
|
||||
|
||||
## 💻 코드 패턴 (Code Patterns)
|
||||
|
||||
**패턴 1:** *(TODO: 이 프로젝트 컨벤션 반영한 구조 스켈레톤)*
|
||||
|
||||
```text
|
||||
# TODO
|
||||
// 매 use
|
||||
const query = new QueryBuilder<User>()
|
||||
.where('age', '>', 18)
|
||||
.where('country', '=', 'US')
|
||||
.orderBy('createdAt', 'desc')
|
||||
.limit(50);
|
||||
```
|
||||
|
||||
## 🤔 의사결정 기준 (Decision Criteria)
|
||||
### Zod schema (DSL-like)
|
||||
```ts
|
||||
import { z } from 'zod';
|
||||
|
||||
**선택 A를 써야 할 때:**
|
||||
- *(TODO)*
|
||||
const UserSchema = z.object({
|
||||
id: z.string().uuid(),
|
||||
email: z.string().email(),
|
||||
age: z.number().int().min(0).max(150),
|
||||
role: z.enum(['admin', 'user']),
|
||||
preferences: z.object({
|
||||
theme: z.enum(['light', 'dark']).default('light'),
|
||||
notifications: z.boolean().default(true),
|
||||
}).optional(),
|
||||
});
|
||||
|
||||
**선택 B를 써야 할 때:**
|
||||
- *(TODO)*
|
||||
type User = z.infer<typeof UserSchema>;
|
||||
```
|
||||
|
||||
**기본값:**
|
||||
> *(TODO)*
|
||||
### External DSL (Lark in Python)
|
||||
```python
|
||||
from lark import Lark, Transformer
|
||||
|
||||
## ❌ 안티패턴 (Anti-Patterns)
|
||||
grammar = """
|
||||
start: rule+
|
||||
rule: NAME ":" condition "→" action
|
||||
condition: "if" CNAME comparison NUMBER
|
||||
comparison: ">" | "<" | "=="
|
||||
action: CNAME "(" CNAME ")"
|
||||
NUMBER: /\\d+/
|
||||
%import common.CNAME
|
||||
%import common.NAME
|
||||
%import common.WS
|
||||
%ignore WS
|
||||
"""
|
||||
|
||||
- **[안티패턴]:** *(TODO: 무엇을 하면 안 되는가 + 이유 + 대신 무엇을)*
|
||||
dsl = """
|
||||
high_score: if score > 90 → notify(student)
|
||||
warn: if attendance < 70 → email(student)
|
||||
"""
|
||||
|
||||
parser = Lark(grammar)
|
||||
tree = parser.parse(dsl)
|
||||
|
||||
class MyDSL(Transformer):
|
||||
def rule(self, items):
|
||||
name, cond, action = items
|
||||
return {'name': name, 'condition': cond, 'action': action}
|
||||
|
||||
print(MyDSL().transform(tree))
|
||||
```
|
||||
|
||||
### NL → SQL (LLM-driven)
|
||||
```python
|
||||
def nl_to_sql(query, schema):
|
||||
prompt = f"""You are a SQL expert.
|
||||
|
||||
Schema:
|
||||
{schema}
|
||||
|
||||
Convert this natural language to SQL:
|
||||
"{query}"
|
||||
|
||||
Return ONLY the SQL, no explanation."""
|
||||
sql = llm.generate(prompt)
|
||||
|
||||
# 매 validate
|
||||
if not is_safe(sql) or has_destructive(sql):
|
||||
raise ValueError('Unsafe SQL')
|
||||
|
||||
return sql
|
||||
```
|
||||
|
||||
### Gherkin (BDD)
|
||||
```gherkin
|
||||
Feature: User login
|
||||
|
||||
Scenario: Successful login with valid credentials
|
||||
Given a registered user with email "test@example.com"
|
||||
And the user's password is "password123"
|
||||
When the user submits the login form
|
||||
Then they are redirected to "/dashboard"
|
||||
And a session cookie is set
|
||||
```
|
||||
|
||||
```python
|
||||
from behave import given, when, then
|
||||
|
||||
@given('a registered user with email "{email}"')
|
||||
def step_user(context, email):
|
||||
context.user = create_user(email)
|
||||
|
||||
@when('the user submits the login form')
|
||||
def step_submit(context):
|
||||
context.response = client.post('/login', {...})
|
||||
|
||||
@then('they are redirected to "{path}"')
|
||||
def step_redirect(context, path):
|
||||
assert context.response.url.endswith(path)
|
||||
```
|
||||
|
||||
### Terraform HCL
|
||||
```hcl
|
||||
resource "aws_instance" "web" {
|
||||
ami = "ami-0c55b159cbfafe1f0"
|
||||
instance_type = var.environment == "prod" ? "t3.large" : "t3.micro"
|
||||
|
||||
tags = merge(var.common_tags, {
|
||||
Name = "web-${var.environment}"
|
||||
})
|
||||
|
||||
lifecycle {
|
||||
create_before_destroy = true
|
||||
prevent_destroy = var.environment == "prod"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Custom config DSL (Python)
|
||||
```python
|
||||
class PipelineDSL:
|
||||
def __init__(self):
|
||||
self.steps = []
|
||||
|
||||
def fetch(self, source: str):
|
||||
self.steps.append(('fetch', source))
|
||||
return self
|
||||
|
||||
def transform(self, fn):
|
||||
self.steps.append(('transform', fn))
|
||||
return self
|
||||
|
||||
def store(self, dest: str):
|
||||
self.steps.append(('store', dest))
|
||||
return self
|
||||
|
||||
def run(self):
|
||||
# 매 execute
|
||||
...
|
||||
|
||||
# 매 use
|
||||
pipeline = (PipelineDSL()
|
||||
.fetch('s3://my-bucket/data.csv')
|
||||
.transform(lambda df: df.dropna())
|
||||
.store('postgres://localhost/clean'))
|
||||
```
|
||||
|
||||
### Tree-sitter (parse)
|
||||
```js
|
||||
const Parser = require('tree-sitter');
|
||||
const SQL = require('tree-sitter-sql');
|
||||
|
||||
const parser = new Parser();
|
||||
parser.setLanguage(SQL);
|
||||
|
||||
const tree = parser.parse('SELECT * FROM users WHERE id = 1');
|
||||
console.log(tree.rootNode.toString());
|
||||
```
|
||||
|
||||
### Validate DSL (LLM-friendly)
|
||||
```python
|
||||
def validate_dsl(text, grammar):
|
||||
"""매 LLM 의 generated DSL 의 validate."""
|
||||
try:
|
||||
parser.parse(text)
|
||||
return {'valid': True}
|
||||
except ParseError as e:
|
||||
# 매 LLM 의 fix 의 ask
|
||||
fixed = llm.generate(f'Fix this DSL parse error:\n{e}\n{text}')
|
||||
return {'valid': False, 'fixed_attempt': fixed}
|
||||
```
|
||||
|
||||
### DSL 의 evaluation
|
||||
```python
|
||||
class CompiledDSL:
|
||||
def __init__(self, ast):
|
||||
self.ast = ast
|
||||
|
||||
def execute(self, context):
|
||||
return self._eval(self.ast, context)
|
||||
|
||||
def _eval(self, node, ctx):
|
||||
if node.type == 'literal': return node.value
|
||||
if node.type == 'variable': return ctx[node.name]
|
||||
if node.type == 'binary_op':
|
||||
left = self._eval(node.left, ctx)
|
||||
right = self._eval(node.right, ctx)
|
||||
return apply_op(node.op, left, right)
|
||||
```
|
||||
|
||||
## 매 결정 기준
|
||||
| 상황 | DSL Type |
|
||||
|---|---|
|
||||
| Domain expert reads | External DSL |
|
||||
| Programmer power | Internal (fluent) |
|
||||
| Configuration | YAML / HCL / TOML |
|
||||
| Build | Make / Gradle / Bazel |
|
||||
| Test BDD | Gherkin |
|
||||
| LLM interface | NL → DSL |
|
||||
| Constrained safety | External w/ verifier |
|
||||
|
||||
**기본값**: 매 internal DSL (TypeScript / Python fluent) for code-heavy. 매 external for non-dev.
|
||||
|
||||
## 🔗 Graph
|
||||
- 부모: [[Programming-Languages]] · [[Software-Design]]
|
||||
- 변형: [[External-DSL]] · [[Internal-DSL]] · [[Fluent-API]]
|
||||
- 응용: [[SQL]] · [[GraphQL]] · [[Terraform]] · [[Gherkin]] · [[Diagrams_as_Code]]
|
||||
- Adjacent: [[Bounded-Contexts]] · [[Domain-Driven-Design]] · [[Articulateness]] · [[Be-Detailed]] · [[Container_Queries]] (CSS DSL)
|
||||
|
||||
## 🤖 LLM 활용
|
||||
**언제**: 매 internal API design. 매 NL-to-DSL system. 매 IaC. 매 BDD.
|
||||
**언제 X**: 매 simple imperative task.
|
||||
|
||||
## ❌ 안티패턴
|
||||
- **DSL 의 Turing-complete 의 force**: 매 verification lose.
|
||||
- **No documentation / examples**: 매 adoption fail.
|
||||
- **Premature DSL** (small project): 매 over-engineering.
|
||||
- **NL → DSL 의 unvalidated**: 매 destructive (e.g., DROP TABLE).
|
||||
- **DSL leak host language**: 매 loose abstraction.
|
||||
|
||||
## 🧪 검증 / 중복
|
||||
- Verified (Fowler "DSLs", PoEAA, ANTLR / Tree-sitter docs).
|
||||
- 신뢰도 A.
|
||||
- Related: [[Bounded-Contexts]] · [[Articulateness]] · [[Diagrams_as_Code]] · [[Custom-ESLint-Rules-Development]] · [[Be-Detailed]].
|
||||
|
||||
## 🕓 Changelog
|
||||
| 날짜 | 변경 |
|
||||
|---|---|
|
||||
| 2026-05-08 | Phase 1 |
|
||||
| 2026-05-10 | Manual cleanup — internal/external + 매 zod / Lark / Gherkin / NL-to-SQL / Terraform code |
|
||||
|
||||
Reference in New Issue
Block a user