Skip to content

Commit 2853de3

Browse files
committed
add some missing files, clean up a little
1 parent d2e4664 commit 2853de3

File tree

9 files changed

+293
-108
lines changed

9 files changed

+293
-108
lines changed

TODO.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
## TODO
33

4+
- [ ] `newt/Problem.newt` coverage issues
5+
- [ ] Maybe make the editor json more compact
46
- [ ] Remove erased args from primitive functions
57
- [ ] consider moving primitive functions to a support file
68
- Downside here is that we lose some dead code elimination, but it better supports bootstrapping when calling convention changes.

newt/Boehm.newt

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
module Boehm
2+
3+
-- https://okmij.org/ftp/tagless-final/course/Boehm-Berarducci.html
4+
5+
import Prelude
6+
7+
data Exp = Lit Int
8+
| Neg Exp
9+
| ADD Exp Exp
10+
11+
ti1 : Exp
12+
ti1 = ADD (Lit 8) (Neg (ADD (Lit 1) (Lit 2)))
13+
14+
view : Exp → String
15+
view (Lit n) = show n
16+
view (Neg e) = "(-" ++ view e ++ ")"
17+
view (ADD e1 e2) = "("++ view e1 ++ " + " ++ view e2 ++ ")"
18+
19+
record ExpOps a where
20+
olit : Int → a
21+
oneg : a → a
22+
oadd : a → a → a
23+
24+
foldExp : ∀ a. (Int → a) → (a → a) → (a → a → a) → Exp → a
25+
foldExp onlit onneg onadd (Lit n) = onlit n
26+
foldExp onlit onneg onadd (Neg e) = onneg (foldExp onlit onneg onadd e)
27+
foldExp onlit onneg onadd (ADD e1 e2) =
28+
onadd (foldExp onlit onneg onadd e1)
29+
(foldExp onlit onneg onadd e2)
30+
31+
foldExp' : ∀ a. ExpOps a → Exp → a
32+
foldExp' ops = foldExp ops.olit ops.oneg ops.oadd
33+
34+
viewOps : ExpOps String
35+
viewOps = MkExpOps show
36+
(\ e => "(-" ++ e ++ ")")
37+
(\ e1 e2 => "(" ++ e1 ++ " + " ++ e2 ++ ")")
38+
39+
-- ok, I feel like I've done this before?
40+
-- or it was an example in another of his articles
41+
pushNeg : Exp → Exp
42+
pushNeg e@(Lit _) = e
43+
pushNeg e@(Neg (Lit _)) = e
44+
pushNeg (Neg (Neg e)) = e
45+
pushNeg (Neg (ADD e1 e2)) = (ADD (pushNeg $ Neg e1) (pushNeg $ Neg e2))
46+
pushNeg (ADD e1 e2) = ADD (pushNeg e1) (pushNeg e2)
47+
48+
ti1Norm : Exp
49+
ti1Norm = pushNeg ti1
50+
51+
ti1NormView ti1NNormView : String
52+
ti1NormView = view ti1Norm
53+
54+
ti1NNormView = view (pushNeg (Neg ti1))
55+
56+
ExpBB1 : U
57+
ExpBB1 = ∀ a. ExpOps a → a
58+
59+
-- curry record (he newtypes this. do we need to?)
60+
61+
ExpBB : U
62+
ExpBB = ∀ a. (Int → a) → (a → a) → (a → a → a) → a
63+
64+
lit : Int → ExpBB
65+
lit x = \onlit onneg onadd => onlit x
66+
67+
neg : ExpBB → ExpBB
68+
neg e = \ {a} onlit onneg onadd => onneg (e {a} onlit onneg onadd)
69+
70+
add : ExpBB → ExpBB → ExpBB
71+
add e1 e2 = \ {a} onlit onneg onadd =>
72+
onadd (e1 {a} onlit onneg onadd)
73+
(e2 {a} onlit onneg onadd)
74+
75+
bbOps : ExpOps ExpBB
76+
bbOps = MkExpOps lit neg add
77+
78+
viewBB : ExpBB → String
79+
-- FIXME - without the {String} it is unifying Int → String with U
80+
-- Like an insert is not happening?
81+
viewBB e = e {String} onlit onneg onadd
82+
where
83+
onlit : Int → String
84+
onlit n = show n
85+
onneg : String → String
86+
onneg e = "(-" ++ e ++ ")"
87+
onadd : String → String → String
88+
onadd e1 e2 = "(" ++ e1 ++ " , " ++ e2 ++ ")"
89+
90+
91+
-- extensionally:
92+
-- e (olit bbOps) (oneg bbOps) (oAdd bbOps) == e
93+
94+
data ExpF a = FLit Int
95+
| FNeg a
96+
| FAdd a a
97+
98+
roll : ExpF ExpBB → ExpBB
99+
roll (FLit n) = lit n
100+
roll (FNeg e) = neg e
101+
-- FIXME a {a} arg is being stripped off of the type before it's compared with ExpBB
102+
roll (FAdd e1 e2) = add e1 e2
103+
104+
unroll : ExpBB → ExpF ExpBB
105+
unroll e = e onlit onneg onadd
106+
where
107+
onlit : Int → ExpF ExpBB
108+
onlit n = FLit n
109+
onneg : ExpF ExpBB → ExpF ExpBB
110+
onneg e = FNeg (roll e)
111+
onadd : ExpF ExpBB → ExpF ExpBB → ExpF ExpBB
112+
onadd e1 e2 = FAdd (roll e1) (roll e2)
113+
114+
115+
main : IO Unit
116+
main = do
117+
putStrLn ti1NormView
118+
putStrLn ti1NNormView
119+

