fix off-by-two error in dataConstructorish

This commit is contained in:
Arya Irani 2018-09-04 14:27:59 -04:00
parent e534a5581c
commit 90c4fd7d67
7 changed files with 91 additions and 3 deletions

View File

@ -95,7 +95,7 @@ object BuiltinTypes {
}
}
def dataConstructorish(req: (Array[Value]) => Value, decompile: Term,
def dataConstructorish(req: Array[Value] => Value, decompile: Term,
paramNames: Name*): Computation = {
val arity = paramNames.length
val body: Computation = arity match {
@ -119,7 +119,7 @@ object BuiltinTypes {
(r,rec,top,stackU,x1,x0,stackB,x1b,x0b) => {
// arity = 3, argsStart = top.toInt, argsStop = top.toInt + 1
// arity = 4, argsStart = top.toInt - 1, argsStop = top.toInt + 1
val argsStart = top.toInt - ((arity - compilation.K) + 1)
val argsStart = top.toInt + 1 - (arity - compilation.K)
val argsStop = top.toInt + 1
val args = new Array[Value](arity)
locally {

View File

@ -0,0 +1,17 @@
-- board piece
type P = X | O | E
type Board = Board P P
use Board.Board
use P O X E
case Board X O X
of Board a b c -> a
-- gives this error:
-- This looks like a function call, but with a Board where the function should be. Are you missing an operator?
-- ^^^^^
-- 13 | case Board X O X
-- ^^^^^

View File

@ -0,0 +1,12 @@
-- board piece
type Board = Board UInt64 UInt64 UInt64
use Board.Board
-- uncommenting these gives errors from NPE to array index out of bounds -1, -2
-- x = 1
-- y = 2
case Board 77 88 99
of Board a b c -> c

View File

@ -0,0 +1,17 @@
-- board piece
type P = X | O | E
type Board = Board P P P P P P P P P
use Board.Board
use P O X E
whatevs a b c = a
b = Board X O X O X X O E X
x = 1
y = 2
z = 3
case b of
Board a b c d e f g h i -> a

View File

@ -0,0 +1,43 @@
-- board piece
type P = X | O | E
type Board = Board P P P P P P P P P
use Board Board
use P O X E
use Optional Some None
orElse a b =
case a of
None -> b
a -> a
namespace P where
(/=) : P -> P -> Boolean
a /= b = not (a == b)
(==) : P -> P -> Boolean
a == b = case (a,b) of
(X,X) -> true
(O,O) -> true
_ -> false
b = (Board X O X
O X X
O E X)
isWin board =
same : P -> P -> P -> Optional P
same a b c = if and (and (a P.== b) (a P.== c)) (a P./= E)
then Some a
else None
case board of
-- vertical top/center/bottom
-- horizontal left/center/right
-- diagonal rising/falling
Board a b c
d e f
g h i -> (same a b c)
isWin b
-- Some 3

View File

@ -61,4 +61,3 @@ isWin board =
isWin (Board X O X
O X X
O E X)