change syntax for javascript code literals
This commit is contained in:
@@ -24,9 +24,9 @@ max x y = case x < y of
|
||||
True => y
|
||||
False => x
|
||||
|
||||
pfunc repr : {a : U} -> a -> String := "(a,o) => ''+o"
|
||||
pfunc jrepr : {a : U} -> a -> String := "(a,o) => JSON.stringify(o, null, ' ')"
|
||||
pfunc toInt : String -> Int := "s => Number(s)"
|
||||
pfunc repr : {a : U} -> a -> String := `(a,o) => ''+o`
|
||||
pfunc jrepr : {a : U} -> a -> String := `(a,o) => JSON.stringify(o, null, ' ')`
|
||||
pfunc toInt : String -> Int := `s => Number(s)`
|
||||
|
||||
mapM : {a b c : U} -> (a -> Either b c) -> List a -> Either b (List c)
|
||||
mapM f Nil = Right Nil
|
||||
|
||||
@@ -1,162 +0,0 @@
|
||||
module Lib
|
||||
|
||||
-- Prelude
|
||||
data Unit : U where
|
||||
MkUnit : Unit
|
||||
|
||||
data Bool : U where
|
||||
True : Bool
|
||||
False : Bool
|
||||
|
||||
data Nat : U where
|
||||
Z : Nat
|
||||
S : Nat -> Nat
|
||||
|
||||
data Maybe : U -> U where
|
||||
Just : {a : U} -> a -> Maybe a
|
||||
Nothing : {a : U} -> Maybe a
|
||||
|
||||
data Either : U -> U -> U where
|
||||
Left : {a b : U} -> a -> Either a b
|
||||
Right : {a b : U} -> b -> Either a b
|
||||
|
||||
|
||||
infixr 7 _::_
|
||||
data List : U -> U where
|
||||
Nil : {a : U} -> List a
|
||||
_::_ : {a : U} -> a -> List a -> List a
|
||||
|
||||
Cons : {a : U} -> a -> List a -> List a
|
||||
Cons x xs = x :: xs
|
||||
|
||||
-- TODO where clauses
|
||||
reverse' : {A : U} -> List A -> List A -> List A
|
||||
reverse' Nil acc = acc
|
||||
reverse' (x :: xs) acc = reverse' xs (x :: acc)
|
||||
|
||||
reverse : {A : U} -> List A -> List A
|
||||
reverse xs = reverse' xs Nil
|
||||
|
||||
length : {a : U} -> List a -> Nat
|
||||
length Nil = Z
|
||||
length (x :: xs) = S (length xs)
|
||||
|
||||
infixr 0 _,_
|
||||
|
||||
data Pair : U -> U -> U where
|
||||
_,_ : {a b : U} -> a -> b -> Pair a b
|
||||
|
||||
-- Idris says it special cases to deal with unification issues
|
||||
infixr 0 _$_
|
||||
|
||||
_$_ : {a b : U} -> (a -> b) -> a -> b
|
||||
f $ a = f a
|
||||
|
||||
-- JS Bridge
|
||||
|
||||
ptype Dummy
|
||||
|
||||
|
||||
ptype World
|
||||
data IO : U -> U where
|
||||
MkIO : {a : U} -> (World -> Pair World a) -> IO a
|
||||
|
||||
-- TODO unified Number for now
|
||||
ptype Int
|
||||
ptype String
|
||||
|
||||
ptype Char
|
||||
|
||||
ptype Array : U -> U
|
||||
|
||||
pfunc arrayToList : {a : U} -> Array a -> List a := "
|
||||
(a, arr) => {
|
||||
let rval = Nil(a)
|
||||
for (let i = arr.length - 1; i >= 0; i--) {
|
||||
rval = Cons(a, arr[i], rval)
|
||||
}
|
||||
return rval
|
||||
}
|
||||
"
|
||||
|
||||
pfunc listToArray : {a : U} -> List a -> Array a := "
|
||||
(a, l) => {
|
||||
let rval = []
|
||||
while (l.tag !== 'Nil') {
|
||||
rval.push(l.h1)
|
||||
l = l.h2
|
||||
}
|
||||
return rval
|
||||
}
|
||||
"
|
||||
pfunc alen : {a : U} -> Array a -> Int := "(a,arr) => arr.length"
|
||||
pfunc aget : {a : U} -> Array a -> Int -> a := "(a, arr, ix) => arr[ix]"
|
||||
pfunc aempty : {a : U} -> Unit -> Array a := "() => []"
|
||||
|
||||
pfunc getArgs : List String := "arrayToList(String, process.argv)"
|
||||
-- Maybe integrate promises?
|
||||
|
||||
|
||||
pfunc ord : Char -> Int := "(c) => c.charCodeAt(0)"
|
||||
|
||||
pfunc _<_ : Int -> Int -> Bool := "(x,y) => (x < y) ? True : False"
|
||||
pfunc _<=_ : Int -> Int -> Bool := "(x,y) => (x <= y) ? True : False"
|
||||
pfunc _+_ : Int -> Int -> Int := "(x,y) => x + y"
|
||||
pfunc _-_ : Int -> Int -> Int := "(x,y) => x - y"
|
||||
pfunc _*_ : Int -> Int -> Int := "(x,y) => x * y"
|
||||
pfunc _/_ : Int -> Int -> Int := "(x,y) => x / y"
|
||||
|
||||
infix 6 _<_ _<=_
|
||||
infixl 8 _+_ _-_
|
||||
infixl 9 _*_ _/_
|
||||
|
||||
-- Ideally we'd have newt write the arrows for us to keep things correct
|
||||
-- We'd still have difficulty with callbacks...
|
||||
pfunc fs : Dummy := "require('fs')"
|
||||
pfunc readFile : (fn : String) -> String := "(fn) => fs.readFileSync(fn, 'utf8')"
|
||||
pfunc log : {a : U} -> a -> Dummy := "(a, obj) => console.log(obj)"
|
||||
|
||||
pfunc p_strHead : (s : String) -> Char := "(s) => s[0]"
|
||||
pfunc p_strTail : (s : String) -> String := "(s) => s[0]"
|
||||
|
||||
pfunc trim : String -> String := "s => s.trim()"
|
||||
pfunc split : String -> String -> List String := "(s, by) => {
|
||||
let parts = s.split(by)
|
||||
let rval = Nil(String)
|
||||
parts.reverse()
|
||||
parts.forEach(p => { rval = _$3A$3A_(List(String), p, rval) })
|
||||
return rval
|
||||
}"
|
||||
|
||||
pfunc slen : String -> Int := "s => s.length"
|
||||
pfunc sindex : String -> Int -> Char := "(s,i) => s[i]"
|
||||
|
||||
|
||||
infixl 7 _++_
|
||||
pfunc _++_ : String -> String -> String := "(a,b) => a + b"
|
||||
|
||||
|
||||
pfunc trace : {a : U} -> String -> a -> a := "(_, lab, a) => {
|
||||
console.log(lab,a)
|
||||
return a
|
||||
}"
|
||||
|
||||
pfunc unpack : String -> List Char
|
||||
:= "(s) => {
|
||||
let acc = Nil(Char)
|
||||
for (let i = s.length - 1; 0 <= i; i--) acc = _$3A$3A_(Char, s[i], acc)
|
||||
return acc
|
||||
}"
|
||||
|
||||
foldl : {A B : U} -> (B -> A -> B) -> B -> List A -> B
|
||||
foldl f acc Nil = acc
|
||||
foldl f acc (x :: xs) = foldl f (f acc x) xs
|
||||
|
||||
map : {A B : U} -> (A -> B) -> List A -> List B
|
||||
map f Nil = Nil
|
||||
map f (x :: xs) = f x :: map f xs
|
||||
|
||||
|
||||
infixl 9 _∘_
|
||||
_∘_ : {A B C : U} -> (B -> C) -> (A -> B) -> A -> C
|
||||
(f ∘ g) x = f (g x)
|
||||
@@ -182,13 +182,13 @@ ptype Int
|
||||
ptype Char
|
||||
|
||||
-- probably want to switch to Int or implement magic Nat
|
||||
pfunc length : String → Nat := "(s) => {
|
||||
pfunc length : String → Nat := `(s) => {
|
||||
let rval = Z
|
||||
for (let i = 0; i < s.length; s++) rval = S(rval)
|
||||
return rval
|
||||
}"
|
||||
}`
|
||||
|
||||
pfunc sconcat : String → String → String := "(x,y) => x + y"
|
||||
pfunc sconcat : String → String → String := `(x,y) => x + y`
|
||||
instance Concat String where
|
||||
_++_ = sconcat
|
||||
|
||||
@@ -196,7 +196,7 @@ data Unit : U where
|
||||
MkUnit : Unit
|
||||
|
||||
ptype Array : U → U
|
||||
pfunc listToArray : {a : U} -> List a -> Array a := "
|
||||
pfunc listToArray : {a : U} -> List a -> Array a := `
|
||||
(a, l) => {
|
||||
let rval = []
|
||||
while (l.tag !== 'Nil') {
|
||||
@@ -205,52 +205,52 @@ pfunc listToArray : {a : U} -> List a -> Array a := "
|
||||
}
|
||||
return rval
|
||||
}
|
||||
"
|
||||
`
|
||||
|
||||
pfunc alen : {a : U} -> Array a -> Int := "(a,arr) => arr.length"
|
||||
pfunc aget : {a : U} -> Array a -> Int -> a := "(a, arr, ix) => arr[ix]"
|
||||
pfunc aempty : {a : U} -> Unit -> Array a := "() => []"
|
||||
pfunc alen : {0 a : U} -> Array a -> Int := `(a,arr) => arr.length`
|
||||
pfunc aget : {0 a : U} -> Array a -> Int -> a := `(a, arr, ix) => arr[ix]`
|
||||
pfunc aempty : {0 a : U} -> Unit -> Array a := `() => []`
|
||||
|
||||
pfunc arrayToList : {a} → Array a → List a := "(a,arr) => {
|
||||
pfunc arrayToList : {0 a} → Array a → List a := `(a,arr) => {
|
||||
let rval = Nil(a)
|
||||
for (let i = arr.length - 1;i >= 0; i--) {
|
||||
rval = _$3A$3A_(a, arr[i], rval)
|
||||
}
|
||||
return rval
|
||||
}"
|
||||
}`
|
||||
|
||||
|
||||
|
||||
-- for now I'll run this in JS
|
||||
pfunc lines : String → List String := "(s) => arrayToList(s.split('\\n'))"
|
||||
pfunc lines : String → List String := `(s) => arrayToList(s.split('\n'))`
|
||||
|
||||
pfunc p_strHead : (s : String) -> Char := "(s) => s[0]"
|
||||
pfunc p_strTail : (s : String) -> String := "(s) => s[0]"
|
||||
pfunc p_strHead : (s : String) -> Char := `(s) => s[0]`
|
||||
pfunc p_strTail : (s : String) -> String := `(s) => s[0]`
|
||||
|
||||
pfunc trim : String -> String := "s => s.trim()"
|
||||
pfunc split : String -> String -> List String := "(s, by) => {
|
||||
pfunc trim : String -> String := `s => s.trim()`
|
||||
pfunc split : String -> String -> List String := `(s, by) => {
|
||||
let parts = s.split(by)
|
||||
let rval = Nil(String)
|
||||
parts.reverse()
|
||||
parts.forEach(p => { rval = _$3A$3A_(List(String), p, rval) })
|
||||
return rval
|
||||
}"
|
||||
}`
|
||||
|
||||
pfunc slen : String -> Int := "s => s.length"
|
||||
pfunc sindex : String -> Int -> Char := "(s,i) => s[i]"
|
||||
pfunc slen : String -> Int := `s => s.length`
|
||||
pfunc sindex : String -> Int -> Char := `(s,i) => s[i]`
|
||||
|
||||
-- TODO represent Nat as number at runtime
|
||||
pfunc natToInt : Nat -> Int := "(n) => {
|
||||
pfunc natToInt : Nat -> Int := `(n) => {
|
||||
let rval = 0
|
||||
while (n.tag === 'S') {
|
||||
n = n.h0
|
||||
rval++
|
||||
}
|
||||
return rval
|
||||
}"
|
||||
}`
|
||||
|
||||
pfunc fastConcat : List String → String := "(xs) => listToArray(undefined, xs).join('')"
|
||||
pfunc replicate : Nat -> Char → String := "(n,c) => c.repeat(natToInt(n))"
|
||||
pfunc fastConcat : List String → String := `(xs) => listToArray(undefined, xs).join('')`
|
||||
pfunc replicate : Nat -> Char → String := `(n,c) => c.repeat(natToInt(n))`
|
||||
|
||||
-- I don't want to use an empty type because it would be a proof of void
|
||||
ptype World
|
||||
@@ -266,12 +266,12 @@ instance Monad IO where
|
||||
MkIORes a w => mab a w
|
||||
pure a = \ w => MkIORes a w
|
||||
|
||||
pfunc putStrLn : String -> IO Unit := "(s) => (w) => {
|
||||
pfunc putStrLn : String -> IO Unit := `(s) => (w) => {
|
||||
console.log(s)
|
||||
return MkIORes(Unit,MkUnit,w)
|
||||
}"
|
||||
}`
|
||||
|
||||
pfunc showInt : Int -> String := "(i) => String(i)"
|
||||
pfunc showInt : Int -> String := `(i) => String(i)`
|
||||
|
||||
class Show a where
|
||||
show : a → String
|
||||
@@ -282,18 +282,18 @@ instance Show String where
|
||||
instance Show Int where
|
||||
show = showInt
|
||||
|
||||
pfunc ord : Char -> Int := "(c) => c.charCodeAt(0)"
|
||||
pfunc ord : Char -> Int := `(c) => c.charCodeAt(0)`
|
||||
|
||||
infix 6 _<=_
|
||||
pfunc _<=_ : Int -> Int -> Bool := "(x,y) => (x <= y) ? True : False"
|
||||
pfunc _<=_ : Int -> Int -> Bool := `(x,y) => (x <= y) ? True : False`
|
||||
|
||||
|
||||
pfunc unpack : String -> List Char
|
||||
:= "(s) => {
|
||||
:= `(s) => {
|
||||
let acc = Nil(Char)
|
||||
for (let i = s.length - 1; 0 <= i; i--) acc = _$3A$3A_(Char, s[i], acc)
|
||||
return acc
|
||||
}"
|
||||
}`
|
||||
|
||||
|
||||
|
||||
@@ -306,10 +306,10 @@ _∘_ : {A B C : U} -> (B -> C) -> (A -> B) -> A -> C
|
||||
(f ∘ g) x = f (g x)
|
||||
|
||||
|
||||
pfunc addInt : Int → Int → Int := "(x,y) => x + y"
|
||||
pfunc mulInt : Int → Int → Int := "(x,y) => x * y"
|
||||
pfunc subInt : Int → Int → Int := "(x,y) => x - y"
|
||||
pfunc ltInt : Int → Int → Bool := "(x,y) => x < y ? True : False"
|
||||
pfunc addInt : Int → Int → Int := `(x,y) => x + y`
|
||||
pfunc mulInt : Int → Int → Int := `(x,y) => x * y`
|
||||
pfunc subInt : Int → Int → Int := `(x,y) => x - y`
|
||||
pfunc ltInt : Int → Int → Bool := `(x,y) => x < y ? True : False`
|
||||
|
||||
instance Mul Int where
|
||||
x * y = mulInt x y
|
||||
|
||||
@@ -118,8 +118,8 @@ isVowel _ = False
|
||||
|
||||
-- And primitive functions have a type and a javascript definition:
|
||||
|
||||
pfunc plusInt : Int -> Int -> Int := "(x,y) => x + y"
|
||||
pfunc plusString : String -> String -> String := "(x,y) => x + y"
|
||||
pfunc plusInt : Int -> Int -> Int := `(x,y) => x + y`
|
||||
pfunc plusString : String -> String -> String := `(x,y) => x + y`
|
||||
|
||||
-- We can make them Plus instances:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user