DSLs shine in an AI world

March 24 6 min read

AI and DSLs: A Match Made in Coding Heaven

I’ve recently been delving deeper into how AI interacts with programming languages and development practices, particularly focusing on Domain-Specific Languages (DSLs). DSLs—languages specifically designed to handle particular domains or contexts—are not new, but AI’s role in amplifying their potential has become increasingly intriguing.

The big question I’m exploring: Can AI elevate niche, highly specific DSLs? My experimentation so far suggests the answer is a resounding “yes.”

Why AI and DSLs Work So Well Together

AI models thrive on clarity, specificity, and structured data. General-purpose languages like JavaScript, Python, and Java provide structure but also offer immense flexibility, creating numerous valid paths for a given solution. This flexibility often results in non-deterministic outputs from AI models, as I’ve personally experienced when requesting simple applications multiple times—each version different in nuanced yet important ways.

Practical Insights: The Non-Deterministic Problem

To illustrate, when I asked an AI model (ChatGPT) to create a simple social media app similar to Twitter three separate times, I encountered significantly varied outputs:

Answer 1

class UserCreate(BaseModel):
    username: str
    full_name: str

class PostCreate(BaseModel):
    content: str
    post_id: str

class FollowUser(BaseModel):
    follower_id: str
    followee_id: str

Answer 2

class UserCreate(BaseModel):
    username: str

class PostCreate(BaseModel):
    content: str
    user_id: int

class FollowCreate(BaseModel):
    follower_id: int
    followed_id: int

Answer 3

class UserCreate(BaseModel):
    username: str

class PostCreate(BaseModel):
    content: str
    owner_id: int

class FollowRequest(BaseModel):
    follower_id: int
    followed_id: int

These outputs demonstrate inconsistency in naming conventions, data types, and even included attributes—highlighting the inherent ambiguity and non-determinism of general-purpose languages when generated by AI.

How DSLs Address This Issue

In contrast, DSLs like SQL, Terraform, Mermaid.js, and even regular expressions provide a much narrower scope. They offer:

  • Specificity: Tailored to a specific domain or problem.
  • Declarative Syntax: Clearly defined rules and structure.
  • Reduced Ambiguity: Limited choices simplify AI understanding and reasoning.

This clarity and structure significantly improve AI’s accuracy and consistency in code generation and interpretation.

DSLs and Diagrams: Enhancing Determinism and Communication

DSLs, particularly diagrammatic DSLs like Mermaid.js, are highly effective in coaxing AI to produce deterministic outputs by enforcing existing structures, naming conventions, and explicit relationships. Diagrams also excel at being token-efficient, significantly reducing the computational overhead for AI models.

Moreover, DSL conventions and syntax communicate far more effectively than natural language, minimizing ambiguity and enhancing clarity.

Practical use cases include:

  • Understanding legacy codebases: Diagrams effectively visualize complex code relationships, aiding in comprehension.
  • Code reviews and planning: DSL-based diagrams are consumable and structured, making them ideal for creating cursor rules, performing code reviews, and facilitating team planning and discussions.
  • Knowledge transfer: Diagrams provide a clear, visual representation of system architecture and interactions, making them excellent tools for onboarding new developers or documenting systems.

AI-Powered Diagram-to-Code Workflows

An exciting use case I’ve explored extensively is transforming diagrams into executable code, an area where AI combined with DSLs excels. For instance, I provided GPT-4 with a LeetCode problem diagrammatically represented and subsequently generated fully functional Python code. Remarkably, this solution passed over 350 LeetCode test cases without requiring additional context or tweaking.

This illustrates the power of structured, visual, DSL-based inputs in generating accurate, reliable code.

Challenges and the Future

Despite clear benefits, challenges persist. AI still struggles with less common or newly updated DSLs, much like mainstream languages experience gaps post-major updates. AI’s performance hinges on training data quality and freshness. Thus, while mainstream DSLs perform exceptionally well, newer DSLs or major updates temporarily lag behind.

Moving forward, leveraging AI to quickly update training models with new DSL specifications and documentation could bridge this gap rapidly, ensuring AI remains reliable even amidst frequent tech evolution.

Final Thoughts

Pairing AI with DSLs presents a compelling opportunity to elevate software development. DSLs’ structured nature enables AI to consistently generate precise and efficient code, substantially reducing friction and boosting productivity. As I continue exploring, I’m eager to see how further integration and refinement of AI tools can enhance both mainstream and niche DSLs alike—potentially reshaping the way we approach software engineering entirely.