newt/Equality.newt

Lines changed: 0 additions & 24 deletions
This file was deleted.

newt/Problem.newt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module Problem
2+
3+
data Unit : U where
4+
MkUnit : Unit
5+
6+
infixr 7 _::_
7+
data List : U -> U where
8+
Nil : {A : U} -> List A
9+
_::_ : {A : U} -> A -> List A -> List A
10+
11+
-- prj/menagerie/papers/combinatory
12+
13+
infixr 6 _~>_
14+
data Type : U where
15+
ι : Type
16+
_~>_ : Type -> Type -> Type
17+
18+
A : U
19+
A = Unit
20+
21+
Val : Type -> U
22+
Val ι = A
23+
Val (x ~> y) = Val x -> Val y
24+
25+
Ctx : U
26+
Ctx = List Type
27+
28+
data Ref : Type -> Ctx -> U where
29+
Z : {σ : Type} {Γ : Ctx} -> Ref σ (σ :: Γ)
30+
S : {σ τ : Type} {Γ : Ctx} -> Ref σ Γ -> Ref σ (τ :: Γ)
31+
32+
data Term : Ctx -> Type -> U where
33+
App : {Γ : Ctx} {σ τ : Type} -> Term Γ (σ ~> τ) -> Term Γ σ -> Term Γ τ
34+
Lam : {Γ : Ctx} {σ τ : Type} -> Term (σ :: Γ) τ -> Term Γ (σ ~> τ)
35+
Var : {Γ : Ctx} {σ : Type} -> Ref σ Γ → Term Γ σ
36+
37+
infixr 7 _:::_
38+
data Env : Ctx -> U where
39+
ENil : Env Nil
40+
_:::_ : {Γ : Ctx} {σ : Type} → Val σ → Env Γ → Env (σ :: Γ)
41+
42+
-- FIXME there is a problem here with coverage checking
43+
-- if we split Z first, we are fine.
44+
-- ENil is an impossible case, but we need to look at the constructors
45+
-- if we're running backwards, so
46+
-- lookup () ENil
47+
-- we don't have that notation yet.
48+
49+
lookup : {σ : Type} {Γ : Ctx} → Ref σ Γ → Env Γ → Val σ
50+
lookup Z (x ::: y) = x
51+
-- and we have to way to say no cases here, either...
52+
-- lookup ref ENil = case ref of {}
53+
-- This does work
54+
-- lookup Z env = case env of (x ::: y) => x
55+
lookup (S i) (x ::: env) = lookup i env

newt/TypeClass.newt

Lines changed: 0 additions & 84 deletions
This file was deleted.

tests/BadAlt.newt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module BadAlt
2+
3+
import Prelude
4+
5+
foo : List Int → Int
6+
foo (xs :< x) = x

