-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto.hs
538 lines (454 loc) · 15.4 KB
/
proto.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
{-
yeah, move it to the browser
http://www.cs.ox.ac.uk/bernard.sufrin/edit.pdf
how to make it work in the browser?
- first idea:
- input: wait for keyboard entry? say put an event handler on the whole page
- output: just translate the tree after every edit into HTML
- use ATS + linear types and translate to JS
how would it work?
- https://github.com/daniel3735928559/guppy
- https://clearly.pl/
- maintain an HTML tree of divs or spans
- represent the current cursor/selection by some HTML element too
- BUT! how does it mesh with our current code??? doesn't blend in!
way forward: use abstract types instead of manipulating lists....
-}
data Tree = Fork !Label ![Tree]
deriving (Eq, Ord, Show)
label :: Tree -> Label
label (Fork l _) = l
children :: Tree -> [Tree]
children (Fork _ ts) = ts
atomic :: Tree -> Bool
atomic = null . children
hole:: Tree
hole = Fork Hole []
type Path = [Layer]
type Layer = (Label{-label of focussed subtree-}, [Tree]{-children to the left-}, [Tree]{-children to the right-})
type SubTree = (Path, Tree)
embed :: Tree -> Layer -> Tree
embed t (l,left,right) = Fork l (reverse left++[t]++right)
selected :: SubTree -> Tree
selected (_, t) = t
path :: SubTree -> Path
path (p, _) = p
host :: SubTree -> Tree
host (p,t) = foldl embed t p
root :: Tree -> SubTree
root t = ([],t)
{-
host (root t) = t
selected(root t) = t
-}
backToTop :: SubTree -> SubTree
backToTop = until topmost up
left :: SubTree -> SubTree
left ((lab, last:l, r):ls, sel) = ((lab, l, sel:r):ls, last)
left st@((_, [], _):_, _) = st
left st@([], _) = st
leftmost :: SubTree -> Bool
leftmost ((_, [], _):_ ,_) = True
leftmost ([], _) = True
leftmost _ = False
firstChild :: SubTree -> SubTree -- moves to the left until leftmost
firstChild = until leftmost left
right :: SubTree -> SubTree
right st@((lab, l, first:r):ls, sel) =
let res = ((lab, sel:l, r):ls, first) in res
right st@((_, _, []):_, _) = st
right st@([], _) = st
lastChild :: SubTree -> SubTree -- moves to the right until rightmost
lastChild = until rightmost right
rightmost :: SubTree -> Bool
rightmost ((_, _, []):_ ,_) = True
rightmost ([], _) = True
rightmost _ = False
{-
left (right st) = st, if not (rightmost st)
right (left st) = st, if not (leftmost st)
-}
up :: SubTree -> SubTree
up (layer:above,t) = (above, embed t layer)
up st@([],_) = st
topmost :: SubTree -> Bool
topmost ([], _) = True
topmost _ = False
down (p, Fork lab (t:ts))= ((lab, [], ts):p, t)
down st@(_, Fork _ []) = st
bottommost (_,Fork _ []) = True
bottommost _ = False
{-
down (up st) = st, if not (topmost st) && leftmost st
up (down st) = st, if not (bottommost st)
-}
atHole :: SubTree -> Bool
atHole (ls, (Fork Hole [])) = True
atHole _ = False
next :: SubTree -> SubTree
next st = if bottommost st then (rightup st) else (down st)
rightup :: SubTree -> SubTree
rightup = right . until (\st -> topmost st || not (rightmost st)) up
preorder :: Tree -> [Tree]
preorder = map selected . takeOneWhile (not . topmost) . iterate next . root
takeOneWhile :: (SubTree -> Bool) -> [SubTree] -> [SubTree]
takeOneWhile p xs = takeWhile p xs
nextSuchThat :: (SubTree -> Bool) -> SubTree -> SubTree
nextSuchThat p st =
if p st' then st' else st
where st' = until (\ st -> topmost st || p st) next (next st)
{-
The search stops if a topmost node is reached, but note that
nextSuchThat p st = st
only if st has no proper next successor that satisfies p.
-}
replace :: Tree -> SubTree -> SubTree
replace s (p, t) = (p, s)
{-
replace (selected st) (replace s st) = st
-}
insert :: Label -> SubTree -> SubTree
insert lab st =
let r = template lab in
let st' = treeinsert r st in
st'
treeinsert :: Tree -> SubTree -> SubTree
treeinsert t st = if atHole st then(replace t st)
else ((replace (selected st) . down . replace t)) st
{-
selection(treeinsert t st) = selection st, if not (atHole st)
-}
kill :: SubTree -> SubTree
kill = replace hole
promote :: SubTree -> SubTree
promote st = (replace (selected st) . up) st
{-
kill (treeinsert t st) = st, if atHole st
promote (treeinsert t st) = st, if not (atHole st) && not(atomic t)
id (treeinsert t st) = st, if not (atHole st) && atomic t
replace (selected st) (kill st) = st
-}
situation :: SubTree -> Path
situation (c:cs, t) = [c]
situation ([], t) = []
graft :: Path -> SubTree -> SubTree
graft cs (p, t) = (cs++p, t)
{-
graft (situation st) (promote st) = st
-}
enter :: Label -> SubTree -> SubTree
enter l = nextSuchThat atHole . insert l
entry :: Label -> SubTree -> SubTree
entry l = enter l . reduce l
reduce :: Label -> SubTree -> SubTree
reduce l = until (irreducible l) up
irreducible :: Label -> SubTree -> Bool
irreducible l st = atHole st || topmost st || (not . rightmost) st || (not . producable l . label . selected . up) st
producable :: Label -> Label -> Bool
producable op2 op1 = (op1==op2 && assl op1) || prec op1 > prec op2
type SubTree' = ([Path],Tree)
unflatten :: SubTree -> SubTree'
unflatten (ps, t) = (ps:[], t)
flatten :: SubTree' -> SubTree
flatten (ps,t) = (concat ps,t)
lift :: (SubTree -> SubTree) -> (SubTree' -> SubTree')
lift f (p:ps,t) = (q:ps,s) where (q,s) = f (p,t)
entry' = lift . entry
left' = lift left
firstChild' = lift firstChild
right' = lift right
lastChild' = lift lastChild
up' = lift up
down' = lift down
backToTop' = lift backToTop
insert' = lift . insert
replace' = lift . replace
open :: SubTree' -> SubTree'
open (p,t) = ([]:p,t)
close :: SubTree' -> SubTree'
close (p:ps,t) = right' (ps, s) where s = host (p, t)
{- ------------------------- -}
-- anything to do with the target language
data Label =
Hole
| If
| Add
| Mul
| Var !String
| Const !Int
| Lam
| Annot -- type annotation
| App -- function application
deriving (Eq, Ord, Show)
assl :: Label -> Bool {- associates to the left -}
assl Add = True
assl Mul = True
assl App = True
assl _ = False
prec :: Label -> Int {- precedence -}
prec App = 1500
prec If = 1000
prec Add = 100
prec Mul = 120
prec Annot = 50
prec Lam = -1
prec _ = 0
template :: Label -> Tree
template App = Fork App [hole,hole]
template If = Fork If [hole, hole, hole]
template Lam = Fork Lam [hole, hole]
template Add = Fork Add [hole, hole]
template Mul = Fork Mul [hole, hole]
template Annot = Fork Annot [hole, hole]
template l = Fork l []
{- ------------------------- -}
-- simple driver for testing
data NavType = NTleft | NTright | NTup | NTdown | NTfirst | NTlast | NTtop
deriving (Eq, Ord, Show)
data Command =
Entry !Tree -- begin with this tree
| LParen -- open paren
| RParen -- close paren
| Move !NavType -- navigate the tree
| Replace !Tree -- replace focussed subtree with the new tree
| Insert !Label -- insert a new label
| Enter !Label -- enter a new label, taking precedence into account
deriving (Eq, Ord, Show)
data ModelState =
MSnothing
| MSsubtree !SubTree
| MSparenthesized !SubTree'
deriving (Eq, Ord, Show)
type Model =
-- either SubTree, or SubTree' or Nothing
(ModelState, String)
present :: Command -> Model -> Model
present (Entry t) d = (MSparenthesized (unflatten (root t)), "")
present (Move nt) (tree, errorMsg) =
case tree of
MSnothing -> (tree, "Cannot move without a tree")
MSsubtree x -> (MSsubtree ((case nt of
NTleft -> left
NTright -> right
NTup -> up
NTdown -> down
NTfirst -> firstChild
NTlast -> lastChild
NTtop -> backToTop) x), "")
MSparenthesized x ->
(MSparenthesized ((case nt of
NTleft -> left'
NTright -> right'
NTup -> up'
NTdown -> down'
NTfirst -> firstChild'
NTlast -> lastChild'
NTtop -> backToTop') x), "")
present (Replace t) (tree, errorMsg) =
case tree of
MSnothing -> (tree, "Cannot replace without a tree")
MSsubtree x -> (MSsubtree (replace t x), "")
MSparenthesized x -> (MSparenthesized (replace' t x), "")
present (Insert lab) (tree, errorMsg) =
case tree of
MSnothing -> (tree, "Cannot insert without a tree")
MSsubtree x -> (MSsubtree (insert lab x), "")
MSparenthesized x -> (MSparenthesized (insert' lab x), "")
present (Enter lab) (tree, errorMsg) =
case tree of
MSnothing -> (tree, "Cannot enter without a tree")
MSsubtree x -> (MSsubtree (entry lab x), "")
MSparenthesized x -> (MSparenthesized (entry' lab x), "")
present LParen (tree, errorMsg) =
case tree of
MSnothing -> (tree, "Cannot insert left paren without a tree")
MSsubtree (x,t) -> (MSparenthesized (x:[], t), "") -- begin a parenthesized group
MSparenthesized t -> (MSparenthesized (open t), "")
present RParen (tree, errorMsg) =
case tree of
MSnothing -> (tree, "Cannot insert right paren without a tree")
MSsubtree t -> (MSsubtree t, "") -- leave it the same!
MSparenthesized t -> (MSparenthesized (close t), "")
present c (d, _) = (d, "Unable to perform command: " ++ show c)
scriptPresent :: Model -> [Command] -> IO Model
scriptPresent mdl [] = return mdl
scriptPresent mdl@(dat, errmsg) (x:xs) = do
print ("applying cmd: " ++ show x)
let mdl' = present x mdl
print ("result: " ++ show mdl')
scriptPresent mdl' xs
scriptPresentResult :: Model -> [Command] -> IO Tree
scriptPresentResult mdl xs = do
(d, e) <- scriptPresent mdl xs
case d of
MSnothing -> fail "should have been some data"
MSsubtree x -> return (host x)
MSparenthesized x -> return (host . flatten $ x)
checkExpect :: (Eq a, Show a) => a -> a -> IO ()
checkExpect x y =
if x == y then
putStrLn $ "OK: " ++ show x
else do
putStrLn "Error! Mismatched terms"
putStrLn $ "Expected: " ++ show x
putStrLn $ "Actual: " ++ show y
fail "should have been equal"
main :: IO ()
main = do
print "test A"
e1 <- scriptPresentResult
(MSnothing, "")
[Entry (template Add), Move NTdown, Insert (Const 1), Move NTright, Insert (Const 2), Move NTup]
checkExpect (Fork Add [Fork (Const 1) [], Fork (Const 2) []]) e1
print "test B"
e2 <- scriptPresentResult
(MSnothing, "")
[
Entry hole,
Insert (Var "A"),
Insert Add,
Move NTright,
Insert (Var "B"),
Insert Add,
Move NTright,
Insert (Var "C")] -- result should be A + (B + C)
checkExpect (Fork Add [Fork (Var "A") [], Fork Add [Fork (Var "B") [], Fork (Var "C") []]]) e2
print "test C"
-- a*b+c*d
e3 <- scriptPresentResult
(MSnothing, "")
[Entry hole, Enter (Var "A"), Enter Mul, Enter (Var "B"), Enter Add, Enter (Var "C"), Enter Mul, Enter (Var "D")]
checkExpect
(Fork Add [
Fork Mul [Fork (Var "A") [], Fork (Var "B") []],
Fork Mul [Fork (Var "C") [], Fork (Var "D") []]])
e3
print "test D"
-- (a+b)*(c+d)
e4 <- scriptPresentResult
(MSnothing, "")
[Entry hole,
LParen, Enter (Var "A"), Enter Add, Enter (Var "B"), RParen,
Enter Mul,
LParen, Enter (Var "C"), Enter Add, Enter (Var "D"), RParen]
checkExpect
(Fork Mul [
Fork Add [Fork (Var "A") [], Fork (Var "B") []],
Fork Add [Fork (Var "C") [], Fork (Var "D") []]])
e4
{-
print "test D0'"
let r0 = root (Fork If [hole,Fork (Var "A") [],hole])
print $ "r0 = " ++ show r0
let r1 = next r0 -- down r0
print $ "r1 = " ++ show r1
--let r2 = right r1
--print $ "r2 = " ++ show r2
-}
-- NOTE: still a difference between enter & insert...
-- the below is not possible to do with "Enter" alone
print "test D0"
e4_0 <- scriptPresentResult (MSnothing, "") [Entry hole, Enter If, Enter (Var "x")]
checkExpect (Fork If [Fork (Var "x") [],hole,hole]) e4_0
print "test D1"
-- "if a then b else c"
e4_1 <- scriptPresentResult
(MSnothing, "")
[Entry hole, Enter If, Enter (Var "A"), Enter (Var "B"), Enter (Var "C")]
checkExpect
(Fork If [Fork (Var "A") [], Fork (Var "B") [], Fork (Var "C") []])
e4_1
print "test E"
-- (if a then b else c) + d
e5 <- scriptPresentResult
(MSnothing, "")
[Entry hole, LParen, Enter If, Enter (Var "A"), Enter (Var "B"), Enter (Var "C"), RParen, Enter Add, Enter (Var "D")]
checkExpect
(Fork Add [
Fork If [
Fork (Var "A") [],
Fork (Var "B") [],
Fork (Var "C") []
],
Fork (Var "D") []])
e5
-- \ab+c*d
print "test F"
e6 <- scriptPresentResult
(MSnothing, "")
[Entry hole, Enter Lam, Enter (Var "A"), Enter (Var "B"), Enter Add, Enter (Var "C"), Enter Mul, Enter (Var "D")]
checkExpect
(Fork Lam [
Fork (Var "A") [],
Fork Add [
Fork (Var "B") [],
Fork Mul [
Fork (Var "C") [],
Fork (Var "D") []
]
]
])
e6
-- a + b * d : t
print "test G"
e7 <- scriptPresentResult
(MSnothing, "")
[Entry hole, Enter (Var "A"), Enter Add, Enter (Var "B"), Enter Mul, Enter (Var "D"), Enter Annot, Enter (Var "T")]
checkExpect
(Fork Annot [
Fork Add [
Fork (Var "A") [],
Fork Mul [
Fork (Var "B") [],
Fork (Var "D") []
]
],
Fork (Var "T") []
])
e7
print "test fun-app"
e7 <- scriptPresentResult
(MSnothing, "")
[Entry hole, Enter (Var "F"), Enter App, Enter (Var "X"), Enter App, Enter (Var "Y")]
checkExpect
(Fork App [Fork App [Fork (Var "F") [], Fork (Var "X") []], Fork (Var "Y") []])
e7
{-
real languages?
- freeform comment nodes
- begin end blocks (or the like, e.g. { } blocks)
- different kind of brackets
some languages also have variadic constructs
- e.g. f (x, y)
- lists, arrays, etc. (literals), e.g. [a,b,c,d]
- this is same as with fun-app, actually
- how do we handle fun-app ???
(a b) --> (a Apply b)
fun-app associates to the left:
f x y z ==> ((f x) y) z
some languages also have different constructs
- let|local-in-end
- E where E1
- fun|val a = b (declaration list)
ATS to JS?
undo?
what's a fun real language? say regexes? or relational algebra
One of the salient features of the model is the representation of a selection as a sequence of
context layers paired with the selected subtree. This representation makes it easy to reason about
selections – and as an added bonus its Haskell implementation is quite efficient. Moreover it can
straightforwardly be generalised to support a style of selection in which a number of adjacent
siblings are simultaneously selected. This style of selection is necessary when editing structures
which use variadic nodes to represent trees formed of semantically associative operators – such
as sequential composition (of commands in a programming language), and juxtaposition (of
words, paragraphs, etc. in a natural language).
- what is this style of selection?
- what if you had multiple trees under selection/focus AT ONCE? e.g. a list of trees
- you could also make a separate MODE for that. so if user enters BEGIN, say
- the editor starts the mode where it will
A further layer of design which we hinted at earlier introduces a scratchpad and cut buffer,
and supports more radical structural changes and better undoability properties. The details are
straighforward and only space considerations led us to exclude them.
Finally, we have ignored user-interface issues related to input of commands, to layout of the
visible representation of the tree, and to navigation and selection using mouse gestures. We
intend to pursue all these in a subsequent paper in the same constructive mathematical style.
-}