-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathslow-eq.patch
179 lines (179 loc) · 5.39 KB
/
slow-eq.patch
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
150,152c150,158
< {-# LANGUAGE CPP #-}
< {-# LANGUAGE DeriveFunctor, DeriveFoldable #-}
< {-# LANGUAGE FlexibleContexts, FlexibleInstances, LambdaCase #-}
---
> module Main where
> import Base
> import Charser
> import System
> foreign export ccall "main" main
> {- GHC edition:
> {-# LANGUAGE LambdaCase #-}
> module Main where
> import Control.Applicative (asum)
154,166c160,161
< import Data.Foldable
< import Data.Function
< import Data.List (intersect, union, delete, elemIndex)
< #ifdef __HASTE__
< import Control.Monad
< import Haste.DOM
< import Haste.Events
< import Text.Parsec hiding (space)
< (<>) = (++)
< type Charser = Parsec String ()
< lowerChar = lower; upperChar = upper; alphaNumChar = alphaNum;
< digitChar = digit; space = spaces; some = many1
< #else
---
> import Data.Function ((&))
> import Data.List
170c165
< #endif
---
> -}
172,236c167,178
< #ifdef __HASTE__
< main :: IO ()
< main = withElems ["out", "rules", "order", "term", "knuthbendix", "rewrite"] $
< \[oEl, rEl, ordEl, tEl, kbB, rwB] -> do
< let
< setup buttonName rs precs t = do
< Just b <- elemById buttonName
< let
< go = do
< setProp oEl "value" ""
< setProp rEl "value" rs
< setProp ordEl "value" precs
< setProp tEl "value" t
< void $ b `onEvent` Click $ const $ go
< when (buttonName == "group") go
< setup "group" (unlines
< [ "(x * y) * z = x * (y * z)"
< , "1 * x = x"
< , "I x * x = 1"
< ]) "1 * I" "(I x * x) * y"
< setup "one" "F (F x) = G x" "" "F(F(F(F(F(x)))))"
< setup "peano" (unlines
< [ "0 + x = x"
< , "S x + y = S (x + y)"
< , "0 * x = 0"
< , "S x * y = y + (x * y)"]) "" "S(S(S(S(0)))) * S(S(0)) + S(S(S(0)))"
< setup "sort" (unlines
< [ "Max 0 x = x"
< , "Max x 0 = x"
< , "Max (S x) (S y) = S (Max x y)"
< , "Min 0 x = 0"
< , "Min x 0 = 0"
< , "Min (S x) (S y) = S (Min x y)"
< , "Sort Nil = Nil"
< , "Sort (Cons x y) = Insert x (Sort y)"
< , "Insert x Nil = Cons x Nil"
< , "Insert x (Cons y z) = Cons (Max x y) (Insert (Min x y) z)"
< ]) "" $ concat
< [ "Sort ("
< , "Cons (S (S (S 0))) ("
< , "Cons (S 0) ("
< , "Cons (S (S (S (S 0)))) ("
< , "Cons (S 0) ("
< , "Cons (S (S (S (S (S 0))))) Nil)))))"
< ]
<
< let parseRules = sequence . map (parse rule "") . lines
< void $ kbB `onEvent` Click $ const $ do
< parseRules <$> getProp rEl "value" >>= \case
< Left _ -> setProp oEl "value" "bad rules: parse error"
< Right rs -> do
< opList <- words <$> getProp ordEl "value"
< case knuthBendix (lpoGT $ weigh opList) rs of
< Nothing -> setProp oEl "value" "completion failed"
< Just rs' -> do
< setProp oEl "value" ""
< setProp rEl "value" $ unlines $ show <$> rs'
< void $ rwB `onEvent` Click $ const $ do
< parseRules <$> getProp rEl "value" >>= \case
< Left _ -> setProp oEl "value" "bad rules: parse error"
< Right rs -> parse expr "" <$> getProp tEl "value" >>= \case
< Left _ -> setProp oEl "value" "bad term: parse error"
< Right x -> do
< setProp oEl "value" $ show $ fixRewrite rs x
< #endif
---
> parseRules = sequence . map (parse rule "") . lines
>
> demo s opList = do
> rs <- case parseRules s of
> Left e -> Left $ show e
> Right rs -> Right rs
> maybe (Left "completion failed") Right
> $ knuthBendix (lpoGT $ weigh $ words opList) rs
>
> main = do
> print $ parseRules "(x * y) * z = x * (y * z)\n1 * x = x\nI x * x = 1\n"
> print $ demo "(x * y) * z = x * (y * z)\n1 * x = x\nI x * x = 1\n" "1 * I"
246,247c188,200
< data ExpF a = C String | V a | ExpF a :@ ExpF a deriving (Eq, Functor, Foldable)
< type Exp = ExpF String
---
> data ExpF a = C String | V a | ExpF a :@ ExpF a deriving Eq
> instance Functor ExpF where
> fmap f = \case
> V s -> V $ f s
> x :@ y -> fmap f x :@ fmap f y
> C s -> C s
>
> elemExp want = \case
> V s -> s == want
> x :@ y -> elemExp want x || elemExp want y
> C s -> False
>
> type Exp = ExpF [Char]
268a222
> sp :: Charser a -> Charser a
299c253
< a :@ b -> if elem s t then Nothing else Just [(s, t)]
---
> a :@ b -> if elemExp s t then Nothing else Just [(s, t)]
461c415
< | V v <- t = s /= t && v `elem` s
---
> | V v <- t = s /= t && v `elemExp` s
488c442,444
< where wt s = elemIndex s precs
---
> where
> wt s = elemIndex s precs
> elemIndex x xs = lookup x $ zip xs [0..]
609c565
< >>= (\e -> rec eqs (delete e todo) [e])
---
> >>= (\e -> rec eqs (filter (/= e) todo) [e])
667a624,650
>
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> <script>
> const ctx = {};
> function run() {
> ctx.inp = (new TextEncoder()).encode(document.getElementById("rules").value);
> ctx.out = [], ctx.cursor = 0;
> ctx.instance.exports.main();
> const out = (new TextDecoder()).decode(Uint8Array.from(ctx.out));
> console.log(out);
> }
> async function loadWasm() {
> try {
> ctx.instance = (await WebAssembly.instantiateStreaming(fetch('eq.wasm'), {env:
> { putchar: c => ctx.out.push(c)
> , eof : () => ctx.cursor == ctx.inp.length
> , getchar: () => ctx.inp[ctx.cursor++]
> }})).instance;
> console.log("running...");
> run();
> } catch(err) {
> console.log(err);
> }
> }
> loadWasm();
> </script>
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++