-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from stefan-hoeck/lenses
[ new ] record lenses
- Loading branch information
Showing
6 changed files
with
124 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
module Control.Barbie | ||
|
||
import public Control.ApplicativeB | ||
import public Control.DistributiveB | ||
import public Control.FunctorB | ||
import public Control.ApplicativeB | ||
import public Control.RecordB | ||
import public Control.TraversableB |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Control.RecordB | ||
|
||
import Monocle.Lens | ||
|
||
%default total | ||
|
||
public export | ||
interface RecordB (0 k : Type) (0 t : (k -> Type) -> Type) | t where | ||
constructor MkRecordB | ||
field : (0 f : k -> Type) -> (v : k) -> Lens' (t f) (f v) | ||
|
||
export %inline | ||
field' : RecordB k t => (v : k) -> Lens' (t f) (f v) | ||
field' = field f | ||
|
||
export %inline | ||
getField : RecordB k t => (v : k) -> t f -> f v | ||
getField v = get_ (field f v) | ||
|
||
export %inline | ||
setField : RecordB k t => (v : k) -> f v -> t f -> t f | ||
setField v = setL (field f v) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
module Derive.RecordB | ||
|
||
import Language.Reflection.Util | ||
import Derive.BarbieInfo | ||
|
||
%default total | ||
|
||
-------------------------------------------------------------------------------- | ||
-- Claims | ||
-------------------------------------------------------------------------------- | ||
|
||
blensTpe : (k : TTImp) -> TTImp -> TTImp | ||
blensTpe k arg = | ||
`( | ||
(0 f : ~(k) -> Type) | ||
-> (v : ~(k)) | ||
-> Lens' (~(arg) f) (f v) | ||
) | ||
|
||
export | ||
blensClaim : Visibility -> (fun : Name) -> (p : BarbieInfo) -> Decl | ||
blensClaim vis fun p = | ||
simpleClaim vis fun $ piAll (blensTpe p.kind p.applied) p.implicits | ||
|
||
||| Top-level declaration implementing the `Eq` interface for | ||
||| the given data type. | ||
export | ||
recordImplClaim : Visibility -> (impl : Name) -> (p : BarbieInfo) -> Decl | ||
recordImplClaim vis impl p = | ||
let tpe := piAll `(RecordB ~(p.kind) ~(p.applied)) p.implicits | ||
in implClaimVis vis impl tpe | ||
|
||
-------------------------------------------------------------------------------- | ||
-- Definitions | ||
-------------------------------------------------------------------------------- | ||
|
||
export | ||
recordImplDef : (fld, impl : Name) -> Decl | ||
recordImplDef fld impl = def impl [patClause (var impl) `(MkRecordB ~(var fld))] | ||
|
||
lclause : (String -> String) -> Name -> BoundArg 0 RegularNamed -> Clause | ||
lclause f fun (BA x _ _) = | ||
let fld := argName x | ||
nme := UN $ Basic (f $ nameStr fld) | ||
u := update [ISetField [nameStr fld] `(x)] `(y) | ||
in patClause `(~(var fun) _ ~(var nme)) `(lens ~(var fld) $ \x,y => ~(u)) | ||
|
||
export | ||
lensDef : (String -> String) -> Name -> Con n vs -> Decl | ||
lensDef f fun c = | ||
def fun (lclause f fun <$> (boundArgs regularNamed c.args [] <>> [])) | ||
|
||
-------------------------------------------------------------------------------- | ||
-- Deriving | ||
-------------------------------------------------------------------------------- | ||
|
||
||| Generate declarations and implementations for `RecordB` | ||
||| for a given data type. | ||
export | ||
RecordBVis : | ||
(String -> String) | ||
-> Visibility | ||
-> List Name | ||
-> ParamTypeInfo | ||
-> Res (List TopLevel) | ||
RecordBVis f vis nms p = case barbieArgs p.info.args of | ||
Just prf => case p.info.cons of | ||
[c] => | ||
let nlens := funName p "bfield" | ||
impl := implName p "RecordB" | ||
bti := BI p prf | ||
in Right | ||
[ TL (blensClaim vis nlens bti) (lensDef f nlens c) | ||
, TL (recordImplClaim vis impl bti) | ||
(recordImplDef nlens impl) | ||
] | ||
_ => failRecord "RecordB" | ||
Nothing => Left $ "Not a barbie type" | ||
|
||
||| Alias for `RecordBVis Public` | ||
export %inline | ||
RecordB : | ||
(String -> String) | ||
-> List Name | ||
-> ParamTypeInfo | ||
-> Res (List TopLevel) | ||
RecordB f = RecordBVis f Public |