Can someone please tell me the differences between
Require Name.
Require Import Name.
Import Name
?
Require: load an external library (typically from the standard library or the user-contribs/ folder);
Import: imports the names in a module. For example, if you have a function f in a module M, by doing Import M., you will only need to type f instead of M.f;
Require Import: does both Require and Import.
Related
It seems I can't get the Coq Import system right.
I found pow_succ_r in Coq.Arith.PeanoNat.
So I imported it and hoped it is usable
Require Import Coq.Arith.PeanoNat.
Print pow_succ_r.
I get the following error:
pow_succ_r not a defined object.
Notice the line Module Nat near the top of the documentation. It means that the subsequent declarations are inside the Nat module. So, you can access the symbol as Nat.pow_succ_r.
In general, if you are looking for a symbol, use the Locate command:
Locate pow_succ_r.
(*
Constant
Coq.Arith.PeanoNat.Nat.pow_succ_r
(shorter name to refer to it in current context is Nat.pow_succ_r)
*)
What is the difference between require and import?
iex> require Integer
Integer
iex> Integer.is_odd(3)
true
and
iex> import List, only: [duplicate: 2]
List
iex> duplicate :ok, 3
[:ok, :ok, :ok]
It seems that they both do the same thing... get macros or functions from other modules.
From the documentation:
We use import whenever we want to easily access functions or macros from other modules without using the fully-qualified name.
also
Note that importing a module automatically requires it.
So if you import Integer, you can directly call is_odd, you don't need Integer.is_odd
require
According to this article:
Macro function is being evaluated during compilation. If you want to use it, you need to compile it first. This is exactly what require does.
In the background, it also gives an alias to required module, which means that you can pass as option just like with alias:
require TestModule, as: Test
import
The import directive allows you to skip the module part by importing all or some of the functions/macros:
import IO, only: [puts: 1]
puts "Hello"
As already mentioned, it also calls require in the background to compile it first.
I'm trying to build a CLI. I choose to use 'yang' to do so. I'm new to it and can't find out how to import existing moduls. As exemple I found a module for ospf on github (https://github.com/YangModels/yang/blob/master/vendor/cisco/xe/1631/ietf-ospf.yang) and I would like to import it in my own moduls. Can this be done? how?
EDIT1:
module mininet {
/* name space */
namespace "http://tail-f.com/ns/example/mininet";
prefix mininet;
import ietf-ospf {
prefix ospf;
revision-date 2015-03-09
}
leaf area-id-type {
type yang:area-id-type;
}
}
So I tried to do it this way using Piotr Babij help. Unfortunately this isn't working. What do I need to change?
area-id-type is a typedef of ietf-ospf. The error I have is te following one:
mininet.yang:12:3: error: trailing garbage after module
mininet.yang:12:3: error: unterminated statement
You may import other modules in your own modules by using the import statement. It is described in both RFC 7950 for YANG 1.1 and in RFC 6020 for YANG 1.0. In YANG 1.1 you may import two different revisions of the same module. Other that that, the import statement works the same in both versions.
In practice the basic import looks like this:
module acme-system {
namespace "http://acme.example.com/system";
prefix "acme";
import ietf-yang-types {
prefix "yang";
revision-date 2013-07-15;
}
leaf acme-ip-address {
type yang:dotted-quad;
}
}
If you omit the optional revision-date statement then an undefined module revision is imported. So, in general, it is a good practive to use it.
The mandatory prefix statement lets you to refer to the things in the imported module. In the example the prefix of the imported ietf-yang-types module is yang and, thanks to that, it is clear that yang:dotted-quad refers to a type from that module. In your case you have set the prefix to ospf, so you should have ospf:area-id-type to refer to a type definition from that module. If you import multiple modules you need to ensure their prefixes are unique.
Additionally, you are importing the oldest available revision of the ietf-ospf module. I just hope that this is what you really want to do.
Anyway, once you import a module you are allowed to:
use any grouping and typedef defined at the top level in the imported module or its submodules.
use any extension, feature, and identity defined in the imported module or its submodules.
use any node in the imported module's schema tree in must, path, and when statements, or as the target node in augment and deviation statements.
In the above example the typedef dotted-quad from the ietf-yang-types is used in the acme-system module.
I need to use the part of the standard library called Coq.Arith.PeanoNat (https://coq.inria.fr/library/Coq.Arith.PeanoNat.html).
I've tried either importing the entire Arith library or just this module, but I can't use it either way.
Every other library I've tried works just fine. When I do Require Import Bool. I compile and I can use it correctly. Upon Print Bool. I can take a look at all the functions inside in the next format:
Module
Bool
:= Struct
Definition...
.
.
.
End
When I do either Require Import Arith.PeanoNat. or Require Import Arith. I get this as immediate output:
[Loading ML file z_syntax_plugin.cmxs ... done]
[Loading ML file quote_plugin.cmxs ... done]
[Loading ML file newring_plugin.cmxs ... done]
<W> Grammar extension: in [tactic:simple_tactic], some rule has been masked
<W> Grammar extension: in [tactic:simple_tactic], some rule has been masked
<W> Grammar extension: in [tactic:simple_tactic], some rule has been masked
<W> Grammar extension: in [tactic:simple_tactic], some rule has been masked
<W> Grammar extension: in [tactic:simple_tactic], some rule has been masked
When I ask Coq Print Arith.PeanoNat it outputs: Module Arith := Struct End, it seems to be empty. When I try to use anything from the library, for example le_le under boolean comparisons, I get the standard Error: leb_le not a defined object. I have updated Coq and the libraries, and I have no idea of what might be going on here. I'd appreciate your input in fixing this library problem.
If I am not mistaken, Require is the keyword to load a file. Import has to do with managing name spaces. Often they are used together, as in Require Import PeanoNat., but they are really doing two different things.
When coq files (DirName/FileName.vo) are loaded with Require, it is as if the contents of FileName.vo is wrapped in Module DirName.FileName ... End. Everyting defined in the file is then accessed with DirName.FileName.Name.
The file can itself have modules M inside of it, and to get to M's contents, one has to type DirName.FileName.ModuleName.Name1 etc.
Import is used to get all the definitions up to the top level. By doing Import DirName.FileName.ModuleName the module Name1 is now imported to the top level, and can be referenced without the long path.
In your example above, the file Arith/PeanoNat.vo defines the module Nat. Actually, that is all it defines. So if you do Require Import Arith.PeanoNat you get PeanoNat.Nat at the top level. And then Import PeanoNat.Nat will bring Nat to the top level. Note that you can't do Require Import PeanoNat.Nat because it is no .vo file.
Coq can sometimes find a .vo file without you having to specify the whole path, so you can also do Require Import PeanoNat. and coq will find the file. If you wonder where it found it, do Locate PeanoNat.
Coq < Require Import PeanoNat.
Coq < Locate PeanoNat.
Module Coq.Arith.PeanoNat
Another Nat is also available from another place than PeanoNat.
Coq < Require Import Nat.
Warning: Notation _ + _ was already used in scope nat_scope
Warning: Notation _ * _ was already used in scope nat_scope
Warning: Notation _ - _ was already used in scope nat_scope
Coq < Locate Nat.
Module Coq.Init.Nat
So, you don't Import a library, you Require it. You use Import to not have to use the full path name. I hope this helps you debug what is happening.
When I try Print Arith.PeanoNat, the output is slightly different: I get Module PeanoNat := Struct Module Nat End and then even though leb_le is not in scope, Nat.leb_le is.
(I run 8.5beta2 in case that's relevant).
With Perl's standard module exports you can specify sets of functions to export/import by tag.
So, you can have sets like :all or :private or :test or whatever.
Hisorically, the advice seems to be that if you just want some semi-private functions then have an "inner" module and re-export the "default" parts of it.
I'm starting to want a semi-private interface for testing purposes. So, is that still the way to do it, or am I missing something in a recent ghc?
No, it's not possible in Haskell.
The Haskell solution to this problem is just to have separate modules. E.g.
module Foo.Private
( private1
, private2
, private3
) where
...
module Foo
( public1
, public2
, public3
) where
...
Additionally, if you want Foo.Private to re-export everything from Foo:
module Foo.Private
( module Foo
, private1
, private2
, private3
) where
import Foo
...
But if Foo in turn imports Foo.Private, then you'll have either to use
recursive modules, or to move the actual definitions to a third module,
say Foo.Base, which would be imported by Foo and Foo.Private.
Also, unlike perl, ghc doesn't support defining multiple modules in the same
file yet, which makes this solution somewhat heavyweight.
You can import a subset of functions using this:
import Data.List (nub, sort)
That will only import nub and sort from the Data.List module. Say, in case you don't want something to be imported, then:
import Data.List hiding (sort)
This will import all the functions in Data.List module except the sort function.
And in case if you want to export only certain functions from your module then define them like this:
module Data.List
( sort,
nub
) where
That will only export the sort and the nub functions in the module.
the haskell version of doctest runs code such that the non-exported functions are in scope.