This commit is contained in:
2025-07-18 20:47:45 -04:00
parent bb2ae861b3
commit 800cec28de
9 changed files with 86 additions and 64 deletions

View File

@@ -1,6 +1,7 @@
## TODO
- [ ] Raw is duplicated between Lib.Syntax and Lib.Compile, but not detected
- [ ] vscode - run newt when switching editors
- [ ] case split
- We could fake this up:

View File

@@ -7,6 +7,7 @@
- workaround is to visit the file first
- we can put them in the zip file and pull them over the IPC
- [ ] make phone layout automatic
- [ ] case split &c
- [x] move newt to a worker (shim + newt + listener)
- [x] tabs for source, compiler output
- [x] Show errors in editor
@@ -19,4 +20,6 @@
- [x] save to url (copy from idris2-playground)
- [ ] click on url
- [ ] settings
- compilation is now optional, what else do we need for newt?

View File

@@ -1,23 +1,15 @@
/-
Ok, so this is newt, a dependent typed programming language that
I am implementing to learn how they work. It targets javascript
and borrows a lot of syntax from Idris and Agda.
This is newt, a self-hosted, dependent typed programming language that
compiles to javascript and borrows a lot of syntax from Idris and Agda.
This page is a very simple web playground based on the monaco editor.
It runs newt, compiled by Idris2, in a web worker.
This page is a very simple web playground based on the codemirror editor.
It runs newt in a web worker.
Block comments follow Lean because they're easier to type on a
US keyboard.
The output, to the right, is somewhat noisy and obtuse. You'll see
INFO and sometimes ERROR messages that show up in the editor view
on hover. I'm emitting INFO for solved metas.
The Day1.newt and Day2.newt are last year's advent of code, translated
from Lean.
-/
-- One-line comments begin with two hyphens
@@ -27,10 +19,11 @@
module Tour
-- We can import other modules, with a flat namespace and no cycles,
-- diamonds are ok
-- diamonds are ok, Prelude is not imported by default, only explicitly
-- imported modules are in scope
-- commented out until we preload other files into the worker
-- import Lib
-- commented out because we redefine parts of Prelude in this file.
-- import Prelude
-- We're calling the universe U and are doing type in type for now
@@ -39,10 +32,14 @@ data Nat : U where
Z : Nat
S : Nat -> Nat
-- Nat-shaped data is turned into numbers in codegen.
-- Multiple names are allowed on the left:
data Bool : U where
True False : Bool
-- Enum shaped data becomes a bare string (the constructor name) in codegen.
-- function definitions are equations using dependent pattern matching
plus : Nat -> Nat -> Nat
plus Z m = m
@@ -58,7 +55,7 @@ plus' = \ n m => case n of
-- We can define operators. Mixfix is supported, but we don't
-- allow ambiguity (so you can't have both [_] and [_,_]). See
-- the Reasoning.newt sample for a mixfix example.
-- the Reasoning.newt sample file for a mixfix example.
infixl 2 _≡_
-- Here is an equality, like Idris, everything goes to the right of the colon
@@ -84,8 +81,6 @@ instance Add Nat where
Z + m = m
(S n) + m = S (n + m)
-- and it now finds the implicits, you'll see the solutions to the
-- implicits if you hover over the `+`.
two : Nat
two = S Z + S Z
@@ -96,14 +91,13 @@ foo a b = ?
-- Newt compiles to javascript, there is a tab to the right that shows the
-- javascript output. There is some erasure, but inlining is not being done
-- yet. Dead code elimination will be done if there is a `main` function.
-- That is not the case in this file.
-- yet.
-- We can define native types, if the type is left off, it defaults to U
ptype SomePrimType : U
-- The names of these three types are special, primitive numbers, strings,
-- The types Int, String, Char are special. Primitive numbers, strings,
-- and characters inhabit them, respectively. We can match on primitives, but
-- must provide a default case:
@@ -208,3 +202,25 @@ prod xs ys = do
y <- ys
pure (x, y)
data Unit = MkUnit
ptype World
data IORes a = MkIORes a World
IO : U -> U
IO a = World -> IORes a
instance Monad IO where
bind ma mab = \ w => case ma w of
MkIORes a w => mab a w
pure = MkIORes
pfunc putStrLn uses (MkIORes MkUnit) : String -> IO Unit := `(s) => (w) => {
console.log(s)
return Prelude_MkIORes(null,Prelude_MkUnit,w)
}`
main : IO Unit
main = putStrLn "Hello, World!"

View File

@@ -1,3 +1,4 @@
/// <reference types="vite/client" />
declare module "*.css";
export {};
declare global {

29
playground/src/help.md Normal file
View File

@@ -0,0 +1,29 @@
# Newt Playground
Newt is a dependent typed programming language that compiles to javascript. This playground embeds the newt compiler and a codemirror based editor.
The editor will typecheck the file with newt and render errors as the file is changed. The current file is saved to localStorage and will be restored if there is no data in the URL. Cmd-s or Ctrl-s will create a url embedding the file contents. There is a layout toggle for phone use.
## Tabs
**Output** - Displays the compiler output, which is also used to render errors and info annotations in the editor.
**JS** - Displays the javascript translation of the file
**Console** - Displays the console output from running the javascript
**Help** - Displays this help file
## Buttons
▶ Compile and run the current file in an iframe, console output is collected to the console tab.
📋 Embed the current file in the URL and copy to clipboard
↕ or ↔ Toggle vertical or horziontal layout (for mobile)
## Keyboard
*C-s or M-s* - Embed the current file in the URL and copy to clipboard

View File

@@ -1,4 +1,4 @@
import { effect, signal } from "@preact/signals";
import { signal } from "@preact/signals";
import { Diagnostic } from "@codemirror/lint";
import { useEffect, useRef, useState } from "preact/hooks";
import { h, render, VNode } from "preact";
@@ -15,6 +15,7 @@ import { CMEditor } from "./cmeditor.ts";
import { deflate } from "./deflate.ts";
import { inflate } from "./inflate.ts";
import { IPC } from "./ipc.ts";
import helpText from "./help.md?raw";
let topData: undefined | TopData;
@@ -37,14 +38,14 @@ function md2nodes(md: string) {
let list: VNode[] | undefined
for (let line of md.split("\n")) {
if (line.startsWith('- ')) {
list = list ?? []
if (!list) {
list = []
rval.push(h('ul', {}, list))
}
list.push(h('li', {}, mdline2nodes(line.slice(2))))
continue
}
if (list) {
rval.push(h('ul', {}, list))
list = undefined
}
list = undefined
if (line.startsWith('# ')) {
rval.push(h('h2', {}, mdline2nodes(line.slice(2))))
} else if (line.startsWith('## ')) {
@@ -277,35 +278,7 @@ function Result() {
}
function Help() {
return h("div", { id: "help" },
md2nodes(`
# Newt Playground
The editor will typecheck the file with newt and render errors as the file is changed. The current file is saved to localStorage and will be restored if there is no data in the URL. Cmd-s / Ctrl-s will create a url embedding the file contents. There is a layout toggle for phone use.
## Tabs
**Output** - Displays the compiler output, which is also used to render errors and info annotations in the editor.
**JS** - Displays the javascript translation of the file
**Console** - Displays the console output from running the javascript
**Help** - Displays this help file
## Buttons
▶ Compile and run the current file in an iframe, console output is collected to the console tab.
📋 Embed the current file in the URL and copy to clipboard
↕ or ↔ Toggle vertical or horziontal layout (for mobile)
## Keyboard
*C-s or M-s* - Embed the current file in the URL and copy to clipboard
`)
)
return h("div", { id: "help" }, md2nodes(helpText))
}
function Console() {

View File

@@ -92,8 +92,8 @@ fcCol (MkFC file (l, c)) = c
class HasFC a where
getFC : a -> FC
primNS : List String
primNS = ("Prim" :: Nil)
emptyFC : FC
emptyFC = MkFC "" (0,0)

View File

@@ -1162,9 +1162,9 @@ buildLitCases ctx prob fc scnm scty = do
-- If we lookupRaw "String", we could get different answers in different contexts.
-- maybe Hardwire this one
stringType intType charType : QName
stringType = QN ("Prim" :: Nil) "String"
intType = QN ("Prim" :: Nil) "Int"
charType = QN ("Prim" :: Nil) "Char"
stringType = QN primNS "String"
intType = QN primNS "Int"
charType = QN primNS "Char"
litTyName : Literal -> QName
litTyName (LString str) = stringType
@@ -1213,6 +1213,8 @@ buildTree ctx prob@(MkProb ((MkClause fc constraints Nil expr) :: cs) ty) = do
-- If we try on creation, we're looping at the moment, because of the possibility
-- of Ord a -> Ord b -> Ord (a \x b). Need to cut earlier when solving or switch to
-- Idris method...
-- This is necessary for tests/InferenceIssue.newt
-- solveAutos below is the important part
scty' <- case scty' of
(VMeta fc1 ix sp) => do
meta <- lookupMeta ix

View File

@@ -21,9 +21,6 @@ import Lib.Syntax
import Node
import Serialize
primNS : List String
primNS = ("Prim" :: Nil)
jsonTopContext : M Json
jsonTopContext = do
top <- getTop