-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathu-conjure.hs
194 lines (161 loc) · 5.29 KB
/
u-conjure.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
-- u-conjure.hs -- u-Conjure
--
-- This is a prototype for Conjure, a library for conjuring code
-- out of partially implemented functions.
--
--
-- Copyright (C) 2021-2024 Rudy Matela
-- Distributed under the 3-Clause BSD licence (see the file LICENSE).
--
--
-- To run this you need to have both LeanCheck and Express installed:
--
-- $ cabal install leancheck
-- $ cabal install express
--
-- If installation fails, use v1-install:
--
-- $ cabal v1-install leancheck
-- $ cabal v1-install express
import Data.List
import Data.Maybe
import Data.Express
import Data.Typeable
import Test.LeanCheck.Error
square :: Int -> Int
square 0 = 0
square 1 = 1
square 2 = 4
square 3 = 9
square 4 = 16
add :: Int -> Int -> Int
add 0 0 = 0
add 0 1 = 1
add 1 0 = 1
add 1 1 = 2
factorial :: Int -> Int
factorial 0 = 1
factorial 1 = 1
factorial 2 = 2
factorial 3 = 6
factorial 4 = 24
second :: [Int] -> Int
second [x,y] = y
second [x,y,z] = y
second [x,y,z,w] = y
-- reverse
reverse' :: [Int] -> [Int]
reverse' [x,y] = [y,x]
reverse' [x,y,z] = [z,y,x]
-- ++
(+++) :: [Int] -> [Int] -> [Int]
[x] +++ [y] = [x,y]
[x,y] +++ [z,w] = [x,y,z,w]
main :: IO ()
main = do
conjure "square" square primitives
conjure "add" add primitives
conjure "factorial" factorial primitives
conjure "factorial" factorial
[ val (0 :: Int)
, val (1 :: Int)
, value "+" ((+) :: Int -> Int -> Int)
, value "*" ((*) :: Int -> Int -> Int)
, value "foldr" (foldr :: (Int -> Int -> Int) -> Int -> [Int] -> Int)
, value ".." (enumFromTo :: Int -> Int -> [Int])
]
conjure "second" second listPrimitives
conjure "++" (+++) listPrimitives
conjure "reverse" reverse' listPrimitives
-- even by using fold and some cheating,
-- this function is out of reach
-- reverse xs = foldr (\x xs -> xs ++ [x]) [] xs
-- reverse xs = foldr (flip (++) . unit) [] xs
conjure "reverse" reverse' $ listPrimitives ++
[ value "unit" ((:[]) :: Int -> [Int])
, value "++" ((++) :: [Int] -> [Int] -> [Int])
-- these last two are cheats:
, value "flip" (flip :: ([Int]->[Int]->[Int]) -> [Int] -> [Int] -> [Int])
, value "." ((.) :: ([Int]->[Int]->[Int]) -> (Int->[Int]) -> Int -> [Int] -> [Int])
]
where
primitives :: [Expr]
primitives =
[ val (0 :: Int)
, val (1 :: Int)
, val (2 :: Int)
, val (3 :: Int)
, value "+" ((+) :: Int -> Int -> Int)
, value "*" ((*) :: Int -> Int -> Int)
, value "-" ((-) :: Int -> Int -> Int)
]
listPrimitives :: [Expr]
listPrimitives =
[ val (0 :: Int)
, val (1 :: Int)
, val ([] :: [Int])
, value "head" (head :: [Int] -> Int)
, value "tail" (tail :: [Int] -> [Int])
, value ":" ((:) :: Int -> [Int] -> [Int])
, value "foldr" (foldr :: (Int -> [Int] -> [Int]) -> [Int] -> [Int] -> [Int])
]
conjure :: Typeable f => String -> f -> [Expr] -> IO ()
conjure nm f primitives = do
print (value nm f) -- prints the type signature
case conjureImplementations nm f primitives of
[] -> putStrLn $ "cannot conjure"
-- es -> putStrLn $ unlines $ map showEq es -- uncomment to show all found variations
(e:_) -> putStrLn $ showEq e
putStrLn ""
where
showEq eq = showExpr (lhs eq) ++ " = " ++ showExpr (rhs eq)
conjureImplementations :: Typeable f => String -> f -> [Expr] -> [Expr]
conjureImplementations nm f primitives =
[ appn -==- e
| e <- candidateExprsFrom $ exs ++ primitives
, isTrue (appn -==- e)
]
where
appn = application nm f primitives
(ef:exs) = unfoldApp appn
isTrue e = all (errorToFalse . eval False) . map (e //-) $ definedBinds appn
definedBinds :: Expr -> [[(Expr,Expr)]]
definedBinds ffxx = [bs | bs <- bss, errorToFalse . eval False $ e //- bs]
where
e = ffxx -==- ffxx
bss = take 360 $ groundBinds ffxx
application :: Typeable f => String -> f -> [Expr] -> Expr
application nm f es = mostGeneralCanonicalVariation $ appn (value nm f)
where
appn ff | isFun ff = case [e | Just (_ :$ e) <- (map (ff $$)) es] of
[] -> error "application: could not find type representative"
(e:_) -> appn (ff :$ holeAsTypeOf e)
| otherwise = ff
candidateExprsFrom :: [Expr] -> [Expr]
candidateExprsFrom = concat . take 7 . expressionsT
where
expressionsT ds = [ds] \/ (delay $ productMaybeWith ($$) es es)
where
es = expressionsT ds
(-==-) :: Expr -> Expr -> Expr
ex -==- ey = headOr (val False) . map (:$ ey) $ mapMaybe ($$ ex)
[ value "==" ((==) :: Int -> Int -> Bool)
, value "==" ((==) :: Bool -> Bool -> Bool)
, value "==" ((==) :: [Int] -> [Int] -> Bool)
, value "==" ((==) :: [Bool] -> [Bool] -> Bool)
]
where
headOr x [] = x
headOr _ (x:_) = x
lhs, rhs :: Expr -> Expr
lhs (((Value "==" _) :$ e) :$ _) = e
rhs (((Value "==" _) :$ _) :$ e) = e
groundBinds :: Expr -> [[(Expr,Expr)]]
groundBinds e = concat $ products [mapT ((,) v) (tiersFor v) | v <- nubVars e]
tiersFor :: Expr -> [[Expr]]
tiersFor e = case show (typ e) of
"Int" -> mapT val (tiers `asTypeOf` [[undefined :: Int]])
"Bool" -> mapT val (tiers `asTypeOf` [[undefined :: Bool]])
"[Int]" -> mapT val (tiers `asTypeOf` [[undefined :: [Int]]])
"[Bool]" -> mapT val (tiers `asTypeOf` [[undefined :: [Bool]]])
_ -> []