tests/Combinatory.newt

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
module Combinatory
2+
3+
-- "A correct-by-construction conversion from lambda calculus to combinatory logic", Wouter Swierstra
4+
5+
data Unit : U where
6+
MkUnit : Unit
7+
8+
infixr 7 _::_
9+
data List : U -> U where
10+
Nil : {A : U} -> List A
11+
_::_ : {A : U} -> A -> List A -> List A
12+
13+
-- prj/menagerie/papers/combinatory
14+
15+
infixr 6 _~>_
16+
data Type : U where
17+
ι : Type
18+
_~>_ : Type -> Type -> Type
19+
20+
A : U
21+
A = Unit
22+
23+
Val : Type -> U
24+
Val ι = A
25+
Val (x ~> y) = Val x -> Val y
26+
27+
Ctx : U
28+
Ctx = List Type
29+
30+
data Ref : Type -> Ctx -> U where
31+
Here : {σ : Type} {Γ : Ctx} -> Ref σ (σ :: Γ)
32+
There : {σ τ : Type} {Γ : Ctx} -> Ref σ Γ -> Ref σ (τ :: Γ)
33+
34+
data Term : Ctx -> Type -> U where
35+
App : {Γ : Ctx} {σ τ : Type} -> Term Γ (σ ~> τ) -> Term Γ σ -> Term Γ τ
36+
Lam : {Γ : Ctx} {σ τ : Type} -> Term (σ :: Γ) τ -> Term Γ (σ ~> τ)
37+
Var : {Γ : Ctx} {σ : Type} -> Ref σ Γ → Term Γ σ
38+
39+
infixr 7 _:::_
40+
data Env : Ctx -> U where
41+
ENil : Env Nil
42+
_:::_ : {Γ : Ctx} {σ : Type} → Val σ → Env Γ → Env (σ :: Γ)
43+
44+
-- TODO there is a problem here with coverage checking
45+
-- I suspect something is being split before it's ready
46+
47+
-- lookup : {σ : Type} {Γ : Ctx} → Ref σ Γ → Env Γ → Val σ
48+
-- lookup Here (x ::: y) = x
49+
-- lookup (There i) (x ::: env) = lookup i env
50+
51+
lookup2 : {σ : Type} {Γ : Ctx} → Env Γ → Ref σ Γ → Val σ
52+
lookup2 (x ::: y) Here = x
53+
lookup2 (x ::: env) (There i) = lookup2 env i
54+
55+
-- TODO MixFix - this was ⟦_⟧
56+
eval : {Γ : Ctx} {σ : Type} → Term Γ σ → (Env Γ → Val σ)
57+
-- there was a unification error in direct application
58+
eval (App t u) env = (eval t env) (eval u env)
59+
eval (Lam t) env = \ x => eval t (x ::: env)
60+
eval (Var i) env = lookup2 env i
61+
62+
data Comb : (Γ : Ctx) → (u : Type) → (Env Γ → Val u) → U where
63+
S : {Γ : Ctx} {σ τ τ' : Type} → Comb Γ ((σ ~> τ ~> τ') ~> (σ ~> τ) ~> (σ ~> τ')) (\ env => \ f g x => (f x) (g x))
64+
K : {Γ : Ctx} {σ τ : Type} → Comb Γ (σ ~> (τ ~> σ)) (\ env => \ x y => x)
65+
I : {Γ : Ctx} {σ : Type} → Comb Γ (σ ~> σ) (\ env => \ x => x)
66+
B : {Γ : Ctx} {σ τ τ' : Type} → Comb Γ ((τ ~> τ') ~> (σ ~> τ) ~> (σ ~> τ')) (\ env => \ f g x => f (g x))
67+
C : {Γ : Ctx} {σ τ τ' : Type} → Comb Γ ((σ ~> τ ~> τ') ~> τ ~> (σ ~> τ')) (\ env => \ f g x => (f x) g)
68+
CVar : {Γ : Ctx} {σ : Type} → (i : Ref σ Γ) → Comb Γ σ (\ env => lookup2 env i)
69+
CApp : {Γ : Ctx} {σ τ : Type} {f : _} {x : _} → Comb Γ (σ ~> τ) f → Comb Γ σ x → Comb Γ τ (\ env => (f env) (x env))
70+
71+
sapp : {Γ : Ctx} {σ τ ρ : Type} {f : _} {x : _} →
72+
Comb Γ (σ ~> τ ~> ρ) f →
73+
Comb Γ (σ ~> τ) x →
74+
Comb Γ (σ ~> ρ) (\ env y => (f env y) (x env y))
75+
sapp (CApp K t) I = t
76+
sapp (CApp K t) (CApp K u) = CApp K (CApp t u)
77+
-- was out of pattern because of unexpanded lets.
78+
sapp (CApp K t) u = CApp (CApp B t) u
79+
sapp t (CApp K u) = CApp (CApp C t) u
80+
sapp t u = CApp (CApp S t) u
81+
82+
abs : {Γ : Ctx} {σ τ : Type} {f : _} → Comb (σ :: Γ) τ f → Comb Γ (σ ~> τ) (\ env x => f (x ::: env))
83+
abs S = CApp K S
84+
abs K = CApp K K
85+
abs I = CApp K I
86+
abs B = CApp K B
87+
abs C = CApp K C
88+
abs (CApp t u) = sapp (abs t) (abs u)
89+
-- lookup2 was getting stuck, needed to re-eval the types in the rewritten env.
90+
abs (CVar Here) = I
91+
abs (CVar (There i)) = CApp K (CVar i)
92+
93+
translate : {Γ : Ctx} {σ : Type} → (tm : Term Γ σ) → Comb Γ σ (eval tm)
94+
translate (App t u) = CApp (translate t) (translate u)
95+
translate (Lam t) = abs (translate t)
96+
translate (Var i) = CVar i

0 commit comments

Comments
 (0)