Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[prototype] Support base's classes and methods in Plutus Tx #5219

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .stylish-haskell.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ steps:
remove_redundant: false

- trailing_whitespace: {}
columns: 100
columns: 99
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

newline: native
language_extensions:
- DataKinds
Expand Down
1 change: 1 addition & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ packages: doc/read-the-docs-site
prettyprinter-configurable
word-array
stubs/plutus-ghc-stub
with-compiler: /home/zliu41/ghc/_build/stage1/bin/ghc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can integrate the GHC patch with Nix inside a local .nix file?


-- We never, ever, want this.
write-ghc-environment-files: never
Expand Down
4 changes: 2 additions & 2 deletions plutus-benchmark/lists/test/Sum/left-fold-data.budget.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 450587685
| mem: 1461262})
({cpu: 422780685
| mem: 1340362})
4 changes: 2 additions & 2 deletions plutus-benchmark/lists/test/Sum/right-fold-data.budget.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 457487685
| mem: 1491262})
({cpu: 429680685
| mem: 1370362})
14 changes: 7 additions & 7 deletions plutus-benchmark/nofib/src/PlutusBenchmark/NoFib/Clausify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module PlutusBenchmark.NoFib.Clausify where
import PlutusBenchmark.Common (Term, compiledCodeToTerm)

import PlutusTx qualified as Tx
import PlutusTx.Prelude as Plutus
import PlutusTx.Prelude as Plutus hiding ((*), (+), (-), (/=), (<), (<=), (==), (>), (>=))
import Prelude qualified as Haskell

