Is there any way to enable n+k patterns in Frege? - frege

In Haskell I have to start ghci with -XNPlusKPatterns or add {-# LANGUAGE NPlusKPatterns #-} in source file to make this work:
pred :: Int -> Int
pred 0 = 0
pred (n + 1) = n
Is n+k pattern deliberately passed over in Frege or there exists a way to enable it?

No, (n+k) patterns were already obsolete when frege was written, so they're not included.

Related

Purpose of anonymous modules in Agda

Going at the root of Agda standard library, and issuing the following command:
grep -r "module _" . | wc -l
Yields the following result:
843
Whenever I encounter such anonymous modules (I assume that's what they are called), I quite cannot figure out what their purpose is, despite of their apparent ubiquity, nor how to use them because, by definition, I can't access their content using their name, although I assume this should be possible, otherwise their would be no point in even allowing them to be defined.
The wiki page:
https://agda.readthedocs.io/en/v2.6.1/language/module-system.html#anonymous-modules
has a section called "anonymous modules" which is in fact empty.
Could somebody explain what the purpose of anonymous modules is ?
If possible, any example to emphasize the relevance of the definition of such modules, as well as how to use their content would be very much appreciated.
Here are the possible ideas I've come up with, but none of them seems completely satisfying:
They are a way to regroup thematically identical definitions inside an Agda file.
Their name is somehow infered by Agda when using the functions they provide.
Their content is only meant to be visible / used inside their englobing module (a bit like a private block).
Anonymous modules can be used to simplify a group of definitions which share some arguments. Example:
open import Data.Empty
open import Data.Nat
<⇒¬≥ : ∀ {n m} → n < m → n ≥ m → ⊥
<⇒¬≥ = {!!}
<⇒> : ∀ {n m} → n < m → m > n
<⇒> = {!!}
module _ {n m} (p : n < m) where
<⇒¬≥′ : n ≥ m → ⊥
<⇒¬≥′ = {!!}
<⇒>′ : m > n
<⇒>′ = {!!}
Afaik this is the only use of anonymous modules. When the module _ scope is closed, you can't refer to the module anymore, but you can refer to its definitions as if they hadn't been defined in a module at all (but with extra arguments instead).

Is there a perl6 counterpart of powershells get-member to "analyze" a variable(-object)?

Question:
Is there/What is the Perl6 counterpart of Powershells get-member to "analyse" the attributes of a variable?
Explanation:
In Perl 6 you can get properties/attributes of a variable, e.g.:
my $num=16.03;
say $num.numerator; # output: 1603
say $num.denominator; # output: 100
say $num.nude; # output: (1603 100)
say $num.WHAT; # output: (Rat)
How can I find out, which attributes/properties (numerator etc.) and methods/functions (WHAT) a variable has?
In Powershell I would pipe the variable to get-member, like:
$num | get-member and would get all properties and function displayed.
The best way would be to consult the docs for whatever type .WHAT told you, e.g. https://docs.perl6.org/type/Rat for Rat.
If you must have it programmatically, you can ask the object for its methods with .^methods.
> my $num = 16.03
16.03
> $num.^methods
(Rat FatRat Range atanh Bridge sign sqrt asech sin tan atan2 acosech truncate
asinh narrow base floor abs conj acosh pred new asec cosec acotan cosh ceiling
nude acos acosec sech unpolar log exp roots cotan norm sinh tanh acotanh Int
Num Real sec asin rand polymod log10 cos round REDUCE-ME succ base-repeating
cis cosech isNaN Complex cotanh atan perl WHICH Str ACCEPTS gist Bool Numeric
DUMP numerator denominator)
You can similarly see the attributes ('properties') with .^attributes, but any that you should access will have accessor methods anyway, so you shouldn't really need to do that.

The Java interface doesn't consider all constraints

I've successfully generated the scip Java interface. During testing face, I've encountered this problem: the Java interface returns immediately a so called optimal solution (problem is solved [optimal solution found]), but some of the constraints are not being considered - the solution generated is a zero-solution (actually, not a solution).
I've tried running the same problem in scip binary in the terminal, and works fine.
This is the problem that I'm testing:
param fichier:="/home/sebastien/workspace/DodenPlanning_teamDC/data.txt";
param nb_custom:= 11 ;
set Ind :=
{ read fichier as "<1s>" comment "#" use nb_custom} ;
param px[Ind] :=
read fichier as "<1s> 2n" comment "#" use nb_custom ;
param py[Ind] :=
read fichier as "<1s> 3n" comment "#" use nb_custom;
defnumb dist(i,j) := sqrt((px[i]-px[j])^2 + (py[i]-py[j])^2) ;
var x[Ind*Ind] binary ;
var r[Ind] integer <= card(Ind) - 1 ; # depot has rank 0, others have ranks 1 .. nb_points
minimize cost: sum <i,j> in Ind*Ind : dist(i,j) * x[i,j] ;
subto unique_predecessor : forall <j> in Ind do sum <i> in Ind : x[i,j]==1;
subto unique_successor : forall <j> in Ind do sum <k> in Ind : x[j,k]==1;
subto first_rank : r["0"] == 0 ; # because 0 is the starting point.
subto other_rank : # if there is edge i-> j then rank(j) = rank(i) + 1
forall <i,j> in Ind*Ind with j != "0" do
vif x[i,j] == 1 then r[j] == r[i] + 1 end ;
It's a modelisation of a VPR-problem. The solution returned by the interface is a solution where each vertex is only linked to himself (as this minimised the cost: equals 0), but this not a solution considering the other constraints.
We have verified that it is not a translation problem: a problem file was generated manually by simple and was solved by both ships binary in the terminal and by the Java interface.
The file is read by both: it generated the same number of variables, constraints etc.
Is the number of constraints limited in the Java interface? We have solved smaller problems which
Is the number of constraints limited in the Java interface? We have solved smaller problems which returned the same solution in both the scip binary and the Java interface.

How to enable hints and warnings in the online REPL

I figured that I can do it on the command line REPL like so:
java -jar frege-repl-1.0.3-SNAPSHOT.jar -hints -warnings
But how can I do the same in http://try.frege-lang.org
Hints and warnings are already enabled by default. For example,
frege> f x = f x
function f :: α -> β
3: application of f will diverge.
Perhaps we can make it better by explicitly saying it as warning or hint (instead of colors distinguishing them) something like:
[Warning] 3: application of f will diverge.
and providing an option to turn them on/off.
Update:
There was indeed an issue (Thanks Ingo for pointing that out!) with showing warnings that are generated in a later phase during the compilation. This issue has been fixed and the following examples now correctly display warnings in the REPL:
frege> h x = 0; h false = 42
function h :: Bool -> Int
4: equation or case alternative cannot be reached.
frege> f false = 6
function f :: Bool -> Int
5: function pattern is refutable, consider
adding a case for true

How to prevent common sub-expression elimination (CSE) with GHC

Given the program:
import Debug.Trace
main = print $ trace "hit" 1 + trace "hit" 1
If I compile with ghc -O (7.0.1 or higher) I get the output:
hit
2
i.e. GHC has used common sub-expression elimination (CSE) to rewrite my program as:
main = print $ let x = trace "hit" 1 in x + x
If I compile with -fno-cse then I see hit appearing twice.
Is it possible to avoid CSE by modifying the program? Is there any sub-expression e for which I can guarantee e + e will not be CSE'd? I know about lazy, but can't find anything designed to inhibit CSE.
The background of this question is the cmdargs library, where CSE breaks the library (due to impurity in the library). One solution is to ask users of the library to specify -fno-cse, but I'd prefer to modify the library.
How about removing the source of the trouble -- the implicit effect -- by using a sequencing monad that introduces that effect? E.g. the strict identity monad with tracing:
data Eval a = Done a
| Trace String a
instance Monad Eval where
return x = Done x
Done x >>= k = k x
Trace s a >>= k = trace s (k a)
runEval :: Eval a -> a
runEval (Done x) = x
track = Trace
now we can write stuff with a guaranteed ordering of the trace calls:
main = print $ runEval $ do
t1 <- track "hit" 1
t2 <- track "hit" 1
return (t1 + t2)
while still being pure code, and GHC won't try to get to clever, even with -O2:
$ ./A
hit
hit
2
So we introduce just the computation effect (tracing) sufficient to teach GHC the semantics we want.
This is extremely robust to compile optimizations. So much so that GHC optimizes the math to 2 at compile time, yet still retains the ordering of the trace statements.
As evidence of how robust this approach is, here's the core with -O2 and aggressive inlining:
main2 =
case Debug.Trace.trace string trace2 of
Done x -> case x of
I# i# -> $wshowSignedInt 0 i# []
Trace _ _ -> err
trace2 = Debug.Trace.trace string d
d :: Eval Int
d = Done n
n :: Int
n = I# 2
string :: [Char]
string = unpackCString# "hit"
So GHC has done everything it could to optimize the code -- including computing the math statically -- while still retaining the correct tracing.
References: the useful Eval monad for sequencing was introduced by Simon Marlow.
Reading the source code to GHC, the only expressions that aren't eligible for CSE are those which fail the exprIsBig test. Currently that means the Expr values Note, Let and Case, and expressions which contain those.
Therefore, an answer to the above question would be:
unit = reverse "" `seq` ()
main = print $ trace "hit" (case unit of () -> 1) +
trace "hit" (case unit of () -> 1)
Here we create a value unit which resolves to (), but which GHC can't determine the value for (by using a recursive function GHC can't optimise away - reverse is just a simple one to hand). This means GHC can't CSE the trace function and it's 2 arguments, and we get hit printed twice. This works with both GHC 6.12.4 and 7.0.3 at -O2.
I think you can specify the -fno-cse option in the source file, i.e. by putting a pragma
{-# OPTIONS_GHC -fno-cse #-}
on top.
Another method to avoid common subexpression elimination or let floating in general is to introduce dummy arguments. For example, you can try
let x () = trace "hi" 1 in x () + x ()
This particular example won't necessarily work; ideally, you should specify a data dependency via dummy arguments. For instance, the following is likely to work:
let
x dummy = trace "hi" $ dummy `seq` 1
x1 = x ()
x2 = x x1
in x1 + x2
The result of x now "depends" on the argument dummy and there is no longer a common subexpression.
I'm a bit unsure about Don's sequencing monad (posting this as answer because the site doesn't let me add comments). Modifying the example a bit:
main :: IO ()
main = print $ runEval $ do
t1 <- track "hit 1" (trace "really hit 1" 1)
t2 <- track "hit 2" 2
return (t1 + t2)
This gives us the following output:
hit 1
hit 2
really hit 1
That is, the first trace fires when the t1 <- ... statement is executed, not when t1 is actually evaluated in return (t1 + t2). If we define the monadic bind operator as
Done x >>= k = k x
Trace s a >>= k = k (trace s a)
instead, the output will reflect the actual evaluation order:
hit 1
really hit 1
hit 2
That is, the traces will fire when the (t1 + t2) statement is executed, which is (IMO) what we really want. For example, if we change (t1 + t2) to (t2 + t1), this solution produces the following output:
hit 2
really hit 2
hit 1
The output of the original version remains unchanged, and we don't see when our terms are really evaluated:
hit 1
hit 2
really hit 2
Like the original solution, this also works with -O3 (tested on GHC 7.0.3).