Difference between revisions of "Stdlib"
m |
(→The Easy Way) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | == The Easy Way == |
||
+ | You can load up the whole standard environment using the builtin set_com primitive. One exception are IO functions, which must be explicitly asked for (because of obvious security issues). Currently, you also have to manually |
||
+ | add infix rules for user-defined functions, like so |
||
+ | <source lang="haskell"> |
||
+ | set_com (open "0b21518a6bed97b38745615071c91f976c0c1a96") |
||
+ | sleep = Prim "89010250203552d538a6981e396d648999f2a62d" |
||
+ | infixr $ 0 |
||
+ | infixl >> 1 |
||
+ | infixl >>= 1 |
||
+ | infixl >>= 1 |
||
+ | infixr =<< 1 |
||
+ | infixr $ 0 |
||
+ | infixl ++ 4 |
||
+ | infixl !% 5 |
||
+ | -- these are for match expressions |
||
+ | infixl // 0 |
||
+ | infixl $$ 1 |
||
+ | infixl @ 2 |
||
+ | </source> |
||
+ | |||
+ | If you want to extend your stdenv, you can use get_com to get the current working commit. |
||
+ | |||
+ | commit =<< get_com |
||
+ | |||
+ | Use caution with get_com, since storing its return value leads to a recursive object and in infinite loop during evaluation. This will be fixed in the future by dis-allowing writes to commit objects (using an O(log n) functional re-write instead). |
||
+ | |||
+ | == The Hard Way == |
||
<source lang="haskell"> |
<source lang="haskell"> |
||
/* Stdenv v 0.4 |
/* Stdenv v 0.4 |
Latest revision as of 16:49, 22 October 2014
The Easy Way
You can load up the whole standard environment using the builtin set_com primitive. One exception are IO functions, which must be explicitly asked for (because of obvious security issues). Currently, you also have to manually add infix rules for user-defined functions, like so <source lang="haskell"> set_com (open "0b21518a6bed97b38745615071c91f976c0c1a96") sleep = Prim "89010250203552d538a6981e396d648999f2a62d" infixr $ 0 infixl >> 1 infixl >>= 1 infixl >>= 1 infixr =<< 1 infixr $ 0 infixl ++ 4 infixl !% 5 -- these are for match expressions infixl // 0 infixl $$ 1 infixl @ 2 </source>
If you want to extend your stdenv, you can use get_com to get the current working commit.
commit =<< get_com
Use caution with get_com, since storing its return value leads to a recursive object and in infinite loop during evaluation. This will be fixed in the future by dis-allowing writes to commit objects (using an O(log n) functional re-write instead).
The Hard Way
<source lang="haskell"> /* Stdenv v 0.4
* * To be run once, create some useful standard objects, and save * as a commit structure (for later loading into stdenv-s). * * Create some useful primitives from the base commands, * 'typeof' = λ (t:⋆) (x:t) → t -- compilation step not yet complete (and presently skipped) * 'commit' : {} -> IO(string) * 'open' : string -> {} -- retrieve a previously committed dir. * 'Prim' : string -> ? -- outputs a prim * 'Base' : string -> ⋆ -- outputs the type of a base object */
-- Test types Nat : ⋆ Z : Nat Succ : Nat → Nat
-- Simple functions id = λ x → x seq = λ a b → b const = λ a b → a flip = λ f a b → f b a
>>= = applyIO =<< = flip (>>=) >> = λ a b → applyIO a (const b) infixl >>= 1 infixr =<< 1 infixl >> 1
-- $ : <a,b : 'kind> -> (a -> b) -> a -> b $ : (a -> b) -> a -> b $ = fun f x -> f x infixr $ 0
IO : (a : 'kind) -> 'kind ST : (a : 'kind) (r : 'kind) -> 'kind
rm := Prim =<< commit {
name = "rm", ptype = type String -> IO(Nil), n=1, extn = 1, -- IO prim API = "", reduce = "
- include <ast.h>
- include <cli.h>
Ast *reduce(prim_cb_t *cb, struct Prim *p) {
struct Environ *e = (struct Environ *)cb->st;
if(p->t[0]->type != TBase || p->t[0]->base->type != TString) { printf(\"rm() called on non-string\\n\"); return mkNil(); }
p9remove(e, p->t[0]->base->name); return mkNil();
}", }
/* More work to do: unitIO applyIO unitST applyST
rm pwd ls cd open gc_stat
- /
-- Save current project as a commit. -- commit_proj "stdenv-0.2"
unApply = Prim "1572ad8f02e44c195ee3cfff433f0fa81df75268"
-- string cat ++ = Prim "04abdf66335ce0504c9e7a1707445e2f700fc185" -- string slice !% = Prim "992740f62d4ca2f2ca9f6f6cf9274d8b9626a7c2" strlen = Prim "2081afb248e480a8f6ca6220156c69d7ac938ef8" infixl ++ 4 infix !% 5
sleep = Prim "89010250203552d538a6981e396d648999f2a62d" </source>