type Var = Integer
Expand Down Expand Up @@ -84,11 +84,11 @@ elim (Eqv f f') = Con (elim (Imp f f')) (elim (Imp f' f))
-- insertion of an item into an ordered list
-- Note: this is a corrected version from Colin (94/05/03 WDP)
{-# INLINABLE insert #-}
insert :: (Ord t) => t -> [t] -> [t]
insert :: (Haskell.Ord t) => t -> [t] -> [t]
insert x [] = [x]
insert x p@(y:ys) =
if x < y then x : p
else if x > y then y : insert x ys
if x Haskell.< y then x : p
else if x Haskell.> y then y : insert x ys
else p

-- shift negation to innermost positions
Expand All @@ -104,7 +104,7 @@ negin p = p
-- does any symbol appear in both consequent and antecedent of clause
{-# INLINABLE tautclause #-}
tautclause :: LRVars -> Bool
tautclause (c,a) = [x | x <- c, x `elem` a] /= []
tautclause (c,a) = [x | x <- c, x `elem` a] Haskell./= []

-- form unique clausal axioms excluding tautologies
{-# INLINABLE unicl #-}
Expand All @@ -121,8 +121,8 @@ while p f x = if p x then while p f (f x) else x

{-# INLINABLE replicate #-}
replicate :: Integer -> a -> [a]
replicate n a = if n <= 0 then []
else a:(replicate (n-1) a)
replicate n a = if n Haskell.<= 0 then []
else a:(replicate (n Haskell.- 1) a)

{-# INLINABLE formula1 #-}
formula1 :: Formula -- % (a = a) = (a = a) = (a = a)
Expand Down
22 changes: 11 additions & 11 deletions plutus-benchmark/nofib/src/PlutusBenchmark/NoFib/Knights.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import PlutusBenchmark.NoFib.Knights.Queue

import PlutusCore.Pretty qualified as PLC
import PlutusTx qualified as Tx
import PlutusTx.Prelude as Tx
import PlutusTx.Prelude as Tx hiding ((*), (+), (-), (/=), (<), (<=), (==), (>), (>=))
import Prelude qualified as Haskell

{-# INLINABLE zipConst #-}
Expand All @@ -27,7 +27,7 @@ zipConst a (b:bs) = (a,b) : zipConst a bs

{-# INLINABLE grow #-}
grow :: (Integer,ChessSet) -> [(Integer,ChessSet)]
grow (x,y) = zipConst (x+1) (descendents y)
grow (x,y) = zipConst (x Haskell.+ 1) (descendents y)

{-# INLINABLE isFinished #-}
isFinished :: (Integer,ChessSet) -> Bool
Expand All @@ -36,23 +36,23 @@ isFinished (_,y) = tourFinished y
{-# INLINABLE interval #-}
interval :: Integer -> Integer -> [Integer]
interval a b =
if a > b then []
else a:(interval (a+1) b)
if a Haskell.> b then []
else a:(interval (a Haskell.+ 1) b)


{-# INLINABLE repl #-}
repl :: Integer -> Integer -> [Integer]
repl n a =
if n == 0 then []
else a:(repl (n-1) a)
if n Haskell.== 0 then []
else a:(repl (n Haskell.- 1) a)

-- % Original version used infinite lists.
{-# INLINABLE mkStarts #-}
mkStarts :: Integer -> [(Integer, ChessSet)]
mkStarts sze =
let l = [startTour (x,y) sze | x <- interval 1 sze, y <- interval 1 sze]
numStarts = Tx.length l -- = sze*sze
in Tx.zip (repl numStarts (1-numStarts)) l
in Tx.zip (repl numStarts (1 Haskell.- numStarts)) l

{-# INLINABLE root #-}
root :: Integer -> Queue (Integer, ChessSet)
Expand All @@ -72,13 +72,13 @@ type Solution = (Integer, ChessSet)

{-# INLINABLE depthSearch #-}
-- % Added a depth parameter to stop things getting out of hand in the strict world.
depthSearch :: (Eq a) => Integer -> Queue a -> (a -> [a]) -> (a -> Bool) -> Queue a
depthSearch :: (Haskell.Eq a) => Integer -> Queue a -> (a -> [a]) -> (a -> Bool) -> Queue a
depthSearch depth q growFn finFn
| depth == 0 = []
| depth Haskell.== 0 = []
| emptyQueue q = []
| finFn (inquireFront q) = (inquireFront q):
(depthSearch (depth-1) (removeFront q) growFn finFn)
| otherwise = depthSearch (depth-1)
(depthSearch (depth Haskell.- 1) (removeFront q) growFn finFn)
| otherwise = depthSearch (depth Haskell.- 1)
(addAllFront (growFn (inquireFront q))
(removeFront q))
growFn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
{-# LANGUAGE NoImplicitPrelude #-}
-- Turning this off makes things fail, should investigate why
{-# OPTIONS_GHC -fno-strictness #-}
{-# OPTIONS_GHC -fexpose-all-unfoldings #-}
{-# OPTIONS_GHC -fno-enable-builtin-rules #-}

module PlutusBenchmark.NoFib.Knights.ChessSetList
( Tile,
Expand All @@ -25,8 +27,9 @@ import GHC.Generics
import PlutusBenchmark.NoFib.Knights.Sort
import PlutusBenchmark.NoFib.Knights.Utils

import PlutusTx.Prelude as Tx
import PlutusTx.Prelude as Tx hiding ((*), (+), (-), (/=), (<), (<=), (==), (>), (>=))

import Prelude ((*), (+), (-), (/=), (<=), (==), (>))
import Prelude qualified as Haskell


Expand All @@ -40,10 +43,10 @@ data ChessSet = Board
-- square).
deriving stock (Generic)
deriving anyclass (NFData)
instance Tx.Eq ChessSet where
instance Haskell.Eq ChessSet where
_ == _ = True

instance Tx.Ord ChessSet where
instance Haskell.Ord ChessSet where
_ <= _ = True

{-# INLINABLE createBoard #-}
Expand Down Expand Up @@ -124,7 +127,7 @@ pieceAtTile x0 (Board _ _ _ ts)


{-# INLINABLE notIn #-}
notIn :: Eq a => a -> [a] -> Bool
notIn :: Haskell.Eq a => a -> [a] -> Bool
notIn _ [] = True
notIn x (a:as) = (x /= a) && (notIn x as)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -fno-enable-builtin-rules #-}

module PlutusBenchmark.NoFib.Knights.KnightHeuristic
( ChessSet,
Expand All @@ -10,7 +11,8 @@ module PlutusBenchmark.NoFib.Knights.KnightHeuristic
import PlutusBenchmark.NoFib.Knights.ChessSetList
import PlutusBenchmark.NoFib.Knights.Sort (quickSort)

import PlutusTx.Prelude as Tx
import PlutusTx.Prelude as Tx hiding ((*), (+), (-), (/=), (<), (<=), (==), (>), (>=))
import Prelude ((*), (+), (-), (<=), (==), (>=))

data Direction = UL | UR | DL |DR | LU | LD | RU | RD

Expand Down Expand Up @@ -44,8 +46,8 @@ canMove board dir
{-# INLINABLE canMoveTo #-}
canMoveTo :: Tile -> ChessSet -> Bool
canMoveTo t@(x,y) board
= (x Tx.>= 1) && (x Tx.<= sze) &&
(y Tx.>= 1) && (y Tx.<= sze) &&
= (x >= 1) && (x <= sze) &&
(y >= 1) && (y <= sze) &&
isSquareFree t board
where
sze = sizeBoard board
Expand Down
21 changes: 9 additions & 12 deletions plutus-benchmark/nofib/src/PlutusBenchmark/NoFib/Knights/Sort.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@ module PlutusBenchmark.NoFib.Knights.Sort
quickSort
) where

import PlutusTx.Prelude qualified as Tx

{-# INLINABLE insertSort #-}
insertSort :: (Tx.Ord a) => [a] -> [a]
insertSort :: (Ord a) => [a] -> [a]
insertSort xs = foldr insertion [] xs

{-# INLINABLE insertion #-}
insertion :: (Tx.Ord a) => a -> [a] -> [a]
insertion :: (Ord a) => a -> [a] -> [a]
insertion x [] = [x]
insertion x (y:ys)
| x Tx.<= y = x:y:ys
| x <= y = x:y:ys
| otherwise = y:insertion x ys

{-# INLINABLE mergeSort #-}
mergeSort :: (Tx.Ord a) => [a] -> [a]
mergeSort :: (Ord a) => [a] -> [a]
mergeSort xs
= if (n <=1 ) then xs
else
Expand All @@ -31,18 +29,18 @@ mergeSort xs
n = length xs

{-# INLINABLE mergeList #-}
mergeList :: (Tx.Ord a) => [a] -> [a] -> [a]
mergeList :: (Ord a) => [a] -> [a] -> [a]
mergeList [] ys = ys
mergeList xs [] = xs
mergeList (x:xs) (y:ys)
| x Tx.<= y = x:mergeList xs (y:ys)
| x <= y = x:mergeList xs (y:ys)
| otherwise = y:mergeList (x:xs) ys

{-# INLINABLE quickSort #-}
quickSort :: (Tx.Ord a) => [a] -> [a]
quickSort :: (Ord a) => [a] -> [a]
quickSort [] = []
quickSort (x:xs) = (quickSort [y | y<-xs, y Tx.< x]) ++ [x] ++
(quickSort [y | y<-xs, y Tx.>= x])
quickSort (x:xs) = (quickSort [y | y<-xs, y < x]) ++ [x] ++
(quickSort [y | y<-xs, y >= x])

{-% These don't work in Plutus, and aren't used in the original program.
{-# INLINABLE lazySortLe #-}
Expand Down Expand Up @@ -133,4 +131,3 @@ test4 = [500,499..1]
test5 = take 10 (randomIntegers 123213 342234)
test6 = take 100 (randomIntegers 123213 342234)
test7 = take 1000 (randomIntegers 123213 342234)

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

module PlutusBenchmark.NoFib.Knights.Utils where

import PlutusTx.Prelude
import PlutusTx.Prelude hiding ((*), (+), (-), (/=), (<), (<=), (==), (>), (>=))
import Prelude qualified as Haskell

{-# INLINABLE take' #-}
take' :: Integer -> [a] -> [a]
take' _ [] = []
take' n (a:as) = if n<=0 then [] else a:(take' (n-1) as)
take' n (a:as) = if n Haskell.<= 0 then [] else a:(take' (n Haskell.- 1) as)
12 changes: 6 additions & 6 deletions plutus-benchmark/nofib/src/PlutusBenchmark/NoFib/LastPiece.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ data Sex = Male | Female
{-# INLINABLE sumList #-}
sumList :: [Integer] -> Integer
sumList [] = 0
sumList (h:t) = h + sumList t
sumList (h:t) = h Haskell.+ sumList t

{-# INLINABLE numSolutions #-}
numSolutions :: Solution -> Integer
Expand Down Expand Up @@ -84,7 +84,7 @@ search :: Square -> Sex -- Square we are up to
search _ _ board []
= Soln board -- Finished
search (row,col) sex board ps -- Next row
| col == (maxCol+1) = search (row+1, 1) (flipSex sex) board ps
| col Haskell.== (maxCol Haskell.+ 1) = search (row Haskell.+ 1, 1) (flipSex sex) board ps
search square sex board ps -- Occupied square
| isJust (check board square) = search (next square) (flipSex sex) board ps
search square sex board ps
Expand Down Expand Up @@ -132,11 +132,11 @@ fit board square pid (o:os) =

{-# INLINABLE add #-}
add :: Square -> Offset -> Square
add (row,col) (orow, ocol) = (row + orow, col + ocol)
add (row,col) (orow, ocol) = (row Haskell.+ orow, col Haskell.+ ocol)

{-# INLINABLE next #-}
next :: Square -> Square
next (row,col) = (row,col+1)
next (row,col) = (row, col Haskell.+ 1)

{-# INLINABLE maxRow #-}
{-# INLINABLE maxCol #-}
Expand All @@ -157,7 +157,7 @@ check :: Board -> Square -> Maybe PieceId
check board square = -- Map.lookup square board
case board of
[] -> Nothing
(square',pid):board' -> if square == square' then Just pid else check board' square
(square',pid):board' -> if square Haskell.== square' then Just pid else check board' square

{-# INLINABLE extend #-}
extend :: Board -> Square -> PieceId -> Board
Expand All @@ -166,7 +166,7 @@ extend board square pid = (square, pid): board -- Map.insert square pid board
{-# INLINABLE extend_maybe #-}
extend_maybe :: Board -> Square -> PieceId -> Maybe Board
extend_maybe board square@(row,col) pid
| row > maxRow || col < 1 || col > maxCol
| row Haskell.> maxRow || col Haskell.< 1 || col Haskell.> maxCol
= Nothing
| otherwise
= case check board square of
Expand Down
4 changes: 2 additions & 2 deletions plutus-benchmark/nofib/src/PlutusBenchmark/NoFib/Prime.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ import GHC.Generics

import PlutusBenchmark.Common (Term, compiledCodeToTerm)

import Prelude ((*), (+), (-), (<), (<=), (==), (>=))
import Prelude qualified as Haskell

import PlutusCore.Pretty qualified as PLC
import PlutusTx qualified as Tx
import PlutusTx.Builtins (divideInteger, modInteger)
import PlutusTx.Prelude as Tx hiding (even)
import PlutusTx.Prelude as Tx hiding (even, (*), (+), (-), (/=), (<), (<=), (==), (>=))

---------------- Extras ----------------

Expand Down Expand Up @@ -84,7 +85,6 @@ powerMod a b m =
@y@^3 &\geq & @x@, \mbox{ and}\\
(@y@-1)^3 &<& @x@.
\end{array}\]

My implementation uses Newton's method.
-}
{-# INLINABLE cubeRoot #-}
Expand Down
Loading